Merge pull request #45 from fsateler/master
Allow custom table name in postgres too
This commit is contained in:
commit
093edba65d
|
@ -36,6 +36,7 @@ function PG(client) {
|
|||
}
|
||||
|
||||
PG.prototype.define = function (descr) {
|
||||
if (!descr.settings) descr.settings = {};
|
||||
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) {
|
||||
var sql = 'UPDATE "' + model + '" SET ' + this.toFields(model, data) +
|
||||
var sql = 'UPDATE "' + this.table(model) + '" SET ' + this.toFields(model, data) +
|
||||
' WHERE "id" = ' + data.id;
|
||||
|
||||
this.query(sql, function (err) {
|
||||
|
@ -62,7 +67,7 @@ PG.prototype.save = function (model, data, callback) {
|
|||
*/
|
||||
PG.prototype.create = function (model, data, callback) {
|
||||
var fields = this.toFields(model, data,true);
|
||||
var sql = 'INSERT INTO "' + model + '"';
|
||||
var sql = 'INSERT INTO "' + this.table(model) + '"';
|
||||
if (fields) {
|
||||
sql += ' ' + fields;
|
||||
} else {
|
||||
|
@ -149,7 +154,7 @@ PG.prototype.fromDatabase = function (model, data) {
|
|||
};
|
||||
|
||||
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) {
|
||||
if (err) return callback(err);
|
||||
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) {
|
||||
var sql = 'DELETE FROM "' + model + '" WHERE "id" = ' + id;
|
||||
var sql = 'DELETE FROM "' + this.table(model) + '" WHERE "id" = ' + id;
|
||||
this.query(sql, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
|
@ -177,7 +182,7 @@ PG.prototype.destroy = function destroy(model, id, callback) {
|
|||
|
||||
// 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 "' + this.table(model) + '"' + this.toFilter(model, filter), function (err, data) {
|
||||
if (err) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
@ -247,7 +252,7 @@ PG.prototype.toFilter = function (model, filter) {
|
|||
};
|
||||
|
||||
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) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
@ -259,7 +264,7 @@ PG.prototype.count = function count(model, callback, where) {
|
|||
var self = this;
|
||||
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);
|
||||
callback(err, res && res.items[0] && res.items[0].cnt);
|
||||
});
|
||||
|
@ -311,7 +316,7 @@ PG.prototype.autoupdate = function (cb) {
|
|||
var wait = 0;
|
||||
Object.keys(this._models).forEach(function (model) {
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
@ -358,7 +363,7 @@ PG.prototype.alterTable = function (model, actualFields, done) {
|
|||
});
|
||||
|
||||
if (sql.length) {
|
||||
this.query('ALTER TABLE "' + model + '" ' + sql.join(',\n'), done);
|
||||
this.query('ALTER TABLE "' + this.table(model) + '" ' + sql.join(',\n'), done);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
|
@ -379,11 +384,11 @@ PG.prototype.alterTable = function (model, actualFields, done) {
|
|||
};
|
||||
|
||||
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) {
|
||||
this.query('CREATE TABLE "' + model +
|
||||
this.query('CREATE TABLE "' + this.table(model) +
|
||||
'" (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue