Skip to content

Commit 5d5e1c8

Browse files
gitfyaheckmann
authored andcommitted
delta updates handled - emb-doc
Resolved Conflicts: lib/document.js lib/schema/documentarray.js test/model.test.js
1 parent bef68bd commit 5d5e1c8

4 files changed

Lines changed: 36 additions & 24 deletions

File tree

‎lib/document.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ Document.prototype.set = function (path, val, type) {
311311
})) {
312312

313313
if (this.isNew) {
314-
this._activePaths.modify(pathToMark);
314+
this.markModified(pathToMark);
315315
} else {
316316
var priorVal = this.get(path);
317317
if (!this.isDirectModified(pathToMark) && !deepEqual(val, priorVal)) {
318-
this._activePaths.modify(pathToMark);
318+
this.markModified(pathToMark);
319319
}
320320
}
321321

‎lib/schema/documentarray.js‎

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,38 @@ DocumentArray.prototype.doValidate = function (array, fn, scope) {
9898
*/
9999

100100
DocumentArray.prototype.cast = function (value, doc, init) {
101+
var retval = value;
101102
if (Array.isArray(value)) {
102-
if (!(value instanceof MongooseDocumentArray))
103-
value = new MongooseDocumentArray(value, this.path, doc);
104-
105-
for (var i = 0, l = value.length; i < l; i++) {
106-
if (!(value[i] instanceof Subdocument)) {
107-
var doc = new this.caster(null, value);
108-
109-
if (init) {
110-
// skip _id for pre-init hooks
111-
delete doc._doc._id;
103+
if (!(value instanceof MongooseDocumentArray)) {
104+
if(doc)
105+
retval = doc.get(this.path);
106+
if(!retval)
107+
retval = new MongooseDocumentArray([], this.path, doc);
108+
109+
for (var i = 0, l = value.length; i < l; i++) {
110+
var subdoc = null;
111+
if (!(value[i] instanceof Subdocument)) {
112+
for(var j = 0, k = retval.length; value[i]['_id'] && j < k; j++) {
113+
if(retval[j]._id.toString() ===
114+
value[i]['_id'].toString()) {
115+
subdoc = retval[j];
116+
break;
117+
}
118+
}
119+
if(!subdoc) {
120+
subdoc = new this.caster(null, retval);
121+
Array.prototype.push.apply(retval, [subdoc]);
122+
}
123+
if(init) {
124+
delete subdoc._doc._id;
125+
subdoc.init(value[i]);
126+
} else {
127+
subdoc.set (value[i]);
128+
}
112129
}
113-
114-
value[i] = init
115-
? doc.init(value[i])
116-
: doc.set (value[i]);
117130
}
118131
}
119-
120-
return value;
132+
return retval;
121133
} else {
122134
return this.cast([value], doc);
123135
}

‎lib/types/embedded.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ EmbeddedDocument.prototype.__proto__ = Document.prototype;
4141
var oldSet = Document.prototype.set;
4242

4343
EmbeddedDocument.prototype.set = function (path) {
44-
this.markModified(path);
44+
//this.markModified(path);
4545
return oldSet.apply(this, arguments);
4646
};
4747

‎test/model.test.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,9 @@ module.exports = {
700700

701701
BlogPost.create(doc, function (err, post) {
702702
BlogPost.findById(post.id, function (err, postRead) {
703-
should.strictEqual(null, err);
704-
db.close();
705-
//set the same data again back to the document.
706-
//expected result, nothing should be set to modified
707-
postRead.set(postRead.toObject());
703+
//set the same data again back to the document.
704+
//expected result, nothing should be set to modified
705+
postRead.set(post.toObject());
708706

709707
postRead.isModified('title').should.be.false;
710708
postRead.isModified('slug').should.be.false;
@@ -719,6 +717,8 @@ module.exports = {
719717
//uncomment this, this should the bug with respect to Embedded array
720718
//documents where it is set to modified even though it is not
721719
postRead.isModified('comments').should.be.false;
720+
721+
db.close();
722722
});
723723
});
724724
},

0 commit comments

Comments
 (0)