camo
Advanced tools
Comparing version 0.11.3 to 0.11.4
@@ -0,1 +1,7 @@ | ||
## 0.11.4 (2016-01-19) | ||
Bugfixes: | ||
- Fixed issue with saving/loading deeply nested embedded documents ([#35](https://github.com/scottwrobinson/camo/issues/35)) | ||
- Removed `_id` and `_schema._id` from `EmbeddedDocument` class ([#31](https://github.com/scottwrobinson/camo/issues/31)) | ||
- Allow user to specify a custom _id ([#29](https://github.com/scottwrobinson/camo/issues/29)) | ||
## 0.11.3 (2015-12-29) | ||
@@ -2,0 +8,0 @@ Features: |
@@ -263,3 +263,2 @@ "use strict"; | ||
_.keys(d).forEach(function(key) { | ||
var value = null; | ||
@@ -274,2 +273,3 @@ if (d[key] === null) { | ||
if (key in instance._schema) { | ||
var type = instance._schema[key].type; | ||
@@ -493,2 +493,4 @@ | ||
_toData(keep) { | ||
var that = this; | ||
if (keep === undefined || keep === null) { | ||
@@ -500,10 +502,19 @@ keep = {}; | ||
var values = _.clone(this); | ||
_.keys(values).forEach(function(k) { | ||
var values = {}; | ||
_.keys(this).forEach(function(k) { | ||
if (_.startsWith(k, '_')) { | ||
if (k === '_id' && keep._id) { | ||
if (k !== '_id' || !keep._id) { | ||
return; | ||
} else { | ||
values[k] = that[k]; | ||
} | ||
delete values[k]; | ||
} else if (isEmbeddedDocument(that[k])) { | ||
values[k] = that[k]._toData(); | ||
} else if (isArray(that[k]) && that[k].length > 0 && isEmbeddedDocument(that[k][0])) { | ||
values[k] = []; | ||
that[k].forEach(function(v) { | ||
values[k].push(v._toData()); | ||
}); | ||
} else { | ||
values[k] = that[k]; | ||
} | ||
@@ -510,0 +521,0 @@ }); |
@@ -37,3 +37,3 @@ "use strict"; | ||
} else { | ||
db.updateOne({ _id: id }, { $set: values }, function(error, result) { | ||
db.updateOne({ _id: id }, { $set: values }, { upsert: true }, function(error, result) { | ||
if (error) return reject(error); | ||
@@ -40,0 +40,0 @@ return resolve(); |
@@ -67,3 +67,3 @@ "use strict"; | ||
} else { | ||
db.update({ _id: id }, { $set: values }, function(error, result) { | ||
db.update({ _id: id }, { $set: values }, { upsert: true }, function(error, result) { | ||
if (error) return reject(error); | ||
@@ -70,0 +70,0 @@ return resolve(result); |
@@ -8,2 +8,11 @@ "use strict"; | ||
super(); | ||
// TODO: Move _id logic out of BaseDocument. | ||
// A better fix to this issue is to remove | ||
// _schema._id and _id from BaseDocument. But | ||
// since quite a bit of _id logic is still | ||
// in BD, we'll have to use this fix until | ||
// it is removed | ||
delete this._schema._id; | ||
delete this._id; | ||
} | ||
@@ -10,0 +19,0 @@ |
{ | ||
"name": "camo", | ||
"version": "0.11.3", | ||
"version": "0.11.4", | ||
"description": "A class-based ES6 ODM for Mongo-like databases.", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -44,2 +44,34 @@ "use strict"; | ||
describe('general', function() { | ||
it('should not have an _id', function(done) { | ||
class EmbeddedModel extends EmbeddedDocument { | ||
constructor() { | ||
super(); | ||
this.str = String; | ||
} | ||
} | ||
class DocumentModel extends Document { | ||
constructor() { | ||
super(); | ||
this.mod = EmbeddedModel; | ||
this.num = { type: Number }; | ||
} | ||
} | ||
var data = DocumentModel.create(); | ||
data.mod = EmbeddedModel.create(); | ||
data.mod.str = 'some data'; | ||
data.num = 1; | ||
data.save().then(function() { | ||
expect(data.mod._id).to.be.undefined; | ||
return DocumentModel.loadOne({ num: 1 }); | ||
}).then(function(d) { | ||
expect(d.mod._id).to.be.undefined; | ||
}).then(done, done); | ||
}); | ||
}); | ||
describe('types', function() { | ||
@@ -128,2 +160,48 @@ it('should allow embedded types', function(done) { | ||
it('should save nested array of embeddeds', function(done) { | ||
class Point extends EmbeddedDocument { | ||
constructor() { | ||
super(); | ||
this.x = Number; | ||
this.y = Number; | ||
} | ||
} | ||
class Polygon extends EmbeddedDocument { | ||
constructor() { | ||
super(); | ||
this.points = [Point]; | ||
} | ||
} | ||
class WorldMap extends Document { | ||
constructor() { | ||
super(); | ||
this.polygons = [Polygon]; | ||
} | ||
} | ||
var map = WorldMap.create(); | ||
var polygon1 = Polygon.create(); | ||
var polygon2 = Polygon.create(); | ||
var point1 = Point.create({ x: 123.45, y: 678.90 }); | ||
var point2 = Point.create({ x: 543.21, y: 987.60 }); | ||
map.polygons.push(polygon1); | ||
map.polygons.push(polygon2); | ||
polygon2.points.push(point1); | ||
polygon2.points.push(point2); | ||
map.save().then(function() { | ||
return WorldMap.loadOne(); | ||
}).then(function(m) { | ||
expect(m.polygons).to.have.length(2); | ||
expect(m.polygons[0]).to.be.instanceof(Polygon); | ||
expect(m.polygons[1]).to.be.instanceof(Polygon); | ||
expect(m.polygons[1].points).to.have.length(2); | ||
expect(m.polygons[1].points[0]).to.be.instanceof(Point); | ||
expect(m.polygons[1].points[1]).to.be.instanceof(Point); | ||
}).then(done, done); | ||
}); | ||
it('should allow nested initialization of embedded types', function(done) { | ||
@@ -520,4 +598,4 @@ | ||
expect(preValidateCalled).to.be.equal(true); | ||
expect(postValidateCalled).to.be.equal(true); | ||
expect(preSaveCalled).to.be.equal(true); | ||
expect(postValidateCalled).to.be.equal(true); | ||
expect(postSaveCalled).to.be.equal(true); | ||
@@ -524,0 +602,0 @@ |
@@ -5,2 +5,3 @@ "use strict"; | ||
var expect = require('chai').expect; | ||
var ObjectId = require('mongodb').ObjectId; | ||
var connect = require('../index').connect; | ||
@@ -34,4 +35,29 @@ var Document = require('../index').Document; | ||
done(); | ||
}); | ||
}); | ||
describe('id', function() { | ||
it('should allow custom _id values', function(done) { | ||
class School extends Document { | ||
constructor() { | ||
super(); | ||
this.name = String; | ||
} | ||
} | ||
var school = School.create(); | ||
school._id = new ObjectId('1234567890abcdef12345678'); | ||
school.name = 'Springfield Elementary'; | ||
school.save().then(function() { | ||
validateId(school); | ||
expect(school._id.toString()).to.be.equal('1234567890abcdef12345678'); | ||
return School.loadOne(); | ||
}).then(function(s) { | ||
validateId(s); | ||
expect(s._id.toString()).to.be.equal('1234567890abcdef12345678'); | ||
}).then(done, done); | ||
}); | ||
}); | ||
describe('query', function() { | ||
@@ -38,0 +64,0 @@ class User extends Document { |
@@ -94,2 +94,27 @@ "use strict"; | ||
describe('id', function() { | ||
it('should allow custom _id values', function(done) { | ||
class School extends Document { | ||
constructor() { | ||
super(); | ||
this.name = String; | ||
} | ||
} | ||
var school = School.create(); | ||
school._id = '1234567890abcdef'; | ||
school.name = 'South Park Elementary'; | ||
school.save().then(function() { | ||
validateId(school); | ||
expect(school._id).to.be.equal('1234567890abcdef'); | ||
return School.loadOne(); | ||
}).then(function(s) { | ||
validateId(s); | ||
expect(s._id).to.be.equal('1234567890abcdef'); | ||
}).then(done, done); | ||
}); | ||
}); | ||
describe('indexes', function() { | ||
@@ -96,0 +121,0 @@ it('should reject documents with duplicate values in unique-indexed fields', function(done) { |
200106
4637