Racer model explained - kristianmandrup/racer-mw GitHub Wiki
All model operations happen on paths which represent nested JSON objects. These paths must be globally unique within a particular database and Redis journal
For example, the model:
{
title: 'Fruit store',
fruits: [
{ name: 'banana', color: 'yellow' },
{ name: 'apple', color: 'red' },
{ name: 'lime', color: 'green' }
]
}
Would have paths like title
, fruits.1
, and fruits.0.color
. Any path segment that is a number must be an index of an array.
Non-synced Page data
Almost all non-synced data within an application should be stored underneath the _page
local collection. This enables Derby to automatically cleanup as the user navigates between pages. Right before rendering a new page, Derby calls model.destroy('_page')
, which removes all data, references, event listeners, and reactive functions underneath the _page
collection.
Scoped models
Scoped models provide a more convenient way to interact with commonly used paths. They support the same methods, and they provide the path argument to accessors, mutators, event methods, and subscription methods. Also, wherever a path is accepted in a racer method, a scoped model can typically be used as well.
scoped = model.at ( subpath )
The subpath
is the relative reference path to set. The path is appended if called on a scoped model.
scoped
, is the scoped model returned.
scoped = model.at ( subpath )
// retrieve scoped model
room = model.at('_page.room');
room.at('name').set('Fun room');
console.log(room.get()); // => {name: 'Fun room'}
GUIDs
Models provide a method to create globally unique ids. These can be used as part of a path or within mutator methods.
guid = model.id ( )
guid
returns a globally unique identifier that can be used for model operations
Queries
Racer can fetch or subscribe to queries based on a model value or a database-specific query. When fetching or subscribing to a query, all of the documents associated with that query are also fetched or subscribed.
query = model.query ( collectionName, path )
collectionName
: The name of a collection from which to get documents
path
: A model path whose value contains a documentId or an array of documentIds
query = model.query ( collectionName, databaseQuery )
collectionName
: The name of a collection from which to get documents
databaseQuery
: A query in the database native format, such as a MongoDB query
The livedb-mongo adapter supports most MongoDB queries that you could pass to the Mongo find()
method. See the Mongo DB query documentation and the query selectors reference. Only full documents may be returned. Also, cursor methods are not directly available, so $orderby
should be used for sorting, and skips and limits should be specified as $skip
and $limit
. There is currently no findOne()
equivalent—Use $limit: 1
instead.
After a query is subscribed or fetched, its results can be returned directly via query.get()
. It is also possible to to create a live-updating results set in the model via query.ref()
.
results = query.get ( )
results
: Creates and returns an array of each of the document objects matching the query
scoped = query.ref(path)
path
: Local path at which to create an updating refList of the queries results
scoped
: Returns a model scoped to the path at which results are output