This is the 16th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

I. Model synchronization

Model synchronization to database table fields. Sequelize automatically executes SQL queries against the database by calling the asynchronous function model.sync(options), which returns a Promise. You only modify the table structure in the database, not the model on the JavaScript side

  • User.sync()– If the table does not exist, create the table (if it already exists, do nothing)
  • User.sync({ force: true })– The table will be created, and if the table already exists, it will be deleted first
  • User.sync({ alter: true })– This checks the current state of the table in the database (which columns it has, their data types, etc.) and then makes the necessary changes in the table to match the model.

1. Single model User synchronization

await User.sync({ force: true });
Copy the code

2. Sequelize instance synchronizes all model fields

await sequelize.sync({ force: true });
Copy the code

3. Delete the table of the model

await User.drop();
Copy the code

4. Delete all tables

awaitsequelize.drop(); \Copy the code

5. Check the matching database name

sequelize.sync({ force: true, match: /_test$/ });
Copy the code

6. Set the default values for the fields

sequelize.define('User', {
  name: {
    type: DataTypes.STRING,
    defaultValue: "John Doe"}});Copy the code

The data types of the fields in the model

const { DataTypes } = require("sequelize"); // Import the built-in data types
Copy the code

1. String (String)

DataTypes.STRING             // VARCHAR(255)
DataTypes.STRING(1234)       // VARCHAR(1234)
DataTypes.STRING.BINARY      // VARCHAR BINARY
DataTypes.TEXT               // TEXT
DataTypes.TEXT('tiny')       // TINYTEXT
DataTypes.CITEXT             // CITEXT only PostgreSQL and SQLite.
DataTypes.TSVECTOR           / / TSVECTOR PostgreSQL only.
Copy the code

Boolean (Boolean) 2.

DataTypes.BOOLEAN            // TINYINT(1)
Copy the code

3. Numbers (Number)

DataTypes.INTEGER            // INTEGER
DataTypes.BIGINT             // BIGINT
DataTypes.BIGINT(11)         // BIGINT(11)

DataTypes.FLOAT              // FLOAT
DataTypes.FLOAT(11)          // FLOAT(11)
DataTypes.FLOAT(11.10)      / / FLOAT (11, 10)

DataTypes.REAL               / / REAL PostgreSQL only.
DataTypes.REAL(11)           / / REAL only PostgreSQL (11).
DataTypes.REAL(11.12)       / / REAL only PostgreSQL (11, 12).

DataTypes.DOUBLE             // DOUBLE
DataTypes.DOUBLE(11)         // DOUBLE(11)
DataTypes.DOUBLE(11.10)     / / DOUBLE (11, 10)

DataTypes.DECIMAL            // DECIMAL
DataTypes.DECIMAL(10.2)     / / a DECIMAL (1, 2)
Copy the code

Date of 4.

DataTypes.DATE       // DATETIME applies to mysql/SQLite, TIMESTAMP with time zone applies to Postgres
DataTypes.DATE(6)    // DATETIME(6) applies to mysql 5.6.4+. Supports 6 decimal seconds of precision
DataTypes.DATEONLY   // DATE without time
Copy the code

5. UUID

With DataTypes.UUID,PostgreSQL and SQLite generate UUID DataTypes, and MySQL generates CHAR(36) DataTypes. Sequelize Use sequelize. UUIDV1 or sequelize. UUIDV4 to automatically generate UUids

{
  type: DataTypes.UUID,
  defaultValue: Sequelize.UUIDV4 / / or Sequelize UUIDV1
}
Copy the code