The DataStore abstract class
The Mock middleware uses DataStore
classes to store its data, and you can use the DataStore
API to to add/modify/remove this mock data, which is very handy for demos and POCs. Refer to the Mock middleware documentation to find out how to specify which DataStore
class is used. Refer to the Sample 2 walkthrough to see how to initialize the data store with data.
TIP: This is an abstract base class, which means you should not use this class directly. Instead, you should use one of its child classes: MemoryDataStore or FileDataStore. Or, if you want to store your data somewhere else — such as a SQL database, a Cloud service, etc. — then you can create your own child class that inherits from
DataStore
.
Methods
get(resource, callback)
Returns the specified resource from the data store
-
resource (required) -
Resource object
orstring
The resource path (such as"/pets/Fido"
) or the Resource object to be retrieved -
callback (optional) -
function(err, resource)
An error-first callback. The second parameter is the requested Resource object, orundefined
if no match was found.
save(resource1, resource2, ..., callback)
Saves the specified resource(s) to the data store. If any of the resources already exist, the new data is merged with the existing data.
-
resources (required) - one or more
Resource objects
The resources to be saved. You can pass one or more Resource objects as separate parameters, or you can pass an array Resource objects. -
callback (optional) -
function(err, resources)
An error-first callback. The second parameter is the Resource object, or array of Resource objects that were saved.
delete(resource1, resource2, ..., callback)
Deletes the specified resource(s) from the data store.
-
resources (required) - one or more
Resource objects
The resources to be deleted. You can pass one or more Resource objects as separate parameters, or you can pass an array Resource objects. -
callback (optional) -
function(err, resources)
An error-first callback. The second parameter is the Resource object, or array of Resource objects that were deleted.
Only the resources that were actually deleted are returned. If you specify multiple resources, and none of them exist in the data store, then an empty array will be returned. If you specify a single resource to be deleted, and it doesn’t exist, thenundefined
will be returned.
getCollection(collection, callback)
Returns all resources in the specified collection
-
collection (required) -
string
The collection path (such as"/pets"
,"/users/jdoe/orders"
, etc.) -
callback (optional) -
function(err, resources)
An error-first callback. The second parameter is an array of all Resource objects in the collection. If there are no resources in the collection, then the array is empty.
deleteCollection(collection, callback)
Deletes all resources in the specified collection
-
collection (required) -
string
The collection path (such as"/pets"
,"/users/jdoe/orders"
, etc.) -
callback (optional) -
function(err, resources)
An error-first callback. The second parameter is an array of all Resource objects that were deleted. If nothing was deleted, then the array is empty.