diff --git a/lib/adapters/sql/converter.js b/lib/adapters/sql/converter.js index 42b24524..b17e7215 100644 --- a/lib/adapters/sql/converter.js +++ b/lib/adapters/sql/converter.js @@ -4,13 +4,26 @@ var utils = require('utilities') module.exports = new (function () { this.COLUMN_NAME_DELIMITER = '"'; + /** + * Get the TableName of the Model. First checks if it + * was defined on the model, otherwise it performs a pluralization of + * the model name + * + * @param {string} name - the name of the model + * @returns {model.ModelDescription.tableName|string} + * @private + */ this._tableizeModelName = function (name) { - return utils.string.getInflection(name, 'filename', 'plural'); + // Get the Correct model name (in case the name is passed in with the wrong case) + var modelName = utils.string.getInflection(name, 'constructor', 'singular'); + + return (model.descriptionRegistry[modelName] && model.descriptionRegistry[modelName].tableName) + || utils.string.getInflection(name, 'filename', 'plural'); }; this._modelizeTableName = function (name, ownerName) { var modelName - , ownerModelName + , ownerModelName; modelName = utils.string.getInflection(name, 'constructor', 'singular'); if (ownerName && name != ownerName) { ownerModelName = utils.string.getInflection(ownerName, 'constructor', 'singular'); diff --git a/lib/index.js b/lib/index.js index 469842c7..c26257d7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -596,6 +596,9 @@ utils.mixin(model, new (function () { ModelDefinition.prototype = new model.ModelDefinitionBase(name); defined = new ModelDefinition(); + // Set the table name from the instance or model definition + model.descriptionRegistry[name].tableName = defined.tableName || (ModelDefinition.tableName || null); + // Create the constructor function to use when calling static // ModelCtor.create. Gives them the proper instanceof value, // and .valid, etc. instance-methods. @@ -907,6 +910,7 @@ model.ModelDefinitionBase = function (name) { }; this.name = name; + this.tableName = null; this.setAdapter = function (name, config) { var adapter = adapters.create(name, config); @@ -1055,6 +1059,7 @@ model.ModelDescription = function (name) { this.name = name; this.properties = {}; this.associations = {}; + this.tableName = null; }; model.PropertyDescription = function (name, datatype, o) {