allows diff db cols naming conventions 4 discover
This commit is contained in:
parent
3a08b9d9ae
commit
baeb76e911
|
@ -28,6 +28,8 @@ const g = require('strong-globalize')();
|
||||||
const juggler = require('..');
|
const juggler = require('..');
|
||||||
const deprecated = require('depd')('loopback-datasource-juggler');
|
const deprecated = require('depd')('loopback-datasource-juggler');
|
||||||
const Transaction = require('loopback-connector').Transaction;
|
const Transaction = require('loopback-connector').Transaction;
|
||||||
|
const pascalCase = require('change-case').pascalCase;
|
||||||
|
const camelCase = require('change-case').camelCase;
|
||||||
|
|
||||||
if (process.env.DEBUG === 'loopback') {
|
if (process.env.DEBUG === 'loopback') {
|
||||||
// For back-compatibility
|
// For back-compatibility
|
||||||
|
@ -1450,17 +1452,22 @@ function capitalize(str) {
|
||||||
return str.charAt(0).toUpperCase() + ((str.length > 1) ? str.slice(1).toLowerCase() : '');
|
return str.charAt(0).toUpperCase() + ((str.length > 1) ? str.slice(1).toLowerCase() : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromDBName(dbName, camelCase) {
|
/**
|
||||||
if (!dbName) {
|
* Renames db column names with different naming conventions:
|
||||||
return dbName;
|
* camelCase for property names as it's LB default naming convention for properties,
|
||||||
|
* or keep the name the same if needed.
|
||||||
|
*
|
||||||
|
* @param {*} name name defined in database
|
||||||
|
* @param {*} caseFunction optional. A function to convert the name into different case.
|
||||||
|
*/
|
||||||
|
function fromDBName(name, caseFunction) {
|
||||||
|
if (!name) {
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
const parts = dbName.split(/-|_/);
|
if (typeof caseFunction === 'function') {
|
||||||
parts[0] = camelCase ? parts[0].toLowerCase() : capitalize(parts[0]);
|
return caseFunction(name);
|
||||||
|
|
||||||
for (let i = 1; i < parts.length; i++) {
|
|
||||||
parts[i] = capitalize(parts[i]);
|
|
||||||
}
|
}
|
||||||
return parts.join('');
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1567,13 +1574,14 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
|
||||||
cb = options;
|
cb = options;
|
||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
cb = cb || utils.createPromiseCallback();
|
cb = cb || utils.createPromiseCallback();
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
const dbType = this.connector.name;
|
const dbType = this.connector.name;
|
||||||
|
|
||||||
let nameMapper;
|
let nameMapper;
|
||||||
|
const disableCamelCase = !!options.disableCamelCase;
|
||||||
|
|
||||||
if (options.nameMapper === null) {
|
if (options.nameMapper === null) {
|
||||||
// No mapping
|
// No mapping
|
||||||
nameMapper = function(type, name) {
|
nameMapper = function(type, name) {
|
||||||
|
@ -1586,11 +1594,19 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) {
|
||||||
// Default name mapper
|
// Default name mapper
|
||||||
nameMapper = function mapName(type, name) {
|
nameMapper = function mapName(type, name) {
|
||||||
if (type === 'table' || type === 'model') {
|
if (type === 'table' || type === 'model') {
|
||||||
return fromDBName(name, false);
|
return fromDBName(name, pascalCase);
|
||||||
} else if (type == 'fk') {
|
} else if (type == 'fk') {
|
||||||
return fromDBName(name + 'Rel', true);
|
if (disableCamelCase) {
|
||||||
|
return fromDBName(name + 'Rel');
|
||||||
|
} else {
|
||||||
|
return fromDBName(name + 'Rel', camelCase);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return fromDBName(name, true);
|
if (disableCamelCase) {
|
||||||
|
return fromDBName(name);
|
||||||
|
} else {
|
||||||
|
return fromDBName(name, camelCase);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1770,12 +1786,17 @@ DataSource.prototype.discoverSchemasSync = function(modelName, options) {
|
||||||
if (!columns || columns.length === 0) {
|
if (!columns || columns.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
const disableCamelCase = !!options.disableCamelCase;
|
||||||
|
|
||||||
const nameMapper = options.nameMapper || function mapName(type, name) {
|
const nameMapper = options.nameMapper || function mapName(type, name) {
|
||||||
if (type === 'table' || type === 'model') {
|
if (type === 'table' || type === 'model') {
|
||||||
return fromDBName(name, false);
|
return fromDBName(name, pascalCase);
|
||||||
} else {
|
} else {
|
||||||
return fromDBName(name, true);
|
if (disableCamelCase) {
|
||||||
|
return fromDBName(name);
|
||||||
|
} else {
|
||||||
|
return fromDBName(name, camelCase);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^3.1.0",
|
"async": "^3.1.0",
|
||||||
|
"change-case": "^4.1.1",
|
||||||
"debug": "^4.1.0",
|
"debug": "^4.1.0",
|
||||||
"depd": "^2.0.0",
|
"depd": "^2.0.0",
|
||||||
"inflection": "^1.6.0",
|
"inflection": "^1.6.0",
|
||||||
|
|
|
@ -73,7 +73,7 @@ describe('Memory connector with mocked discovery', function() {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should convert table/column names to camel cases', function(done) {
|
it('should convert table names to pascal cases and column names to camel case', function(done) {
|
||||||
ds.discoverSchemas('INVENTORY', {}, function(err, schemas) {
|
ds.discoverSchemas('INVENTORY', {}, function(err, schemas) {
|
||||||
if (err) return done(err);
|
if (err) return done(err);
|
||||||
schemas.should.have.property('STRONGLOOP.INVENTORY');
|
schemas.should.have.property('STRONGLOOP.INVENTORY');
|
||||||
|
@ -86,6 +86,19 @@ describe('Memory connector with mocked discovery', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should keep the column names the same as database', function(done) {
|
||||||
|
ds.discoverSchemas('INVENTORY', {disableCamelCase: true}, function(err, schemas) {
|
||||||
|
if (err) return done(err);
|
||||||
|
schemas.should.have.property('STRONGLOOP.INVENTORY');
|
||||||
|
const s = schemas['STRONGLOOP.INVENTORY'];
|
||||||
|
s.name.should.be.eql('Inventory');
|
||||||
|
Object.keys(s.properties).should.be.eql(
|
||||||
|
['PRODUCT_ID', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
|
||||||
|
);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should convert table/column names with custom mapper', function(done) {
|
it('should convert table/column names with custom mapper', function(done) {
|
||||||
ds.discoverSchemas('INVENTORY', {
|
ds.discoverSchemas('INVENTORY', {
|
||||||
nameMapper: function(type, name) {
|
nameMapper: function(type, name) {
|
||||||
|
|
Loading…
Reference in New Issue