yarn add sequelize-cli
it is clear from the --help command how to generate a migration folder and migration file. The only trouble is to use them with ES6 syntax.
From the official document:
https://sequelize.org/master/manual/migrations.html#using-babelwe add
1 | yarn add babel-register |
1 2 3 4 5 6 7 8 9 10 11 | // .sequelizerc require( "babel-register" ); const path = require( 'path' ); module.exports = { 'config' : path.resolve( 'config' , 'config.json' ), 'models-path' : path.resolve( 'models' ), 'seeders-path' : path.resolve( 'seeders' ), 'migrations-path' : path.resolve( 'migrations' ) } |
https://sequelize.org/master/manual/query-interface.htmlOfficial document also says that in migration file we can export async function up and async function down instead of returning a chain of promises (i.e., a promise)! For example it happens that I want to add a column for users to implement mobile push notification, then I need to add a column called push_notification_token, I can do the following in our migration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | "use strict" ; import { modelNames } from "../src/enums/modelNames" ; import { Sequelize, DataTypes } from "sequelize" ; module.exports = { async up(queryInterface, Sequelize) { await queryInterface.addColumn(modelNames.USER + "s" , "push_notification_token" , { type: DataTypes.STRING, allowNull: true }); }, async down(queryInterface, Sequelize) { await queryInterface.removeColumn( modelNames.USER + "s" , "push_notification_token" , {} ); } }; |
1 2 3 4 5 | Loaded configuration file "config\config.js". Using environment "development". == 20200615141047-add-push-notification-token-to-users-table: migrating ======= ERROR: regeneratorRuntime is not defined |
1 | yarn add babel-plugin-transform-runtime |
1 2 3 4 5 6 7 8 | { "presets": ["env"], "plugins": [ ["transform-runtime", { "regenerator": true }] ] } |
No comments:
Post a Comment