Comparing version 0.5.0 to 0.5.1
@@ -9,4 +9,9 @@ /** | ||
var wrap = require('co-monk'); | ||
var util = require('./util'); | ||
var isFunction = util.isFunction; | ||
var isObject = util.isObject; | ||
var isArray = util.isArray; | ||
/** | ||
@@ -124,8 +129,8 @@ * Mongorito | ||
// iterate and call set on each item | ||
if ('object' === typeof key) { | ||
if (isObject(key)) { | ||
var attrs = key; | ||
for (key in attrs) { | ||
this.set(key, attrs[key]); | ||
} | ||
Object.keys(attrs).forEach(function (key) { | ||
this.set(key, attrs[key]); | ||
}, this); | ||
@@ -146,3 +151,8 @@ return; | ||
Object.keys(defaults).forEach(function (key) { | ||
if (undefined == this.get(key)) this.set(key, defaults[key]); | ||
var defaultValue = defaults[key]; | ||
var actualValue = this.get(key); | ||
if (undefined == actualValue) { | ||
this.set(key, defaultValue); | ||
} | ||
}, this); | ||
@@ -159,19 +169,47 @@ }, | ||
before: function (action, method) { | ||
if ('string' === typeof method) method = this[method]; | ||
hook: function (when, action, method) { | ||
if (isObject(when)) { | ||
var hooks = when; | ||
Object.keys(hooks).forEach(function (key) { | ||
var parts = key.split(':'); | ||
var when = parts[0]; | ||
var action = parts[1]; | ||
var method = hooks[key]; | ||
this.hook(when, action, method); | ||
}, this); | ||
return; | ||
} | ||
if (isArray(method)) { | ||
var methods = method; | ||
methods.forEach(function (method) { | ||
this.hook(when, action, method); | ||
}, this); | ||
return; | ||
} | ||
this.hooks.before[action].push(method); | ||
if (false === isFunction(method)) method = this[method]; | ||
if ('around' === when) { | ||
this.hooks.before[action].push(method); | ||
this.hooks.after[action].unshift(method); | ||
} else { | ||
this.hooks[when][action].push(method); | ||
} | ||
}, | ||
before: function (action, method) { | ||
this.hook('before', action, method); | ||
}, | ||
after: function (action, method) { | ||
if ('string' === typeof method) method = this[method]; | ||
this.hooks.after[action].push(method); | ||
this.hook('after', action, method); | ||
}, | ||
around: function (action, method) { | ||
this.before(action, method); | ||
if ('string' === typeof method) method = this[method]; | ||
this.hooks.after[action].unshift(method); | ||
this.hook('around', action, method); | ||
}, | ||
@@ -191,17 +229,18 @@ | ||
// revert populated documents to _id's | ||
var populate = this.options.populate; | ||
for (var key in populate) { | ||
var value = this.get(key); | ||
if (value instanceof Array) { | ||
value = value.map(function (doc) { | ||
return doc.get('_id'); | ||
}); | ||
} else { | ||
value = value.get('_id'); | ||
} | ||
this.set(key, value); | ||
} | ||
var populate = this.options.populate || emptyObject; | ||
Object.keys(populate).forEach(function (key) { | ||
var value = this.get(key); | ||
if (isArray(value)) { | ||
value = value.map(function (doc) { | ||
return doc.get('_id'); | ||
}); | ||
} else { | ||
value = value.get('_id'); | ||
} | ||
this.set(key, value); | ||
}, this); | ||
yield this.runHooks('before', 'save'); | ||
@@ -370,1 +409,3 @@ var result = yield fn.call(this); | ||
exports.Model = Model = Class.extend(InstanceMethods, StaticMethods); | ||
var emptyObject = {}; |
@@ -5,6 +5,12 @@ /** | ||
var ObjectID = require('monk/node_modules/mongoskin').ObjectID; | ||
var Class = require('class-extend'); | ||
var util = require('./util'); | ||
var isObjectID = util.isObjectID; | ||
var isRegExp = util.isRegExp; | ||
var isObject = util.isObject; | ||
var isString = util.isString; | ||
/** | ||
@@ -26,13 +32,16 @@ * Query | ||
// iterate over that object and call .where(key, value) | ||
if ('object' === typeof key) { | ||
if (isObject(key)) { | ||
var conditions = key; | ||
for (key in conditions) { | ||
this.where(key, conditions[key]); | ||
} | ||
Object.keys(conditions).forEach(function (key) { | ||
this.where(key, conditions[key]); | ||
}, this); | ||
} else if ('string' === typeof key) { | ||
} | ||
if (isString(key)) { | ||
// if only one argument was supplied | ||
// save the key in this.lastKey | ||
// for future methods, like .equals() | ||
if (!value) { | ||
if (undefined == value) { | ||
this.lastKey = key; | ||
@@ -44,5 +53,5 @@ return this; | ||
// 2. if object and not ObjectID | ||
if (value instanceof RegExp) { | ||
if (isRegExp(value)) { | ||
value = { $regex: value }; | ||
} else if ('object' === typeof value && !(value instanceof ObjectID)) { | ||
} else if (isObject(value) && false === isObjectID(value)) { | ||
value = { $elemMatch: value }; | ||
@@ -120,2 +129,5 @@ } | ||
// fields to populate | ||
var populate = Object.keys(options.populate); | ||
var docs = yield collection.find(this.query, options); | ||
@@ -128,3 +140,6 @@ | ||
// options.populate is a key-model pair object | ||
for (var key in options.populate) { | ||
var j = 0; | ||
var key; | ||
while (key = populate[j++]) { | ||
// model to use when populating the field | ||
@@ -137,6 +152,8 @@ var model = options.populate[key]; | ||
if (value instanceof Array) { | ||
var subdocs = value.map(function (id) { | ||
return model.findById(id.toString()); | ||
}); | ||
// convert each _id to String | ||
// and then convert it to | ||
// findById function | ||
var subdocs = value.map(String).map(model.findById, model); | ||
// find sub documents | ||
value = yield subdocs; | ||
@@ -143,0 +160,0 @@ } else { |
{ | ||
"name": "mongorito", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "ES6 generator-based MongoDB ODM. It rocks.", | ||
@@ -5,0 +5,0 @@ "author": "Vadim Demedes <vdemedes@gmail.com>", |
15815
7
501