What is Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more. Sequelize Installation Sequelize is available via (or ). npm yarn npm install --save sequelize You’ll also have to manually install the driver for your database of choice: # One the following: $ 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 of Setting up Connection To connect to the database, you must create a Sequelize instance. This can be done by either passing the connection parameters separately to the Sequelize constructor or by passing a single connection URI: Sequelize = ( ); sequelize = Sequelize( , , , { : , : }); const require 'sequelize' const new 'database' 'username' 'password' host 'localhost' dialect /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */ If you’re connecting to the database from a single process, you should create only one Sequelize instance. Sequelize will set up a connection pool on initialization. This connection pool can be configured through the constructor’s options parameter (using ), as is shown in the following example: options.pool sequelize = Sequelize( , { pool: { : , : , : , : } const new /* ... */ // ... max 5 min 0 acquire 30000 idle 10000 Modeling Tables User = sequelize.define( , { firstName: { : Sequelize.STRING, : }, : { : Sequelize.STRING } }, { }); const 'user' // attributes type allowNull false lastName type // allowNull defaults to true // options Internally, calls . sequelize.define Model.init The above code tells Sequelize to expect a table named users in the database with the fields and . The table name is automatically pluralized by default (a library called is used under the hood to do this). This behavior can be stopped for a specific model by using the : true option, or for all models by using the define option from the . firstName lastName inflection freezeTableName Sequelize constructor Sequelize also defines by default the fields id (primary key), and to every model. createdAt updatedAt If you want Sequelize to automatically create the table (or modify it as needed) according to your model definition, you can use the method, as follows: sync User.sync({ : }).then( { User.create({ : , : }); }); // Note: using `force: true` will drop the table if it already exists force true => () // Now the `users` table in the database corresponds to the model definition return firstName 'John' lastName 'Hancock' Querying in Sequelize A few simple queries are shown below: User.findAll().then( { .log( , .stringify(users, , )); }); User.create({ : , : }).then( { .log( , jane.id); }); User.destroy({ : { : } }).then( { .log( ); }); User.update({ : }, { : { : } }).then( { .log( ); }); // Find all users => users console "All users:" JSON null 4 // Create a new user firstName "Jane" lastName "Doe" => jane console "Jane's auto-generated ID:" // Delete everyone named "Jane" where firstName "Jane" => () console "Done" // Change everyone without a last name to "Doe" lastName "Doe" where lastName null => () console "Done" Datatypes in Sequelize Below are some of the datatypes supported by sequelize. Sequelize.STRING Sequelize.STRING( ) Sequelize.STRING.BINARY Sequelize.TEXT Sequelize.TEXT( ) Sequelize.CITEXT Sequelize.INTEGER Sequelize.BIGINT Sequelize.BIGINT( ) Sequelize.FLOAT Sequelize.FLOAT( ) Sequelize.FLOAT( , ) // VARCHAR(255) 1234 // VARCHAR(1234) // VARCHAR BINARY // TEXT 'tiny' // TINYTEXT // CITEXT PostgreSQL and SQLite only. // INTEGER // BIGINT 11 // BIGINT(11) // FLOAT 11 // FLOAT(11) 11 10 // FLOAT(11,10) Raw Queries in Sequelize As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can use the function sequelize.query. By default the function will return two arguments — a results array, and an object containing metadata (affected rows etc.). Note that since this is a raw query, the metadata (property names etc.) is dialect specific. Some dialects return the metadata “within” the results object (as properties on an array). However, two arguments will always be returned, but for MSSQL and MySQL it will be two references to the same object. sequelize.query( ).then( { }) "UPDATE users SET y = 42 WHERE x = 12" ( ) => [results, metadata] // Results will be an empty array and metadata will contain the number of affected rows. In cases where you don’t need to access the metadata you can pass in a query type to tell sequelize how to format the results. For example, for a simple select query you could do: sequelize.query( ).then( { }) "UPDATE users SET y = 42 WHERE x = 12" ( ) => [results, metadata] // Results will be an empty array and metadata will contain the number of affected rows. Please refer for more details. https://sequelize.org