Collections are the most common Resource Type in Deployd. They allow the user to store and load data from their app's Store. Behind the scenes, they validate incoming requests and execute event scripts for get
, post
, put
, delete
, and validate
. If all event scripts execute without error (or cancel()
ing), the request is proxied to the collection's Store
.
A Collection
inherits from Resource
. Any constructor that inherits from Collection
must include its own Collection.external
prototype object.
Example inheriting from Collection
:
var Collection = require('deployd/lib/resources/collection');
var util = require('util');
function MyCollection(name, options) {
Collection.apply(this, arguments);
}
MyCollection.external = Collection.external;
util.inherits(MyCollection, Collection);
The backing persistence abstraction layer. Supports saving and reading data from a database. See Store for more info.
Validate the request body
against the Collection's properties
and return an object containing any errors
.
body
{Object}The object to validate
create
{Boolean}Should validate a new object being created
errors
{Object}Sanitize the request body
against the Collection's properties
object and return an object containing only properties that exist in the
collection.config.properties
object.
body
{Object}sanitized
{Object}Sanitize the request query
against the collection.properties
and return an object containing only properties that exist in the
collection.properties
object.
query
{Object}sanitizedQuery
{Object}Parse the ctx.url
for an id. Override this to change how an object's id is parsed out of a url.
ctx
{Context}Find all the objects in a collection that match the given ctx.query
. Then execute a get
event script, if one exists, using each object found. Finally call fn(err)
passing an error
if one occurred.
ctx
{Context}fn(err)
Execute a delete
event script, if one exists, using each object found. Then remove a single object that matches the ctx.query.id
. Finally call fn(err)
passing an error
if one occurred.
ctx
{Context}fn(err)
First execute a validate
event script if one exists. If the event does not error, try to save the ctx.body
into the store. If ctx.body.id
exists, perform an update
and execute the put
event script. Otherwise perform an insert
and execute the post
event script. Finally call fn(err)
, passing an error
if one occurred.
ctx
{Context}fn(err)
Let us know if you have any ideas to improve our docs. Open an issue on github, send us an email, or tweet us.
This entire site, including documentation written in markdown is available on github. Pull requests are appreciated!