What is typeorm-naming-strategies?
The typeorm-naming-strategies npm package provides custom naming strategies for TypeORM, allowing developers to define how database table and column names are generated. This can be particularly useful for ensuring consistency and readability in database schemas.
What are typeorm-naming-strategies's main functionalities?
SnakeCaseNamingStrategy
The SnakeCaseNamingStrategy converts camelCase or PascalCase names to snake_case. This is useful for databases that prefer or require snake_case naming conventions.
const { SnakeNamingStrategy } = require('typeorm-naming-strategies');
const { createConnection } = require('typeorm');
createConnection({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
entities: [__dirname + '/entity/*.js'],
namingStrategy: new SnakeNamingStrategy()
}).then(connection => {
// Here you can start to work with your entities
}).catch(error => console.log(error));
CustomNamingStrategy
The CustomNamingStrategy allows developers to define their own naming conventions. In this example, table names are suffixed with '_tbl' and column names are converted to lowercase.
const { DefaultNamingStrategy, Table } = require('typeorm');
class CustomNamingStrategy extends DefaultNamingStrategy {
tableName(className, customName) {
return customName ? customName : className.toLowerCase() + '_tbl';
}
columnName(propertyName, customName, embeddedPrefixes) {
return (embeddedPrefixes.join('_') + (customName ? customName : propertyName)).toLowerCase();
}
}
const { createConnection } = require('typeorm');
createConnection({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'test',
password: 'test',
database: 'test',
entities: [__dirname + '/entity/*.js'],
namingStrategy: new CustomNamingStrategy()
}).then(connection => {
// Here you can start to work with your entities
}).catch(error => console.log(error));
Other packages similar to typeorm-naming-strategies
typeorm
TypeORM itself provides basic naming strategies and is a powerful ORM for TypeScript and JavaScript (ES7, ES6, ES5). It supports many database systems and allows for custom naming strategies, but typeorm-naming-strategies offers more predefined strategies out of the box.
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It provides a flexible way to define naming conventions through its model definitions and options, but it does not offer as many predefined naming strategies as typeorm-naming-strategies.
objection
Objection.js is an SQL-friendly ORM for Node.js, built on top of the SQL query builder Knex.js. It allows for custom naming conventions through its model definitions and plugins, but it does not have the same level of predefined strategies as typeorm-naming-strategies.
Typeorm naming strategies
This package provides a few (one, at the moment) useful custom naming strategies. It alterates the name of columns, relations and other fields in database.
For example, using the snake strategy, if you have a model like this:
class User {
@Column()
createdAt;
}
In the DB the createdAt
field will be created_at
Naming strategies available
Installation
It's available as an npm package
npm install typeorm-naming-strategies --save
Or using yarn
yarn add typeorm-naming-strategies
Usage
import { createConnection } from 'typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
await createConnection({
...
namingStrategy: new SnakeNamingStrategy(),
});
Alternatively you can use it in combination with a ormconfig.js
const SnakeNamingStrategy = require("typeorm-naming-strategies").SnakeNamingStrategy
module.exports = {
...
namingStrategy: new SnakeNamingStrategy(),
}