Explain Codes LogoExplain Codes Logo

How to make Sequelize use singular table names

javascript
sequelize
database
model
Alex KataevbyAlex Kataev·Nov 27, 2024
TLDR

Set freezeTableName: true in your Sequelize model definition to enforce the use of singular table names:

const Model = sequelize.define('Model', {/* attributes */}, { freezeTableName: true });

This property prevents automatic pluralization ensuring that Sequelize uses the exact model name as the table name.

Table naming: An overview

In Sequelize, table names are pluralized by default. For some projects, singular table names are preferred. Sequelize allows you to control this behavior.

Granular control over table naming

Customizing table names

To specify a custom table name, use the tableName property:

const User = sequelize.define('User', {/* attributes */}, { freezeTableName: true, tableName: 'MyUserTable' // Your custom table name });

This gives you the freedom to follow your naming conventions. Your Sequelize models can now reflect your business terminology or comply with existing database schemas.

Global setting for singular table names

Apply the freezeTableName: true option within the Sequelize constructor for a global effect on all models:

const sequelize = new Sequelize('db', 'user', 'pass', { define: { freezeTableName: true } });

This tactics ensures consistent singular table names across your entire database.

Adjusting automatic behaviors

Sequelize offers several options like timestamps, underscored, and paranoid to further customize table behavior.

  • timestamps: false prevents Sequelize from auto-generating createdAt and updatedAt fields.
  • underscored: true replaces camelCasing with underscore_lower_case for field names.
  • paranoid: true keeps deleted records by using a deletedAt timestamp column.

Precise naming with 'name' parameter

For more granular control over how Sequelize refers to your model in associations, use the name parameters with plural and singular values:

const Entity = sequelize.define('Entity', {/* attributes */}, { name: { singular: 'Entity', plural: 'Entities' // // A whole bag of Entities 🛍️ } });

Tackling the Issue of Pluralization

Overriding automatic pluralization

To completely override default pluralization, apply the freezeTableName: true option.

const sequelize = new Sequelize('db', 'user', 'pass', { define: { freezeTableName: true } });

This approach offers a simple, consistent solution especially for new projects.

Applying global options to all models

Use define property to set common options for all your models:

const sequelize = new Sequelize('db', 'user', 'pass', { define: { freezeTableName: true, underscored: true, timestamps: true // These timestamps walk into a bar... ⏰ } });

Implementing complex behaviors with hooks

Sequelize supports hooks for custom behaviors when working with models:

sequelize.define('Model', { // attributes }, { hooks: { beforeCreate: (model, options) => { // Insert sophisticated business logic here. Free beer if it's a one-liner! 🍺 } } });

Whatever you need to do with your table data, there's probably a hook for that.