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
attributes
: define attributes no spaces between fields
Timezone
const sequelize = new Sequelize('sqlite::memory:', {
timezone: '+05:30'
// or
timezone: 'Asia/Seoul'
});
related