cascading-relations
Advanced tools
Comparing version 0.7.8 to 0.7.9
36
index.js
@@ -27,9 +27,25 @@ var Q, dot, mongoose, _; | ||
}); | ||
schema.methods.$__movePopulated = function() { | ||
var info, orig, path, val, _ref; | ||
schema.methods.$__movePopulated = function(paths) { | ||
var info, orig, path, val, _i, _len, _results; | ||
if (paths == null) { | ||
paths = null; | ||
} | ||
if (this.$__.populated != null) { | ||
this._related = {}; | ||
_ref = this.$__.populated; | ||
for (path in _ref) { | ||
info = _ref[path]; | ||
if (this._related == null) { | ||
this._related = {}; | ||
} | ||
if (paths) { | ||
if (!(paths instanceof Array)) { | ||
paths = [paths]; | ||
} | ||
} else { | ||
paths = _.keys(this.$__.populated); | ||
} | ||
_results = []; | ||
for (_i = 0, _len = paths.length; _i < _len; _i++) { | ||
path = paths[_i]; | ||
info = this.$__.populated[path]; | ||
if (info == null) { | ||
continue; | ||
} | ||
val = info.value; | ||
@@ -42,14 +58,16 @@ orig = dot.get(this, path); | ||
dot.set(this._related, path, orig, true); | ||
_results.push(delete this.$__.populated[path]); | ||
} | ||
return delete this.$__.populated[path]; | ||
return _results; | ||
} | ||
}; | ||
schema.methods.populate = function() { | ||
var args, callback, | ||
var args, callback, paths, | ||
_this = this; | ||
args = _.values(arguments); | ||
paths = args[0]; | ||
callback = args.pop(); | ||
args.push(function(err, doc) { | ||
if (!err) { | ||
_this.$__movePopulated(); | ||
_this.$__movePopulated(paths); | ||
return callback(err, doc); | ||
@@ -56,0 +74,0 @@ } else { |
{ | ||
"name": "cascading-relations", | ||
"version": "0.7.8", | ||
"version": "0.7.9", | ||
"description": "Alternate populate/save/delete functionality for mongoose to allow for cascading relations", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -341,3 +341,3 @@ var barClass, barSchema, bazClass, bazSchema, dot, fooClass, fooSchema, mongoose, relations, should; | ||
}); | ||
it('should be accurate when you put with less relations (implicit delete)', function(done) { | ||
it('should be accurate when you put with fewer relations (implicit delete)', function(done) { | ||
var foo, | ||
@@ -421,3 +421,3 @@ _this = this; | ||
}); | ||
return it('should still work with mongoose populate on query, which automatically creates a new model instance when you push', function(done) { | ||
it('should still work with mongoose populate on query, which automatically creates a new model instance when you push', function(done) { | ||
var myFoo; | ||
@@ -457,2 +457,35 @@ myFoo = new fooClass({ | ||
}); | ||
return it('should handle multiple calls of populate', function(done) { | ||
var myFoo; | ||
myFoo = new fooClass({ | ||
title: 'Test Foo', | ||
_related: { | ||
_bars: [ | ||
{ | ||
title: 'Test Bar' | ||
} | ||
], | ||
_bar: { | ||
title: 'Test bar 2' | ||
} | ||
} | ||
}); | ||
return myFoo.cascadeSave(function(err) { | ||
if (err) { | ||
return done(err); | ||
} | ||
myFoo._related._bars.length.should.equal(1); | ||
myFoo._bars.length.should.equal(1); | ||
return fooClass.findById(myFoo._id).exec(function(err, foo) { | ||
return foo.populate('_bars', function(err) { | ||
return foo.populate('_bar', function(err) { | ||
foo._related._bars[0].title.should.equal('Test Bar'); | ||
foo._related._bar.title.should.equal('Test bar 2'); | ||
foo._bar.toString().should.equal(foo._related._bar._id.toString()); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
52858
928