seraph_model provides some convenient functions for storing and retrieving typed nodes from a neo4j database. It is intended to work with seraph.
### Quick examplevar db = 'http://localhost:7474'var model = ; var User = ; User
Documentation
How to do things
Model instance methods ## Creating a new model
seraph_model(seraphDbObject, modelTypeName)
You can create a new model by calling the function returned by requiring
seraph_model
. There are no instances of this model, only objects, which are
passed to the model itself in order to perform work on it. Much like seraph
itself.
It works by indexing each object under a nodes
index. Each different model is
simply an item in that index, with all of the instances of that model attached
to it.
Example
var db = 'http://localhost:7474';var Beer = db 'beer'; Beer;
Preparers are functions that are called upon an object to transform it before saving it. A preparer is a function that takes an object and a callback, and calls back with an error and the updated object.
Preparers can also do validation. If a preparer returns an error, it will be passed to the save callback as if it were a validation error. However, if you just want to do validation and not mutate the object, use a validator instead.
You can manually prepare an object by using the model.prepare function.
Example
var { fs;} modelpreparers; model; mode;
You can manually validate an object by using the model.validate function.
Example
var { if objectage >= 21 ; else ; } modelvalidators; model; model;
Fields are a way of whitelisting which properties are allowed on an object to be saved. Upon saving, all properties which are not in the whitelist are stripped.
Example
beerfields = 'name' 'brewery' 'style'; beer
Saves or updates an object in the database. The steps for doing this are:
object
is prepared using model.prepareobject
is validated using model.validate. If validation fails, the callback is called immediately with an error.object
is saved using seraph.saveobject
is indexed as this type of model using seraph.index
There is also the internal model._unsafeSave
method which only performs
steps 3 & 4. However, use of that method is discouraged and unsupported.
The object returned is given an ID. See seraph.save for more information and an example (they are operationally identical).
## model.findAll(callback(err, allOfTheseModels))Finds all of the objects that were saved with this type.
## model.where(callback(err, matchingModels))This is a operationally similar to seraph.find, but is restricted to searching for other objects indexed as this kind of model. See the quick example for an example of this in action.
## model.prepare(object, callback(err, preparedObject))Prepares an object by using the model.preparers
array of functions to mutate
it. For more information, see Adding preparers
Validates that an object is ready for saving by calling each of the functions in
the model.validators
array. For more information, see
Adding validators
This is an array of property names which acts as a whitelist for property names in objects to be saved. If it is set, any properties in objects to be saved that are not included in this array are stripped. See Setting a properties whitelist for more information and examples.
## model.cypherStart()Returns the appropriate START point for a cypher query for this kind of model. Example:
var beer = ; beer; // -> 'node:nodes(type = "Beer")'
You can then use this in a seraph find
or query
call. Example:
db;