polyclay
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -5,4 +5,4 @@ exports.Model = require('./lib/polyclay').Model; | ||
exports.mixin = require('./lib/mixins').mixin; | ||
exports.CouchAdapter = require('./lib/couch'); | ||
exports.RedisAdapter = require('./lib/redis'); | ||
exports.LevelupAdapter = require('./lib/levelup'); | ||
exports.CouchAdapter = require('./lib/adapters/couch'); | ||
exports.RedisAdapter = require('./lib/adapters/redis'); | ||
exports.LevelupAdapter = require('./lib/adapters/levelup'); |
@@ -202,3 +202,3 @@ // General storage interface. | ||
{ | ||
if (!self.__dirty && !serialized._attachments) return callback(null, 'OK'); | ||
if (!self.isDirty() && !serialized._attachments) return callback(null, 'OK'); | ||
modelfunc.adapter.update(self, serialized, function(err, response) | ||
@@ -205,0 +205,0 @@ { |
{ | ||
"name": "polyclay", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "a schema-enforcing model class for node with optional key-value store persistence", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# 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](https://github.com/cloudhead/cradle), [Redis](http://redis.io/), or [LevelUP](https://github.com/rvagg/node-levelup). Polyclay gives you the safety of type-enforcing properties without making you write a lot of boilerplate. | ||
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](https://github.com/cloudhead/cradle), to [Redis](http://redis.io/), or to [LevelUP](https://github.com/rvagg/node-levelup). Polyclay gives you the safety of type-enforcing properties without making you write a lot of boilerplate. | ||
@@ -301,24 +301,5 @@ Current version: __1.2.0__ | ||
```javascript | ||
var MixinName = {}; | ||
polyclay.mixin(ModelClass, MixinName); | ||
``` | ||
Here's an example. It defines two date fields and a method that uses one: | ||
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. | ||
`custom`: A hash of custom functions to add to the model prototype as getters & setters. Each entry in this hash must have the following form: | ||
```javascript | ||
'name': | ||
{ | ||
'getter': function() {}, | ||
'setter': function() {} | ||
} | ||
``` | ||
Here's an example mixin: | ||
```javascript | ||
var HasTimestamps = | ||
@@ -336,4 +317,28 @@ { | ||
}; | ||
polyclay.mixin(ModelClass, HasTimestamps); | ||
``` | ||
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. | ||
`custom`: A hash of custom functions to add to the model prototype as getters & setters. | ||
Here's a simple example of a custom property: | ||
```javascript | ||
var SillyNameMixin = | ||
{ | ||
custom: | ||
{ | ||
name: | ||
{ | ||
'getter': function() { return this._name; }, | ||
'setter': function(v) { this._name = v.toLowerCase(); } | ||
} | ||
} | ||
} | ||
``` | ||
## Example | ||
@@ -421,3 +426,3 @@ | ||
comment.touch(); | ||
console.log(comment.__dirty); // true | ||
console.log(comment.isDirty()); // true | ||
@@ -424,0 +429,0 @@ comment.rollback(); // version is now 1 and modified the same as created |
@@ -247,2 +247,3 @@ /*global describe:true, it:true, before:true, after:true */ | ||
instance.frogs = 'This is bunch of frogs.'; | ||
instance.isDirty().should.equal.true; | ||
instance.save(function(err, response) | ||
@@ -391,49 +392,2 @@ { | ||
it('calls a beforeSave() hook before saving a model', function(done) | ||
{ | ||
hookTest = new Model(); | ||
hookTest.key = '3'; | ||
hookTest.name = 'hook test'; | ||
hookTest.should.not.have.property('beforeSaveCalled'); | ||
hookTest.save(function(err, res) | ||
{ | ||
should.not.exist(err); | ||
hookid = hookTest.key; | ||
hookTest.should.have.property('beforeSaveCalled'); | ||
hookTest.beforeSaveCalled.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('calls afterSave() after saving a model', function() | ||
{ | ||
hookTest.should.have.property('afterSaveCalled'); | ||
hookTest.afterSaveCalled.should.equal(true); | ||
}); | ||
it('calls afterLoad() after loading a model from the db', function(done) | ||
{ | ||
hookTest.should.not.have.property('afterLoadCalled'); | ||
Model.get(hookid, function(err, loaded) | ||
{ | ||
should.not.exist(err); | ||
loaded.should.have.property('afterLoadCalled'); | ||
loaded.afterLoadCalled.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('calls beforeDestroy() before destroying a model', function(done) | ||
{ | ||
hookTest.should.not.have.property('beforeDestroyCalled'); | ||
hookTest.destroy(function(err, deleted) | ||
{ | ||
should.not.exist(err); | ||
hookTest.should.have.property('beforeDestroyCalled'); | ||
hookTest.beforeDestroyCalled.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('can remove a document from the db', function(done) | ||
@@ -506,2 +460,29 @@ { | ||
it('removes attachments when removing an object', function(done) | ||
{ | ||
var obj = new Model(); | ||
obj.key = 'cats'; | ||
obj.frogs = 'Cats do not eat frogs.'; | ||
obj.name = 'all about cats'; | ||
obj.save(function(err, reply) | ||
{ | ||
should.not.exist(err); | ||
reply.should.equal('OK'); | ||
obj.destroy(function(err, destroyed) | ||
{ | ||
should.not.exist(err); | ||
var k = Model.adapter.attachmentKey('cats'); | ||
Model.adapter.redis.hkeys(k, function(err, reply) | ||
{ | ||
should.not.exist(err); | ||
reply.should.be.an('array'); | ||
reply.length.should.equal(0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('inflate() handles bad json by assigning properties directly', function() | ||
@@ -508,0 +489,0 @@ { |
Sorry, the diff of this file is not supported yet
433
118144
3277