new DataStore(opts)
The DataStore
class is an extension of Container. Not only does
DataStore
manage mappers, but also collections. DataStore
implements the
asynchronous Mapper methods, such as Mapper#find and
Mapper#create. If you use the asynchronous DataStore
methods
instead of calling them directly on the mappers, then the results of the
method calls will be inserted into the store's collections. You can think of
a DataStore
as an Identity Map
for the ORM
(the Mappers).
import {DataStore} from 'js-data'
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
opts |
Object |
<optional> |
Configuration options. See Container. |
- Source:
Returns:
- Type
- DataStore
Example
import {DataStore} from 'js-data' import HttpAdapter from 'js-data-http' const store = new DataStore() const UserMapper = store.defineMapper('user') // Call "find" on "UserMapper" (Stateless ORM) UserMapper.find(1).then(function (user) { // retrieved a "user" record via the http adapter, but that's it // Call "find" on "store" for the "user" mapper (Stateful DataStore) return store.find('user', 1) }).then(function (user) { // not only was a "user" record retrieved, but it was added to the // store's "user" collection const cachedUser = store.getCollection('user').get(1) user === cachedUser // true })
Extends
Members
-
_listeners :Object
-
Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.
Type:
- Object
- Inherited From:
- Source:
-
mapperClass :function
-
Constructor function to use in Container#defineMapper to create a new mapper.
Type:
- function
- Inherited From:
- Source:
-
mapperDefaults :Object
-
Defaults options to pass to Container#mapperClass when creating a new mapper.
Type:
- Object
- Inherited From:
- Source:
Methods
-
addToCache(name, data, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
data
* Data from which data should be selected for add.
opts
Object <optional>
Configuration options.
- Source:
-
cachedFind(name, id, opts)
-
Retrieve a cached
find
result, if any.Parameters:
Name Type Description name
string The
name
argument passed to DataStore#find.id
string | number The
id
argument passed to DataStore#find.opts
Object The
opts
argument passed to DataStore#find.- Source:
-
cachedFindAll(name, hash, opts)
-
Retrieve a cached
findAll
result, if any.Parameters:
Name Type Description name
string The
name
argument passed to DataStore#findAll.hash
string The result of calling DataStore#hashQuery on the
query
argument passed to DataStore#findAll.opts
Object The
opts
argument passed to DataStore#findAll.- Source:
-
cacheFind(name, data, id, opts)
-
Cache a
find
result. The default implementation does the following:// Find and return the record from the data store return this.get(name, id)
Override this method to customize.
Parameters:
Name Type Description name
string The
name
argument passed to DataStore#find.data
* The result to cache.
id
string | number The
id
argument passed to DataStore#find.opts
Object The
opts
argument passed to DataStore#find.- Source:
-
cacheFindAll(name, data, hash, opts)
-
Cache a
findAll
result. The default implementation does the following:// Find and return the records from the data store return this.filter(name, utils.fromJson(hash))
Override this method to customize.
Parameters:
Name Type Description name
string The
name
argument passed to DataStore#findAll.data
* The result to cache.
hash
string The result of calling DataStore#hashQuery on the
query
argument passed to DataStore#findAll.opts
Object The
opts
argument passed to DataStore#findAll.- Source:
-
count(name, query, opts)
-
Proxy for Mapper#count.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
query
Object <optional>
Passed to Model.count.
opts
Object <optional>
Passed to Model.count.
- Inherited From:
- Source:
Returns:
- Type
- Promise
-
create(name, record, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
record
Object Passed to Mapper#create.
opts
Object <optional>
Passed to Mapper#create. See Mapper#create for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
createMany(name, records, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
records
Array Passed to Mapper#createMany.
opts
Object <optional>
Passed to Mapper#createMany. See Mapper#createMany for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
createRecord(name, props, opts)
-
Proxy for Mapper#createRecord.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
props
Object Passed to Mapper#createRecord.
opts
Object <optional>
Passed to Mapper#createRecord. See Mapper#createRecord for configuration options.
- Inherited From:
- Source:
Returns:
- Type
- Promise
-
dbg(name, args)
-
Proxy for Mapper#dbg.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#dbg.
- Inherited From:
- Source:
-
defineMapper(name, opts)
-
Create a new mapper and register it in this container.
Parameters:
Name Type Argument Description name
string Name under which to register the new Mapper. Mapper#name will be set to this value.
opts
Object <optional>
Configuration options. Passed to Container#mapperClass when creating the new Mapper.
- Inherited From:
- Source:
Returns:
- Type
- Mapper
Example
import {Container} from 'js-data' const container = new Container({ mapperDefaults: { foo: 'bar' } }) const userMapper = container.defineMapper('user') userMapper.foo // "bar"
-
destroy(name, id, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
id
string | number Passed to Mapper#destroy.
opts
Object <optional>
Passed to Mapper#destroy. See Mapper#destroy for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
destroyAll(name, query, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
query
Object <optional>
Passed to Mapper#destroyAll.
opts
Object <optional>
Passed to Mapper#destroyAll. See Mapper#destroyAll for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
emit(event, args)
-
Trigger an event on this Component.
Parameters:
Name Type Argument Description event
string Name of event to emit.
args
* <optional>
<repeatable>
Arguments to pass to any listeners.
- Inherited From:
- Source:
-
find(name, id, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
id
string | number Passed to Mapper#find.
opts
Object <optional>
Passed to Mapper#find.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
findAll(name, query, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
query
Object <optional>
Passed to Model.findAll.
opts
Object <optional>
Passed to Model.findAll.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
getAdapter(name)
-
Return the registered adapter with the given name or the default adapter if no name is provided.
Parameters:
Name Type Argument Description name
string <optional>
The name of the adapter to retrieve.
- Inherited From:
- Source:
Returns:
The adapter.
- Type
- Adapter
-
getAdapterName(opts)
-
Return the name of a registered adapter based on the given name or options, or the name of the default adapter if no name provided.
Parameters:
Name Type Argument Description opts
Object | string <optional>
The name of an adapter or options, if any.
- Inherited From:
- Source:
Returns:
The name of the adapter.
- Type
- string
-
getAdapters()
-
Return the registered adapters of this container.
- Inherited From:
- Source:
Returns:
- Type
- Adapter
-
getCollection(name)
-
TODO
Parameters:
Name Type Description name
string Name of the LinkedCollection to retrieve.
- Source:
Returns:
- Type
- LinkedCollection
-
getMapper(name)
-
Return the mapper registered under the specified name.
Parameters:
Name Type Description name
string - Inherited From:
- Source:
Returns:
- Type
- Mapper
Example
import {Container} from 'js-data' const container = new Container() const userMapper = container.defineMapper('user') userMapper === container.getMapper('user') // true
-
getSchema(name)
-
Proxy for Mapper#is.
Parameters:
Name Type Description name
string Name of the Mapper to target.
- Inherited From:
- Source:
-
is(name, args)
-
Proxy for Mapper#is.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#is.
- Inherited From:
- Source:
-
log(name, args)
-
Proxy for Mapper#log.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#log.
- Inherited From:
- Source:
-
off(event, listener)
-
Remove an event listener from this Component. If no listener is provided, then all listeners for the specified event will be removed. If no event is specified then all listeners for all events will be removed.
Parameters:
Name Type Argument Description event
string <optional>
Name of event to unsubsribe to.
listener
function <optional>
Listener to remove.
- Inherited From:
- Source:
-
on(event, listener, ctx)
-
Register a new event listener on this DataStore.
Proxy for Container#on. If an event was emitted by a Mapper or Collection in the DataStore, then the name of the Mapper or Collection will be prepended to the arugments passed to the provided event handler.
Parameters:
Name Type Argument Description event
string Name of event to subsribe to.
listener
function Listener function to handle the event.
ctx
* <optional>
Optional content in which to invoke the listener.
- Overrides:
- Source:
-
registerAdapter(name, adapter, opts)
-
Register an adapter on this container under the given name. Adapters registered on a container are shared by all mappers in the container.
Parameters:
Name Type Argument Description name
string The name of the adapter to register.
adapter
Adapter The adapter to register.
opts
Object <optional>
Configuration options.
Properties
Name Type Argument Default Description default
boolean <optional>
false Whether to make the adapter the default adapter for all Mappers in this container.
- Inherited From:
- Source:
Example
import {Container} from 'js-data' import HttpAdapter from 'js-data-http' const container = new Container() container.registerAdapter('http', new HttpAdapter, { default: true })
-
sum(name, field, query, opts)
-
Proxy for Mapper#sum.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
field
string Passed to Model.sum.
query
Object <optional>
Passed to Model.sum.
opts
Object <optional>
Passed to Model.sum.
- Inherited From:
- Source:
Returns:
- Type
- Promise
-
toJSON(name, args)
-
Proxy for Mapper#toJSON.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#toJSON.
- Inherited From:
- Source:
-
update(name, id, record, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
id
string | number Passed to Mapper#update.
record
Object Passed to Mapper#update.
opts
Object <optional>
Passed to Mapper#update. See Mapper#update for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
updateAll(name, props, query, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
props
Object Passed to Mapper#updateAll.
query
Object <optional>
Passed to Mapper#updateAll.
opts
Object <optional>
Passed to Mapper#updateAll. See Mapper#updateAll for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise
-
updateMany(name, records, opts)
-
TODO
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
records
Array.<Object> | Array.<Record> Passed to Mapper#updateMany.
opts
Object <optional>
Passed to Mapper#updateMany. See Mapper#updateMany for more configuration options.
- Overrides:
- Source:
Returns:
- Type
- Promise