Skip to content

Commit 784b341

Browse files
committed
Fixes lazyload properties (it had fixed "id" key), updates tests for mongodb
Some cleanup on mongodb driver
1 parent c036ada commit 784b341

File tree

4 files changed

+61
-46
lines changed

4 files changed

+61
-46
lines changed

lib/Drivers/DML/mongodb.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
8585
}
8686

8787
return cursor.toArray(function (err, docs) {
88+
if (err) {
89+
throw err;
90+
return cb(err);
91+
}
92+
8893
var pending = 0;
8994

9095
for (var i = 0; i < docs.length; i++) {
@@ -106,7 +111,7 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
106111
}
107112

108113
if (pending === 0) {
109-
return cb(err, docs);
114+
return cb(null, docs);
110115
}
111116
});
112117
};
@@ -280,6 +285,7 @@ Driver.prototype.hasMany = function (Model, association) {
280285
};
281286

282287
Driver.prototype.update = function (table, changes, conditions, cb) {
288+
convertToDB(changes);
283289
convertToDB(conditions);
284290

285291
return this.db.collection(table).update(
@@ -308,55 +314,64 @@ Driver.prototype.clear = function (table, cb) {
308314
function convertToDB(obj) {
309315
for (var k in obj) {
310316
if (Array.isArray(obj[k])) {
311-
if (k == "_id") {
312-
for (var i = 0; i < obj[k].length; i++) {
313-
obj[k][i] = new mongodb.ObjectID(obj[k][i]);
314-
}
317+
for (var i = 0; i < obj[k].length; i++) {
318+
obj[k][i] = convertToDBVal(k, obj[k][i]);
315319
}
316320

317321
obj[k] = { $in: obj[k] };
318322
continue;
319323
}
320324

321-
if (obj[k] && typeof obj[k].sql_comparator == "function") {
322-
var val = (k != "_id" ? obj[k].val : new mongodb.ObjectID(obj[k].val));
323-
var comp = obj[k].sql_comparator();
324-
var condition = {};
325-
326-
switch (comp) {
327-
case "gt":
328-
case "gte":
329-
case "lt":
330-
case "lte":
331-
case "ne":
332-
condition["$" + comp] = val;
333-
break;
334-
case "eq":
335-
condition = val;
336-
break;
337-
case "between":
338-
condition["$min"] = obj[k].from;
339-
condition["$max"] = obj[k].to;
340-
break;
341-
case "like":
342-
condition["$regex"] = obj[k].expr.replace("%", ".*");
343-
break;
344-
}
345-
346-
obj[k] = condition;
347-
continue;
348-
}
349-
350-
if (k == "_id" && typeof obj[k] == "string") {
351-
obj[k] = new mongodb.ObjectID(obj[k]);
352-
}
325+
obj[k] = convertToDBVal(k, obj[k]);
353326
}
354327
}
355328

356329
function convertFromDB(obj) {
357330
for (var k in obj) {
358331
if (obj[k] instanceof mongodb.ObjectID) {
359332
obj[k] = obj[k].toString();
333+
} else if (obj[k] instanceof mongodb.Binary) {
334+
obj[k] = new Buffer(obj[k].value(), "binary");
360335
}
361336
}
362337
}
338+
339+
function convertToDBVal(key, value) {
340+
if (value && typeof value.sql_comparator == "function") {
341+
var val = (key != "_id" ? value.val : new mongodb.ObjectID(value.val));
342+
var comp = value.sql_comparator();
343+
var condition = {};
344+
345+
switch (comp) {
346+
case "gt":
347+
case "gte":
348+
case "lt":
349+
case "lte":
350+
case "ne":
351+
condition["$" + comp] = val;
352+
break;
353+
case "eq":
354+
condition = val;
355+
break;
356+
case "between":
357+
condition["$min"] = val.from;
358+
condition["$max"] = val.to;
359+
break;
360+
case "like":
361+
condition["$regex"] = val.expr.replace("%", ".*");
362+
break;
363+
}
364+
365+
return condition;
366+
}
367+
368+
if (Buffer.isBuffer(value)) {
369+
return new mongodb.Binary(value);
370+
}
371+
372+
if (key == "_id" && typeof value == "string") {
373+
value = new mongodb.ObjectID(value);
374+
}
375+
376+
return value;
377+
}

lib/LazyLoad.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
1212
Object.defineProperty(Instance, "get" + method, {
1313
value: function (cb) {
1414
var conditions = {};
15-
conditions[Model.id] = Instance.id;
15+
conditions[Model.id] = Instance[Model.id];
1616

1717
Model.find(conditions, { cache: false }).only(property).first(function (err, item) {
1818
return cb(err, item ? item[property] : null);
@@ -25,7 +25,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
2525
Object.defineProperty(Instance, "remove" + method, {
2626
value: function (cb) {
2727
var conditions = {};
28-
conditions[Model.id] = Instance.id;
28+
conditions[Model.id] = Instance[Model.id];
2929

3030
Model.find(conditions, { cache: false }).only(property).first(function (err, item) {
3131
if (err) {
@@ -35,8 +35,9 @@ function addLazyLoadProperty(name, Instance, Model, property) {
3535
return cb(null);
3636
}
3737

38-
item[Model.id] = Instance.id;
3938
item[property] = null;
39+
item[Model.id] = Instance[Model.id];
40+
4041
return item.save(cb);
4142
});
4243

@@ -47,18 +48,18 @@ function addLazyLoadProperty(name, Instance, Model, property) {
4748
Object.defineProperty(Instance, "set" + method, {
4849
value: function (data, cb) {
4950
var conditions = {};
50-
conditions[Model.id] = Instance.id;
51+
conditions[Model.id] = Instance[Model.id];
5152

52-
Model.find(conditions, { cache: false }).only(property).first(function (err, item) {
53+
Model.find(conditions, { cache: false }).first(function (err, item) {
5354
if (err) {
5455
return cb(err);
5556
}
5657
if (!item) {
5758
return cb(null);
5859
}
5960

60-
item[Model.id] = Instance.id;
6161
item[property] = data;
62+
6263
return item.save(cb);
6364
});
6465

test/integration/property-lazyload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe("LazyLoad properties", function() {
9191
John.setPhoto(OtherPersonPhoto, function (err) {
9292
should.equal(err, null);
9393

94-
Person.get(1, function (err, John) {
94+
Person.find().first(function (err, John) {
9595
should.equal(err, null);
9696

9797
John.should.be.a("object");

test/run.js

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

3434
if (proto == "mongodb" && [ "model-aggregate",
35-
"property-lazyload", "property-number-size",
36-
"smart-types" ].indexOf(name) >= 0) return false;
35+
"property-number-size", "smart-types" ].indexOf(name) >= 0) return false;
3736

3837
return true;
3938
}

0 commit comments

Comments
 (0)