diff --git a/lib/sql.js b/lib/sql.js index 085051e..1b562a6 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -421,17 +421,13 @@ SQLConnector.prototype.propertyName = function(model, column) { }; /** - * Get the id column name + * Get the id column name. * @param {String} model The model name * @returns {String} The id column name */ SQLConnector.prototype.idColumn = function(model) { - let name = this.getDataSource(model).idColumnName(model); - const dbName = this.dbName; - if (typeof dbName === 'function') { - name = dbName(name); - } - return name; + const idName = this.getDataSource(model).getModelDefinition(model).idName(); + return this.column(model, idName); }; /** diff --git a/test/sql.test.js b/test/sql.test.js index 51dafb5..57d9725 100644 --- a/test/sql.test.js +++ b/test/sql.test.js @@ -17,6 +17,7 @@ const ds = new juggler.DataSource({ /* eslint-disable one-var */ let connector; let Customer; +let Order; /* eslint-enable one-var */ describe('sql connector', function() { @@ -58,6 +59,24 @@ describe('sql connector', function() { address: String, }, {testdb: {table: 'CUSTOMER'}}); + Order = ds.createModel('order', + { + id: { + id: true, + type: Number, + testdb: { + column: 'orderId', + dataType: 'INTEGER', + }, + }, des: { + type: String, + name: 'des', + testdb: { + column: 'description', + }, + }, + }, + {testdb: {table: 'ORDER'}}); }); it('should map table name', function() { @@ -80,6 +99,12 @@ describe('sql connector', function() { expect(column).to.eql('LASTNAME'); }); + it('uses database-specific column name over property name even if the column name \ + does not follow the connector-specific configuration (UPPERCASE)', function() { + const column = connector.column('order', 'des'); + expect(column).to.eql('description'); + }); + it('prefers property name when database is different', function() { const column = connector.column('customer', 'middleName'); expect(column).to.eql('middle_name'); @@ -104,6 +129,13 @@ describe('sql connector', function() { expect(idCol).to.eql('NAME'); }); + it('should map id column name even if the column name does not \ + follow the connector-specific configuration (UPPERCASE)', function() { + const idCol = connector.idColumn('order'); + // shouldn't be converted to ORDERID + expect(idCol).to.eql('orderId'); + }); + it('should find escaped id column name', function() { const idCol = connector.idColumnEscaped('customer'); expect(idCol).to.eql('`NAME`');