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?
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:
If you have to pick a global convention on both sides, JavaScript should win - ie, camelCase.
Good advice, thank you!
Most helpful comment
Two choices:
If you have to pick a global convention on both sides, JavaScript should win - ie, camelCase.