npm install sequelize-cli
require("dotenv").config();
const { DB_USER, DB_PASS, DB_HOST } = process.env;
module.exports = {
uat: {
username: DB_USER,
password: DB_PASS,
database: "test-db",
host: DB_HOST,
dialect: "mysql",
},
prd: {
username: DB_USER,
password: DB_PASS,
database: "test-db",
host: DB_HOST,
dialect: "mysql",
},
};
sequelize-cli migration:create --migrations-path ./database/migration --name
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('Users', 'isMale', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
});
await queryInterface.addColumn('Users', 'count', {
type: Sequelize.TINYINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
});
},
async down(queryInterface) {
await queryInterface.removeColumn('Users', 'isMale');
await queryInterface.removeColumn('Users', 'count');
}
};
NODE_ENV=uat npx sequelize-cli db:migrate --migrations-path ./database/migration
NODE_ENV=uat npx sequelize-cli db:migrate:undo --migrations-path ./database/migration
> sequelize-cli 會自動在 db 新增一張 sequlizeMeta 表,用來記錄執行過的 migratiion script
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
const campaignId = 1;
await queryInterface.bulkInsert('Campaigns', [
{
id: campaignId,
name: 'Yield Pass Phase-1',
createdAt: Sequelize.literal('NOW()'),
updatedAt: Sequelize.literal('NOW()'),
},
]);
await queryInterface.sequelize.query(`
INSERT INTO "Tasks" ("campaignId", "createdAt", "updatedAt", title, details) VALUES
(1, NOW(), NOW(), 'test', 'test');
`);
await queryInterface.sequelize.query(`
INSERT INTO "Rewards" ("taskId", category, points, "createdAt", "updatedAt") VALUES
(1, 'test', 200, NOW(), NOW());
`);
},
async down(queryInterface) {
await queryInterface.bulkDelete('Rewards', null, {});
await queryInterface.bulkDelete('Tasks', null, {});
await queryInterface.bulkDelete('Campaigns', null, {});
},
};
npx sequelize-cli db:seed:all
npx sequelize db:seed --seed temp-seeder.js
npx sequelize-cli db:seed:undo