Javascript: SQL Table/Column names and Javascript named in a isomorphic way

Created on 23 Mar 2017  路  4Comments  路  Source: airbnb/javascript

How should I name sql database columns and tables as nodejs code always is using its reference as object keys. How to name essentially id names but columns too.

The way things is right now

mysql> SHOW COLUMNS FROM Person;
+------------+----------+------+-----+---------+----------------+
| Field 
+------------+----------+------+-----+---------+----------------+
| Id 
| Name
| LivingCountry
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

Will be used on the javascript nodejs app like

        let city = {
            Id: 2,
            Name: 'Kent',
            LivingCountry: 'New Yourk'
        }

Is is correct to do like below?

mysql> SHOW COLUMNS FROM person;
+------------+----------+------+-----+---------+----------------+
| Field
+------------+----------+------+-----+---------+----------------+
| personId
| name
| livingCountry
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

So, object on nodejs app will be used like this:

        let city = {
            personId: 2,
            name: 'Kent',
            livingCountry: 'New Yourk'
        }

The question is important because I would like to easy code and maintain the code. SQL has its pattern but the app is essentially javascript typed. It will be better if I make SQL Table/Column names and Javascript named in a isomorphic way.

What do you experts say about that?

question

Most helpful comment

Two choices:

  1. use camelCase everywhere, but keep using snake_case only for server-generated attributes
  2. always manually convert to camelCase after you get the data back from the network, and use camelCase everywhere.

If you have to pick a global convention on both sides, JavaScript should win - ie, camelCase.

All 4 comments

It's more correct, yes. Only constructor functions should be capitalized.

It's also uncommon in my experience in SQL to capitalize column names in that manner; they're typically lowercased and snake_cased.

@ljharb How would you deal with such situation: if your data layer was to return your model's attributes snake_cased but your front end was to expect its object attributes to be camelCase ?

I find myself wondering if it would be better to adopt a global convention (snake_case vs camelCase on both sides) or to have a point of conversion at the backend or the front end (middleware ?) doing the conversion from one to the other format.

Two choices:

  1. use camelCase everywhere, but keep using snake_case only for server-generated attributes
  2. always manually convert to camelCase after you get the data back from the network, and use camelCase everywhere.

If you have to pick a global convention on both sides, JavaScript should win - ie, camelCase.

Good advice, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olalonde picture olalonde  路  3Comments

graingert picture graingert  路  3Comments

zurfyx picture zurfyx  路  3Comments

ryankask picture ryankask  路  3Comments

ar
mbifulco picture mbifulco  路  3Comments