preface

The Sequelize ORM is a Promise-based Nodejs ORM.

ORM (Object-relational Mapping) is such a tool. It can map databases into objects and simplify operations on databases into operations on objects.

start

Install Sequelize

npm install --save sequelize
Copy the code

Installing the database Drive

Select one of the following databases based on your actual database:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server
Copy the code

Connect to the database. To connect to the database, create a Sequelize instance. You can do either of the following.

const { Sequelize } = require('sequelize');

// Method 1: Pass a URL
const sequelize = new Sequelize('sqlite::memory:') // Example for sqlite
const sequelize = new Sequelize('postgres://user:[email protected]:5432/dbname') // Example for postgres

// Method 2: Pass parameters separately (sqlite)
const sequelize = new Sequelize({
 dialect: 'sqlite'.storage: 'path/to/database.sqlite'
});

Method 2: Pass parameters separately (other imperatives)
const sequelize = new Sequelize('database'.'username'.'password', {
 host: 'localhost'.dialect:  'mysql'/* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
Copy the code

Test tips

Instead of creating a database locally, you can use the following methods:

const { Sequelize, Op, Model, DataTypes } = require("sequelize");
const sequelize = new Sequelize("sqlite::memory:");
Copy the code

Model Model

concept

The model is the core concept of Sequelize, and a Model is an abstraction of a table in a database. In Sequelize it is a class that inherits from Model.

Models have a name, which is not necessarily the same as the table in the database (you can set it arbitrarily). In general, model names are singular (such as User), while table names in databases are plural (Users).

The model definition

Use sequelize.define

const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize('sqlite::memory:');

const User = sequelize.define('User', {
  firstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  lastName: {
    type: DataTypes.STRING
  }
}, {
  // Other model options go here
});

// `sequelize.define` also returns the model
console.log(User === sequelize.models.User); // true
Copy the code

Synchronization of models

Unidirectional synchronization of the model’s contained information to a database table can be done as follows

  • User.sync() – if the table does not exist, it will be created; If it already exists, do nothing
  • User.sync({force: true}) – Force the creation of a new table; Delete it if it exists before
  • User.sync({alter: true}) – Check whether the fields in the table are identical to the model, if not, only make changes as needed

Delete table

await User.drop();
console.log("The Users table has been deleted");

await sequelize.drop();
console.log("All tables deleted!");
Copy the code

Association Association

Sequelize supports the following standard associations: one-to-one, one-to-many, and many-to-many.

  • HasOne
  • BelongsTo
  • HasMany associations
  • BelongsToMany associated

To create a one-to-one relationship, establish a connection using hasOne and belongsTo;

To create a one-to_many relationship, establish a connection using hasMany and belongsTo;

To create a many-to-many relationship, use two belongsToMany connections.