diff --git a/lib/Drivers/DML/mongodb.js b/lib/Drivers/DML/mongodb.js index 9579362d..fbf99b39 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, @@ -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); @@ -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/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/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; 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 }