| Authors: | Kris Zyp |
|---|---|
| Project owner: | Kris Zyp |
| since: | 1.6.0 |
Contents
dojo/store is an uniform interface for the access and manipulation of stored data. dojo/store is intended to supersede, integrate, and improve upon the dojo/data API and dojox/storage API with a simple, easy to implement and extend API, based on HTML5/W3C's IndexedDB object store API. The dojox/storage API is already a compatible subset of the Dojo object store API.
Every method in the API is optional, it's presence indicating support for that feature. Every method can return a promise (except where noted otherwise) to represent an asynchronous completion of the action. (Some of these are still wavering a bit in W3C's object store API):
| Method | Description |
|---|---|
| get(id) | Retrieves an object by its identifier, returning the object. |
| query(query, options) | Queries the store using the provided query. The returned value should be an array or a promise with forEach() , map(), filter(), reduce(), subscribe(), and optional close() and watch() methods, and a total property (the total may be a promise). The options parameter may include the following properties (all are optional):
|
| put(object, options) | Saves the given object. The (optional) options argument may be an object with the following properties (each of these is optional and advisory, stores are not required to implement or respond to each property):
|
| add(object, options) | Create a new object. The (optional) options argument is defined the same as put() (except overwrite is assumed to be false). |
| remove(id) | Delete the object by id. |
| getIdentity(object) | Returns an object's identity |
| queryEngine(query, options) | This takes a query and query options and returns a function that can execute the provided query on a JavaScript array. The queryEngine may be replace to provide more sophisticated querying capabilities. The returned query function may have a matches property that can be used to determine if an object matches the query. |
| transaction() | Starts a transaction and returns a transaction object. The transaction object should include:
Note that a store user might not call transaction() prior to using put, delete, etc. in which case these operations effectively could be thought of as "auto-commit" style actions. Also note that within a transaction, notification events may still occur immediately, rather after a commit. Consequently, aborting a transaction may require sending new notification events to notify of the rollback of the data. |
| getChildren(object, options) | Returns the children of an object. The options parameter may include the same properties as query() options. |
| getMetadata(object) | Returns any metadata about the object. This may include attribution, cache directives, history, or version information. |
| Property | Type | Description |
|---|---|---|
| idProperty | String | Name of the property to use as the identifier |
| data | Array of Objects | If the store has a collection of cached objects, it can make this available in this property. This is included so an additional layer could add referential integrity cleanup on object deletion (which is a pain to implement). |
Objects returned from a dojo/store should primarily be treated as normal hash objects and have standard JavaScript properties to access their data and modify their data. However, methods may also be defined on the objects returned by the store (once again, they are optional). These methods should not be the object's own properties ( hasOwnProperty(methodName) should return false), but rather should be inherited from one of the object's prototypes). This is to ensure ease of enumeration of data properties. In particular, a store may choose to return objects that are instances of dojo/Stateful (although none of the core stores do this).
When a store is wrapped with dojo/store/Observable, one can listen for changes in data through the observe method on the result set (the object returned from a query). The observe method has the following signature:
| Method | Description |
|---|---|
| observe(listener, includeObjectUpdates) | The listener function is called with following arguments: listener(object, removedFrom, insertedInto);
The includeObjectUpdates argument indicates whether or not to include updates to objects that don't affect the inclusion or order of the changed object. If this is not set to true, object updates that don't result in addition, removal, or change in order won't trigger the listener. |
| close() | When close() is called on a result set, notifications will no longer be fired. |
The following stores, store wrappers, and utilities are part of Dojo Core. These provide a solid base of good modular components for using stores and building more complex store technology. The following two core stores based on the typical pattern of in-memory and server-based data stores:
An in-memory object store that queries, modifies, and accesses client-side in-memory data. This can be created with a simple array of JavaScript objects.
An server-oriented JSON/REST object store that queries, modifies, and accesses data through RESTful HTTP requests. This would fulfill the conceptual role of dojox/data/JsonRestStore, dojox/data/QueryReadStore and dojox/data/ServiceStore.
There is also an adapter store for using legacy dojo/data stores with the new API:
And there is an adapter that allows you to use a new dojo/store with the old dojo/data API:
We are also moving in the direction of providing composable functionality by providing store "wrappers" or store "middleware" that takes a store and adds functionality. Several key store wrappers:
This augments a store with the data monitoring capability, adding a observe method on the query result sets that notifies of data changes.
Adds caching capability to the store. This eliminates the need for a base store to deal with caching concerns.
With this one can easily mix and match wrappers and base stores to achieve various types of functionality. A common pattern may be:
require(["dojo/store/Memory", "dojo/store/Observable"], function(Memory, Observable){
var store = Observable(new Memory({ data: someData }));
});
There are also a couple of utility modules:
dojo/store/util/SimpleQueryEngine
This is basic query engine that provides simple object hash style filtering or function based filtering.
This utility will take an array or a promise for an array and return a result set object with all the standard iterative methods that should be available on a result set (forEach(), map(), and filter()).
The purpose of using this style of notifications in dojo/store (instead of the dojo/data notification style of events on the store) is to deal with several problems that have been observed with dojo/data notifications. First, it neglects that fact that most of the time users only want to listen to events from the queried subset of the items in the store, and that subscriptions can be costly. While subscriptions are usually cheap on the client side, carte blanche subscriptions can actually be very expensive on the server side (with Comet-style notifications), forcing the server to send excessive events and then forcing the client to filter them.
Also, this subscription deals with an issue that has been experienced with dojo/data in that often new items and item changes can affect query results in ways that the widget can not understand because the queries are opaque to the widget. For example with a grid, all onNew items result in an addition of a row, regardless of whether they are a new item and actually match the query. The grid itself knows nothing (and should know nothing) of how queries work, and so it can't filter these events. By putting notifications on the query action itself, notifications can be stated in terms of how they affect a given query result set, which is what the widget ultimately cares about.
Different event names have been used to be clear that the semantics are different than the dojo/data notifications. The distinctive events are defined by how they affect the query result set (not whether they have come into or gone out of existence by some definition of existence). Also, the onUpdate() applies to an entire object, not per property modifications.