We are using TypeORM and Postgresql and I'm curious about naming conventions.
Given that there are perfectly appropriate styles and naming conventions for databases that are separate from the perfectly good ones used for Javascript, is it considered better practice to force databases to use the code convention or to force code to use the database convention, or to translate everything?
For example:
It's common practice to use the SQl style defined in Joe Celko's SQL Programming Style for the database. This advocates for snake_case for the column names.
It's also common practice to name variables in camelCase when programming in JavaScript and all the documentation on typeorm.
So, when these two worlds collide, is it best practice to force one to the other or to translate every multi-word entity in the definitions to do the mapping.
This isn't really a question of how to do that but rather if there is a common practice one way of the other.
The three possibilities for a column representing User Id are:
1: Translate everything
@Column( { name: user_id } )
userId: number;
2: Use the database convention in the code
@Column()
user_id: number;
3: Use the coding convention in the database
@Column()
userId: number
You can support both in TypeOrm by adjusting your ormconfig.js to support Typeorm Naming Strategies package. This will allow you to have coding convention within the code and database naming convention within the database.
It can be set up by:
npm i --save typeorm-naming-strategies
Then within your ormconfig.js, add the following lines:
const SnakeNamingStrategy = require('typeorm-naming-strategies')
.SnakeNamingStrategy;
module.exports = {
name: 'development',
type: 'postgres',
host: 'localhost',
port: 5432,
...
namingStrategy: new SnakeNamingStrategy(),
}
TypeOrm will now follow the snake_case convention when naming columns within postgres
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With