diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 5a718824..2c6e1436 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -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; + return this.db.collection(table).update( conditions, { @@ -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) { @@ -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 = {}; @@ -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; diff --git a/lib/Property.js b/lib/Property.js index b96cc1b2..9adf3706 100644 --- a/lib/Property.js +++ b/lib/Property.js @@ -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; @@ -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); }