Sequelize

Install

npm i sequelize
# One of the following:
npm install mariadb
npm install pg pg-hstore # Postgres
npm install mysql2

npm install sqlite3
npm install tedious # Microsoft SQL Server

Quick start

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

class User extends Model {}
User.init({
  username: DataTypes.STRING,
  birthday: DataTypes.DATE
}, { sequelize, modelName: 'user' });

(async () => {
  await sequelize.sync();
  const jane = await User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20)
  });
  console.log(jane.toJSON());
})();

Connection

// Option 2: Passing parameters separately (other dialects)
const sequelize = new Sequelize('database', 'username', 'password', {
  host: 'localhost',
  dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});

Template

npm i -g sequelize-cli
sequelize init

Model

npx sequelize-cli model:generate --name like --attributes userId:integer,articleId:integer
npx sequelize-cli db:migrate

Timezone

const sequelize = new Sequelize('sqlite::memory:', {
  timezone: '+05:30'
  // or
  timezone: 'Asia/Seoul'
});

related

What Else?
inflearn react api server -50% 할인쿠폰: 15108-f2af1e086101 buy me a coffee