Comparing version 1.7.3 to 1.7.4
1.7.4 / 2011-07-25 | ||
=================== | ||
* fixed; sparse now a valid seperate schema option | ||
* fixed; now catching cast errors in queries | ||
* fixed; calling new Schema with object created in vm.runInNewContext now works (#384) [Sija] | ||
* fixed; String enum was disallowing null | ||
* fixed; Find by nested document _id now works (#389) | ||
1.7.3 / 2011-07-16 | ||
@@ -3,0 +12,0 @@ =================== |
@@ -292,3 +292,3 @@ | ||
exports.version = '1.7.3'; | ||
exports.version = '1.7.4'; | ||
@@ -295,0 +295,0 @@ /** |
@@ -170,8 +170,12 @@ var utils = require('./utils') | ||
// Apply the casting; similar code for $elemMatch in schema/array.js | ||
remainingConds = {}; | ||
pathLastHalf = split.slice(j).join('.'); | ||
remainingConds[pathLastHalf] = val; | ||
castingQuery = new Query(remainingConds); | ||
castingQuery.cast(model, schematype.caster); | ||
obj[path] = castingQuery._conditions[pathLastHalf]; | ||
if (schematype.caster) { | ||
remainingConds = {}; | ||
pathLastHalf = split.slice(j).join('.'); | ||
remainingConds[pathLastHalf] = val; | ||
castingQuery = new Query(remainingConds); | ||
castingQuery.cast(schematype.caster); | ||
obj[path] = castingQuery._conditions[pathLastHalf]; | ||
} else { | ||
obj[path] = val; | ||
} | ||
} | ||
@@ -800,8 +804,15 @@ | ||
this.cast(model); | ||
var promise = new Promise(callback); | ||
try { | ||
this.cast(model); | ||
} catch (err) { | ||
return promise.error(err); | ||
} | ||
var castQuery = this._conditions; | ||
model.collection.findOne(castQuery, options, function (err, doc) { | ||
if (err) return callback(err); | ||
if (!doc) return callback(null, null); | ||
if (err) return promise.error(err); | ||
if (!doc) return promise.complete(null); | ||
@@ -812,4 +823,4 @@ var casted = new model(); | ||
casted.init(doc, function (err) { | ||
if (err) return callback(err); | ||
callback(null, casted); | ||
if (err) return promise.error(err); | ||
promise.complete(casted); | ||
}); | ||
@@ -833,3 +844,9 @@ }); | ||
var model = this.model; | ||
this.cast(model); | ||
try { | ||
this.cast(model); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var castQuery = this._conditions; | ||
@@ -852,3 +869,9 @@ model.collection.count(castQuery, callback); | ||
var model = this.model; | ||
this.cast(model); | ||
try { | ||
this.cast(model); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var castQuery = this._conditions; | ||
@@ -874,7 +897,18 @@ model.collection.distinct(field, castQuery, callback); | ||
this.cast(model); | ||
try { | ||
this.cast(model); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var castQuery = this._conditions; | ||
var castingQuery = {_conditions: doc}; | ||
this.cast.call(castingQuery, model); | ||
try { | ||
this.cast.call(castingQuery, model); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var castDoc = castingQuery._conditions; | ||
@@ -902,6 +936,12 @@ | ||
this.op = 'remove'; | ||
var model = this.model | ||
, options = this._optionsForExec(model); | ||
this.cast(model); | ||
try { | ||
this.cast(model); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
var castQuery = this._conditions; | ||
@@ -908,0 +948,0 @@ model.collection.remove(castQuery, options, callback); |
@@ -104,3 +104,3 @@ | ||
if (obj[i].constructor == Object && (!obj[i].type || obj[i].type.type)) { | ||
if (obj[i].constructor.name == 'Object' && (!obj[i].type || obj[i].type.type)) { | ||
if (Object.keys(obj[i]).length) | ||
@@ -160,3 +160,3 @@ this.add(obj[i], prefix + i + '.'); | ||
Schema.interpretAsType = function (path, obj) { | ||
if (obj.constructor != Object) | ||
if (obj.constructor.name != 'Object') | ||
obj = { type: obj }; | ||
@@ -171,3 +171,3 @@ | ||
if (type.constructor == Object) { | ||
if (type.constructor.name == 'Object') { | ||
return new Types.Mixed(path, obj); | ||
@@ -174,0 +174,0 @@ } |
@@ -35,17 +35,21 @@ | ||
SchemaString.prototype.enum = function(){ | ||
for (var i = 0, l = arguments.length; i < l; i++){ | ||
if (arguments[i] == null){ | ||
if (this.enumValidator){ | ||
this.enumValidator = false; | ||
this.validators = this.validators.filter(function(v){ | ||
return v[1] != 'enum'; | ||
}); | ||
} | ||
break; | ||
} else | ||
SchemaString.prototype.enum = function () { | ||
var len = arguments.length; | ||
if (!len || undefined === arguments[0] || false === arguments[0]) { | ||
if (this.enumValidator){ | ||
this.enumValidator = false; | ||
this.validators = this.validators.filter(function(v){ | ||
return v[1] != 'enum'; | ||
}); | ||
} | ||
return; | ||
} | ||
for (var i = 0; i < len; i++) { | ||
if (undefined !== arguments[i]) { | ||
this.enumValues.push(this.cast(arguments[i])); | ||
} | ||
} | ||
if (!this.enumValidator){ | ||
if (!this.enumValidator) { | ||
var values = this.enumValues; | ||
@@ -52,0 +56,0 @@ this.enumValidator = function(v){ |
@@ -82,3 +82,7 @@ | ||
SchemaType.prototype.unique = function (bool) { | ||
this._index = bool ? { unique: true } : null; | ||
if (!this._index || Object !== this._index.constructor) { | ||
this._index = {}; | ||
} | ||
this._index.unique = bool; | ||
return this; | ||
@@ -88,2 +92,18 @@ }; | ||
/** | ||
* Adds an unique index | ||
* | ||
* @param {Boolean} | ||
* @api private | ||
*/ | ||
SchemaType.prototype.sparse = function (bool) { | ||
if (!this._index || Object !== this._index.constructor) { | ||
this._index = {}; | ||
} | ||
this._index.sparse = bool; | ||
return this; | ||
}; | ||
/** | ||
* Adds a setter | ||
@@ -90,0 +110,0 @@ * |
{ | ||
"name": "mongoose" | ||
, "description": "Mongoose MongoDB ORM" | ||
, "version": "1.7.3" | ||
, "version": "1.7.4" | ||
, "author": "Guillermo Rauch <guillermo@learnboost.com>" | ||
@@ -6,0 +6,0 @@ , "keywords": ["mongodb", "mongoose", "orm", "data", "datastore", "nosql"] |
@@ -886,3 +886,3 @@ | ||
should.strictEqual(err, null); | ||
var id = created.comments[1]._id.id; | ||
var id = created.comments[1]._id.toString(); | ||
BlogPostB.findOne({'comments._id': id}, function (err, found) { | ||
@@ -889,0 +889,0 @@ db.close(); |
@@ -19,3 +19,4 @@ | ||
, MongooseNumber = mongoose.Types.Number | ||
, MongooseArray = mongoose.Types.Array; | ||
, MongooseArray = mongoose.Types.Array | ||
, vm = require('vm') | ||
@@ -185,7 +186,7 @@ /** | ||
var Test = new Schema({ | ||
complex: { type: String, enum: ['a', 'b', 'c'] } | ||
complex: { type: String, enum: ['a', 'b', undefined, 'c', null] } | ||
}); | ||
Test.path('complex').should.be.an.instanceof(SchemaTypes.String); | ||
Test.path('complex').enumValues.should.eql(['a', 'b', 'c']); | ||
Test.path('complex').enumValues.should.eql(['a', 'b', 'c', null]); | ||
Test.path('complex').validators.should.have.length(1); | ||
@@ -195,3 +196,3 @@ | ||
Test.path('complex').enumValues.should.eql(['a', 'b', 'c', 'd', 'e']); | ||
Test.path('complex').enumValues.should.eql(['a', 'b', 'c', null, 'd', 'e']); | ||
@@ -202,2 +203,10 @@ Test.path('complex').doValidate('x', function(err){ | ||
Test.path('complex').doValidate(undefined, function(err){ | ||
err.should.be.an.instanceof(ValidatorError); | ||
}); | ||
Test.path('complex').doValidate(null, function(err){ | ||
should.strictEqual(null, err); | ||
}); | ||
Test.path('complex').doValidate('da', function(err){ | ||
@@ -705,12 +714,41 @@ err.should.be.an.instanceof(ValidatorError); | ||
Tobi.path('name')._index.should.be.true; | ||
Tobi.path('name').index({ unique: true }); | ||
Tobi.path('name')._index.should.eql({ unique: true }); | ||
Tobi.path('name').unique(false); | ||
Tobi.path('name')._index.should.eql({ unique: false}); | ||
Tobi.path('name')._index.should.eql({ unique: true }); | ||
var T1 = new Schema({ | ||
name: { type: String, sparse: true } | ||
}); | ||
T1.path('name')._index.should.eql({ sparse: true }); | ||
var T2 = new Schema({ | ||
name: { type: String, unique: true } | ||
}); | ||
T2.path('name')._index.should.eql({ unique: true }); | ||
var T3 = new Schema({ | ||
name: { type: String, sparse: true, unique: true } | ||
}); | ||
T3.path('name')._index.should.eql({ sparse: true, unique: true }); | ||
var T4 = new Schema({ | ||
name: { type: String, unique: true, sparse: true } | ||
}); | ||
var i = T4.path('name')._index; | ||
i.unique.should.be.true; | ||
i.sparse.should.be.true; | ||
var T5 = new Schema({ | ||
name: { type: String, index: { sparse: true, unique: true } } | ||
}); | ||
var i = T5.path('name')._index; | ||
i.unique.should.be.true; | ||
i.sparse.should.be.true; | ||
}, | ||
'test defining compound indexes': function(){ | ||
var Tobi = new Schema({ | ||
name: { type: String, index: true } | ||
, last: { type: Number } | ||
, last: { type: Number, sparse: true } | ||
}); | ||
@@ -722,2 +760,3 @@ | ||
[{ name: 1 }, {}] | ||
, [{ last: 1 }, { sparse: true }] | ||
, [{ firstname: 1, last: 1}, {unique: true}] | ||
@@ -782,4 +821,29 @@ ]); | ||
should.strictEqual(undefined, schema.virtuals.id); | ||
}, | ||
'schema creation works with objects from other contexts': function () { | ||
var str = 'code = {' + | ||
' name: String' + | ||
', arr1: Array ' + | ||
', arr2: { type: [] }' + | ||
', date: Date ' + | ||
', num: { type: Number }' + | ||
', bool: Boolean' + | ||
', nest: { sub: { type: {}, required: true }}' + | ||
'}'; | ||
var script = vm.createScript(str, 'testSchema.vm'); | ||
var sandbox = { code: null }; | ||
script.runInNewContext(sandbox); | ||
var Ferret = new Schema(sandbox.code); | ||
Ferret.path('nest.sub').should.be.an.instanceof(SchemaTypes.Mixed); | ||
Ferret.path('name').should.be.an.instanceof(SchemaTypes.String); | ||
Ferret.path('arr1').should.be.an.instanceof(SchemaTypes.Array); | ||
Ferret.path('arr2').should.be.an.instanceof(SchemaTypes.Array); | ||
Ferret.path('date').should.be.an.instanceof(SchemaTypes.Date); | ||
Ferret.path('num').should.be.an.instanceof(SchemaTypes.Number); | ||
Ferret.path('bool').should.be.an.instanceof(SchemaTypes.Boolean); | ||
} | ||
}; |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
614892
14530
15