Comparing version 0.1.7 to 0.1.8
@@ -6,8 +6,8 @@ var orm = require(__dirname + "/../lib/orm"); | ||
var Person = db.define("person", { | ||
"created" : { "type": "date" }, | ||
"name" : { "type": "string" }, | ||
"surname" : { "type": "string", "def": "" }, | ||
"age" : { "type": "int" }, | ||
"male" : { "type": "bool", "def": true }, | ||
"meta" : { "type": "struct" } | ||
"created" : Date, | ||
"name" : String, | ||
"surname" : String, | ||
"age" : Number, | ||
"male" : Boolean, | ||
"meta" : Object | ||
}, { | ||
@@ -38,2 +38,3 @@ "methods" : { | ||
createJohn(function (John) { | ||
console.log(John); | ||
// create the Pet Deco (if it does not exist) | ||
@@ -40,0 +41,0 @@ createDeco(function (Deco) { |
var orm = require(__dirname + "/../lib/orm"); | ||
orm.connect("mongodb://localhost:9999/dfs", function (success, db) { | ||
orm.connect("mongodb://localhost/dfs", function (success, db) { | ||
if (!success) { | ||
@@ -10,9 +10,9 @@ console.log("Error %d: %s", db.number, db.message); | ||
var Doc = db.define("docs", { | ||
"path" : { "type": "string" }, | ||
"key" : { "type": "string" }, | ||
"meta" : { "type": "struct" }, | ||
"copies" : { "type": "number" }, | ||
"size" : { "type": "number" }, | ||
"hash" : { "type": "string" }, | ||
"location" : { "type": "struct" } | ||
"path" : String, | ||
"key" : String, | ||
"meta" : Object, | ||
"copies" : Number, | ||
"size" : Number, | ||
"hash" : String, | ||
"location" : Object | ||
}); | ||
@@ -39,15 +39,15 @@ | ||
// var mydoc = new Doc({ | ||
// "path": "/my/path", | ||
// "key" : "mykey", | ||
// "meta": { "my": "doc" }, | ||
// "copies": 1, | ||
// "size": 12345, | ||
// "hash": "myhash", | ||
// "location": "nowhere" | ||
// }); | ||
// mydoc.save(function (err, doc) { | ||
// console.log(err); | ||
// console.log(doc); | ||
// }); | ||
var mydoc = new Doc({ | ||
"path": "/my/path", | ||
"key" : "mykey", | ||
"meta": { "my": "doc" }, | ||
"copies": 1, | ||
"size": 12345, | ||
"hash": "myhash", | ||
"location": "nowhere" | ||
}); | ||
mydoc.save(function (err, doc) { | ||
console.log(err); | ||
console.log(doc); | ||
}); | ||
}); |
@@ -175,2 +175,4 @@ var DBClient = function (client) { | ||
} | ||
opts.port = parseInt(opts.port, 10); | ||
@@ -177,0 +179,0 @@ client = new mongodb.Db(opts.database, new mongodb.Server(opts.host, opts.port, {})); |
@@ -57,2 +57,3 @@ var util = require("util"), | ||
case "struct": | ||
case "object": | ||
case "text": field += " TEXT"; break; | ||
@@ -123,14 +124,18 @@ case "num": | ||
var _table = collection.toLowerCase(collection); | ||
var _query = "SELECT * FROM `" + _table + "`"; | ||
var _query = "SELECT * FROM `" + _table + "`", tmp, _values = []; | ||
config = config || {}; | ||
if (config.conditions) _query = this._addQueryConditions(_query, config.conditions); | ||
if (config.conditions) { | ||
tmp = this._addQueryConditions(config.conditions); | ||
_query += tmp[0]; | ||
_values = _values.concat(tmp[1]); | ||
} | ||
if (config.order) _query = this._addQueryOrder(_query, config.order); | ||
if (config.limit) _query = this._addQueryLimit(_query, config.limit, config.skip); | ||
console.log(_query); | ||
//console.log(_query, _values); | ||
if (typeof config.callback == "function") { | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _values, function (err, info) { | ||
if (err) { | ||
@@ -144,3 +149,3 @@ config.callback(err); | ||
} else { | ||
return new DBQuery(this._client.query(_query)); | ||
return new DBQuery(this._client.query(_query, _values)); | ||
} | ||
@@ -150,13 +155,15 @@ }; | ||
var _table = collection.toLowerCase(collection); | ||
var _query = "DELETE FROM `" + _table + "`"; | ||
var _query = "DELETE FROM `" + _table + "`", tmp, _values = []; | ||
config = config || {}; | ||
if (config.conditions) _query = this._addQueryConditions(_query, config.conditions); | ||
if (config.conditions) { | ||
tmp = this._addQueryConditions(config.conditions); | ||
_query += tmp[0]; | ||
_values = _values.concat(tmp[1]); | ||
} | ||
if (config.order) _query = this._addQueryOrder(_query, config.order); | ||
if (config.limit) _query = this._addQueryLimit(_query, config.limit, config.skip); | ||
//console.log(_query); | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _values, function (err, info) { | ||
if (err) { | ||
@@ -182,3 +189,4 @@ config.callback(err); | ||
var _table = collection.toLowerCase(); | ||
var _query = "INSERT INTO `" + _table + "` (%fields) VALUES (%values)", _fields = [], _values = []; | ||
var _query = "INSERT INTO `" + _table + "` (%fields) VALUES (%values)", | ||
_fields = [], _values = [], _escapes = []; | ||
@@ -189,9 +197,7 @@ for (k in data) { | ||
_fields.push("`" + k + "`"); | ||
_values.push("?"); | ||
switch (typeof data[k]) { | ||
case "number": | ||
_values.push(data[k]); | ||
break; | ||
case "boolean": | ||
_values.push(data[k] ? 1 : 0); | ||
_escapes.push(data[k] ? 1 : 0); | ||
break; | ||
@@ -202,3 +208,19 @@ case "undefined": | ||
default: | ||
_values.push("'" + data[k].replace("'", "\\'") + "'"); | ||
if (data[k] === null) { | ||
_escapes.push(null); | ||
} else { | ||
if (typeof data[k] == "object") { | ||
if (data[k].getTime) { | ||
if (data[k].toString() != "Invalid Date") { | ||
_escapes.push(Math.round(data[k].getTime() / 1e3)); | ||
_values[_values.length - 1] = "FROM_UNIXTIME(?)"; | ||
} | ||
} else { | ||
_escapes.push(data[k]); | ||
} | ||
} else { | ||
_escapes.push(data[k]); | ||
} | ||
} | ||
} | ||
@@ -210,5 +232,5 @@ } | ||
//console.log(_query); | ||
//console.log(_query, _escapes); | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _escapes, function (err, info) { | ||
if (err) { | ||
@@ -224,3 +246,3 @@ callback(err); | ||
var _table = collection.toLowerCase(); | ||
var _query = "UPDATE `" + _table + "` SET %values WHERE `id`=" + id, _values = []; | ||
var _query = "UPDATE `" + _table + "` SET %values WHERE `id`=" + id, _updates = [], _values = []; | ||
@@ -231,14 +253,25 @@ for (k in data) { | ||
switch (typeof data[k]) { | ||
case "number": | ||
_values.push("`" + k + "`=" + data[k]); | ||
break; | ||
case "boolean": | ||
_values.push("`" + k + "`=" + (data[k] ? 1 : 0)); | ||
_values.push(data[k] ? 1 : 0); | ||
_updates.push("`" + k + "`=?"); | ||
break; | ||
default: | ||
if (data[k] === null) { | ||
_values.push("`" + k + "`=NULL"); | ||
_values.push(null); | ||
_updates.push("`" + k + "`=?"); | ||
} else { | ||
if (typeof data[k] == "object") console.dir(data[k]); | ||
_values.push("`" + k + "`='" + data[k].replace("'", "\\'") + "'"); | ||
if (typeof data[k] == "object") { | ||
if (data[k].getTime) { | ||
if (data[k].toString() != "Invalid Date") { | ||
_values.push(Math.round(data[k].getTime() / 1e3)); | ||
_updates.push("`" + k + "`=FROM_UNIXTIME(?)"); | ||
} | ||
} else { | ||
_values.push(data[k]); | ||
_updates.push("`" + k + "`=?"); | ||
} | ||
} else { | ||
_values.push(data[k]); | ||
_updates.push("`" + k + "`=?"); | ||
} | ||
} | ||
@@ -248,6 +281,7 @@ } | ||
_query = _query.replace("%values", _values.join(", ")); | ||
//console.log(_query); | ||
_query = _query.replace("%values", _updates.join(", ")); | ||
this._client.query(_query, function (err, info) { | ||
//console.log(_query, _values); | ||
this._client.query(_query, _values, function (err, info) { | ||
if (err) { | ||
@@ -261,4 +295,4 @@ callback(err); | ||
}; | ||
DBClient.prototype._addQueryConditions = function (query, conditions) { | ||
var _conditions = [], prop, op; | ||
DBClient.prototype._addQueryConditions = function (conditions) { | ||
var _conditions = [], _values = [], prop, op, i; | ||
@@ -283,13 +317,18 @@ for (k in conditions) { | ||
switch (typeof conditions[k]) { | ||
case "number": | ||
_conditions.push("`" + prop + "`" + op + conditions[k]); | ||
break; | ||
case "boolean": | ||
_conditions.push("`" + prop + "`" + op + (conditions[k] ? 1 : 0)); | ||
_conditions.push("`" + prop + "`" + op + "?"); | ||
_values.push(conditions[k] ? 1 : 0); | ||
break; | ||
default: | ||
if (Array.isArray(conditions[k])) { | ||
_conditions.push("`" + prop + "` " + (op == "!=" ? "NOT " : "") + "IN ('" + conditions[k].join("', '") + "')"); | ||
if (conditions[k].length > 0) { | ||
_conditions.push("`" + prop + "` " + (op == "!=" ? "NOT " : "") + "IN (" + (new Array(conditions[k].length)).join("?,") + "?)"); | ||
_values = _values.concat(conditions[k]); | ||
} else { | ||
// ? | ||
_conditions.push("`" + prop + "` " + (op == "!=" ? "NOT " : "") + "IN (NULL)"); | ||
} | ||
} else { | ||
_conditions.push("`" + prop + "`" + op + "'" + conditions[k].replace("'", "\\'") + "'"); | ||
_conditions.push("`" + prop + "`" + op + "?"); | ||
_values.push(conditions[k]); | ||
} | ||
@@ -299,3 +338,3 @@ } | ||
return query + " WHERE " + _conditions.join(" AND "); | ||
return [ " WHERE " + _conditions.join(" AND "), _values ]; | ||
}; | ||
@@ -302,0 +341,0 @@ DBClient.prototype._addQueryOrder = function (query, order) { |
@@ -62,2 +62,3 @@ var util = require("util"), | ||
case "struct": | ||
case "object": | ||
case "text": field += " TEXT"; break; | ||
@@ -64,0 +65,0 @@ case "num": |
160
lib/orm.js
@@ -68,3 +68,3 @@ var crypto = require("crypto"); | ||
}; | ||
var addManyAssociationMethods = function (self, association, field) { | ||
var addManyAssociationMethods = function (self, association, field, associationModel) { | ||
var camelCaseAssociation = association.substr(0, 1).toUpperCase() + association.substr(1); | ||
@@ -212,2 +212,3 @@ var collection = model + "_" + association; | ||
if (err) return cb(null); | ||
if (data.length == 0) return cb([]); | ||
@@ -219,3 +220,3 @@ var ids = []; | ||
orm._db.selectRecords(model, { | ||
orm._db.selectRecords(associationModel._ORM.collection, { | ||
"conditions": { "id": ids }, | ||
@@ -226,3 +227,3 @@ "callback" : function (err, data) { | ||
for (var i = 0; i < data.length; i++) { | ||
data[i] = new Model(data[i]); | ||
data[i] = new associationModel(data[i]); | ||
} | ||
@@ -239,30 +240,47 @@ | ||
var Model = function (data) { | ||
if (data) { | ||
for (k in data) { | ||
if (!data.hasOwnProperty(k)) continue; | ||
data || (data = {}); | ||
if (fields.hasOwnProperty(k)) { | ||
switch (fields[k].type) { | ||
case "bool": | ||
case "boolean": | ||
data[k] = (data[k] == 1); | ||
break; | ||
case "struct": | ||
if (typeof data[k] == "string") { | ||
try { | ||
data[k] = (data[k].length > 0 ? JSON.parse(data[k]) : {}); | ||
} catch (e) { | ||
data[k] = {}; | ||
} | ||
} | ||
for (k in fields) { | ||
if (!fields.hasOwnProperty(k)) continue; | ||
if (!data.hasOwnProperty(k) && fields[k].hasOwnProperty("def")) this[k] = fields[k].def; | ||
} | ||
for (k in data) { | ||
if (!data.hasOwnProperty(k)) continue; | ||
if (!fields.hasOwnProperty(k) && k != "id") { | ||
// this was wrong, I don't think we should preserve | ||
// undescribed properties | ||
//this[k] = data[k]; | ||
continue; | ||
} | ||
if (k == "id") { | ||
// fixed property | ||
Object.defineProperty(this, "id", { | ||
"value": data.id, | ||
"enumerable": true | ||
}); | ||
continue; | ||
} | ||
switch (fields[k].type) { | ||
case "bool": | ||
case "boolean": | ||
data[k] = (data[k] == 1 || data[k] === true); | ||
break; | ||
case "struct": | ||
case "object": | ||
if (typeof data[k] == "string") { | ||
try { | ||
data[k] = (data[k].length > 0 ? JSON.parse(data[k]) : {}); | ||
} catch (e) { | ||
data[k] = {}; | ||
} | ||
} | ||
} | ||
} | ||
this[k] = data[k]; | ||
} | ||
this[k] = data[k]; | ||
} | ||
for (k in fields) { | ||
if (!fields.hasOwnProperty(k)) continue; | ||
if (!data.hasOwnProperty(k) && fields[k].def) this[k] = fields[k].def; | ||
} | ||
for (var i = 0; i < associations.length; i++) { | ||
@@ -332,2 +350,3 @@ switch (associations[i].type) { | ||
case "struct": | ||
case "object": | ||
if (this[k]) { | ||
@@ -471,3 +490,3 @@ data[k] = (typeof this[k] == "object" ? JSON.stringify(this[k]) : this[k]); | ||
}); | ||
addManyAssociationMethods(this, association, field); | ||
addManyAssociationMethods(this, association, field, model); | ||
}; | ||
@@ -621,4 +640,23 @@ Model.sync = function () { | ||
}; | ||
Model._ORM = { "collection": model }; | ||
return this._models[model] = Model; | ||
colParams || (colParams = {}); | ||
for (k in fields) { | ||
if (!fields.hasOwnProperty(k)) continue; | ||
if (typeof fields[k] == "function") { | ||
fields[k] = { "type": typeof (fields[k]()) }; | ||
} else if (typeof fields[k] == "string") { | ||
fields[k] = { "type": fields[k].toLowerCase() }; | ||
} else if(Array.isArray(fields[k]) && fields[k].length > 0 && fields[k][0].hasMany) { | ||
Model.hasMany(k + (k.substr(-1) != "s" ? "s" : ""), fields[k][0], k); | ||
delete fields[k]; | ||
} | ||
} | ||
this._models[model] = Model; | ||
if (!module.exports.hasOwnProperty(model)) { | ||
module.exports[model] = this._models[model]; | ||
} | ||
return this._models[model]; | ||
}; | ||
@@ -638,41 +676,43 @@ | ||
exports.validators = require("./validators"); | ||
exports.connect = function (/* uri_or_dbtype, [db_object], callback */) { | ||
var rawDb, callback, uri, dbType; | ||
module.exports = { | ||
"validators": require("./validators"), | ||
"connect" : function (/* uri_or_dbtype, [db_object], callback */) { | ||
var rawDb, callback, uri, dbType; | ||
if (arguments.length === 3) { | ||
callback = arguments[2]; | ||
rawDb = arguments[1]; | ||
dbType = arguments[0]; | ||
} else { | ||
callback = arguments[1]; | ||
var url = require("url"); | ||
uri = url.parse(arguments[0]); | ||
if (!uri.protocol) { | ||
return callback(false, { "number": 1, "message": "Protocol not defined" }); | ||
if (arguments.length === 3) { | ||
callback = arguments[2]; | ||
rawDb = arguments[1]; | ||
dbType = arguments[0]; | ||
} else { | ||
callback = arguments[1]; | ||
var url = require("url"); | ||
uri = url.parse(arguments[0]); | ||
if (!uri.protocol) { | ||
return callback(false, { "number": 1, "message": "Protocol not defined" }); | ||
} | ||
dbType = uri.protocol.substr(0, uri.protocol.length - 1); | ||
} | ||
dbType = uri.protocol.substr(0, uri.protocol.length - 1); | ||
} | ||
var path = require("path"), dbPath = __dirname + "/databases/" + dbType + ".js"; | ||
var path = require("path"), dbPath = __dirname + "/databases/" + dbType + ".js"; | ||
path.exists(dbPath, function (exists) { | ||
if (!exists) { | ||
return callback(false, { "number": 2, "message": "Protocol not installed" }); | ||
} | ||
path.exists(dbPath, function (exists) { | ||
if (!exists) { | ||
return callback(false, { "number": 2, "message": "Protocol not installed" }); | ||
} | ||
var db = require(dbPath); | ||
var db = require(dbPath); | ||
var handleResult = function (success, info) { | ||
if (!success) return callback(false, info); | ||
var handleResult = function (success, info) { | ||
if (!success) return callback(false, info); | ||
return callback(true, new ORM(info)); | ||
}; | ||
return callback(true, new ORM(info)); | ||
}; | ||
if (rawDb) { | ||
db.use_db(rawDb, handleResult); | ||
} else { | ||
db.connect(uri, handleResult); | ||
} | ||
}); | ||
if (rawDb) { | ||
db.use_db(rawDb, handleResult); | ||
} else { | ||
db.connect(uri, handleResult); | ||
} | ||
}); | ||
} | ||
}; |
{ | ||
"name" : "orm", | ||
"version" : "0.1.7", | ||
"version" : "0.1.8", | ||
"description" : "NodeJS Object-relational mapping", | ||
@@ -8,2 +8,4 @@ "keywords" : [ | ||
"mysql", | ||
"postgresql", | ||
"mongodb", | ||
"database", | ||
@@ -10,0 +12,0 @@ "relational" |
@@ -200,3 +200,3 @@ NodeJS ORM | ||
<tr> | ||
<td>struct</td> | ||
<td>struct, object</td> | ||
<td>Generic (and simple) object</td> | ||
@@ -203,0 +203,0 @@ <td>TEXT (saved as JSON)</td> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66222
20
2015
9