The constructor function returned by a persistence.define call
cannot only be used to instantiate new objects, it also has some
useful methods of its own:
EntityName.all([session]) returns a query collection (see below) containing
all persisted instances of that object. The session argument is optional and only required when persistence.js is used in a server-side context.EntityName.load([session], [tx], id, callback) loads an particular
object from the database by id or returns null if it has not been
found.EntityName.findBy([session], [tx], property, value, callback) searches
for a particular object based on a property value (this is assumed to
be unique), the callback function is called with the found object or
null if it has not been found.A core concept of persistence.js is the QueryCollection. A
QueryCollection represents a (sometimes) virtual collection that can
be filtered, ordered or paginated. QueryCollections are somewhate
inspired by Google AppEngine's Query
class.
A QueryCollection has the following methods:
filter(property, operator, value)QueryCollection that adds a filter, filtering a
certain property based on an operator and value. Supported operators
are '=', '!=', '<', '<=', '>', '>=', 'in' and 'not in'. Example:
.filter('done', '=', true)or(filter)QueryCollection that contains items either matching
the filters specified before calling or, or the filter represented
in the argument. The filter argument is of a Filter type, there
are three types of filters:
persistence.PropertyFilter, which filters on properties (internally called when filter(...) is used.new persistence.PropertyFilter('done', '=', true)persistence.AndFilter, which is passed two filter objects as arguments, both of which should be true.
Example: new persistence.AndFilter(new persistence.PropertyFilter('done', '=', true), new persistence.PropertyFilter('archived', '=', true))persistence.OrFilter, which is passed two filter objects as arguments, one of which should be true.
Example: new persistence.OrFilter(new persistence.PropertyFilter('done', '=', true), new persistence.PropertyFilter('archived', '=', true))and(filter)or(filter) except that both conditions should hold for items to be in the collection. order(property, ascending)QueryCollection that will order its results by the
property specified in either an ascending (ascending === true) or
descending (ascending === false) order.limit(n)QueryCollection that limits the size of the result
set to n items. Useful for pagination.skip(n)QueryCollection that skips the first n results.
Useful for pagination.prefetch(rel)QueryCollection that prefetches entities linked
through relationship rel, note that this only works for one-to-one
and many-to-one relationships.add(obj)obj to the collection.remove(obj)obj from the collection.list([tx], callback)each([tx], eachCallback)eachCallback function is invoked on each
element of the result objects.forEach([tx], eachCallback)eachone([tx], callback)
Asynchronously fetches the first element of the collection, or null if none.destroyAll([tx], callback)
Asynchronously removes all the items in the collection. Important: this does
not only remove the items from the collection, but removes the items themselves!count([tx], callback)
Asynchronously counts the number of items in the collection. The arguments passed
to the callback function is the number of items.Query collections are returned by:
EntityName.all(), e.g. Task.all()task.tagsEntityName.search() functionExample:
var allTasks = Task.all().filter("done", '=', true).prefetch("category")
.order("name", false).limit(10);
allTasks.list(null, function (results) {
results.forEach(function (r) {
console.log(r.name)
window.task = r;
});
});