Skip to content
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

Merged
merged 1 commit into from
Jan 29, 2020

Conversation

agnes512
Copy link
Contributor

@agnes512 agnes512 commented Jan 27, 2020

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:

  1. modify Juggler (this PR)
  2. update LB3 docs ( docs: add explanations of naming convention of discover loopback.io#928)
  3. update 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
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Commit messages are following our guidelines

lib/datasource.js Outdated Show resolved Hide resolved
lib/datasource.js Outdated Show resolved Hide resolved
Copy link
Contributor

@nabdelgadir nabdelgadir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

lib/datasource.js Outdated Show resolved Hide resolved
lib/datasource.js Outdated Show resolved Hide resolved
lib/datasource.js Outdated Show resolved Hide resolved
@agnes512 agnes512 force-pushed the discover-naming branch 2 times, most recently from 8aeec59 to 60f1726 Compare January 28, 2020 19:14
@@ -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);
Copy link
Contributor

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?

Copy link
Contributor Author

@agnes512 agnes512 Jan 28, 2020

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.


const nameMapper = options.nameMapper || function mapName(type, name) {
if (type === 'table' || type === 'model') {
return fromDBName(name, false);
return pascalCase(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jannyHou jannyHou left a 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.

* @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) {
Copy link
Contributor

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

@@ -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;
Copy link
Contributor

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).

} else if (type == 'fk') {
return fromDBName(name + 'Rel', true);
if (disableCamelCase === true) {
Copy link
Contributor

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)?

Copy link
Contributor Author

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.

lib/datasource.js Outdated Show resolved Hide resolved
Copy link
Contributor

@jannyHou jannyHou left a 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.

@jannyHou
Copy link
Contributor

And please make sure the CI passes. Oracle seems random, postgresql seems related...😞

lib/datasource.js Outdated Show resolved Hide resolved
cb = cb || utils.createPromiseCallback();

const self = this;
const dbType = this.connector.name;

let nameMapper;
const disableCamelCase = options.disableCamelCase === undefined ? false : options.disableCamelCase;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const disableCamelCase = !!options.disableCamelCase;

@agnes512
Copy link
Contributor Author

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 GeoPoint instead of Geopoint. Can we land this PR first and I will fix it in Postgres connector?

@agnes512 agnes512 merged commit f4ec02b into master Jan 29, 2020
@delete-merged-branch delete-merged-branch bot deleted the discover-naming branch January 29, 2020 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants