Skip to content

Commit

Permalink
Added support for Array property type; added support for non-ObjectID…
Browse files Browse the repository at this point in the history
… _ids on MongoDB driver
  • Loading branch information
rafaelkaufmann committed Nov 16, 2013
1 parent b6a5b2c commit 3d7ff3f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
33 changes: 30 additions & 3 deletions lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,11 @@ Driver.prototype.hasMany = function (Model, association) {
};

Driver.prototype.update = function (table, changes, conditions, cb) {
convertToDB(changes, this.config.timezone);
convertToDBTypes(changes, this.config.timezone);
convertToDB(conditions, this.config.timezone);

debugger;

This comment has been minimized.

Copy link
@notheotherben

notheotherben Nov 28, 2013

Can you remove this? Not needed in production code

This comment has been minimized.

Copy link
@rafaelkaufmann

rafaelkaufmann Nov 28, 2013

Author Owner

Already did, please check the following commit (0e0584e) :)

This comment has been minimized.

Copy link
@notheotherben

notheotherben Nov 28, 2013

Thanks, also going to try and get a discussion going on how to implement ObjectID support in a non-hacky manner over in dresende#390.

This comment has been minimized.

Copy link
@rafaelkaufmann

rafaelkaufmann Nov 28, 2013

Author Owner

Much appreciated. 👍


return this.db.collection(table).update(
conditions,
{
Expand Down Expand Up @@ -366,6 +368,12 @@ function convertToDB(obj, timeZone) {
}
}

function convertToDBTypes(obj, timeZone) {
for (var k in obj) {
obj[k] = convertToDBType(k, obj[k], timeZone);
}
}

function convertFromDB(obj, timezone) {
for (var k in obj) {
if (obj[k] instanceof mongodb.ObjectID) {
Expand All @@ -379,9 +387,22 @@ function convertFromDB(obj, timezone) {
}
}

function convertToID(value) {
if (value != null && 'number' != typeof value && (value.length != 12 && value.length != 24)) {
return value;
} else {
return mongodb.ObjectID(value);
}
}

function convertToDBVal(key, value, timezone) {
if (value && typeof value.sql_comparator == "function") {
var val = (key != "_id" ? value.val : new mongodb.ObjectID(value.val));
var val;
if (key == "_id") {
val = convertToID(value.val);
} else {
val = value.val;
}
var comp = value.sql_comparator();
var condition = {};

Expand All @@ -408,12 +429,18 @@ function convertToDBVal(key, value, timezone) {
return condition;
}

return convertToDBType(key, value, timezone);

}

function convertToDBType(key, value, timezone) {

if (Buffer.isBuffer(value)) {
return new mongodb.Binary(value);
}

if (key == "_id" && typeof value == "string") {
value = new mongodb.ObjectID(value);
value = convertToID(value);
}

return value;
Expand Down
5 changes: 4 additions & 1 deletion lib/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ exports.normalize = function (prop, customTypes, Settings) {
case "Object":
prop = { type: "object" };
break;
case "Array":
prop = { type: "array" };
break;
case "Buffer":
prop = { type: "binary" };
break;
Expand All @@ -30,7 +33,7 @@ exports.normalize = function (prop, customTypes, Settings) {
prop = { type: "enum", values: prop };
}

if ([ "text", "number", "boolean", "date", "enum", "object", "binary", "point" ].indexOf(prop.type) === -1) {
if ([ "text", "number", "boolean", "date", "enum", "object", "array", "binary", "point" ].indexOf(prop.type) === -1) {
if (!(prop.type in customTypes)) {
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Unknown property type: " + prop.type);
}
Expand Down

0 comments on commit 3d7ff3f

Please sign in to comment.