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.
Name | Type | Argument | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object |
<optional> |
Configuration options. Properties
|
Type | Description |
---|---|
Container | Unspecified |
Since | Source | Tutorials |
---|---|---|
3.0.0 | Container.js, line 698 |
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
This class extends the Component class.Members
-
_listeners
-
Event listeners attached to this Component. Do not modify. Use Component#on and Component#off instead.
DetailsType Since Source Object 3.0.0 Component.js, line 7 - Inherited From:
-
mapperClass
-
Constructor function to use in Container#defineMapper to create a new mapper.
DetailsType Since Source Constructor 3.0.0 Container.js, line 457 -
mapperDefaults
-
Defaults options to pass to Container#mapperClass when creating a new Mapper.
DetailsType Since Default value Source Object 3.0.0 {}
Container.js, line 446
Methods
-
<static> extend(props, classProps)
-
Create a subclass of this Container.
Method 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.
Return value:Type Description Constructor Subclass of this Container.
DetailsSince Source 3.0.0 Container.js, line 773 Examplesimport {Container} from 'js-data' const CustomContainerClass = Container.extend({ foo () { return 'bar' } }) const customContainer = new CustomContainerClass() console.log(customContainer.foo()) // "bar"
class CustomContainerClass extends Container { foo () { return 'bar' } } const customContainer = new CustomContainerClass() console.log(customContainer.foo()) // "bar"
-
count(name, query, opts)
-
Wrapper for Mapper#count.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
query
Object <optional>
See Mapper#count.
opts
Object <optional>
See Mapper#count.
Return value:Type Description Promise See Mapper#count.
DetailsSince Source See 3.0.0 Container.js, line 13 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.count('post', { status: 'published' }).then((numPublished) => { console.log(numPublished) // e.g. 45 })
-
create(name, props, opts)
-
Wrapper for Mapper#create.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
props
Object See Mapper#create.
opts
Object <optional>
See Mapper#create.
Return value:Type Description Promise See Mapper#create.
DetailsSince Source See 3.0.0 Container.js, line 37 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.create('post', { title: 'Modeling your data', status: 'draft' }).then((post) => { console.log(post) // { id: 1234, status: 'draft', ... } })
-
createMany(name, records, opts)
-
Wrapper for Mapper#createMany.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
records
Array.<Record> See Mapper#createMany.
opts
Object <optional>
See Mapper#createMany.
Return value:Type Description Promise See Mapper#createMany.
DetailsSince Source See 3.0.0 Container.js, line 64 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.createMany('post', [{ title: 'Modeling your data', status: 'draft' }, { title: 'Reading data', status: 'draft' }]).then((posts) => { console.log(posts[0]) // { id: 1234, status: 'draft', ... } console.log(posts[1]) // { id: 1235, status: 'draft', ... } })
-
createRecord(name, props, opts)
-
Wrapper for Mapper#createRecord.
Note: This method does not interact with any adapter, and does not save any data. It only creates new objects in memory.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
props
Object | Array.<Object> See Mapper#createRecord.
opts
Object <optional>
See Mapper#createRecord.
Return value:Type Description Promise See Mapper#createRecord.
DetailsSince Source See 3.0.0 Container.js, line 95 Exampleimport {Container} from 'js-data' const store = new Container() store.defineMapper('post') const post = PostService.createRecord()
-
dbg(name, args)
-
Wrapper for Mapper#dbg.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
args
* <repeatable>
See Mapper#dbg.
DetailsSince Source Overrides See 3.0.0 Container.js, line 117 Component#dbg -
defineMapper(name, opts)
-
Create a new mapper and register it in this container.
Method 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.
Return value:Type Description Mapper Unspecified DetailsSince Source 3.0.0 Container.js, line 503 Exampleimport {Container} from 'js-data' const store = new Container({ mapperDefaults: { foo: 'bar' } }) const userMapper = store.defineMapper('user') userMapper.foo // "bar"
-
destroy(name, id, opts)
-
Wrapper for Mapper#destroy.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
id
String | Number See Mapper#destroy.
opts
Object <optional>
See Mapper#destroy.
Return value:Type Description Promise See Mapper#destroy.
DetailsSince Source See 3.0.0 Container.js, line 128 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.destroy('post', 1234).then(() => { // Blog post #1234 has been destroyed })
-
destroyAll(name, query, opts)
-
Wrapper for Mapper#destroyAll.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
query
Object <optional>
See Mapper#destroyAll.
opts
Object <optional>
See Mapper#destroyAll.
Return value:Type Description Promise See Mapper#destroyAll.
DetailsSince Source See 3.0.0 Container.js, line 152 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.destroyAll('post', { status: 'draft' }).then(() => { // All "draft" blog posts have been destroyed })
-
emit(event, args)
-
Trigger an event on this Component.
Method parameters:Name Type Argument Description event
String Name of event to emit.
args
* <optional>
<repeatable>
Arguments to pass to any listeners.
DetailsSince Source 3.0.0 Component.js, line 98 - Inherited From:
Examplescollection.on('foo', (msg) => { console.log(msg) // "bar" }) collection.emit('foo', 'bar')
store.on('foo', (msg, val1, val2) => { console.log(msg, val1, val2) // "bar" "beep" "boop" }) store.emit('foo', 'bar', 'beep', 'boop')
-
find(name, id, opts)
-
Wrapper for Mapper#find.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
id
String | Number See Mapper#find.
opts
Object <optional>
See Mapper#find.
Return value:Type Description Promise See Mapper#find.
DetailsSince Source See 3.0.0 Container.js, line 176 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.find('post', 1).then((post) => { console.log(post) // { id: 1, ...} })
-
findAll(name, query, opts)
-
Wrapper for Mapper#createRecord.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
query
Object <optional>
See Mapper#findAll.
opts
Object <optional>
See Mapper#findAll.
Return value:Type Description Promise See Mapper#findAll.
DetailsSince Source See 3.0.0 Container.js, line 200 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.findAll('post', { status: 'published' }).then((posts) => { console.log(posts) // [{ id: 1, ...}, ...] })
-
getAdapter(name)
-
Return the registered adapter with the given name or the default adapter if no name is provided.
Method parameters:Name Type Argument Description name
String <optional>
The name of the adapter to retrieve.
Return value:Type Description Adapter The adapter.
DetailsSince Source 3.0.0 Container.js, line 591 -
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.
Method parameters:Name Type Argument Description opts
Object | String <optional>
The name of an adapter or options, if any.
Return value:Type Description String The name of the adapter.
DetailsSince Source 3.0.0 Container.js, line 609 -
getAdapters()
-
Return the registered adapters of this container.
Return value:Type Description Adapter Unspecified DetailsSince Source 3.0.0 Container.js, line 626 -
getMapper(name)
-
Return the mapper registered under the specified name.
Method parameters:Name Type Description name
String Return value:Type Description Mapper Unspecified DetailsSince Source 3.0.0 Container.js, line 637 Exampleimport {Container} from 'js-data' const container = new Container() const userMapper = container.defineMapper('user') userMapper === container.getMapper('user') // true
-
getSchema(name)
-
Wrapper for Mapper#getSchema.
Return value:Type Description Schema See Mapper#getSchema.
DetailsSince Source See 3.0.0 Container.js, line 224 -
is(name, record)
-
Wrapper for Mapper#is.
Method parameters:Name Type Description name
String Name of the Mapper to target.
record
Object | Record See Mapper#is.
DetailsSince Source See 3.0.0 Container.js, line 235 Exampleimport {Container} from 'js-data' const store = new Container() store.defineMapper('post') const post = store.createRecord() console.log(store.is('post', post)) // true // Equivalent to what's above console.log(post instanceof store.getMapper('post').recordClass) // true
-
log(name, args)
-
Wrapper for Mapper#log.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
args
* <repeatable>
See Mapper#log.
DetailsSince Source Overrides See 3.0.0 Container.js, line 257 Component#log -
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.
Method parameters:Name Type Argument Description event
String <optional>
Name of event to unsubsribe to.
listener
Function <optional>
Listener to remove.
DetailsSince Source 3.0.0 Component.js, line 79 - Inherited From:
Examplescollection.off('add', handler)
record.off('change')
store.off()
-
on(event, listener, ctx)
-
Register a new event listener on this Container.
Proxy for Component#on. If an event was emitted by a Mapper in the Container, then the name of the Mapper will be prepended to the arugments passed to the listener.
Method 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.
DetailsSince Source Overrides 3.0.0 Container.js, line 475 Component#on -
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.
Method 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.
DetailsSince Source Tutorials 3.0.0 Container.js, line 659 Exampleimport {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)
-
Wrapper for Mapper#sum.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
field
String See Mapper#sum.
query
Object <optional>
See Mapper#sum.
opts
Object <optional>
See Mapper#sum.
Return value:Type Description Promise See Mapper#sum.
DetailsSince Source See 3.0.0 Container.js, line 268 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('purchase_order') store.sum('purchase_order', 'amount', { status: 'paid' }).then((amountPaid) => { console.log(amountPaid) // e.g. 451125.34 })
-
toJSON(name, records, opts)
-
Wrapper for Mapper#toJSON.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
records
Record | Array.<Record> See Mapper#toJSON.
opts
Object <optional>
See Mapper#toJSON.
Return value:Type Description Object | Array.<Object> See Mapper#toJSON.
DetailsSince Source See 3.0.0 Container.js, line 293 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('person', { schema: { properties: { name: { type: 'string' }, id: { type: 'string' } } } }) const person = store.createRecord('person', { id: 1, name: 'John', foo: 'bar' }) console.log(store.toJSON('person', person)) // {"id":1,"name":"John","foo":"bar"} console.log(store.toJSON('person', person), { strict: true }) // {"id":1,"name":"John"}
-
update(name, id, record, opts)
-
Wrapper for Mapper#update.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
id
String | Number See Mapper#update.
record
Object See Mapper#update.
opts
Object <optional>
See Mapper#update.
Return value:Type Description Promise See Mapper#update.
DetailsSince Source Tutorials See 3.0.0 Container.js, line 323 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.update('post', 1234, { status: 'published', published_at: new Date() }).then((post) => { console.log(post) // { id: 1234, status: 'published', ... } })
-
updateAll(name, update, query, opts)
-
Wrapper for Mapper#updateAll.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
update
Object See Mapper#updateAll.
query
Object <optional>
See Mapper#updateAll.
opts
Object <optional>
See Mapper#updateAll.
Return value:Type Description Promise See Mapper#updateAll.
DetailsSince Source See 3.0.0 Container.js, line 352 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') const update = { status: draft: published_at: null } const query = { userId: 1234 } store.updateAll('post', update, query).then((posts) => { console.log(posts) // [...] })
-
updateMany(name, records, opts)
-
Wrapper for Mapper#updateMany.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
records
Array.<Object> | Array.<Record> See Mapper#updateMany.
opts
Object <optional>
See Mapper#updateMany.
Return value:Type Description Promise See Mapper#updateMany.
DetailsSince Source See 3.0.0 Container.js, line 379 Exampleimport {Container} from 'js-data' import RethinkDBAdapter from 'js-data-rethinkdb' const store = new Container() store.registerAdapter('rethinkdb', new RethinkDBAdapter(), { default: true }) store.defineMapper('post') store.updateMany('post', [ { id: 1234, status: 'draft' }, { id: 2468, status: 'published', published_at: new Date() } ]).then((posts) => { console.log(posts) // [...] })
-
validate(name, records, opts)
-
Wrapper for Mapper#validate.
Method parameters:Name Type Argument Description name
String Name of the Mapper to target.
records
Array.<Object> | Array.<Record> See Mapper#validate.
opts
Object <optional>
See Mapper#validate.
Return value:Type Description Promise See Mapper#validate.
DetailsSince Source See 3.0.0 Container.js, line 406 Exampleimport {Container} from 'js-data' const store = new Container() store.defineMapper('post', { schema: { properties: { name: { type: 'string' }, id: { type: 'string' } } } }) let errors = store.validate('post', { name: 'John' }) console.log(errors) // undefined errors = store.validate('post', { name: 123 }) console.log(errors) // [{ expected: 'one of (string)', actual: 'number', path: 'name' }]