Skip to content

Commit

Permalink
Fix postgres adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Jan 10, 2012
1 parent 029b020 commit c3835d0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
6 changes: 5 additions & 1 deletion lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ AbstractClass.create = function (data) {
};

AbstractClass.exists = function exists(id, cb) {
this.schema.adapter.exists(this.modelName, id, cb);
if (id) {
this.schema.adapter.exists(this.modelName, id, cb);
} else {
cb(new Error('Model::exists requires positive id argument'));
}
};

AbstractClass.find = function find(id, cb) {
Expand Down
59 changes: 37 additions & 22 deletions lib/adapters/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
var Client = require('pg').Client;
var Hash = require('hashish');

exports.initialize = function initializeSchema(schema, callback) {
var s = schema.settings;
schema.client = new Client(s.url ? s.url : {
Expand Down Expand Up @@ -38,7 +39,7 @@ PG.prototype.query = function (sql, callback) {
var log = this.log;
this.client.query(sql, function (err, data) {
log(sql, time);
callback(err, Hash(data.rows));
callback(err, data ? Hash(data.rows) : null);
});
};

Expand All @@ -62,8 +63,10 @@ PG.prototype.create = function (model, data, callback) {
} else {
sql += ' VALUES ()';
}
sql += ' RETURNING id';
this.query(sql, function (err, info) {
callback(err, info && info.insertId);
if (err) return callback(err);
callback(err, info && info.items[0] && info.items[0].id);
});
};

Expand Down Expand Up @@ -91,21 +94,21 @@ PG.prototype.toFields = function (model, data, forCreate) {
};

PG.prototype.toDatabase = function (prop, val) {
if (prop.type.name === 'Number') return val;
if (val === null) return 'NULL';
if (prop.type.name === 'Number') return val;
if (prop.type.name === 'Date') {
if (!val) return 'NULL';
if (!val.toUTCString) {
val = new Date(val);
}
val = [
val.getFullYear(),
val.getMonth() + 1,
val.getDate()
val.getUTCFullYear(),
val.getUTCMonth() + 1,
val.getUTCDate()
].join('-') + ' ' + [
val.getHours(),
val.getMinutes(),
val.getSeconds()
val.getUTCHours(),
val.getUTCMinutes(),
val.getUTCSeconds()
].join(':');
return escape(val);
}
Expand All @@ -129,53 +132,64 @@ PG.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM "' + model + '" WHERE "id" = ' + id + ' LIMIT 1';
this.query(sql, function (err, data) {
if (err) return callback(err);
callback(null, data.length === 1);
callback(null, data.items.length === 1);
});
};

PG.prototype.find = function find(model, id, callback) {
var sql = 'SELECT * FROM "' + model + '" WHERE "id" = ' + id + ' LIMIT 1';
this.query(sql, function (err, data) {
if (data && data.length === 1) {
data[0].id = id;
if (data && data.items && data.items.length === 1) {
data.items[0].id = id;
} else {
data = [null];
data = { items: [null] };
}
callback(err, this.fromDatabase(model, data[0]));
callback(err, this.fromDatabase(model, data.items[0]));
}.bind(this));
};

PG.prototype.destroy = function destroy(model, id, callback) {
var sql = 'DELETE FROM "' + model + '" WHERE "id" = ' + id + ' LIMIT 1';
var sql = 'DELETE FROM "' + model + '" WHERE "id" = ' + id;
this.query(sql, function (err) {
callback(err);
});
};

// TODO: hook up where, order, limit and offset conditions
PG.prototype.all = function all(model, filter, callback) {
this.query('SELECT * FROM "' + model + '"' + this.toFilter(model,filter), function (err, data) {
this.query('SELECT * FROM "' + model + '"' + this.toFilter(model, filter), function (err, data) {
if (err) {
return callback(err, []);
}
callback(err, filter ? data.filter(applyFilter(filter)) : data);
callback(err, filter ? data.items.filter(applyFilter(filter)) : data.items);
}.bind(this));
};

PG.prototype.toFilter = function (model, filter) {
if (typeof filter.where === 'function') {
if (filter && typeof filter.where === 'function') {
return filter();
}
if (!filter) return '';
var props = this._models[model].properties;
var out='';
if(filter.where){
if (filter.where) {
var fields = [];
Object.keys(filter.where).forEach(function (key) {
if (filter.where[key] && filter.where[key].constructor.name === 'RegExp') {
return;
}
if (props[key]) {
fields.push('"' + key + '" = ' + this.toDatabase(props[key], filter.where[key]));
var filterValue = this.toDatabase(props[key], filter.where[key]);
if (filterValue === 'NULL') {
fields.push('"' + key + '" IS ' + filterValue);
} else {
fields.push('"' + key + '" = ' + filterValue);
}
}
}.bind(this));
out += ' where ' + fields.join(' AND ');
if (fields.length) {
out += ' where ' + fields.join(' AND ');
}
}
return out;
};
Expand Down Expand Up @@ -216,7 +230,8 @@ PG.prototype.destroyAll = function destroyAll(model, callback) {

PG.prototype.count = function count(model, callback) {
this.query('SELECT count(*) as cnt FROM "' + model + '"', function (err, res) {
callback(err, err ? null : res[0].cnt);
if (err) return callback(err);
callback(err, res && res.items[0] && res.items[0].cnt);
});
};

Expand Down
4 changes: 3 additions & 1 deletion test/common_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Object.keys(schemas).forEach(function (schemaName) {
if (process.env.ONLY && process.env.ONLY !== schemaName) return;
context(schemaName, function () {
var schema = new Schema(schemaName, schemas[schemaName]);
// schema.log = console.log;
testOrm(schema);
if (specificTest[schemaName]) specificTest[schemaName](schema);
});
Expand Down Expand Up @@ -137,7 +138,7 @@ function testOrm(schema) {
it('should create object', function (test) {
Post.create(function (err, post) {
if (err) throw err;
test.ok(post.id);
test.ok(post.id, 'Id present');
test.ok(!post.title, 'Title is blank');
Post.exists(post.id, function (err, exists) {
if (err) throw err;
Expand Down Expand Up @@ -315,6 +316,7 @@ function testOrm(schema) {

// matching null
Post.all({where: {title: null}}, function (err, res) {

var pass = true;
res.forEach(function (r) {
if (r.title != null) pass = false;
Expand Down

1 comment on commit c3835d0

@giano
Copy link

@giano giano commented on c3835d0 Jan 10, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, absolutely positive. I'll import ASAP

Please sign in to comment.