Comparing version 1.3.6 to 1.3.7
150
lib/Model.js
@@ -93,2 +93,15 @@ /// <reference path="../nodelib/node.js"/> | ||
// Handle any renames | ||
for(var upstream in $.options.renames) { | ||
var downstream = $.options.renames[upstream]; | ||
conditions[upstream] = conditions[upstream] || conditions[downstream]; | ||
if(conditions[upstream] === undefined) | ||
delete conditions[upstream]; | ||
if(conditions[downstream] !== undefined) | ||
delete conditions[downstream]; | ||
} | ||
transform.up(this.options.transforms, conditions); | ||
@@ -127,2 +140,15 @@ | ||
// Handle any renames | ||
for(var upstream in $.options.renames) { | ||
var downstream = $.options.renames[upstream]; | ||
conditions[upstream] = conditions[upstream] || conditions[downstream]; | ||
if(conditions[upstream] === undefined) | ||
delete conditions[upstream]; | ||
if(conditions[downstream] !== undefined) | ||
delete conditions[downstream]; | ||
} | ||
transform.up(this.options.transforms, conditions); | ||
@@ -142,2 +168,10 @@ | ||
/// <param name="object" type="Object">The properties to set on the newly created object</param> | ||
/// </signature> | ||
/// <signature> | ||
/// <summary>Inserts the given object into the database</summary> | ||
/// <param name="object" type="Array" elementType="Object">An array of objects representing the properties to set on the newly created objects</param> | ||
/// </signature> | ||
/// <signature> | ||
/// <summary>Inserts the given object into the database</summary> | ||
/// <param name="object" type="Object">The properties to set on the newly created object</param> | ||
/// <param name="callback" type="Function">A function to be called once the object has been created</param> | ||
@@ -160,9 +194,10 @@ /// </signature> | ||
var created = []; | ||
var prepped = []; | ||
var end = function(err, results) { | ||
if(!callback) return; | ||
var end = function(err, results) { | ||
if(!results) return callback(err); | ||
if(!Array.isArray(results)) results = [results]; | ||
@@ -174,34 +209,55 @@ | ||
var next = function(err, instance) { | ||
if(err) return end(err, created); | ||
if(instance) created.push(instance); | ||
if(object.length === 0) { | ||
$.setupIndexes(); | ||
return end(null, created); | ||
} | ||
var toInsert = object.shift(); | ||
doHook(toInsert); | ||
var prepComplete = function() { | ||
$.collection.insert(prepped, { w: callback ? 1 : 0 }, function(err, inserted) { | ||
if(err) return end(err); | ||
return end(null, inserted.map(function(item) { return $.wrap(item, false); })); | ||
}); | ||
}; | ||
var doInsert = function(err) { | ||
if(err) return next(err); | ||
var prepNext = function(err) { | ||
if(err) return end(err); | ||
if(object.length === 0) return prepComplete(); | ||
var validation = validate($.options.schema, this); | ||
var obj = object.shift(); | ||
if (!validation.passed) return next(validation.toError()); | ||
var postHook = function(err) { | ||
if(err) return prepNext(err); | ||
var instance = $.wrap.call($, this, true); | ||
instance.save(next); | ||
}; | ||
// Validate the object | ||
var validation = validate($.options.schema, obj); | ||
if(!validation.passed) return prepNext(validation.toError()); | ||
var doHook = function(object) { | ||
if ($.options.hooks.beforeCreate) { | ||
if ($.options.hooks.beforeCreate.length === 0) $.options.hooks.beforeCreate.call(object); | ||
else return $.options.hooks.beforeCreate.call(object, doInsert.bind(object)); | ||
} | ||
doInsert.call(object); | ||
// Handle any renames | ||
for(var upstream in $.options.renames) { | ||
var downstream = $.options.renames[upstream]; | ||
obj[upstream] = obj[upstream] || obj[downstream]; | ||
if(obj[upstream] === undefined) | ||
delete obj[upstream]; | ||
if(obj[downstream] !== undefined) | ||
delete obj[downstream]; | ||
} | ||
// Transform the object | ||
transform.up($.options.transforms, obj); | ||
prepped.push(obj); | ||
return prepNext(); | ||
} | ||
// Run the beforeCreate hook | ||
if($.options.hooks.beforeCreate) { | ||
if($.options.hooks.beforeCreate.length > 0) return $.options.hooks.beforeCreate.call(obj, postHook); | ||
else try { | ||
$.options.hooks.beforeCreate.call(obj); | ||
postHook(); | ||
} catch(ex) { | ||
postHook(ex); | ||
} | ||
} else postHook(); | ||
}; | ||
next(); | ||
prepNext(); | ||
}; | ||
@@ -221,4 +277,18 @@ | ||
/// </signature> | ||
// Handle any renames | ||
for(var upstream in this.options.renames) { | ||
var downstream = this.options.renames[upstream]; | ||
conditions[upstream] = conditions[upstream] || conditions[downstream]; | ||
if(conditions[upstream] === undefined) | ||
delete conditions[upstream]; | ||
if(conditions[downstream] !== undefined) | ||
delete conditions[downstream]; | ||
} | ||
transform.up(this.options.transforms, conditions); | ||
this.collection.update(conditions, changes, { w: callback ? 1 : 0, multi: true }, callback); | ||
@@ -243,2 +313,15 @@ }; | ||
// Handle any renames | ||
for(var upstream in this.options.renames) { | ||
var downstream = this.options.renames[upstream]; | ||
conditions[upstream] = conditions[upstream] || conditions[downstream]; | ||
if(conditions[upstream] === undefined) | ||
delete conditions[upstream]; | ||
if(conditions[downstream] !== undefined) | ||
delete conditions[downstream]; | ||
} | ||
transform.up(this.options.transforms, conditions); | ||
@@ -272,2 +355,15 @@ | ||
// Handle any renames | ||
for(var upstream in this.options.renames) { | ||
var downstream = this.options.renames[upstream]; | ||
conditions[upstream] = conditions[upstream] || conditions[downstream]; | ||
if(conditions[upstream] === undefined) | ||
delete conditions[upstream]; | ||
if(conditions[downstream] !== undefined) | ||
delete conditions[downstream]; | ||
} | ||
transform.up(this.options.transforms, conditions); | ||
@@ -274,0 +370,0 @@ |
{ | ||
"name": "iridium", | ||
"version": "1.3.6", | ||
"version": "1.3.7", | ||
"author": "Benjamin Pannell <admin@sierrasoftworks.com>", | ||
@@ -5,0 +5,0 @@ "description": "A custom lightweight ORM for MongoDB designed for power-users", |
@@ -103,2 +103,11 @@ # Iridium | ||
## Preprocessing Framework | ||
The preprocessing framework allows Iridium to convert values from a form better suited to your database into a form more suitable for your application in an entirely transparent manner. This is acomplished through the use of a number of preprocessors which run in the following order when retrieving an object from the database, their order is reversed when pushing an object to the database. | ||
### Transforms | ||
The transforms framework provides a low-level means to convert from one value type to another by means of up/down conversion functions. The up functions are used to convert the value before it is sent UPstream to the database, while down functions are used to convert the database value into the DOWNstream value used by your application. | ||
### Renames | ||
The renames framework allows you to access properties in a manner better suited to your application while keeping the same schema on the database side. This comes in handy when using the *_id* field for fields such as a user's username. | ||
## Credits | ||
@@ -105,0 +114,0 @@ Thanks to [Tadahiro Ishisaka](https://github.com/ishisaka) for his brilliant [nodeintellisense](https://github.com/ishisaka/nodeintellisense) library, it has been used as the basis for IntelliSense support within this module. |
@@ -32,3 +32,86 @@ /// <reference path="../nodelib/node.js"/> | ||
}); | ||
describe('database', function() { | ||
var model = null; | ||
before(function(done) { | ||
model = new Model(db, 'model', { | ||
name: String | ||
}, { | ||
renames: { | ||
_id: 'name' | ||
}, | ||
transforms: { | ||
_id: false | ||
} | ||
}); | ||
model.remove(done); | ||
}); | ||
describe('insertion', function() { | ||
it('should allow a single object to be inserted', function(done) { | ||
model.create({ | ||
name: 'Demo1' | ||
}, function(err, instance) { | ||
if(err) return done(err); | ||
instance.should.have.ownProperty('name', 'Demo1'); | ||
return done(); | ||
}); | ||
}); | ||
it('should have created the instance in the database', function(done) { | ||
model.count({ name: 'Demo1' }, function(err, number) { | ||
if(err) return done(err); | ||
number.should.eql(1); | ||
done(); | ||
}); | ||
}); | ||
it('should allow multiple objects to be inserted', function(done) { | ||
model.create([{ | ||
name: 'Demo2' | ||
}, { | ||
name: 'Demo3' | ||
}], 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'); | ||
return done(); | ||
}); | ||
}); | ||
it('should pass along DB errors', function(done) { | ||
model.create({ | ||
name: 'Demo1' | ||
}, function(err, inserted) { | ||
should.exist(err); | ||
should.not.exist(inserted); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('findOne', function() { | ||
it('should be able to locate a single object', function(done) { | ||
model.findOne({ name: 'Demo1' }, function(err, obj) { | ||
if(err) return done(err); | ||
should.exist(obj); | ||
obj.should.have.ownProperty('name', 'Demo1'); | ||
done(); | ||
}) | ||
}); | ||
it('should not throw an error if it couldn\'t find an object', function(done) { | ||
model.findOne({ name: 'NotFound' }, function(err, obj) { | ||
if(err) return done(err); | ||
should.not.exist(obj); | ||
done(); | ||
}) | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
300336
5820
115