Class: DataStore

DataStore


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


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


<static> extend(props, classProps)

Create a DataStore subclass.

var MyDataStore = DataStore.extend({
  foo: function () { return 'bar' }
})
var store = new MyDataStore()
store.foo() // "bar"
Parameters:
Name Type Argument Default Description
props Object <optional>
{}

Properties to add to the prototype of the subclass.

classProps Object <optional>
{}

Static properties to add to the subclass.

Source:
Returns:

Subclass of DataStore.

Type
function

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.

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.

Source:
Returns:
Type
Promise

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.

Source:
Returns:
Type
Promise

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.

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.

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 DataStoreCollection to retrieve.

Source:
Returns:
Type
DataStoreCollection

getMapper(name)

Return the mapper registered under the specified name.

Parameters:
Name Type Description
name string

Mapper#name.

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

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 })

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.

Source:
Returns:
Type
Promise

updateAll(name, query, props, opts)

TODO

Parameters:
Name Type Argument Description
name string

Name of the Mapper to target.

query Object <nullable>

Passed to Model.updateAll.

props Object

Passed to Model.updateAll.

opts Object <optional>

Passed to Model.updateAll. See Model.updateAll for more configuration options.

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.

Source:
Returns:
Type
Promise