diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 45fc3657..789d4bd5 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -105,7 +105,7 @@ Driver.prototype.close = function (cb) { Driver.prototype.find = function (fields, table, conditions, opts, cb) { var collection = this.db.collection(table); - convertToDB(conditions, this.config.timezone); + convertToDB(conditions, this.config.timezone, this.config.enableGeneralID); var cursor = (fields ? collection.find(conditions, fields) : collection.find(conditions)); @@ -159,7 +159,7 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) { Driver.prototype.count = function (table, conditions, opts, cb) { var collection = this.db.collection(table); - convertToDB(conditions, this.config.timezone); + convertToDB(conditions, this.config.timezone, this.config.enableGeneralID); var cursor = collection.find(conditions); @@ -186,7 +186,7 @@ Driver.prototype.count = function (table, conditions, opts, cb) { }; Driver.prototype.insert = function (table, data, id_prop, cb) { - convertToDB(data, this.config.timezone); + convertToDBTypes(data, this.config.timezone, this.config.enableGeneralID); return this.db.collection(table).insert( data, @@ -325,8 +325,8 @@ Driver.prototype.hasMany = function (Model, association) { }; Driver.prototype.update = function (table, changes, conditions, cb) { - convertToDB(changes, this.config.timezone); - convertToDB(conditions, this.config.timezone); + convertToDBTypes(changes, this.config.timezone, this.config.enableGeneralID); + convertToDB(conditions, this.config.timezone, this.config.enableGeneralID); return this.db.collection(table).update( conditions, @@ -342,7 +342,7 @@ Driver.prototype.update = function (table, changes, conditions, cb) { }; Driver.prototype.remove = function (table, conditions, cb) { - convertToDB(conditions, this.config.timezone); + convertToDB(conditions, this.config.timezone, this.config.enableGeneralID); return this.db.collection(table).remove(conditions, cb); }; @@ -351,7 +351,7 @@ Driver.prototype.clear = function (table, cb) { return this.db.collection(table).remove(cb); }; -function convertToDB(obj, timeZone) { +function convertToDB(obj, timeZone, enableGeneralID) { for (var k in obj) { if ([ 'and', 'or', 'not' ].indexOf(k) >= 0) { for (var j = 0; j < obj[k].length; j++) { @@ -363,14 +363,20 @@ function convertToDB(obj, timeZone) { } if (Array.isArray(obj[k]) && k[0] != '$') { for (var i = 0; i < obj[k].length; i++) { - obj[k][i] = convertToDBVal(k, obj[k][i], timeZone); + obj[k][i] = convertToDBVal(k, obj[k][i], timeZone, enableGeneralID); } obj[k] = { $in: obj[k] }; continue; } - obj[k] = convertToDBVal(k, obj[k], timeZone); + obj[k] = convertToDBVal(k, obj[k], timeZone, enableGeneralID); + } +} + +function convertToDBTypes(obj, timeZone, enableGeneralID) { + for (var k in obj) { + obj[k] = convertToDBType(k, obj[k], timeZone, enableGeneralID); } } @@ -387,9 +393,22 @@ function convertFromDB(obj, timezone) { } } -function convertToDBVal(key, value, timezone) { +function convertToID(value, enableGeneralID) { + if (enableGeneralID && value != null && 'number' != typeof value && (value.length != 12 && value.length != 24)) { + return value; + } else { + return mongodb.ObjectID(value); + } +} + +function convertToDBVal(key, value, timezone, enableGeneralID) { if (value && typeof value.sql_comparator == "function") { - var val = (key != "_id" ? value : new mongodb.ObjectID(value)); + var val; + if (key == "_id") { + val = convertToID(value.val, enableGeneralID); + } else { + val = value.val; + } var comp = value.sql_comparator(); var condition = {}; @@ -416,12 +435,18 @@ function convertToDBVal(key, value, timezone) { return condition; } + return convertToDBType(key, value, timezone, enableGeneralID); + +} + +function convertToDBType(key, value, timezone, enableGeneralID) { + if (Buffer.isBuffer(value)) { return new mongodb.Binary(value); } if (key == "_id" && typeof value == "string") { - value = new mongodb.ObjectID(value); + value = convertToID(value, enableGeneralID); } return value; diff --git a/lib/Instance.js b/lib/Instance.js index 859be697..51b9daa6 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -108,6 +108,12 @@ function Instance(Model, opts) { } instance_saving = true; + for (var k in Model.properties) { + if (opts.changes.indexOf(k) == -1 && + (Model.properties[k].type == 'array' || Model.properties[k].type == 'object')) + opts.changes.push(k); + } + handleValidations(function (err) { if (err) { return saveError(cb, err); @@ -510,7 +516,7 @@ function Instance(Model, opts) { Object.defineProperty(instance, k, { value : opts.methods[k].bind(instance), enumerable : false, - writable : true + writeable : true }); } diff --git a/lib/Property.js b/lib/Property.js index fdfc6ba4..0107e04b 100644 --- a/lib/Property.js +++ b/lib/Property.js @@ -2,7 +2,7 @@ var _ = require('lodash'); var ORMError = require("./Error"); var KNOWN_TYPES = [ - "text", "number", "integer", "boolean", "date", "enum", "object", + "text", "number", "integer", "boolean", "date", "enum", "object", "array", "binary", "point", "serial" ]; @@ -24,6 +24,9 @@ exports.normalize = function (opts) { case "Object": opts.prop = { type: "object" }; break; + case "Array": + opts.prop = { type: "array" }; + break; case "Buffer": opts.prop = { type: "binary" }; break; @@ -40,7 +43,7 @@ exports.normalize = function (opts) { if (KNOWN_TYPES.indexOf(opts.prop.type) === -1 && !(opts.prop.type in opts.customTypes)) { throw new ORMError("Unknown property type: " + opts.prop.type, 'NO_SUPPORT'); - } + } if (!opts.prop.hasOwnProperty("required") && opts.settings.get("properties.required")) { opts.prop.required = true;