Constructor
new Store(options)
Initializer for Store class
Name | Type | Description |
---|---|---|
options | object | options to use for initialization |
Members
data :object
Stores data by type. { todos: { records: observable.map(), // records by id cache: observable.map(), // cached ids by url meta: observable.map() // meta information by url } }
- object
- Default Value
- {}
lastResponseHeaders :object
The most recent response headers according to settings specified as headersOfInterest
- object
- Default Value
- {}
loadedStates :Map
Map of data that has been loaded into the store. This can be observed to know if a given type (or tag) has finished loading.
- Key is a tag that is either the model type or a custom value
- Falue is a Set of JSON-encoded objects with unique urls and queryParams Set[JSON.stringify({ url, type, queryParams, queryTag })]
- Map
loadingStates :Map
Map of data that is in flight. This can be observed to know if a given type (or tag) is still processing.
- Key is a tag that is either the model type or a custom value
- Falue is a Set of JSON-encoded objects with unique urls and queryParams Set[JSON.stringify({ url, type, queryParams, queryTag })]
- Map
pauseSnapshots :boolean
True if models in the store should stop taking snapshots. This is useful when updating records without causing records to become 'dirty', for example when initializing records using add
- boolean
Methods
add(type, props, options) → {object|Array}
Adds an instance or an array of instances to the store. Adds the model to the type records index Adds relationships explicitly. This is less efficient than adding via data if there are also inverse relationships.
const todo = store.add('todos', { name: "A good thing to measure" })
todo.name
=> "A good thing to measure"
const todoArray = [{ name: "Another good thing to measure" }]
const [todo] = store.add('todos', [{ name: "Another good thing to measure" }])
todo.name
=> "Another good thing to measure"
Name | Type | Description |
---|---|---|
type | string | the model type |
props | object | | the properties to use |
options | object | currently supports |
the new record or records
- Type:
- object |
Array
bulkCreate(type, records, options) → {Promise}
Save a collection of new records via a bulk-supported JSONApi endpoint. All records need to be of the same type and not have an existing id.
Name | Type | Description |
---|---|---|
type | string | the model type |
records | Array | to be bulk created |
options | object | {queryParams, extensions} |
the created records
- Type:
- Promise
bulkSave(type, records, options) → {Promise}
Saves a collection of records via a bulk-supported JSONApi endpoint. All records need to be of the same type.
Name | Type | Description |
---|---|---|
type | string | the model type |
records | Array | records that will be bulk saved |
options | object | {queryParams, extensions} |
the saved records
- Type:
- Promise
bulkUpdate(type, records, options) → {Promise}
Updates a collection of records via a bulk-supported JSONApi endpoint. All records need to be of the same type and have an existing id.
Name | Type | Description |
---|---|---|
type | string | the model type |
records | Array | array of records to be bulk updated |
options | object | {queryParams, extensions} |
the saved records
- Type:
- Promise
clearCache(type) → {Set}
Clears the cache for provided record type
Name | Type | Description |
---|---|---|
type | string | the model type |
the cleared set
- Type:
- Set
createModelFromData(data, options) → {object}
Helper to create a new model
Name | Type | Description |
---|---|---|
data | object | id, type, attributes and relationships |
options | object | currently supports |
model instance
- Type:
- object
createOrUpdateModelFromData(data) → {object}
Creates or updates a model
Name | Type | Description |
---|---|---|
data | object | the object will be used to update or create a model |
the record
- Type:
- object
createOrUpdateModelsFromData(data) → {Array}
Create multiple models from an array of data. It will only build objects with defined models, and ignore everything else in the data.
Name | Type | Description |
---|---|---|
data | Array | the array of jsonapi data |
an array of the models serialized
- Type:
- Array
deleteLoadingState(state)
Removes a loading state. If that leaves an empty array for the map key in loadingStates
, will also delete the set. Also adds to loadedStates.
Name | Type | Description |
---|---|---|
state | object | the state to remove |
fetch(url, options) → {Promise}
Wrapper around fetch applies user defined fetch options
Name | Type | Description |
---|---|---|
url | string | the url to fetch |
options | object | override options to use for fetching |
the data from the server
- Type:
- Promise
fetchAll(type, options) → {Promise}
Finds all records with the given type
. Always fetches from the server.
Name | Type | Description |
---|---|---|
type | string | the type to find |
options | object | query params and other options |
Promise.resolve(records) or Promise.reject([Error: [{ detail, status }])
- Type:
- Promise
fetchMany(type, ids, options) → {Promise}
Fetch all records with the given type
and ids
from the server.
Name | Type | Description |
---|---|---|
type | string | the type to get |
ids | string | the ids of the records to get |
options | object | { queryParams } |
Promise.resolve(records) or Promise.reject([Error: [{ detail, status }])
- Type:
- Promise
fetchOne(type, id, options) → {Promise}
Fetches record by id
from the server and returns a Promise.
Name | Type | Description |
---|---|---|
type | string | the record type to fetch |
id | string | the id of the record to fetch |
options | object | { queryParams } |
record result wrapped in a Promise
- Type:
- Promise
fetchUrl(type, queryParams, id, options) → {string}
Builds fetch url based on type, queryParams, id, and options
Name | Type | Description |
---|---|---|
type | string | the type to find |
queryParams | object | params to be used in the fetch |
id | string | a model id |
options | object | options for fetching |
a formatted url
- Type:
- string
findAll(type, options) → {Promise}
Finds all records of the given type
. If any records from the given type from url are in the store, it returns those. Otherwise, it fetches all records from the server.
store.findAll('todos') // fetch triggered => [todo1, todo2, todo3]
store.findAll('todos') // no fetch triggered => [todo1, todo2, todo3]
Query params can be passed as part of the options hash. The response will be cached, so the next time findAll
is called with identical params and values, the store will first look for the local result.
store.findAll('todos', { queryParams: { filter: { start_time: '2020-06-01T00:00:00.000Z', end_time: '2020-06-02T00:00:00.000Z' } } })
Name | Type | Description |
---|---|---|
type | string | the type to find |
options | object | { queryParams } |
Promise.resolve(records) or Promise.reject([Error: [{ detail, status }])
- Type:
- Promise
findMany(type, ids, options) → {Promise}
Finds multiple records of the given type
with the given ids
and returns them wrapped in a Promise. If all records are in the store, it returns those. If some records are in the store, it returns those plus fetches all other records. Otherwise, it fetches all records from the server.
store.findMany('todos', [1, 2, 3]) // fetch triggered => [todo1, todo2, todo3]
store.findMany('todos', [3, 2, 1]) // no fetch triggered => [todo1, todo2, todo3]
Name | Type | Description |
---|---|---|
type | string | the type to find |
ids | string | the ids of the records to find |
options | object | { queryParams } |
a promise that will resolve an array of records
- Type:
- Promise
findOne(type, id, options) → {Promise}
Finds a record by id
, always returning a Promise. If available in the store, it returns that record. Otherwise, it fetches the record from the server.
store.findOne('todos', 5) // fetch triggered => Promise(todo) store.findOne('todos', 5) // no fetch triggered => Promise(todo)
Name | Type | Description |
---|---|---|
type | string | the type to find |
id | string | the id of the record to find |
options | object | { queryParams } |
a promise that will resolve to the record
- Type:
- Promise
getAll(type, options) → {Array}
Gets all records with the given type
from the store. This will never fetch from the server.
Name | Type | Description |
---|---|---|
type | string | the type to find |
options | object | options for fetching queryParams |
array of records
- Type:
- Array
getCachedId(type, id) → {object}
Gets a record from store based on cached query
Name | Type | Description |
---|---|---|
type | string | the model type |
id | string | the id to get |
the cached object
- Type:
- object
getCachedIds(type, url) → {Array}
Gets records from store based on cached query
Name | Type | Description |
---|---|---|
type | string | the model type |
url | string | the url that was requested |
array of ids
- Type:
- Array
getCachedRecord(type, id, queryParams) → {object}
Gets single from store based on cached query
Name | Type | Description |
---|---|---|
type | string | the model type |
id | string | the model id |
queryParams | object | the params to be searched |
record
- Type:
- object
getCachedRecords(type, queryParams, id) → {Array}
Gets records from store based on cached query and any previously requested ids
Name | Type | Description |
---|---|---|
type | string | type of records to get |
queryParams | object | query params that were used for the query |
id | string | optional param if only getting 1 cached record by id |
array of records
- Type:
- Array
getKlass(type) → {function}
Helper to look up model class for type.
Name | Type | Description |
---|---|---|
type | string | the model type |
model constructor
- Type:
- function
getMany(type, ids, options) → {Array}
Get all records with the given type
and ids
from the store. This will never fetch from the server.
Name | Type | Description |
---|---|---|
type | string | the type to get |
ids | string | the ids of the records to get |
options | object | { queryParams } |
array of records
- Type:
- Array
getOne(type, id, options) → {object}
Gets a record from the store. Will never fetch from the server. If given queryParams, it will check the cache for the record.
Name | Type | Description |
---|---|---|
type | string | the type to find |
id | string | the id of the record to get |
options | object | { queryParams } |
record
- Type:
- object
getRecord(type, id) → {object}
Gets individual record from store
Name | Type | Description |
---|---|---|
type | string | the model type |
id | number | the model id |
record
- Type:
- object
getRecords(type) → {Array}
Gets records for type of collection
Name | Type | Description |
---|---|---|
type | string | the model type |
array of objects
- Type:
- Array
getRecordsById(type, ids) → {Array}
Get multiple records by id
Name | Type | Description |
---|---|---|
type | string | the model type |
ids | Array | the ids to find |
array or records
- Type:
- Array
init(options)
Entry point for configuring the store
Name | Type | Description |
---|---|---|
options | object | passed to constructor |
initializeErrorMessages(options)
Configure the error messages returned from the store when API requests fail
Name | Type | Description |
---|---|---|
options | object | for initializing the store options for initializing error messages for different HTTP status codes |
initializeModelIndex(models)
Creates the key/value index of model types
Name | Type | Description |
---|---|---|
models | object | a fallback list of models |
initializeNetworkConfiguration(options)
Configures the store's network options
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | string | the parameters that will be used to set up network requests Properties
|
pickAttributes(properties, type) → {object}
Given a set of properties and type, returns an object with only the properties that are defined as attributes in the model for that type.
properties = { title: 'Do laundry', unrelatedProperty: 'Do nothing' }
pickAttributes(properties, 'todos')
=> { title: 'Do laundry' }
Name | Type | Description |
---|---|---|
properties | object | a full list of properties that may or may not conform |
type | string | the model type |
the scrubbed attributes
- Type:
- object
pickRelationships(properties, type) → {object}
Given a set of properties and type, returns an object with only the properties that are defined as relationships in the model for that type.
properties = { notes: [note1, note2], category: cat1, title: 'Fold Laundry' }
pickRelationships(properties, 'todos')
=> {
notes: {
data: [{ id: '1', type: 'notes' }, { id: '2', type: 'notes' }]
},
category: {
data: { id: '1', type: 'categories' }
}
}
Name | Type | Description |
---|---|---|
properties | object | a full list of properties that may or may not conform |
type | string | the model type |
the scrubbed relationships
- Type:
- object
remove(type, id)
Removes a record from the store by deleting it from the type's record map
Name | Type | Description |
---|---|---|
type | string | the model type |
id | string | of record to remove |
reset(type)
Clears the store of a given type, or clears all if no type given
store.reset('todos') // removes all todos from store store.reset() // clears store
Name | Type | Description |
---|---|---|
type | string | the model type |
setLoadingState(options) → {object}
Sets a loading state when a fetch / deserialization is in flight. Loading states are Sets inside of the loadingStates
Map, so multiple loading states can be in flight at the same time. An optional query tag can be passed to identify the particular query.
const todos = store.fetchAll('todos', { queryTag: 'myTodos' }) store.loadingStates.get('myTodos') => Set([JSON.stringify({ url, type, queryParams, queryTag })])
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | object | options that can be used to build the loading state info Properties
|
the loading state that was added
- Type:
- object
updateRecordFromData(record, data)
Updates a record from a jsonapi hash
Name | Type | Description |
---|---|---|
record | object | a Model record |
data | object | jsonapi-formatted data |
updateRecordsFromResponse(promise, records) → {Promise}
Defines a resolution for an API call that will update a record or set of records with the data returned from the API
Name | Type | Description |
---|---|---|
promise | Promise | a response from the API |
records | object | | to be updated |
a resolved promise after operations have been performed
- Type:
- Promise