From a014fddd8a187cc92bdf90c57a3cc42490cd595b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 23 May 2013 21:09:46 -0700 Subject: [PATCH] Add more methods to map column/property names --- lib/sql.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/sql.js b/lib/sql.js index 121b08bc..440404b0 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -22,7 +22,11 @@ BaseSQL.prototype.queryOne = function (sql, callback) { }; BaseSQL.prototype.dataSource = function(model) { - return this._models[model].model.schema; + var m = this._models[model]; + if(!m) { + console.log(new Error('Model not found: ' + model).stack); + } + return m.model.schema; } BaseSQL.prototype.table = function (model) { @@ -43,12 +47,27 @@ BaseSQL.prototype.column = function (model, property) { return name; }; +BaseSQL.prototype.propertyName = function (model, column) { + var props = this._models[model].properties; + for(var p in props) { + if(this.column(model, p) === column) { + return p; + } + } + return null; +}; + BaseSQL.prototype.idName = function (model) { return this.dataSource(model).idName(model); }; BaseSQL.prototype.idColumn = function (model) { - return this.dataSource(model).idColumnName(model); + var name = this.dataSource(model).idColumnName(model);; + var dbName = this.dbName; + if(typeof dbName === 'function') { + name = dbName(name); + } + return name; }; BaseSQL.prototype.idColumnEscaped = function (model) { @@ -56,7 +75,11 @@ BaseSQL.prototype.idColumnEscaped = function (model) { }; BaseSQL.prototype.id = function (model, prop) { - return this._models[model].properties[prop].id; + var p = this._models[model].properties[prop]; + if(!p) { + console.log(new Error('Property not found: ' + model +'.' + prop).stack); + } + return p.id; }; BaseSQL.prototype.escapeName = function (name) {