new Container(opts)
import {Container} from 'js-data'
The Container
class is a place to store Mapper instances.
Without a container, you need to manage mappers yourself, including resolving circular dependencies among relations. All mappers in a container share the same adapters, so you don't have to add each adapter to all of your mappers.
Parameters:
Name | Type | Argument | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object |
<optional> |
Configuration options. Properties
|
- Source:
Returns:
- Type
- Container
Examples
import {Mapper} from 'js-data' import HttpAdapter from 'js-data-http' const adapter = new HttpAdapter() const userMapper = new Mapper({ name: 'user' }) userMapper.registerAdapter('http', adapter, { default: true }) const commentMapper = new Mapper({ name: 'comment' }) commentMapper.registerAdapter('http', adapter, { default: true }) // This might be more difficult if the mappers were defined in different // modules. userMapper.hasMany(commentMapper, { localField: 'comments', foreignKey: 'userId' }) commentMapper.belongsTo(userMapper, { localField: 'user', foreignKey: 'userId' })
import {Container} from 'js-data' import HttpAdapter from 'js-data-http' const container = new Container() // All mappers in container share adapters container.registerAdapter('http', new HttpAdapter(), { default: true }) // These could be defined in separate modules without a problem. container.defineMapper('user', { relations: { hasMany: { comment: { localField: 'comments', foreignKey: 'userId' } } } }) container.defineMapper('comment', { relations: { belongsTo: { user: { localField: 'user', foreignKey: 'userId' } } } })
Extends
Members
-
mapperClass :function
-
Constructor function to use in Container#defineMapper to create a new mapper.
Type:
- function
- Source:
-
mapperDefaults :Object
-
Defaults options to pass to Container#mapperClass when creating a new mapper.
Type:
- Object
- Source:
Methods
-
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.
- Source:
Returns:
- Type
- Promise
-
create(name, record, opts)
-
Proxy for Mapper#create.
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)
-
Proxy for Mapper#createMany.
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
-
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.
- 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.
- Overrides:
- 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.
- 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)
-
Proxy for Mapper#destroy.
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
-
destroyAll(name, query, opts)
-
Proxy for Mapper#destroyAll.
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.
- Source:
Returns:
- Type
- Promise
-
emit(name, args)
-
Proxy for Mapper#find.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#emit.
- Source:
-
find(name, id, opts)
-
Proxy for Mapper#find.
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)
-
Proxy for Mapper#createRecord.
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.
- 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.
- Source:
Returns:
The name of the adapter.
- Type
- string
-
getAdapters()
-
Return the registered adapters of this container.
- Source:
Returns:
- Type
- Adapter
-
getMapper(name)
-
Return the mapper registered under the specified name.
Parameters:
Name Type Description name
string - 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.
- 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.
- 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.
- Overrides:
- Source:
-
off(name, args)
-
Proxy for Mapper#off.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#off.
- Source:
-
on(name, args)
-
Proxy for Mapper#on.
Parameters:
Name Type Argument Description name
string Name of the Mapper to target.
args
* <repeatable>
Passed to Mapper#on.
- 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.
- 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.
- 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.
- Source:
-
update(name, id, record, opts)
-
Proxy for Mapper#update.
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)
-
Proxy for Mapper#updateAll.
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)
-
Proxy for Mapper#updateMany.
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