Comparing version 0.1.8-3 to 0.1.8-4
@@ -8,38 +8,11 @@ var orm = require(__dirname + "/../lib/orm"); | ||
} | ||
var FTS = db.define("fts", { | ||
"name" : { "type": "string" }, | ||
"body" : { "type": "text", "textsearch": true } | ||
var Person = db.define("person", { | ||
name: String, | ||
created: Date | ||
}); | ||
FTS.sync(); | ||
Person.sync(); | ||
var fts = [ | ||
new FTS({ "name": "John", "body": "John is friend of Jane and Jeremy" }), | ||
new FTS({ "name": "Jane", "body": "This girl is not friendly" }), | ||
new FTS({ "name": "Jeremy", "body": "This guy has no one" }), | ||
new FTS({ "name": "Jessica", "body": "Jessica does not have any friends" }), | ||
new FTS({ "name": "Jasmin", "body": "Jasmin is too young" }) | ||
]; | ||
FTS.clear(function () { | ||
for (var i = 0; i < fts.length; i++) { | ||
fts[i].save(function (err) { | ||
if (err) { | ||
console.log("Error %s: %s", err.number, err.message); | ||
return; | ||
} | ||
console.dir("FTS saved"); | ||
}); | ||
} | ||
Person.find({ name: [ "John Doe" ] }, function (people) { | ||
console.log(people); | ||
}); | ||
setTimeout(function () { | ||
console.log("Searching for 'friends or jeremy or (jane and not jones)' (max 2 results).."); | ||
FTS.textsearch(2, "friends or jeremy or (jane and not jones)", function (people) { | ||
if (people === null) { | ||
return console.log("Error searching"); | ||
} | ||
for (i = 0; i < people.length; i++) { | ||
console.log("%s (%d) = '%s'", people[i].name, people[i].id, people[i].body); | ||
} | ||
}); | ||
}, 1500); | ||
}); |
@@ -41,3 +41,3 @@ module.exports = { | ||
instance.prototype["get" + camelCaseAssociation] = function () { | ||
var instance = this, cb = null, opts = {}; | ||
var cb = null, opts = {}; | ||
@@ -55,4 +55,4 @@ for (var i = 0; i < arguments.length; i++) { | ||
if (instance[association + "_id"] > 0) { | ||
if (instance[association]) return cb(instance[association]); | ||
if (this[association + "_id"] > 0) { | ||
if (this[association]) return cb(this[association]); | ||
@@ -63,3 +63,3 @@ var data = {}; | ||
orm._db.selectRecords(collection, { | ||
"conditions": { "id": instance[association + "_id"] }, | ||
"conditions": { "id": this[association + "_id"] }, | ||
"callback" : function (err, data) { | ||
@@ -79,7 +79,7 @@ if (err || !data || data.length == 0) return cb(null); | ||
instance.prototype["set" + camelCaseAssociation] = function (instance, cb) { | ||
var instance = this; | ||
var self = this; | ||
if (instance === null) { | ||
instance[association + "_id"] = 0; | ||
delete instance[association]; | ||
self[association + "_id"] = 0; | ||
delete self[association]; | ||
@@ -93,4 +93,4 @@ return cb(); | ||
instance[association + "_id"] = savedInstance.id; | ||
instance[association] = savedInstance; | ||
self[association + "_id"] = savedInstance.id; | ||
self[association] = savedInstance; | ||
cb(); | ||
@@ -100,4 +100,4 @@ }); | ||
} | ||
instance[association + "_id"] = instance.id; | ||
instance[association] = instance; | ||
self[association + "_id"] = instance.id; | ||
self[association] = instance; | ||
cb(); | ||
@@ -104,0 +104,0 @@ }; |
@@ -129,15 +129,20 @@ var util = require("util"), | ||
DBClient.prototype.selectRecords = function (collection, config) { | ||
var _table = collection.toLowerCase(collection); | ||
var _query = "SELECT * FROM \"" + _table + "\""; | ||
var _table = collection.toLowerCase(collection), | ||
_query = "SELECT * FROM \"" + _table + "\"", | ||
_escapes = [], tmp; | ||
config = config || {}; | ||
if (config.conditions) _query = this._addQueryConditions(_query, config.conditions); | ||
if (config.conditions) { | ||
tmp = this._addQueryConditions(_query, config.conditions); | ||
_query = tmp[0]; | ||
_escapes = 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, _escapes); | ||
if (typeof config.callback == "function") { | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _escapes, function (err, info) { | ||
if (err) { | ||
@@ -151,3 +156,3 @@ config.callback(err); | ||
} else { | ||
return new DBQuery(this._client.query(_query)); | ||
return new DBQuery(this._client.query(_query, _escapes)); | ||
} | ||
@@ -178,14 +183,19 @@ }; | ||
DBClient.prototype.clearRecords = function (collection, config, callback) { | ||
var _table = collection.toLowerCase(collection); | ||
var _query = "DELETE FROM \"" + _table + "\""; | ||
var _table = collection.toLowerCase(collection), | ||
_query = "DELETE FROM \"" + _table + "\"", | ||
_escapes = []; | ||
config = config || {}; | ||
if (config.conditions) _query = this._addQueryConditions(_query, config.conditions); | ||
if (config.conditions) { | ||
tmp = this._addQueryConditions(_query, config.conditions); | ||
_query = tmp[0]; | ||
_escapes = 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, _escapes); | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _escapes, function (err, info) { | ||
if (err) { | ||
@@ -221,5 +231,2 @@ config.callback(err); | ||
switch (typeof data[k]) { | ||
case "boolean": | ||
_escapes.push(data[k] ? 1 : 0); | ||
break; | ||
case "undefined": | ||
@@ -258,3 +265,4 @@ _values.pop(); | ||
var _table = collection.toLowerCase(); | ||
var _query = "UPDATE \"" + _table + "\" SET %values WHERE \"id\"=" + id, _values = []; | ||
var _query = "UPDATE \"" + _table + "\" SET %values WHERE \"id\"=" + id, | ||
_values = [], _escapes = [], n = 1; | ||
@@ -264,16 +272,11 @@ for (k in data) { | ||
_values.push("\"" + k + "\"=$" + (n++)); | ||
switch (typeof data[k]) { | ||
case "number": | ||
_values.push("\"" + k + "\"=" + data[k]); | ||
case "undefined": | ||
_values.pop(); | ||
n--; | ||
break; | ||
case "boolean": | ||
_values.push("\"" + k + "\"=" + (data[k] ? 1 : 0)); | ||
break; | ||
default: | ||
if (data[k] === null) { | ||
_values.push("\"" + k + "\"=NULL"); | ||
} else { | ||
if (typeof data[k] == "object") console.dir(data[k]); | ||
_values.push("\"" + k + "\"='" + data[k].replace("'", "''") + "'"); | ||
} | ||
_escapes.push(data[k]); | ||
} | ||
@@ -283,5 +286,5 @@ } | ||
_query = _query.replace("%values", _values.join(", ")); | ||
//console.log(_query); | ||
//console.log(_query. _escapes); | ||
this._client.query(_query, function (err, info) { | ||
this._client.query(_query, _escapes, function (err, info) { | ||
if (err) { | ||
@@ -295,4 +298,5 @@ callback(err); | ||
}; | ||
DBClient.prototype._addQueryConditions = function (query, conditions) { | ||
var _conditions = [], prop, op; | ||
DBClient.prototype._addQueryConditions = function (query, conditions, escapes) { | ||
var _conditions = [], prop, op, | ||
_escapes = (escapes || []), n = _escapes.length + 1; | ||
@@ -317,13 +321,18 @@ for (k in conditions) { | ||
switch (typeof conditions[k]) { | ||
case "boolean": | ||
case "number": | ||
_conditions.push('"' + prop + '"' + op + conditions[k]); | ||
_conditions.push("\"" + prop + "\"" + op + conditions[k]); | ||
break; | ||
case "boolean": | ||
_conditions.push('"' + prop + '"' + op + (conditions[k] ? 1 : 0)); | ||
break; | ||
default: | ||
if (Array.isArray(conditions[k])) { | ||
_conditions.push('"' + prop + '" ' + (op == "!=" ? "NOT " : "") + "IN ('" + conditions[k].join("', '") + "')"); | ||
var array_conditions = []; | ||
for (var i = 0; i < conditions[k].length; i++) { | ||
array_conditions.push("$" + (n++)); | ||
_escapes.push(conditions[k][i]); | ||
} | ||
_conditions.push("\"" + prop + "\"" + (op == "!=" ? " NOT" : "") + " IN (" + array_conditions.join(", ") + ")"); | ||
} else { | ||
_conditions.push('"' + prop + '"' + op + "'" + conditions[k].replace("'", "''") + "'"); | ||
_conditions.push("\"" + prop + "\"" + op + "$" + (n++)); | ||
_escapes.push(conditions[k]); | ||
} | ||
@@ -333,3 +342,3 @@ } | ||
return query + " WHERE " + _conditions.join(" AND "); | ||
return [ query + " WHERE " + _conditions.join(" AND "), _escapes ]; | ||
}; | ||
@@ -344,3 +353,3 @@ DBClient.prototype._addQueryOrder = function (query, order) { | ||
return word.toUpperCase(); | ||
return "`" + word + "`"; | ||
return "\"" + word + "\""; | ||
}); | ||
@@ -347,0 +356,0 @@ }; |
@@ -161,3 +161,3 @@ var crypto = require("crypto"), | ||
default: | ||
data[k] = this[k].toString(); | ||
data[k] = (this[k].hasOwnProperty("toString") ? this[k].toString() : this[k]); | ||
} | ||
@@ -164,0 +164,0 @@ } |
@@ -94,3 +94,3 @@ /** | ||
Model.find(query, function (records) { | ||
if (records.length == 0) { | ||
if (!records || records.length == 0) { | ||
return next(); | ||
@@ -97,0 +97,0 @@ } |
{ | ||
"name" : "orm", | ||
"version" : "0.1.8-3", | ||
"version" : "0.1.8-4", | ||
"description" : "NodeJS Object-relational mapping", | ||
@@ -16,2 +16,3 @@ "keywords" : [ | ||
{ "name": "Michael Axiak", "email": "mike@axiak.net" }, | ||
{ "name": "Michael Yagudaev" }, | ||
{ "name": "booo" }, | ||
@@ -18,0 +19,0 @@ { "name": "shaneiseminger" } |
78457
31
2462
12