fix the way it gets column name for id property

This commit is contained in:
Agnes Lin 2020-02-27 13:24:43 -05:00
parent 10477c0815
commit aa26562cbe
2 changed files with 35 additions and 7 deletions

View File

@ -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);
};
/**

View File

@ -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`');