nodee-model
Advanced tools
Comparing version 0.9.1 to 0.9.2
@@ -77,3 +77,3 @@ 'use strict'; | ||
.update({ $inc:{ sortOrder: 1 }}, function(err){ | ||
if(err) next(new Error('Orderable: updating sortOrder failed').cause(err)); | ||
if(err) next(new Error((doc.constructor._name||'Orderable')+': updating sortOrder failed').cause(err)); | ||
else next(); | ||
@@ -80,0 +80,0 @@ }); |
@@ -69,3 +69,3 @@ 'use strict'; | ||
doc.constructor.collection().find({ ancestors:doc.id }).remove(function(err){ | ||
if(err) next(new Error('Tree on beforeRemove: cannot remove descendants').cause(err)); | ||
if(err) next(new Error((doc.constructor._name||'Tree')+' on beforeRemove: cannot remove descendants').cause(err)); | ||
else updateParent('removeRef').call(doc, next); | ||
@@ -82,3 +82,3 @@ }); | ||
if(!doc.ancestors) next(new Error('Tree updateParent: missing ancestors').details({ code:'INVALID', validErrs:{ ancestors:['required'] } })); | ||
if(!doc.ancestors) next(new Error((doc.constructor._name||'Tree')+' updateParent: missing ancestors').details({ code:'INVALID', validErrs:{ ancestors:['required'] } })); | ||
else if(doc.ancestors.length && (doc.constructor.getDefaults().options.storeChildren || | ||
@@ -99,3 +99,3 @@ doc.constructor.getDefaults().options.storeChildrenCount)) { | ||
doc.constructor.collection().findId(parentId).update(updateExp, function(err){ | ||
if(err) next(new Error('Tree updateParent: cannot update parent').cause(err)); | ||
if(err) next(new Error((doc.constructor._name||'Tree')+' updateParent: cannot update parent').cause(err)); | ||
else next(); | ||
@@ -114,8 +114,17 @@ }); | ||
Tree.prototype.validateAncestors = function(cb){ // cb(err) | ||
if(this.ancestors && this.ancestors.length>0){ | ||
var newPath = this.ancestors; | ||
var doc = this; | ||
if(doc.ancestors && doc.ancestors.length>0){ | ||
var newPath = doc.ancestors; | ||
this.constructor.collection().findId(this.ancestors).all(function(err, ancs){ | ||
if(err) cb(new Error('Tree.prototype.validateAncestors: cannot get ancestors').cause(err)); | ||
else if(ancs.length !== newPath.length) cb(new Error('Tree.prototype.validateAncestors: cannot find all ancestors').details({ code:'EXECFAIL' })); | ||
if(doc.id && doc.ancestors.indexOf(doc.id) > -1) return cb(new Error((doc.constructor._name||'Tree')+'.prototype.validateAncestors: Ancestor cannot be document itself').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
var uniqueAncIds = {}; | ||
for(var i=0;i<doc.ancestors.length;i++){ | ||
if(uniqueAncIds[ doc.ancestors[i] ]) return cb(new Error((doc.constructor._name||'Tree')+'.prototype.validateAncestors: Ancestor duplicities').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
uniqueAncIds[ doc.ancestors[i] ] = true; | ||
} | ||
doc.constructor.collection().findId(doc.ancestors).fields({ id:true }).all(function(err, ancs){ | ||
if(err) cb(new Error((doc.constructor._name||'Tree')+'.prototype.validateAncestors: cannot get ancestors').cause(err)); | ||
else if(ancs.length !== newPath.length) cb(new Error((doc.constructor._name||'Tree')+'.prototype.validateAncestors: cannot find all ancestors').details({ code:'EXECFAIL' })); | ||
else { | ||
@@ -127,3 +136,3 @@ var parentPathLength = -1; | ||
if(parentPathLength > -1 && parentPathLength + 1 !== ancs[a].ancestors.length) { | ||
cb(new Error('Tree.prototype.validateAncestors: inconsistent path')); | ||
cb(new Error((doc.constructor._name||'Tree')+'.prototype.validateAncestors: inconsistent path')); | ||
return; | ||
@@ -141,3 +150,3 @@ } | ||
else cb(); | ||
} | ||
}; | ||
@@ -153,3 +162,3 @@ /* | ||
// move inside self is not allowed | ||
if(doc.id===parentId) callback(new Error('Tree.prototype.move: Parent is same as child').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
if(doc.id===parentId) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: Parent is same as child').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
@@ -159,3 +168,3 @@ // dont trust user data, load old document to ensure data integrity | ||
if(err) callback(err); | ||
else if(!oldDoc) callback(new Error('Tree.prototype.move: Document not found').details({ code:'NOTFOUND' })); | ||
else if(!oldDoc) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: Document not found').details({ code:'NOTFOUND' })); | ||
else if(doc.ancestors[ doc.ancestors.length-1 ] === parentId) callback(null, doc); // not moved | ||
@@ -170,3 +179,3 @@ else if(parentId === 'root'){ | ||
if(err) callback(err); | ||
else if(updated!==1) callback(new Error('Tree.prototype.move: Updating failed, document not found').details({ code:'NOTFOUND' })); | ||
else if(updated!==1) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: Updating failed, document not found').details({ code:'NOTFOUND' })); | ||
else updateDescendants(doc, oldDoc, callback); // update all descendants | ||
@@ -179,3 +188,3 @@ }); | ||
if(err) callback(err); | ||
else if(!parent) callback(new Error('Tree.prototype.move: Parent not found').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
else if(!parent) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: Parent not found').details({ code:'INVALID', validErrs:{ ancestors:['invalid'] } })); | ||
else { | ||
@@ -190,3 +199,3 @@ // update document ancestors | ||
if(err) callback(err); | ||
else if(updated!==1) callback(new Error('Tree.prototype.move: Updating failed, document not found').details({ code:'NOTFOUND' })); | ||
else if(updated!==1) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: Updating failed, document not found').details({ code:'NOTFOUND' })); | ||
else updateDescendants(doc, oldDoc, callback); // update all descendants | ||
@@ -203,3 +212,3 @@ }); | ||
doc.constructor.collection().find({ ancestors:doc.id }).fields({ ancestors:1 }).all(function(err, descendants){ | ||
if(err) callback(new Error('Tree.prototype.move: failed to get descendants').cause(err)); | ||
if(err) callback(new Error((doc.constructor._name||'Tree')+'.prototype.move: failed to get descendants').cause(err)); | ||
else if(descendants.length > 0) { | ||
@@ -217,3 +226,3 @@ async.Series.each(descendants, function(i, next){ | ||
doc.constructor.collection().findId(descendants[i].id).update(updSet, function(err, desc){ | ||
if(err) next(new Error('Tree.prototype.move: failed to update descendant').cause(err)); | ||
if(err) next(new Error((doc.constructor._name||'Tree')+'.prototype.move: failed to update descendant').cause(err)); | ||
else next(); | ||
@@ -220,0 +229,0 @@ }); |
{ | ||
"name": "nodee-model", | ||
"preferGlobal": false, | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"author": "NODEE - Matus Szabo <matus@nodee.io>", | ||
@@ -6,0 +6,0 @@ "description": "Advanced, database agnostic ODM", |
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
229520
4915