Sequelize Theory (Part 1)

Tram Ho

Before reading the article, I want everyone to know that this is just a random translation in my free time. If you find it difficult to understand, please refer to the original docs article on the sequelize page at this link . The version I translated is Sequelize V6 ]

Sequelize Theory (part I)

Basic theory of models

I/ Concepts

Model is the class that represents the table in the database. In order to declare a table, you need DATA TYPES. In javascript there is no such thing so Sequelize has support for many data types.

Sequelize supports creating tables with the plural form of model names. For example, the Model name is User , then the database table will be named Users . And if the table is Person , then Sequelize will also understand the plural form as People thanks to the library inflection

Model name and table name can be changed.

II/ How to declare Model

Here is sample code that uses both sequelize and class inheritance methods

  1. Use sequelize.define :

  1. Using class inheritance

III/ Be careful with code shadowing

If you use the second method of inheriting the class, you should not declare the same properties in the class that the model inherits.

Example should not: (Model class already has id attribute, User class declaring more id attribute will be overwritten and lose the getter setter)

The example should:

IV/ Change table name

  1. Force the same table name as the model name:

By using option freezeTableName: true

Eg:

Or declare this option when declaring sequelize at the top of the file

  1. Provide table name directly

Provide the table name when declaring the model:

V/ Model sync

  1. Sync each one

When you define a model, you are telling Sequelize something about its table in the database. However, what if the table really doesn’t exist? If it exists, but it has different columns, less columns or any difference?

This is where model synchronization comes in. A model can be synchronized with the database by calling model.sync(options) , an asynchronous function (returning a Promise ). With this call, Sequelize will automatically execute the SQL query to the database. Note that this only changes the table in the database, not the model on the JavaScript side.

  • User.sync() – Creates the table if it doesn’t exist (and does nothing if it already exists)
  • User.sync({ force: true }) – Create a table, delete it if it already exists, then create it
  • User.sync({ alter: true }) – Checks the current state of the table in the database (which columns exist, their data types, etc.), then makes the necessary changes in the table to make the table match the model.

Eg:

  1. Sync all

By using sequelize.sync() command to automatically sync all models. Eg:

VI/ Drop table

To be able to drop the table associated with a model, we run the .drop() command:

or drop the entire table we run:

However dropping the table or synchronizing with the force method, alter is NOT RECOMMENDED in PRODUCTION.

VII/ Timestamps

By default, sequelize will add two columns createAt and updateAt for each model with data type of DataTypes.DATE .

Note: These fields are automatically updated by Sequelize. Therefore all updates using Queries will usually not trigger this effect. Which must be updated using the built-in function of Sequelize.

This behavior can be turned off by the command:

Or we can also enable whoever we like with the command:

VIII/ Short syntax for column definition

If we just want to define the type of a column, we can write:

IX/ Default value of a column

By default sequelize will treat the default value as NULL , but you can configure the default value:

X/ Data types

Sequelize supports many shared and proprietary data types for each database. Exclusive data types see here .

Also shared data types:

String

Boolean

Numbers

Dates

UUIDs

XI/ Other options of the column

Share the news now

Source : Viblo