From bbe5e1ad8d6b4521cc1abb7b5ed61ae0f8c55dda Mon Sep 17 00:00:00 2001 From: Andrej Adamcik Date: Sat, 31 Dec 2016 14:38:13 +0000 Subject: [PATCH 1/3] retry reconnect --- lib/ORM.js | 11 +++++++++++ lib/Settings.js | 1 + 2 files changed, 12 insertions(+) diff --git a/lib/ORM.js b/lib/ORM.js index 254d32f0..a2aeb2dc 100644 --- a/lib/ORM.js +++ b/lib/ORM.js @@ -174,6 +174,17 @@ function ORM(driver_name, driver, settings) { this.driver.reconnect(function () { this.driver.on("error", onError); }.bind(this)); + + this.driver.reconnect(function (error) { + // if failed try reconnect again after timeout + if (error && this.settings.get("connection.reconnectTime")) { + setTimeout(function() { + onError(error); + }, this.settings.get("connection.reconnectTime")); + } else { + this.driver.on("error", onError); + } + }.bind(this)); if (this.listeners("error").length === 0) { // since user want auto reconnect, diff --git a/lib/Settings.js b/lib/Settings.js index 853a202c..9ec9229e 100644 --- a/lib/Settings.js +++ b/lib/Settings.js @@ -21,6 +21,7 @@ var default_settings = { }, connection : { reconnect : true, + reconnectTime : 10000, pool : false, debug : false } From 21299a27c6d6009fd3ada42c36baa557e3316181 Mon Sep 17 00:00:00 2001 From: Andrej Adamcik Date: Sat, 31 Dec 2016 15:10:24 +0000 Subject: [PATCH 2/3] mongo array and object support --- lib/Drivers/DML/mongodb.js | 23 +++++++++++++++++++++-- lib/Instance.js | 0 lib/Property.js | 5 ++++- 3 files changed, 25 insertions(+), 3 deletions(-) mode change 100755 => 100644 lib/Instance.js diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 9579362d..5c01de52 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, keyProperties, cb) { - convertToDB(data, this.config.timezone); + convertToDBTypes(data, this.config.timezone); return this.db.collection(table).insert( data, @@ -338,7 +338,7 @@ 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); return this.db.collection(table).update( @@ -387,6 +387,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) { @@ -440,6 +446,19 @@ function convertToDBVal(key, value, timezone) { return value; } +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); + } + + return value; +} + Object.defineProperty(Driver.prototype, "isSql", { value: false }); diff --git a/lib/Instance.js b/lib/Instance.js old mode 100755 new mode 100644 diff --git a/lib/Property.js b/lib/Property.js index c00375ba..4ac923aa 100644 --- a/lib/Property.js +++ b/lib/Property.js @@ -3,7 +3,7 @@ var ORMError = require("./Error"); var KNOWN_TYPES = [ "text", "number", "integer", "boolean", "date", "enum", "object", - "binary", "point", "serial" + "array", "binary", "point", "serial" ]; exports.normalize = function (opts) { @@ -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; From 362b5896cab2e6bef43d1aaeaa610f30e4e95cd8 Mon Sep 17 00:00:00 2001 From: Andrej Adamcik Date: Wed, 18 Jan 2017 00:33:24 +0000 Subject: [PATCH 3/3] insert convert id fix --- lib/Drivers/DML/mongodb.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 5c01de52..fbf99b39 100644 --- a/lib/Drivers/DML/mongodb.js +++ b/lib/Drivers/DML/mongodb.js @@ -198,12 +198,12 @@ Driver.prototype.insert = function (table, data, keyProperties, cb) { var i, ids = {}, prop; - if (keyProperties && docs.length) { + if (keyProperties && docs.ops.length) { for (i = 0; i < keyProperties.length; i++) { prop = keyProperties[i]; - if (prop.mapsTo in docs[0]) { - ids[prop.name] = docs[0][prop.mapsTo]; + if (prop.mapsTo in docs.ops[0]) { + ids[prop.name] = docs.ops[0][prop.mapsTo]; } } convertFromDB(ids, this.config.timezone);