Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Array property type; force update of composite properties; added support for non-ObjectID IDs. #387

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
49 changes: 37 additions & 12 deletions lib/Drivers/DML/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
};
Expand All @@ -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++) {
Expand All @@ -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);
}
}

Expand All @@ -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 = {};

Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -510,7 +516,7 @@ function Instance(Model, opts) {
Object.defineProperty(instance, k, {
value : opts.methods[k].bind(instance),
enumerable : false,
writable : true
writeable : true
});
}

Expand Down
7 changes: 5 additions & 2 deletions lib/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
];

Expand All @@ -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;
Expand All @@ -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;
Expand Down