fix the way it gets column name for id property
This commit is contained in:
parent
10477c0815
commit
aa26562cbe
10
lib/sql.js
10
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
|
* @param {String} model The model name
|
||||||
* @returns {String} The id column name
|
* @returns {String} The id column name
|
||||||
*/
|
*/
|
||||||
SQLConnector.prototype.idColumn = function(model) {
|
SQLConnector.prototype.idColumn = function(model) {
|
||||||
let name = this.getDataSource(model).idColumnName(model);
|
const idName = this.getDataSource(model).getModelDefinition(model).idName();
|
||||||
const dbName = this.dbName;
|
return this.column(model, idName);
|
||||||
if (typeof dbName === 'function') {
|
|
||||||
name = dbName(name);
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,6 +17,7 @@ const ds = new juggler.DataSource({
|
||||||
/* eslint-disable one-var */
|
/* eslint-disable one-var */
|
||||||
let connector;
|
let connector;
|
||||||
let Customer;
|
let Customer;
|
||||||
|
let Order;
|
||||||
/* eslint-enable one-var */
|
/* eslint-enable one-var */
|
||||||
|
|
||||||
describe('sql connector', function() {
|
describe('sql connector', function() {
|
||||||
|
@ -58,6 +59,24 @@ describe('sql connector', function() {
|
||||||
address: String,
|
address: String,
|
||||||
},
|
},
|
||||||
{testdb: {table: 'CUSTOMER'}});
|
{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() {
|
it('should map table name', function() {
|
||||||
|
@ -80,6 +99,12 @@ describe('sql connector', function() {
|
||||||
expect(column).to.eql('LASTNAME');
|
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() {
|
it('prefers property name when database is different', function() {
|
||||||
const column = connector.column('customer', 'middleName');
|
const column = connector.column('customer', 'middleName');
|
||||||
expect(column).to.eql('middle_name');
|
expect(column).to.eql('middle_name');
|
||||||
|
@ -104,6 +129,13 @@ describe('sql connector', function() {
|
||||||
expect(idCol).to.eql('NAME');
|
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() {
|
it('should find escaped id column name', function() {
|
||||||
const idCol = connector.idColumnEscaped('customer');
|
const idCol = connector.idColumnEscaped('customer');
|
||||||
expect(idCol).to.eql('`NAME`');
|
expect(idCol).to.eql('`NAME`');
|
||||||
|
|
Loading…
Reference in New Issue