Class: Container

Container


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
Name Type Argument Description
MapperClass function <optional>

Constructor function to use in Container#defineMapper to create a new mapper.

mapperDefaults Object <optional>

Defaults options to pass to Container#MapperClass when creating a new mapper.

Source:
Returns:
Type
Container
Examples

Without Container

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

With Container

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

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


<static> extend(props, classProps)

Create a Container subclass.

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 Container.

Type
function
Example
var MyContainer = Container.extend({
  foo: function () { return 'bar' }
})
var container = new MyContainer()
container.foo() // "bar"

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"

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

Mapper#name.

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.

Source:
Example
import {Container} from 'js-data'
import HttpAdapter from 'js-data-http'
const container = new Container()
container.registerAdapter('http', new HttpAdapter, { default: true })