-
Notifications
You must be signed in to change notification settings - Fork 2
Server ORM
The server's ORM is a little different from most. It makes no attempts to validate anything, and instead provides a straight passthrough.
The primary object used to shuttle data around is a FieldSet (or, more specifically, a subclass thereof). A FieldSet is essentially a bidirectional diff bound to a collection. The best way to explain them is to look at some examples.
var db = require('db');
var Listing = function listing() {};
Listing.prototype = db.FieldSet;
The function name in this case is important (required, in fact) -- it's what's used to associate a the fieldset with a database collection.
Custom methods can be added to any FieldSet subclass to make working with them easier. However, the base FieldSet defines a few methods itself that are used by the ORM, and can be overridden to customize behavior. These are listed below.
- genId([callback]) - Generates an ID for this fieldset. The default implementation uses a string-form uuid. If this method is overridden, the generated ID should be unique across the entire collection.
There are currently two operations available: get and apply.
This operation fetches data from the database, into a fieldset. If the fieldset passed to get does not have an _id field, an error is raised.
Example:
var l = new Listing();
l._id = 'deadbeef12';
db.get(l, function(err) { console.log(l) });
The contents of the database entry for listing:deadbeef12 are merged into l.
This operation applies one or many fieldsets to the database as diffs. Any field present in the fieldset is updated in the database. The modified field on the fieldset will always be updated when it is applied. If a fieldset supplied to this operation is missing an _id field, it will be created by calling the fieldset's getId method.
Example:
var a1 = new Auth();
a.email = 'example@example.com';
a.password = 'DEADBEEF';
var a2 = new Auth();
a.email = 'example2@example.com';
a.password = 'DEADBEEF';
a._id = 'asdf';
db.apply(a1, a2);
In this example, a1 will be inserted and a2 updated (assuming the id is valid).
create
update
delete