Allow custom table name in postgres too

This commit is contained in:
Felipe Sateler 2012-03-09 20:25:24 -03:00
parent d3231e7484
commit f39d3af5bd
1 changed files with 16 additions and 11 deletions

View File

@ -36,6 +36,7 @@ function PG(client) {
} }
PG.prototype.define = function (descr) { PG.prototype.define = function (descr) {
if (!descr.settings) descr.settings = {};
this._models[descr.model.modelName] = descr; this._models[descr.model.modelName] = descr;
}; };
@ -48,8 +49,12 @@ PG.prototype.query = function (sql, callback) {
}); });
}; };
PG.prototype.table = function (model) {
return this._models[model].settings.table || model;
};
PG.prototype.save = function (model, data, callback) { PG.prototype.save = function (model, data, callback) {
var sql = 'UPDATE "' + model + '" SET ' + this.toFields(model, data) + var sql = 'UPDATE "' + this.table(model) + '" SET ' + this.toFields(model, data) +
' WHERE "id" = ' + data.id; ' WHERE "id" = ' + data.id;
this.query(sql, function (err) { this.query(sql, function (err) {
@ -62,7 +67,7 @@ PG.prototype.save = function (model, data, callback) {
*/ */
PG.prototype.create = function (model, data, callback) { PG.prototype.create = function (model, data, callback) {
var fields = this.toFields(model, data,true); var fields = this.toFields(model, data,true);
var sql = 'INSERT INTO "' + model + '"'; var sql = 'INSERT INTO "' + this.table(model) + '"';
if (fields) { if (fields) {
sql += ' ' + fields; sql += ' ' + fields;
} else { } else {
@ -149,7 +154,7 @@ PG.prototype.fromDatabase = function (model, data) {
}; };
PG.prototype.exists = function (model, id, callback) { PG.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM "' + model + '" WHERE "id" = ' + id + ' LIMIT 1'; var sql = 'SELECT 1 FROM "' + this.table(model) + '" WHERE "id" = ' + id + ' LIMIT 1';
this.query(sql, function (err, data) { this.query(sql, function (err, data) {
if (err) return callback(err); if (err) return callback(err);
callback(null, data.items.length === 1); callback(null, data.items.length === 1);
@ -169,7 +174,7 @@ PG.prototype.find = function find(model, id, callback) {
}; };
PG.prototype.destroy = function destroy(model, id, callback) { PG.prototype.destroy = function destroy(model, id, callback) {
var sql = 'DELETE FROM "' + model + '" WHERE "id" = ' + id; var sql = 'DELETE FROM "' + this.table(model) + '" WHERE "id" = ' + id;
this.query(sql, function (err) { this.query(sql, function (err) {
callback(err); callback(err);
}); });
@ -177,7 +182,7 @@ PG.prototype.destroy = function destroy(model, id, callback) {
// TODO: hook up where, order, limit and offset conditions // TODO: hook up where, order, limit and offset conditions
PG.prototype.all = function all(model, filter, callback) { PG.prototype.all = function all(model, filter, callback) {
this.query('SELECT * FROM "' + model + '"' + this.toFilter(model, filter), function (err, data) { this.query('SELECT * FROM "' + this.table(model) + '"' + this.toFilter(model, filter), function (err, data) {
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
@ -247,7 +252,7 @@ PG.prototype.toFilter = function (model, filter) {
}; };
PG.prototype.destroyAll = function destroyAll(model, callback) { PG.prototype.destroyAll = function destroyAll(model, callback) {
this.query('DELETE FROM "' + model + '"', function (err) { this.query('DELETE FROM "' + this.table(model) + '"', function (err) {
if (err) { if (err) {
return callback(err, []); return callback(err, []);
} }
@ -259,7 +264,7 @@ PG.prototype.count = function count(model, callback, where) {
var self = this; var self = this;
var props = this._models[model].properties; var props = this._models[model].properties;
this.query('SELECT count(*) as cnt FROM "' + model + '"' + buildWhere(where), function (err, res) { this.query('SELECT count(*) as cnt FROM "' + this.table(model) + '"' + buildWhere(where), function (err, res) {
if (err) return callback(err); if (err) return callback(err);
callback(err, res && res.items[0] && res.items[0].cnt); callback(err, res && res.items[0] && res.items[0].cnt);
}); });
@ -311,7 +316,7 @@ PG.prototype.autoupdate = function (cb) {
var wait = 0; var wait = 0;
Object.keys(this._models).forEach(function (model) { Object.keys(this._models).forEach(function (model) {
wait += 1; wait += 1;
self.query('SELECT column_name as Field, udt_name as Type, is_nullable as Null, column_default as Default FROM information_schema.COLUMNS WHERE table_name = \''+model+'\'', function (err, fields) { self.query('SELECT column_name as Field, udt_name as Type, is_nullable as Null, column_default as Default FROM information_schema.COLUMNS WHERE table_name = \''+this.table(model)+'\'', function (err, fields) {
self.alterTable(model, fields, done); self.alterTable(model, fields, done);
}); });
}); });
@ -358,7 +363,7 @@ PG.prototype.alterTable = function (model, actualFields, done) {
}); });
if (sql.length) { if (sql.length) {
this.query('ALTER TABLE "' + model + '" ' + sql.join(',\n'), done); this.query('ALTER TABLE "' + this.table(model) + '" ' + sql.join(',\n'), done);
} else { } else {
done(); done();
} }
@ -379,11 +384,11 @@ PG.prototype.alterTable = function (model, actualFields, done) {
}; };
PG.prototype.dropTable = function (model, cb) { PG.prototype.dropTable = function (model, cb) {
this.query('DROP TABLE IF EXISTS "' + model + '"', cb); this.query('DROP TABLE IF EXISTS "' + this.table(model) + '"', cb);
}; };
PG.prototype.createTable = function (model, cb) { PG.prototype.createTable = function (model, cb) {
this.query('CREATE TABLE "' + model + this.query('CREATE TABLE "' + this.table(model) +
'" (\n ' + this.propertiesSQL(model) + '\n)', cb); '" (\n ' + this.propertiesSQL(model) + '\n)', cb);
}; };