If you want migrations, start with the CLI
yarn add sequelize sequelize-cli
npx sequelize-cli init
yarn add pg
config/config.json
{
"development": {
"username": "josh",
"password": null,
"database": "myapp_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "josh",
"password": null,
"database": "myapp_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"use_env_variable": "DATABASE_URL"
}
}
yarn add sqlite3
config/config.json
{
"development": {
"dialect": "sqlite",
"storage": "./development.sqlite3"
},
"test": {
"dialect": "sqlite",
"storage": ":memory"
},
"production": {
"dialect": "sqlite",
"storage": "./production.sqlite3"
}
}
"scripts": {
"seq": "sequelize"
},
yarn seq model:generate --name Message --attributes text:string,url:string
const db = require('../models')
const { Restaurant } = db
const records = await Restaurant.findAll();
const records = await Restaurant.findAll({where: {…}});
const record = await Restaurant.findOne({where: {id: …}});
const record = await Restaurant.create(fields);
await record.update(fields);
$ yarn seq --help
$ yarn seq model:generate
$ yarn seq migration:generate
$ yarn seq seed:generate
$ yarn seq db:create
$ yarn seq db:migrate
$ yarn seq db:migrate:undo
$ yarn seq db:migrate:undo:all
$ yarn seq db:seed:all
$ yarn seq db:seed:undo:all
$ yarn seq db:drop
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Todos', {
id: {
type: Sequelize.UUID,
primaryKey: true,
allowNull: false,
},
//...
Todo.init({
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV1, // if you want server to assign
},
//...