From 3d7ff3f03ee05a312b976dc7d2da81ed03d45af7 Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Sat, 16 Nov 2013 21:06:55 -0200 Subject: [PATCH 1/7] Added support for Array property type; added support for non-ObjectID _ids on MongoDB driver --- lib/Drivers/DML/mongodb.js | 33 ++++++++++++++++++++++++++++++--- lib/Property.js | 5 ++++- 2 files changed, 34 insertions(+), 4 deletions(-) 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); } From 0dc9149717d9daeaf2a6d39962e695eced52c8eb Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Sat, 16 Nov 2013 21:51:17 -0200 Subject: [PATCH 2/7] Force update of composite types --- lib/Instance.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Instance.js b/lib/Instance.js index d2984f75..6cadfac2 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -218,6 +218,12 @@ function Instance(Model, opts) { for (var i = 0; i < opts.changes.length; i++) { changes[opts.changes[i]] = data[opts.changes[i]]; } + + for (var k in Model.properties) { + if ((typeof changes[k] == 'undefined') && ["array","object"].indexOf(Model.properties[k].type) != -1) + changes[k] = data[k]; + } + for (i = 0; i < opts.id.length; i++) { conditions[opts.id[i]] = data[opts.id[i]]; } From 0e0584e28efb6f648e96f43e69539a8090782d3c Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Sat, 16 Nov 2013 21:54:25 -0200 Subject: [PATCH 3/7] Cleaning up after myself --- lib/Drivers/DML/mongodb.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 2c6e1436..42d2b94a 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -328,8 +328,6 @@ Driver.prototype.update = function (table, changes, conditions, cb) { convertToDBTypes(changes, this.config.timezone); convertToDB(conditions, this.config.timezone); - debugger; - return this.db.collection(table).update( conditions, { From 24bc66335d21273e86e58ffb243ce3a44bd050f6 Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Thu, 28 Nov 2013 17:38:20 -0200 Subject: [PATCH 4/7] Adding option to enable experimental support for non-ObjectID ids --- lib/Drivers/DML/mongodb.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 42d2b94a..9b35aca9 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); + convertToDB(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) { - convertToDBTypes(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,24 +351,24 @@ 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 (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) { +function convertToDBTypes(obj, timeZone, enableGeneralID) { for (var k in obj) { - obj[k] = convertToDBType(k, obj[k], timeZone); + obj[k] = convertToDBType(k, obj[k], timeZone, enableGeneralID); } } @@ -385,19 +385,19 @@ function convertFromDB(obj, timezone) { } } -function convertToID(value) { - if (value != null && 'number' != typeof value && (value.length != 12 && value.length != 24)) { +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) { +function convertToDBVal(key, value, timezone, enableGeneralID) { if (value && typeof value.sql_comparator == "function") { var val; if (key == "_id") { - val = convertToID(value.val); + val = convertToID(value.val, enableGeneralID); } else { val = value.val; } @@ -427,18 +427,18 @@ function convertToDBVal(key, value, timezone) { return condition; } - return convertToDBType(key, value, timezone); + return convertToDBType(key, value, timezone, enableGeneralID); } -function convertToDBType(key, value, timezone) { +function convertToDBType(key, value, timezone, enableGeneralID) { if (Buffer.isBuffer(value)) { return new mongodb.Binary(value); } if (key == "_id" && typeof value == "string") { - value = convertToID(value); + value = convertToID(value, enableGeneralID); } return value; From 77899b82fa3ea3cb67100cd8e8c5b928d1ab937b Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Wed, 29 Jan 2014 17:38:58 -0200 Subject: [PATCH 5/7] Minor fix to Mongo driver --- lib/Drivers/DML/mongodb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 9b35aca9..a7766506 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -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, this.config.enableGeneralID); + convertToDBTypes(data, this.config.timezone, this.config.enableGeneralID); return this.db.collection(table).insert( data, From 9703a267dad58c736a68e2ddc06d83190f9877c5 Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Wed, 29 Jan 2014 17:42:17 -0200 Subject: [PATCH 6/7] Fixed: changes to array/object properties would not be saved if no simple properties had changed --- lib/Instance.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/Instance.js b/lib/Instance.js index 6cadfac2..f780d516 100755 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -107,6 +107,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); @@ -219,11 +225,6 @@ function Instance(Model, opts) { changes[opts.changes[i]] = data[opts.changes[i]]; } - for (var k in Model.properties) { - if ((typeof changes[k] == 'undefined') && ["array","object"].indexOf(Model.properties[k].type) != -1) - changes[k] = data[k]; - } - for (i = 0; i < opts.id.length; i++) { conditions[opts.id[i]] = data[opts.id[i]]; } @@ -495,7 +496,7 @@ function Instance(Model, opts) { Object.defineProperty(instance, k, { value : opts.methods[k].bind(instance), enumerable : false, - writable : true + writeable : true }); } @@ -660,4 +661,4 @@ function Instance(Model, opts) { }); return instance; -} +} \ No newline at end of file From 29d24f1e0c5ebd4fb4d8d66303aca3d2a2da0d24 Mon Sep 17 00:00:00 2001 From: Rafael Kaufmann Date: Thu, 15 May 2014 18:09:34 -0300 Subject: [PATCH 7/7] Oops --- lib/Property.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Property.js b/lib/Property.js index e0db0425..0107e04b 100644 --- a/lib/Property.js +++ b/lib/Property.js @@ -25,7 +25,7 @@ exports.normalize = function (opts) { opts.prop = { type: "object" }; break; case "Array": - prop = { type: "array" }; + opts.prop = { type: "array" }; break; case "Buffer": opts.prop = { type: "binary" };