cascading-relations
Advanced tools
Comparing version
39
index.js
@@ -36,8 +36,8 @@ var Q, dot, mongoose; | ||
}); | ||
schema.methods.cascadeSave = function(callback, allow) { | ||
if (allow == null) { | ||
allow = null; | ||
schema.methods.cascadeSave = function(callback, config) { | ||
if (config == null) { | ||
config = null; | ||
} | ||
this.$__.cascadeSave = true; | ||
this.$__.cascadeSaveLimitRelations = allow; | ||
this.$__.cascadeSaveConfig = config; | ||
return this.save(callback); | ||
@@ -51,3 +51,3 @@ }; | ||
var allowed, _i, _len, _ref; | ||
_ref = _this.$__.cascadeSaveLimitRelations; | ||
_ref = _this.$__.cascadeSaveConfig.limit; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
@@ -61,3 +61,3 @@ allowed = _ref[_i]; | ||
}; | ||
if (this.$__.cascadeSaveLimitRelations && !allowedRelation(path)) { | ||
if (this.$__.cascadeSaveConfig && this.$__.cascadeSaveConfig.limit && !allowedRelation(path)) { | ||
deferred.resolve(); | ||
@@ -134,3 +134,3 @@ return deferred.promise; | ||
schema.methods.$__saveRelatedDoc = function(path, data, ref, through) { | ||
var allowed, d, deferred, isArray, method, modelClass, newArr, newMod, orig, _i, _len, _ref, | ||
var allowed, d, deferred, filter, isArray, method, modelClass, newArr, newMod, orig, _i, _len, _ref, | ||
_this = this; | ||
@@ -149,2 +149,8 @@ deferred = Q.defer(); | ||
} | ||
if (this.$__.cascadeSaveConfig && this.$__.cascadeSaveConfig.filter) { | ||
filter = this.$__.cascadeSaveConfig.filter; | ||
data = filter(data, path); | ||
} else { | ||
filter = null; | ||
} | ||
modelClass = mongoose.model(ref); | ||
@@ -176,5 +182,5 @@ orig = this.get(path); | ||
method = 'cascadeSave'; | ||
if (_this.$__.cascadeSaveLimitRelations) { | ||
if (_this.$__.cascadeSaveConfig && (_this.$__.cascadeSaveConfig.limit != null)) { | ||
newArr = []; | ||
_ref = _this.$__.cascadeSaveLimitRelations; | ||
_ref = _this.$__.cascadeSaveConfig.limit; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
@@ -195,3 +201,6 @@ allowed = _ref[_i]; | ||
return deferred.resolve(res); | ||
}, newArr); | ||
}, { | ||
limit: newArr, | ||
filter: filter | ||
}); | ||
}); | ||
@@ -206,7 +215,8 @@ } else { | ||
} | ||
newArr = null; | ||
if ((newMod.cascadeSave != null) && typeof newMod.cascadeSave === 'function') { | ||
method = 'cascadeSave'; | ||
if (this.$__.cascadeSaveLimitRelations) { | ||
if (this.$__.cascadeSaveConfig && (this.$__.cascadeSaveConfig.limit != null)) { | ||
newArr = []; | ||
_ref = this.$__.cascadeSaveLimitRelations; | ||
_ref = this.$__.cascadeSaveConfig.limit; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
@@ -227,3 +237,6 @@ allowed = _ref[_i]; | ||
return deferred.resolve(res); | ||
}, newArr); | ||
}, { | ||
limit: newArr, | ||
filter: filter | ||
}); | ||
} | ||
@@ -230,0 +243,0 @@ return deferred.promise; |
{ | ||
"name": "cascading-relations", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,3 +25,4 @@ var barClass, barSchema, bazClass, bazSchema, fooClass, fooSchema, mongoose, relations, should; | ||
}, | ||
title: String | ||
title: String, | ||
account: String | ||
}); | ||
@@ -215,4 +216,5 @@ | ||
}); | ||
return it('should do a save while limiting cascaded relations', function(done) { | ||
var foo; | ||
it('should do a save while limiting cascaded relations', function(done) { | ||
var foo, | ||
_this = this; | ||
foo = new fooClass({ | ||
@@ -242,6 +244,17 @@ title: 'My Foo', | ||
return foo.cascadeSave(function(err, res) { | ||
_this.foo = res; | ||
should.not.exist(res._related._bar._baz); | ||
return done(); | ||
}, ['_bar', 'multi._bar', 'multi._bar._baz']); | ||
}, { | ||
limit: ['_bar', 'multi._bar', 'multi._bar._baz'], | ||
filter: function(doc) { | ||
doc.account = 'asdf'; | ||
return doc; | ||
} | ||
}); | ||
}); | ||
return it('should apply filter to cascading relations when saving', function(done) { | ||
this.foo._related._bar.account.should.equal('asdf'); | ||
return done(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37393
3.79%641
4.23%