Skip to content

Commit

Permalink
Use model specific instances for schema values
Browse files Browse the repository at this point in the history
Another major optimization, since it means we don't need to define
property accessors on a per-instance basis when creating Instances.
Should vastly improve performance and memory usage for databases which
adhere to their specified schemas, those that don't should add
{ $required: false, $type: ... } to their schemas to take advantage of
these optimizations.
  • Loading branch information
notheotherben committed Jan 10, 2014
1 parent ce0c435 commit bb1d747
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
18 changes: 16 additions & 2 deletions lib/Instance.js
Expand Up @@ -172,10 +172,10 @@ Instance.prototype.__extendSchema = function() {
schema[property] = false;

for(var property in this.__state.model.schema)
schema[property] = this.__state.model.schema[property];
if(schema[property]) delete schema[property];

for(var targetProperty in schema) {
if(!$.hasOwnProperty(targetProperty))
if(!$[targetProperty] && !$.hasOwnProperty(targetProperty))
(function(targetProperty) {
Object.defineProperty($, targetProperty, {
get: function() {
Expand Down Expand Up @@ -215,6 +215,20 @@ Instance.forModel = function(model) {

var proto = {};

_.each(model.schema, function(validator, name) {
Object.defineProperty(proto, name, {
get: function() {
return this.__state.modified[name] === undefined ? null : this.__state.modified[name];
},
set: function(value) {
var validation = validate(validator, value, name, model.extraValidators);
if(!validation.passed) throw validation.toError();
this.__state.modified[name] = value;
},
enumerable: true
});
});

_.each(model.options.virtuals, function(property, name) {
if('function' == typeof property)
Object.defineProperty(proto, name, {
Expand Down
10 changes: 5 additions & 5 deletions test/findOne.js
Expand Up @@ -48,7 +48,7 @@ describe('orm', function () {
model.findOne({ name: 'Demo1' }, function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('name', 'Demo1');
obj.should.have.property('name', 'Demo1');
done();
});
});
Expand All @@ -57,7 +57,7 @@ describe('orm', function () {
model.findOne('Demo1', function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('name', 'Demo1');
obj.should.have.property('name', 'Demo1');
done();
});
});
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('orm', function () {
model.findOne({ name: 'Demo1' }, function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('name', 'Demo1');
obj.should.have.property('name', 'Demo1');
d1instance = obj;
done();
});
Expand All @@ -106,8 +106,8 @@ describe('orm', function () {
model.findOne(d1instance.id, function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('id').with.type('string');
obj.should.have.ownProperty('name', 'Demo1');
obj.should.have.property('id').with.type('string');
obj.should.have.property('name', 'Demo1');
done();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/insertion.js
Expand Up @@ -41,7 +41,7 @@ describe('orm', function () {
name: 'Demo1'
}, function(err, instance) {
if(err) return done(err);
instance.should.have.ownProperty('name', 'Demo1');
instance.should.have.property('name', 'Demo1');
return done();
});
});
Expand All @@ -62,8 +62,8 @@ describe('orm', function () {
}], function(err, instances) {
if(err) return done(err);
should(Array.isArray(instances));
instances[0].should.have.ownProperty('name', 'Demo2');
instances[1].should.have.ownProperty('name', 'Demo3');
instances[0].should.have.property('name', 'Demo2');
instances[1].should.have.property('name', 'Demo3');
return done();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/instance.js
Expand Up @@ -162,9 +162,9 @@ describe('orm', function () {
i.age = 'hello';
}).should.throwError();

i.should.have.ownProperty('id', 'custom_id');
i.should.have.ownProperty('name', 'name');
i.should.have.ownProperty('age', null);
i.should.have.property('id', 'custom_id');
i.should.have.property('name', 'name');
i.should.have.property('age').and.eql(null);
});
});
});
Expand Down

0 comments on commit bb1d747

Please sign in to comment.