-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allows different naming conventions for db columns for discover #1819
Conversation
b1b43ee
to
36e9c17
Compare
36e9c17
to
2edd077
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
8aeec59
to
60f1726
Compare
lib/datasource.js
Outdated
@@ -1586,11 +1594,11 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { | |||
// Default name mapper | |||
nameMapper = function mapName(type, name) { | |||
if (type === 'table' || type === 'model') { | |||
return fromDBName(name, false); | |||
return pascalCase(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agnes512 A question about the behaviour change:
Before we allowed camel case for table name, and IIUC this PR converts all the table names to be pascal case, there is no option to config it to be other naming conventions including camel case.
Is it intended?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we allowed table/model names to be camelCase before because every time we checked the type:
if (type === 'table' || type === 'model') {
return fromDBName(name, false); // this is saying not to use camelCase, use pascal instead
the false
in the second param was set, no options were allowed to configure it.
And since it was converting all table/model names to pascalCase, I replace it with a formal function pascalCase()
to make it more legit.
Before the function fromDBName
handled cases change for type table
, model
, fk
, and column
. I change it to just handle column names only. I change it to just handle column names and fk only
( I realized that I made a mistake here, I should let it handle fk
as well as it's part of property names).
Another reason I think table and model names should just be pascal case is because lots of our naming conventions are based on the model name. We assume model names will be Pascal case, and file names that are generated based on the model names will be snake case, etc. I think allowing customized table/model names might cause some unexpected issues.
lib/datasource.js
Outdated
|
||
const nameMapper = options.nameMapper || function mapName(type, name) { | ||
if (type === 'table' || type === 'model') { | ||
return fromDBName(name, false); | ||
return pascalCase(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agnes512 Good to see we are making progress on cleaning up the naming conventions 👍 I left a question regarding the behaviour change, otherwise LGTM.
lib/datasource.js
Outdated
* @param {*} defcamelCase boolean. Converts the name to camelcase if true as | ||
* it's the default case for variables / properties in LoopBack. | ||
*/ | ||
function fromDBName(colName, defCamelCase) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better to make 2nd argument an optional function (default to camelCase) for case conversion. Using a boolean
argument is not very friendly for JS.
- fromDBName(colName) or fromDBName(colName, camelCase) // convert to camel case
- fromDBName(colName, noCase) // no conversion
- fromDBName(colName, pascalCase) // convert to pascal case
lib/datasource.js
Outdated
@@ -1574,6 +1580,8 @@ DataSource.prototype.discoverSchemas = function(tableName, options, cb) { | |||
const dbType = this.connector.name; | |||
|
|||
let nameMapper; | |||
const defCamelCase = options.useDefaultCase === undefined ? true : options.useDefaultCase; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better way is options.disableCamelCase
(default to false
).
lib/datasource.js
Outdated
} else if (type == 'fk') { | ||
return fromDBName(name + 'Rel', true); | ||
if (disableCamelCase === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason we cannot use if(disableCamelCase)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I thought I need to check the type as well turns out I didn't need to, changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Just have a question for using if(disableCamelCase === true)
, otherwise LGTM.
And please make sure the CI passes. Oracle seems random, postgresql seems related...😞 |
f86c420
to
d13a893
Compare
lib/datasource.js
Outdated
cb = cb || utils.createPromiseCallback(); | ||
|
||
const self = this; | ||
const dbType = this.connector.name; | ||
|
||
let nameMapper; | ||
const disableCamelCase = options.disableCamelCase === undefined ? false : options.disableCamelCase; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const disableCamelCase = !!options.disableCamelCase;
d13a893
to
baeb76e
Compare
The failure of Postgres is because of this line, db.discoverAndBuildModels('GeoPoint', {schema: 'strongloop'}, function(err, schema) {
schema.Geopoint.find(function(err, data) { With the change, the schema name is supposed to be |
Connects to loopbackio/loopback-next#3343
Allows users to keep the property name as the same as the db columns names. It can be set by
options.useDefaultCase
to false.To close #3343, we need to:
lb4 discover
CLI and docs ( need this PR to be landed first)Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machine