Skip to content

Commit 89e2f85

Browse files
committed
Updates mongodb driver to support association extra properties
1 parent 5a9db28 commit 89e2f85

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

lib/Associations/Many.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ exports.prepare = function (Model, associations) {
8080
};
8181
};
8282

83-
exports.extend = function (Model, Instance, Driver, associations, opts) {
83+
exports.extend = function (Model, Instance, Driver, associations, opts, createInstance) {
8484
for (var i = 0; i < associations.length; i++) {
85-
extendInstance(Model, Instance, Driver, associations[i], opts);
85+
extendInstance(Model, Instance, Driver, associations[i], opts, createInstance);
8686
}
8787
};
8888

@@ -105,7 +105,7 @@ exports.autoFetch = function (Instance, associations, opts, cb) {
105105
}
106106
};
107107

108-
function extendInstance(Model, Instance, Driver, association, opts) {
108+
function extendInstance(Model, Instance, Driver, association, opts, createInstance) {
109109
if (Model.settings.get("instance.cascadeRemove")) {
110110
Instance.on("beforeRemove", function () {
111111
Instance[association.delAccessor]();
@@ -205,7 +205,7 @@ function extendInstance(Model, Instance, Driver, association, opts) {
205205
}
206206

207207
if (Driver.hasMany) {
208-
return Driver.hasMany(Model, association).get(Instance, conditions, options, cb);
208+
return Driver.hasMany(Model, association).get(Instance, conditions, options, createInstance, cb);
209209
}
210210

211211
options.__merge = {

lib/Drivers/DML/mongodb.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var mongodb = require("mongodb");
22
var util = require("../../Utilities");
3+
var _ = require('lodash');
34

45
exports.Driver = Driver;
56

@@ -84,11 +85,28 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
8485
}
8586

8687
return cursor.toArray(function (err, docs) {
88+
var pending = 0;
89+
8790
for (var i = 0; i < docs.length; i++) {
8891
convertFromDB(docs[i]);
89-
// docs[i]._id = docs[i]._id.toString();
92+
if (opts.extra && opts.extra[docs[i]._id]) {
93+
docs[i] = _.merge(docs[i], _.omit(opts.extra[docs[i]._id], '_id'));
94+
}
95+
if (opts.createInstance) {
96+
pending += 1;
97+
docs[i] = opts.createInstance(docs[i], {
98+
extra : opts.extra_props
99+
}, function () {
100+
if (--pending) {
101+
return cb(null, docs);
102+
}
103+
});
104+
}
105+
}
106+
107+
if (pending === 0) {
108+
return cb(err, docs);
90109
}
91-
return cb(err, docs);
92110
});
93111
};
94112

@@ -180,7 +198,7 @@ Driver.prototype.hasMany = function (Model, association) {
180198
return cb(null, true);
181199
});
182200
},
183-
get: function (Instance, conditions, options, cb) {
201+
get: function (Instance, conditions, options, createInstance, cb) {
184202
return db.find({
185203
_id : new mongodb.ObjectID(Instance[Model.id])
186204
}, [ association.name ]).toArray(function (err, docs) {
@@ -192,15 +210,20 @@ Driver.prototype.hasMany = function (Model, association) {
192210
}
193211

194212
conditions._id = { $in: [] };
213+
options.extra = {};
195214
for (var i = 0; i < docs[0][association.name].length; i++) {
196215
conditions._id.$in.push(new mongodb.ObjectID(docs[0][association.name][i]._id));
216+
options.extra[docs[0][association.name][i]._id] = docs[0][association.name][i];
197217
}
198218

199219
if (options.order) {
200220
options.order[0] = options.order[0][1];
201221
options.order = util.standardizeOrder(options.order);
202222
}
203223

224+
options.extra_props = association.props;
225+
options.createInstance = createInstance;
226+
204227
return driver.find(null, association.model.table, conditions, options, cb);
205228
});
206229
},

lib/Model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function Model(opts) {
116116
LazyLoad.extend(instance, model, opts.properties);
117117
}
118118
OneAssociation.extend(model, instance, opts.driver, one_associations, assoc_opts);
119-
ManyAssociation.extend(model, instance, opts.driver, many_associations, assoc_opts);
119+
ManyAssociation.extend(model, instance, opts.driver, many_associations, assoc_opts, createInstance);
120120
ExtendAssociation.extend(model, instance, opts.driver, extend_associations, assoc_opts);
121121

122122
OneAssociation.autoFetch(instance, one_associations, assoc_opts, function () {

test/integration/association-hasmany-extra.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ describe("hasMany extra properties", function() {
5656

5757
John.pets.length.should.equal(2);
5858

59-
John.pets[0].should.have.property("id");
6059
John.pets[0].should.have.property("name");
6160
John.pets[0].should.have.property("extra");
6261
John.pets[0].extra.should.be.a("object");

test/run.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ function shouldRunTest(file) {
3131
var name = file.substr(0, file.length - 3);
3232
var proto = process.env.ORM_PROTOCOL;
3333

34-
if (proto == "mongodb" && [ "association-hasmany-extra",
35-
"model-aggregate",
34+
if (proto == "mongodb" && [ "model-aggregate",
3635
"property-lazyload", "property-number-size",
3736
"smart-types" ].indexOf(name) >= 0) return false;
3837

0 commit comments

Comments
 (0)