Comparing version 0.5.0 to 0.5.2
@@ -0,1 +1,10 @@ | ||
## 0.5.2 (2015-06-30) | ||
- Version bump, thanks to NPM. | ||
## 0.5.1 (2015-06-30) | ||
Bugfixes: | ||
- Fixed validation and referencing so `Document`s can be referenced by their object or ID. | ||
## 0.5.0 (2015-06-26) | ||
@@ -2,0 +11,0 @@ |
@@ -158,3 +158,3 @@ "use strict"; | ||
isNativeId(value) { | ||
return value instanceof ObjectId; | ||
return value instanceof ObjectId || String(value).match(/^[a-fA-F0-9]{24}$/); | ||
} | ||
@@ -161,0 +161,0 @@ |
@@ -205,3 +205,3 @@ "use strict"; | ||
isNativeId(value) { | ||
return true; | ||
return String(value).match(/^[a-zA-Z0-9]{16}$/); | ||
} | ||
@@ -208,0 +208,0 @@ |
@@ -8,3 +8,3 @@ "use strict"; | ||
var isArray = require('./validate').isArray; | ||
var isDocument = require('./validate').isDocument; | ||
var isReferenceable = require('./validate').isReferenceable; | ||
var isEmbeddedDocument = require('./validate').isEmbeddedDocument; | ||
@@ -96,6 +96,6 @@ var isString = require('./validate').isString; | ||
_.keys(that._values).forEach(function(key) { | ||
if (isDocument(that._values[key]) || // isDocument OR | ||
(isArray(that._values[key]) && // isArray AND contains value AND value isDocument | ||
if (isReferenceable(that._values[key]) || // isReferenceable OR | ||
(isArray(that._values[key]) && // isArray AND contains value AND value isReferenceable | ||
that._values[key].length > 0 && | ||
isDocument(that._values[key][0]))) { | ||
isReferenceable(that._values[key][0]))) { | ||
@@ -106,6 +106,14 @@ // Handle array of references (ex: { type: [MyObject] }) | ||
that._values[key].forEach(function(v) { | ||
toUpdate[key].push(v.id); | ||
if (DB().isNativeId(v)) { | ||
toUpdate[key].push(v); | ||
} else { | ||
toUpdate[key].push(v.id); | ||
} | ||
}); | ||
} else { | ||
toUpdate[key] = that._values[key].id; | ||
if (DB().isNativeId(that._values[key])) { | ||
toUpdate[key] = that._values[key]; | ||
} else { | ||
toUpdate[key] = that._values[key].id; | ||
} | ||
} | ||
@@ -112,0 +120,0 @@ |
var _ = require('lodash'); | ||
var DB = require('./clients').getClient; | ||
var ObjectId = null; | ||
try { | ||
ObjectId = require('mongodb').ObjectId; | ||
} catch(Error) { } | ||
var isString = function(s) { | ||
@@ -44,9 +40,10 @@ return _.isString(s); | ||
var isObjectId = function(o) { | ||
if (ObjectId === null) { | ||
return false; | ||
} | ||
return String(o).match(/^[a-fA-F0-9]{24}$/); | ||
var isReferenceable = function(r) { | ||
return isDocument(r) || isNativeId(r); | ||
}; | ||
var isNativeId = function(n) { | ||
return DB().isNativeId(n); | ||
}; | ||
var isSupportedType = function(t) { | ||
@@ -75,7 +72,7 @@ return (t === String || t === Number || t === Boolean || | ||
} else if (type.documentClass && type.documentClass() === 'document') { | ||
return isDocument(value); | ||
return isDocument(value) || isString(value); | ||
} else if (type.documentClass && type.documentClass() === 'embedded') { | ||
return isEmbeddedDocument(value); | ||
} else if (type === ObjectId) { | ||
return isObjectId(value); | ||
} else if (type === DB().nativeIdType()) { | ||
return isNativeId(value); | ||
} else { | ||
@@ -134,2 +131,4 @@ throw new Error('Unsupported type: ' + type.name); | ||
exports.isEmbeddedDocument = isEmbeddedDocument; | ||
exports.isReferenceable = isReferenceable; | ||
exports.isNativeId = isNativeId; | ||
exports.isSupportedType = isSupportedType; | ||
@@ -136,0 +135,0 @@ exports.isType = isType; |
{ | ||
"name": "camo", | ||
"version": "0.5.0", | ||
"version": "0.5.2", | ||
"description": "A lightweight ES6 ODM for Mongo-like databases.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -306,2 +306,88 @@ "use strict"; | ||
it('should allow references to be saved using the object or its id', function(done) { | ||
class ReferenceeModel extends Document { | ||
constructor() { | ||
super('referencee3'); | ||
this.str = String; | ||
} | ||
} | ||
class ReferencerModel extends Document { | ||
constructor() { | ||
super('referencer3'); | ||
this.ref1 = ReferenceeModel; | ||
this.ref2 = ReferenceeModel; | ||
this.num = { type: Number }; | ||
} | ||
} | ||
var data = ReferencerModel.create(); | ||
data.ref1 = ReferenceeModel.create(); | ||
var ref2 = ReferenceeModel.create(); | ||
data.ref1.str = 'string1'; | ||
ref2.str = 'string2'; | ||
data.num = 1; | ||
data.ref1.save().then(function() { | ||
validateId(data.ref1); | ||
return data.save(); | ||
}).then(function() { | ||
validateId(data); | ||
return ref2.save(); | ||
}).then(function() { | ||
validateId(ref2); | ||
data.ref2 = ref2.id; | ||
return data.save(); | ||
}).then(function() { | ||
return ReferencerModel.loadOne({num: 1}); | ||
}).then(function(d) { | ||
validateId(d.ref1); | ||
validateId(d.ref2); | ||
expect(d.ref1.str).to.be.equal('string1'); | ||
expect(d.ref2.str).to.be.equal('string2'); | ||
}).then(done, done); | ||
}); | ||
it('should allow array of references to be saved using the object or its id', function(done) { | ||
class ReferenceeModel extends Document { | ||
constructor() { | ||
super('referencee4'); | ||
this.schema({ str: { type: String } }); | ||
} | ||
} | ||
class ReferencerModel extends Document { | ||
constructor() { | ||
super('referencer4'); | ||
this.refs = [ReferenceeModel]; | ||
this.num = Number; | ||
} | ||
} | ||
var data = ReferencerModel.create(); | ||
data.refs.push(ReferenceeModel.create()); | ||
var ref2 = ReferenceeModel.create(); | ||
data.refs[0].str = 'string1'; | ||
ref2.str = 'string2'; | ||
data.num = 1; | ||
data.refs[0].save().then(function() { | ||
validateId(data.refs[0]); | ||
return data.save(); | ||
}).then(function() { | ||
validateId(data); | ||
return ref2.save(); | ||
}).then(function() { | ||
validateId(ref2); | ||
data.refs.push(ref2.id); | ||
return data.save(); | ||
}).then(function() { | ||
return ReferencerModel.loadOne({num: 1}); | ||
}).then(function(d) { | ||
validateId(d.refs[0]); | ||
validateId(d.refs[1]); | ||
expect(d.refs[1].str).to.be.equal('string2'); | ||
}).then(done, done); | ||
}); | ||
it('should allow circular references', function(done) { | ||
@@ -308,0 +394,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
181538
4342