Gathering detailed insights and metrics for sequelize-typescript-migration-lts
Gathering detailed insights and metrics for sequelize-typescript-migration-lts
Gathering detailed insights and metrics for sequelize-typescript-migration-lts
Gathering detailed insights and metrics for sequelize-typescript-migration-lts
migration tool ( makemigration ) for sequelize and typescript developers. (best forked projects conclusion)
npm install sequelize-typescript-migration-lts
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
17 Stars
81 Commits
14 Forks
4 Branches
1 Contributors
Updated on 09 Nov 2024
TypeScript (83.65%)
JavaScript (16.35%)
Cumulative downloads
Total Downloads
Last day
13%
261
Compared to previous day
Last week
-14.4%
783
Compared to previous week
Last month
56.9%
3,719
Compared to previous month
Last year
117.8%
52,587
Compared to previous year
23
Use it from here by the name of sequelize-typescript-migration-lts
Here is the Github discussion, any ideas/improves are very welcome.
1npm i sequelize-typescript-migration-lts 2// Or 3yarn add sequelize-typescript-migration-lts
Thanks kimjbstar, mricharz, syon-development, viinzzz and lou2013 for the awesome lib.
It is based on sequelize-typescript, not supports "sequelize" based model codes. and you need prior knowledge of migration of Sequelize.
This scans models and its decorators to find changes, and generates migration code with this changes so don't need to write up, down function manually. this is like "makemigration" in django framework.
After generate successfully, you can use "migrate" in Sequelize Sequelize Migration Manual
This refers to GitHub - flexxnn/sequelize-auto-migrations: Migration generator && runner for sequelize and its forks, and modified to typescript.
Sometimes, undo(down) action may not work, then you should modify manually. Maybe it's because of ordering of relations of models. That issue is currently in the works.
~2.0.0
for sequelize@~6.0.0
~1.0.0
for sequelize@~4.0.0
1import { join } from 'path' 2import { Sequelize } from "sequelize-typescript"; 3import { SequelizeTypescriptMigration } from "sequelize-typescript-migration-lts"; 4 5const sequelize: Sequelize = new Sequelize({ 6 // .. options 7}); 8 9await SequelizeTypescriptMigration.makeMigration(sequelize, { 10 outDir: join(__dirname, './migrations'), 11 migrationName: "add-awesome-field-in-my-table", 12 preview: false, 13});
let's see example, if you have this two models and run first makeMigration, it detects all table change from nothing.
1@Table 2export class CarBrand extends Model<CarBrand> { 3 @Column 4 name: string; 5 6 @Default(true) 7 @Column(DataType.BOOLEAN) 8 isCertified: boolean; 9 10 @Column 11 imgUrl: string; 12 13 @Column 14 orderNo: number; 15 16 @Column 17 carsCount: number; 18}
1@Table 2export class Car extends Model<Car> { 3 @Column 4 name: string; 5 6 @ForeignKey(() => CarBrand) 7 @Column 8 carBrandId: number; 9 10 @BelongsTo(() => CarBrand) 11 carBrand: CarBrand; 12}
then this code written to 00000001-noname.js in migrations path.
1"use strict"; 2 3var Sequelize = require("sequelize"); 4 5/** 6 * Actions summary: 7 * 8 * createTable "CarBrands", deps: [] 9 * createTable "Cars", deps: [CarBrands] 10 * 11 **/ 12 13var info = { 14 revision: 1, 15 name: "noname", 16 created: "2020-04-12T15:49:58.814Z", 17 comment: "", 18}; 19 20var migrationCommands = [ 21 { 22 fn: "createTable", 23 params: [ 24 "CarBrands", 25 { 26 id: { 27 autoIncrement: true, 28 primaryKey: true, 29 allowNull: false, 30 type: Sequelize.INTEGER, 31 }, 32 name: { 33 type: Sequelize.STRING, 34 }, 35 isCertified: { 36 type: Sequelize.BOOLEAN, 37 }, 38 imgUrl: { 39 type: Sequelize.STRING, 40 }, 41 orderNo: { 42 type: Sequelize.INTEGER, 43 }, 44 carsCount: { 45 type: Sequelize.INTEGER, 46 }, 47 createdAt: { 48 allowNull: false, 49 type: Sequelize.DATE, 50 }, 51 updatedAt: { 52 allowNull: false, 53 type: Sequelize.DATE, 54 }, 55 }, 56 {}, 57 ], 58 }, 59 60 { 61 fn: "createTable", 62 params: [ 63 "Cars", 64 { 65 id: { 66 autoIncrement: true, 67 primaryKey: true, 68 allowNull: false, 69 type: Sequelize.INTEGER, 70 }, 71 name: { 72 type: Sequelize.STRING, 73 }, 74 carBrandId: { 75 onDelete: "NO ACTION", 76 onUpdate: "CASCADE", 77 references: { 78 model: "CarBrands", 79 key: "id", 80 }, 81 allowNull: true, 82 type: Sequelize.INTEGER, 83 }, 84 createdAt: { 85 allowNull: false, 86 type: Sequelize.DATE, 87 }, 88 updatedAt: { 89 allowNull: false, 90 type: Sequelize.DATE, 91 }, 92 }, 93 {}, 94 ], 95 }, 96]; 97 98var rollbackCommands = [ 99 { 100 fn: "dropTable", 101 params: ["Cars"], 102 }, 103 { 104 fn: "dropTable", 105 params: ["CarBrands"], 106 }, 107]; 108 109module.exports = { 110 pos: 0, 111 up: function (queryInterface, Sequelize) { 112 var index = this.pos; 113 return new Promise(function (resolve, reject) { 114 function next() { 115 if (index < migrationCommands.length) { 116 let command = migrationCommands[index]; 117 console.log("[#" + index + "] execute: " + command.fn); 118 index++; 119 queryInterface[command.fn] 120 .apply(queryInterface, command.params) 121 .then(next, reject); 122 } else resolve(); 123 } 124 next(); 125 }); 126 }, 127 down: function (queryInterface, Sequelize) { 128 var index = this.pos; 129 return new Promise(function (resolve, reject) { 130 function next() { 131 if (index < rollbackCommands.length) { 132 let command = rollbackCommands[index]; 133 console.log("[#" + index + "] execute: " + command.fn); 134 index++; 135 queryInterface[command.fn] 136 .apply(queryInterface, command.params) 137 .then(next, reject); 138 } else resolve(); 139 } 140 next(); 141 }); 142 }, 143 info: info, 144};
then you can apply this npx sequelize db:migrate --to 00000001-noname.js
Make sure to have writeMigration in your System under development and that sequelize is all set up
If you change a model and re-run the backend there should be a new file under db/migrations
, but the database won't update automatically. There are easy but important steps:
sequelize db:migrate
sequelize db:migrate:undo
sequelize db:migrate:undo
and continue your amazing workWill be ready soon!
No vulnerabilities found.
No security vulnerabilities found.