Comparing version 1.0.11 to 1.0.12
1.0.12 / 2011-02-14 | ||
=================== | ||
* Minor refactorings [brian] | ||
1.0.11 / 2011-02-14 | ||
@@ -3,0 +8,0 @@ =================== |
@@ -256,3 +256,3 @@ | ||
exports.version = '1.0.11'; | ||
exports.version = '1.0.12'; | ||
@@ -259,0 +259,0 @@ /** |
@@ -10,8 +10,3 @@ | ||
, MongooseError = require('./error') | ||
, Query = require('./query').Query | ||
, FindQuery = require('./query').FindQuery | ||
, FindOneQuery = require('./query').FindOneQuery | ||
, Query = require('./query').Query | ||
, Query = require('./query').Query | ||
, Query = require('./query').Query | ||
, Query = require('./query') | ||
, utils = require('./utils') | ||
@@ -190,3 +185,4 @@ , EventEmitter = utils.EventEmitter | ||
Model.prototype.remove = function (fn) { | ||
if (this.removing || this.removed) return this; | ||
if (this.removing || this.removed) | ||
return this; | ||
@@ -230,3 +226,4 @@ if (!this.removing) { | ||
if (!arrays.length) return next(); | ||
if (!arrays.length) | ||
return next(); | ||
@@ -336,5 +333,17 @@ arrays.forEach(function (array) { | ||
Model.remove = function (conditions, callback) { | ||
if ('function' === typeof conditions) { | ||
callback = conditions; | ||
conditions = {}; | ||
} | ||
var query = new Query(conditions).bind(this); | ||
if ('undefined' == typeof callback) | ||
if ('undefined' === typeof callback) | ||
return query; | ||
var cQuery; | ||
if (cQuery = this._cumulativeQuery) { | ||
merge(query._conditions, cQuery._conditions); | ||
if (query.options && cQuery.options) | ||
merge(query.options, cQuery.options); | ||
delete this._cumulativeQuery; | ||
} | ||
if (!query.model) query.bind(this); | ||
return query.remove(callback); | ||
@@ -360,3 +369,8 @@ }; | ||
Model.find = function (conditions, fields, options, callback) { | ||
if ('function' == typeof fields) { | ||
if ('function' == typeof conditions) { | ||
callback = conditions; | ||
conditions = {}; | ||
fields = null; | ||
options = null; | ||
} else if ('function' == typeof fields) { | ||
callback = fields; | ||
@@ -368,7 +382,2 @@ fields = null; | ||
options = null; | ||
} else if ('function' == typeof conditions) { | ||
callback = conditions; | ||
conditions = {}; | ||
fields = null; | ||
options = null; | ||
} | ||
@@ -381,13 +390,13 @@ | ||
// TODO Add cQuery merging into other commands | ||
var cQuery; | ||
if (cQuery = this._cumulativeQuery) | ||
if (cQuery = this._cumulativeQuery) { | ||
merge(query._conditions, cQuery._conditions); | ||
if (query._fields && cQuery._fields) | ||
merge(query._fields, cQuery._fields); | ||
if (query.options && cQuery.options) | ||
merge(query.options, cQuery.options); | ||
delete this._cumulativeQuery; | ||
} | ||
if (!query.model) | ||
query.bind(this); | ||
delete this._cumulativeQuery; | ||
return query.find(callback); | ||
@@ -418,5 +427,14 @@ }; | ||
if ('function' == typeof options) { | ||
// TODO Handle all 3 of the following scenarios | ||
// Hint: Only some of these scenarios are possible if cQuery is present | ||
// Scenario: findOne(conditions, fields, callback); | ||
// Scenario: findOne(fields, options, callback); | ||
// Scenario: findOne(conditions, options, callback); | ||
callback = options; | ||
options = null; | ||
} else if ('function' == typeof fields) { | ||
// TODO Handle all 2 of the following scenarios | ||
// Scenario: findOne(conditions, callback) | ||
// Scenario: findOne(fields, callback) | ||
// Scenario: findOne(options, callback); | ||
callback = fields; | ||
@@ -433,6 +451,14 @@ fields = null; | ||
var query = new Query(conditions, options).select(fields).bind(this); | ||
if ('undefined' === typeof callback) | ||
if ('undefined' == typeof callback) | ||
return query; | ||
var cQuery; | ||
if (cQuery = this._cumulativeQuery) { | ||
merge(query._conditions, cQuery._conditions); | ||
if (query._fields && cQuery._fields) | ||
merge(query._fields, cQuery._fields); | ||
if (query.options && cQuery.options) | ||
merge(query.options, cQuery.options); | ||
delete this._cumulativeQuery; | ||
} | ||
if (!query.model) query.bind(this); | ||
return query.findOne(callback); | ||
@@ -451,6 +477,12 @@ }; | ||
var query = new Query(conditions).bind(this); | ||
if ('undefined' == typeof callback) | ||
return query; | ||
var cQuery; | ||
if (cQuery = this._cumulativeQuery) { | ||
merge(query._conditions, cQuery._conditions); | ||
if (query.options && cQuery.options) | ||
merge(query.options, cQuery.options); | ||
delete this._cumulativeQuery; | ||
} | ||
if (!query.model) query.bind(this); | ||
return query.count(callback); | ||
@@ -546,10 +578,31 @@ }; | ||
Model.update = function (conditions, doc, options, callback) { | ||
if ('function' === typeof options) { | ||
if ('function' === typeof doc) { | ||
// Scenario: update(doc, callback); | ||
callback = doc; | ||
doc = conditions; | ||
conditions = {}; | ||
options = null; | ||
} else if ('function' === typeof options) { | ||
callback = options; | ||
options = null; | ||
if (!Object.keys(doc).length || this._objectHasPathKeys(doc)) { | ||
// Scenario: update(conditions, doc, callback) | ||
options = null; | ||
} else { | ||
// Scenario: update(doc, options, callback) | ||
options = doc; | ||
doc = conditions; | ||
conditions = {}; | ||
} | ||
} | ||
var query = new Query(conditions, options).bind(this); | ||
if ('undefined' === typeof callback) { | ||
if ('undefined' == typeof callback) | ||
return query; | ||
var cQuery; | ||
if (cQuery = this._cumulativeQuery) { | ||
merge(query._conditions, cQuery._conditions); | ||
if (query.options && cQuery.options) | ||
merge(query.options, cQuery.options); | ||
delete this._cumulativeQuery; | ||
} | ||
if (!query.model) query.bind(this); | ||
return query.update(doc, callback); | ||
@@ -559,2 +612,19 @@ }; | ||
/** | ||
* True if the first level of keys contains at least one | ||
* key that is equal to a path name. | ||
* | ||
* @param {Object} object | ||
* @return {Boolean} | ||
* @api private | ||
*/ | ||
Model._objectHasPathKeys = function (object) { | ||
var objectKeys = Object.keys(object) | ||
, anyPaths = Object.keys(this.schema.paths).some( function (path) { | ||
return ~objectKeys.indexOf(path); | ||
}); | ||
return anyPaths; | ||
}; | ||
/** | ||
* Compiler utility. | ||
@@ -561,0 +631,0 @@ * |
@@ -35,4 +35,4 @@ var utils = require('./utils') | ||
criteria = {}; | ||
} | ||
if (criteria instanceof Query) { | ||
} else if (criteria instanceof Query) { | ||
// TODO Merge options, too | ||
merge(this._conditions, criteria._conditions); | ||
@@ -630,2 +630,2 @@ } else { | ||
exports.Query = Query; | ||
module.exports = Query; |
@@ -9,3 +9,5 @@ | ||
, VirtualType = require('./virtualtype') | ||
, utils = require('./utils'); | ||
, utils = require('./utils') | ||
, NamedScope = require('./namedscope') | ||
, Query = require('./query'); | ||
@@ -368,68 +370,2 @@ /** | ||
var Query = require('./query').Query; | ||
function NamedScope () {} | ||
NamedScope.prototype.query; | ||
NamedScope.prototype.with = function () { | ||
var q = this.query || (this.query = new Query()); | ||
q.with.apply(q, arguments); | ||
return q; | ||
}; | ||
/** | ||
* @param {NamedScope} target | ||
* @param {Object} getters | ||
*/ | ||
NamedScope.prototype.decorate = function (target, getters) { | ||
var name = this.name | ||
, block = this.block | ||
, query = this.query; | ||
if (block) { | ||
if (block.length === 0) { | ||
Object.defineProperty(target, name, { | ||
get: getters.block0(block) | ||
}); | ||
} else { | ||
target[name] = getters.blockN(block); | ||
} | ||
} else { | ||
Object.defineProperty(target, name, { | ||
get: getters.basic(query) | ||
}); | ||
} | ||
}; | ||
NamedScope.prototype.compile = function (model) { | ||
var allScopes = this.scopesByName | ||
, scope; | ||
for (var k in allScopes) { | ||
scope = allScopes[k]; | ||
scope.decorate(model, { | ||
block0: function (block) { | ||
return function () { | ||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this)); | ||
block.call(cquery); | ||
return this; | ||
}; | ||
}, | ||
blockN: function (block) { | ||
return function () { | ||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this)); | ||
block.apply(cquery, arguments); | ||
return this; | ||
}; | ||
}, | ||
basic: function (query) { | ||
return function () { | ||
var cquery = this._cumulativeQuery || (this._cumulativeQuery = new Query().bind(this)); | ||
cquery.find(query); | ||
return this; | ||
}; | ||
} | ||
}); | ||
} | ||
}; | ||
Schema.prototype.namedScope = function (name, fn) { | ||
@@ -436,0 +372,0 @@ var namedScopes = this.namedScopes || (this.namedScopes = new NamedScope) |
@@ -16,3 +16,3 @@ /** | ||
, MongooseArray = require('../types').Array | ||
, Query = require('../query').Query; | ||
, Query = require('../query'); | ||
@@ -19,0 +19,0 @@ /** |
{ | ||
"name": "mongoose" | ||
, "description": "Mongoose MongoDB ORM" | ||
, "version": "1.0.11" | ||
, "version": "1.0.12" | ||
, "author": "Guillermo Rauch <guillermo@learnboost.com>" | ||
@@ -6,0 +6,0 @@ , "keywords": ["mongodb", "mongoose", "orm", "data", "datastore", "nosql"] |
@@ -10,4 +10,3 @@ | ||
, random = require('mongoose/utils').random | ||
, Query = require('mongoose/query').Query | ||
, FindQuery = require('mongoose/query').FindQuery | ||
, Query = require('mongoose/query') | ||
, Schema = mongoose.Schema | ||
@@ -14,0 +13,0 @@ , SchemaType = mongoose.SchemaType |
@@ -10,4 +10,3 @@ | ||
, random = require('mongoose/utils').random | ||
, Query = require('mongoose/query').Query | ||
, FindQuery = require('mongoose/query').FindQuery | ||
, Query = require('mongoose/query') | ||
, Schema = mongoose.Schema | ||
@@ -14,0 +13,0 @@ , SchemaType = mongoose.SchemaType |
@@ -1,23 +0,4 @@ | ||
//BoundQuery | ||
// | ||
//Query.prototype.with(criteria) | ||
//Query.prototype.with(criteria, callback) | ||
//Query.prototype.with(path, val, callback) | ||
// | ||
//Query.prototype.with(criteria).find() | ||
//Query.prototype.with(criteria).remove() | ||
//Query.prototype.with(criteria).findOne() | ||
//Query.prototype.with(criteria).update() | ||
//Query.prototype.with(criteria).count() | ||
// | ||
//var query = UserNS.find(...); | ||
// | ||
//var twenties = new Query().with('age').gte(20).lt(30); | ||
//var male = new Query().with('gender', 'male'); | ||
//var active = new Query().with('lastLogin').gte(+new Date - (24 * 3600 * 1000)); | ||
// | ||
//UserNS.twenties = Query.with('age').gte(20).lt(30); | ||
//UserNS.male = Query.with('gender', 'male'); | ||
//UserNS.active = Query.with('lastLogin').get(+new Date - (24 * 3600 * 1000)); | ||
// | ||
//UserNS.namedScope({ | ||
@@ -29,8 +10,2 @@ // twenties: Query.with('age').gte(20).lt(30) | ||
// | ||
//UserNS.namedScope('twenties').with('age').gte(20).lt(30); | ||
// | ||
//UserNS.namedScope('olderThan', function (age) { | ||
// return this.with('age').gt(age); | ||
//}); | ||
// | ||
//UserNS.find(twenties, male, active, function (err, found) { | ||
@@ -40,2 +15,3 @@ //}); | ||
//// twenties.male OR twenties.active | ||
//UserNS.twenties.male.OR.twenties.active.find(callback); | ||
//UserNS.find(twenties.male, twenties.active, function (err, found) { | ||
@@ -46,5 +22,2 @@ //}); | ||
//}); | ||
// | ||
//UserNS.twenties.male.active.find(callback); | ||
// | ||
//UserNS.twenties.male.active.remove(callback); | ||
@@ -69,3 +42,3 @@ | ||
age: Number | ||
, gender: {type: String} | ||
, gender: String | ||
, lastLogin: Date | ||
@@ -94,11 +67,12 @@ }); | ||
// TODO Add in tests for using named scopes with findOne, update, remove | ||
module.exports = { | ||
'basic named scopes should work': function () { | ||
'basic named scopes should work, for find': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create({gender: 'male'}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({gender: 'male'}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({gender: 'female'}, function (err, _) { | ||
UserNS.create( | ||
{gender: 'male'} | ||
, {gender: 'male'} | ||
, {gender: 'female'} | ||
, function (err, _) { | ||
should.strictEqual(err, null); | ||
@@ -110,14 +84,13 @@ UserNS.male.find( function (err, found) { | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
); | ||
}, | ||
'dynamic named scopes should work': function () { | ||
'dynamic named scopes should work, for find': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create({age: 21}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({age: 22}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({age: 19}, function (err, _) { | ||
UserNS.create( | ||
{age: 21} | ||
, {age: 22} | ||
, {age: 19} | ||
, function (err, _) { | ||
should.strictEqual(err, null); | ||
@@ -129,14 +102,13 @@ UserNS.olderThan(20).find( function (err, found) { | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
); | ||
}, | ||
'named scopes built on top of dynamic named scopes should work': function () { | ||
'named scopes built on top of dynamic named scopes should work, for find': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create({age: 21}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({age: 22}, function (err, _) { | ||
should.strictEqual(err, null); | ||
UserNS.create({age: 19}, function (err, _) { | ||
UserNS.create( | ||
{age: 21} | ||
, {age: 22} | ||
, {age: 19} | ||
, function (err, _) { | ||
should.strictEqual(err, null); | ||
@@ -148,7 +120,6 @@ UserNS.twenties.find( function (err, found) { | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
); | ||
}, | ||
'chaining named scopes should work': function () { | ||
'chaining named scopes should work, for find': function () { | ||
var db = start() | ||
@@ -171,2 +142,80 @@ , UserNS = db.model('UserNS', 'users_' + random()); | ||
}, | ||
'basic named scopes should work, for remove': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create( | ||
{gender: 'male'} | ||
, {gender: 'male'} | ||
, {gender: 'female'} | ||
, function (err, _) { | ||
UserNS.male.remove( function (err) { | ||
should.strictEqual(err, null); | ||
UserNS.male.find( function (err, found) { | ||
db.close(); | ||
should.strictEqual(err, null); | ||
found.should.have.length(0); | ||
}); | ||
}); | ||
} | ||
); | ||
}, | ||
// TODO multi-updates | ||
'basic named scopes should work, for update': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create( | ||
{gender: 'male'} | ||
, {gender: 'male'} | ||
, {gender: 'female'} | ||
, function (err, male1, male2, female1) { | ||
should.strictEqual(err, null); | ||
UserNS.male.update({gender: 'female'}, function (err) { | ||
should.strictEqual(err, null); | ||
UserNS.female.find( function (err, found) { | ||
should.strictEqual(err, null); | ||
found.should.have.length(2); | ||
UserNS.male.find( function (err, found) { | ||
db.close(); | ||
should.strictEqual(err, null); | ||
found.should.have.length(1); | ||
}); | ||
}); | ||
}); | ||
} | ||
); | ||
}, | ||
'chained named scopes should work, for findOne': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create( | ||
{age: 100, gender: 'male'} | ||
, function (err, maleCentenarian) { | ||
should.strictEqual(err, null); | ||
UserNS.male.olderThan(99).findOne( function (err, found) { | ||
db.close(); | ||
should.strictEqual(err, null); | ||
found._id.should.eql(maleCentenarian._id); | ||
}); | ||
} | ||
); | ||
}, | ||
'hybrid use of chained named scopes and ad hoc querying should work': function () { | ||
var db = start() | ||
, UserNS = db.model('UserNS', 'users_' + random()); | ||
UserNS.create( | ||
{age: 100, gender: 'female'} | ||
, function (err, femaleCentenarian) { | ||
should.strictEqual(null, err); | ||
UserNS.female.with('age').gt(99).findOne( function (err, found) { | ||
db.close(); | ||
should.strictEqual(err, null); | ||
found._id.should.eql(femaleCentenarian._id); | ||
}); | ||
} | ||
); | ||
} | ||
// 'using chained named scopes in a find': function () { | ||
@@ -173,0 +222,0 @@ // var db = start() |
@@ -6,3 +6,3 @@ | ||
var Query = require('mongoose/query').Query; | ||
var Query = require('mongoose/query'); | ||
@@ -9,0 +9,0 @@ /** |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
78
1403825
191
24790