Polyclay
Polymer modeling clay for node.js. A model schema definition with type validations, dirty-state tracking, and rollback. Models are optionally persistable to CouchDB using cradle, Redis, LevelUP, and Cassandra. Polyclay gives you the safety of type-enforcing properties without making you write a lot of boilerplate.
Installing
npm install polyclay
Building a model
Polyclay builds a model constructor function from options that you pass to its buildClass
function, similar to the way Backbone builds constructors.
var MyModel = polyclaymodel;
Valid options:
properties
: hash of named properties with types; see detailed discussion below
optional
: Array of string names of properties the model might have. Optional properties don't have types, but they do have convenient getters & setters defined for you. They are also persisted in CouchDB if they are present.
required
: Array of string names of properties that must be present. The model will not validate if required properties are missing.
enumerables
: enum; a property that is constrained to values in the given array of strings. The provided setter accepts either integer or string name values. The provided getter returns the string. The value persisted in the database is an int representing the position in the array.
methods
: Hash of methods to add to the object. You can instead decorate the returned constructor prototype with object methods.
initialize
: Function to call as the last step of the returned constructor. Provide an implementation to do any custom initialization for your model.this
will be the newly constructed object.
singular
: The singular noun to use to describe your model. For example, "comment". Added to the model prototype as Model.prototype.singular
.
plural
: The plural noun to use to describe your model; used by the persistence layer to name a database when appropriate. For example, "comments". Added to the model prototype as Model.prototype.plural
.
Valid data types
Polyclay properties must have types declared. Getters and setter functions will be defined for each that enforce the types. Supported types are:
string
: string; undefined and null are disallowed; default is empty string
array
: array; default is [] or new Array()
number
: any number; default is 0
boolean
: true/false; default is false
date
: attribute setter can take a date object, a milliseconds number, or a parseable date string; default is new Date()
hash
: object/hashmap/associative array/dictionary/choose your lingo; default is {}
reference
: pointer to another polyclay object; see documentation below; default is null
References
Reference properties are pointers to other polyclay-persisted objects. When Polyclay builds a reference property, it provides two sets of getter/setters. First, it defines a model.reference_id
property, which is a string property that tracks the key
of the referred-to object. It also defines model.reference()
and model.set_reference()
functions, used to define the js property model.reference
. This provides runtime-only access to the pointed-to object. Inflating it later is an exercise for the application using this model and the persistence layer.
In this example, widgets have an owner
property that points to another object:
var Widget = polyclayModel;polyclay; var widget = ;widgetname = 'eludium phosdex';widgetowner = marvin; // marvin is an object we have already from somewhere else;;widget;
Temporary fields
You can set any other fields on an object that you want for run-time purposes. polyclay prefixes all of its internal properties with __
(double underscore) to avoid conflicts with typical field names.
Validating
Validate an object by calling valid()
. This method returns a boolean: true if valid, false if not. It tests if all typed properties contain valid data and if all required properties. If your model prototype defines a validator()
method, this method will be called by valid()
.
If an object has errors, an errors
field will be set. This field is a hash. Keys are the names of invalid fields and values are textual descriptions of the problem with the field.
invalid data
: property value does not match the required type
missing
: required property is missing
Methods added to model prototypes
obj.valid()
Returns true if all required properties are present, the values of all typed properties are acceptable, and validator()
(if defined on the model) returns true.
obj.rollback()
Roll back the values of fields to the last stored value. (Probably could be better.)
obj.serialize()
Serialize the model as a hash. Includes optional properties.
obj.toJSON()
Serialize the model as a string by calling JSON.stringify()
. Includes optional properties.
obj.clearDirty()
Clears the dirty bit. The object cannot be rolled back after this is called. This is called by the persistence layer on a successful save.
Persisting in CouchDB, Redis, LevelUP, or Cassandra
Four key/value store adapters exist for polyclay:
- polyclay-couch: CouchDB
- polyclay-redis: Redis
- polyclay-levelup: LevelUP
- polyclay-cassandra: Cassandra
Documentation for those adapters will eventually move from here to those repos.
Once you've built a polyclay model, you can mix persistence methods into it:
polyclay;polyclay;``` You can then set up its access to CouchDB by giving it an existing Cradle connection object plus the name of the database where this model should store its objects. The couch adapter wants two fields in its options hash: a cradle connection and a database name. For instance: ```javascriptvar adapterOptions = connection: dbname: 'widgets';ModelFunction;``` If you do not pass a dbname, the adapter will fall back to using the model's `plural`. This is often the expected name for a database. Every model instance has a pointer to the adapter on its `adapter` field. The adapter in turn gives you access to the cradle connection on `objadapterconnection` and the database on `objadapterdb`. For the redis adapter, specify host & port of your redis server. The 'dbname' option is used to namespace keys. The redis adapter will store models in hash keys of the form `<dbname>:<key>`. It will also use a set at key `<dbname>:ids` to track model ids. ```javascriptvar options = host: 'localhost' port: 6379 dbname: 'widgets' // optional;ModelFunction;``` The redis client is available at `objadapterredis`. The db name falls back to the model plural if you don't include it. The dbname is used to namespace model keys. For LevelUP: ```javascriptvar options = dbpath: '/path/to/leveldb/dir' dbname: 'widgets' // optional;ModelFunction;``` The Levelup object is available at `objadapterdb`. The attachments data store is available at `objadapterattachdb`. ### Defining views You can define views to be added to your couch databases when they are created. Add a `design` field to your constructor function directly. Let's add some simple views to the Widget model we created above, one to fetch widgets by owner and one to fetch them by name. ```javascriptWidgetdesign = views: by_owner: map: "function(doc) {\n emit(doc.owner_id, doc);\n}" language: "javascript" by_name: map: "function(doc) {\n emit(doc.name, doc);\n}" language: "javascript" ;``` Call `Widget` to create the 'widgets' database in your CouchDB instance. It will have a design document named "_design/widgets" with the two views above defined. The provision method nothing for Redis- or LevelUP-backed models. ### Persistence class methods All functions return promises if a callback is not provided. `;``` ## Events Polyclay uses [LucidJS](http://robertwhurst.github.io/LucidJS/) to emit events when interesting things happen to your model object. `change`: when any property has been changed `change<prop>`: after a specific property has been changed `update`: after the objects's properties have been updated in `` `rollback`: after the object has been rolled back to a previous state `after-load`: after the object has been loaded from storage & a model instantiated `before-save`: before the object is saved to storage in `` `after-save`: after a save to storage has succeeded, before callback `before-destroy`: before deleting the object from storage in `` `after-destroy`: after deleting the object from storage in `` Here's an example of a property change listener: ```javascriptvar obj = ;obj; widgetname = 'planet x'; ``` See the Lucid documentation for other things you can do with triggering and listening to events on your polyclay objects. ## Mixins A bundle of fields and methods that you wish to add to several model classes while allowing Polyclay to reduce the boilerplate for you. Here's an example. It defines two date fields and a method that uses one: ```javascriptvar HasTimestamps = properties: created: 'date' modified: 'date' methods: { thismodified = Date; } statics: { return lcreated - rcreated; } ; polyclay;``` Mixin objects have three fields. `properties`: A hash of property names & types, exactly as in a base model definition. `methods`: A hash of method names & implementations to add to the model prototype. `statics`: Functions to add to the model function directly; class methods. `custom`: A hash of custom functions to add to the model prototype as getters & setters. Here's a simple example of a custom property: ```javascriptvar SillyNameMixin = custom: name: { return this_name; } { this_name = v; } ``` ## Example Here's an example taken verbatim from the project I wrote this module for: ```javascriptvar Comment = polyclayModel; Commentdesign = views: by_target: map: "function(doc) {\n emit(doc.target_id, doc);\n}" language: "javascript" by_owner: map: "function(doc) {\n emit(doc.owner_id, doc);\n}" language: "javascript" by_owner_target: map: "function(doc) {\n emit(doc.owner_id + '|' + doc.target_id, doc);\n}" language: "javascript" ; Comment{ if typeof owner === 'object' owner = ownerkey; Commentadapterdb;}; polyclay; // as defined abovepolyclay; var opts = connection: dbname: 'comments';Comment;Comment; var comment = ;console;commentversion = "foo"; // throws an errorcommentversion = 2; // sets the attributecommentstate = 'yoinks'; // throws an errorcommentstate = 'deleted';console;commentstate = 1;console;comment;console; // true comment; // version is now 1 and modified the same as createdcommenttempfield = 'whatever'; // not persisted in couch``` ## Contributors C J Silverio @ceejbot Kit Cambridge @kitcambridge ## License MIT