cascading-relations
Advanced tools
Comparing version 0.7.4 to 0.7.5
26
index.js
@@ -22,6 +22,11 @@ var Q, dot, mongoose; | ||
schema.post('init', function(next) { | ||
var info, orig, path, val, _ref; | ||
this.$__movePopulated(); | ||
return true; | ||
}); | ||
schema.methods.$__movePopulated = function() { | ||
var info, orig, path, val, _ref, _results; | ||
if (this.$__.populated != null) { | ||
this._related = {}; | ||
_ref = this.$__.populated; | ||
_results = []; | ||
for (path in _ref) { | ||
@@ -32,7 +37,20 @@ info = _ref[path]; | ||
dot.set(this, path, val, true); | ||
dot.set(this._related, path, orig, true); | ||
_results.push(dot.set(this._related, path, orig, true)); | ||
} | ||
return _results; | ||
} | ||
return true; | ||
}); | ||
}; | ||
schema.methods.populate = function(fields, callback) { | ||
var _this = this; | ||
return mongoose.Document.prototype.populate.apply(this, [ | ||
fields, function(err, doc) { | ||
if (!err) { | ||
_this.$__movePopulated(); | ||
return callback(err, doc); | ||
} else { | ||
return callback(err, doc); | ||
} | ||
} | ||
]); | ||
}; | ||
schema.methods.cascadeSave = function(callback, config) { | ||
@@ -39,0 +57,0 @@ if (config == null) { |
{ | ||
"name": "cascading-relations", | ||
"version": "0.7.4", | ||
"description": "", | ||
"version": "0.7.5", | ||
"description": "Alternate populate/save/delete functionality for mongoose to allow for cascading relations", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -100,3 +100,2 @@ var barClass, barSchema, bazClass, bazSchema, dot, fooClass, fooSchema, mongoose, relations, should; | ||
return mongoose.model('Foo').find().populate('multi._bar').populate('multi._bars').exec(function(err, res) { | ||
console.log(res); | ||
res[0].multi._bar.toString().should.equal(bar._id.toString()); | ||
@@ -343,3 +342,3 @@ res[0]._related.multi._bar.title.should.equal('My Bar'); | ||
}); | ||
return it('should be accurate when you put with less relations (implicit delete)', function(done) { | ||
it('should be accurate when you put with less relations (implicit delete)', function(done) { | ||
var foo, | ||
@@ -369,2 +368,29 @@ _this = this; | ||
}); | ||
return it('should rearrange when running populate on document rather than query', function(done) { | ||
var foo, | ||
_this = this; | ||
foo = new fooClass({ | ||
title: 'My Foo', | ||
_related: { | ||
_bars: [ | ||
{ | ||
title: 'First Bar' | ||
}, { | ||
title: 'Second Bar' | ||
} | ||
] | ||
} | ||
}); | ||
return foo.cascadeSave(function(err, res) { | ||
return fooClass.findById(res._id, function(err, foo) { | ||
return foo.populate('_bars', function(err, foo) { | ||
should.exist(foo._related); | ||
should.exist(foo._related._bars); | ||
foo._related._bars.length.should.equal(2); | ||
foo._bars[0].toString().should.equal(foo._related._bars[0]._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
45768
798