gg-seqaumig
v0.0.2
Published
Automate Sequelize migrations with TypeScript and JavaScript, supporting dynamic schemas.
Downloads
12
Maintainers
Readme
gg-seqaumig
Seqaumig : Automate Sequelize migrations with TypeScript and JavaScript, supporting dynamic schemas.
Installation
You can install gg-seqaumig through npm, as such:
npm install gg-seqaumig --save
Set options
new Seqaumig().setOptions({
sequelize: sequelize,
dialect: 'postgres',
migrationsPath: path.resolve('src/db/migrations'),
seedersPath: path.resolve('src/db/seeders'),
publicSchema: 'public',
schema: {
migrationsPath: path.resolve('src/db/PrivateSchema/migrations'),
seedersPath: path.resolve('src/db/PrivateSchema/seeders'),
}
});
How to create schema
await SeqaumigSchema.createSchema(tenant.db_schema, true);
await SeqaumigSchema.deleteSchema(tenant.db_schema, true)
How to run migrations
await SeqaumigMigration.createMigrationTable(tenant.db_schema, true);
await SeqaumigMigration.migrate(tenant.db_schema);
await SeqaumigMigration.migrateUndoLatest(tenant.db_schema);
await SeqaumigMigration.migrateUndo(tenant.db_schema);
How to run seeders
await SeqaumigSeeders.createSeederTable(tenant.db_schema, true);
await SeqaumigSeeders.seed(tenant.db_schema);
await SeqaumigSeeders.seedUndo(tenant.db_schema);
Migration file
'use strict';
const {DataTypes} = require("sequelize");
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(queryInterface.dynamicSchema('tbl_user_order'),
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.BIGINT,
validate: {
min: 0
},
},
user_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
schema: 'static',
model: queryInterface.publicSchema('tbl_user'),
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
order_id: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
schema: 'static',
model: queryInterface.dynamicSchema('tbl_order'),
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable(queryInterface.dynamicSchema('tbl_user_order'));
}
};
Seeder file
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.bulkInsert(queryInterface.dynamicSchema('tbl_user'),
[
{
"id": 1,
"name": "Alex",
"created_at": new Date(),
"updated_at": new Date(),
}
]);
},
async down(queryInterface, Sequelize) {
await queryInterface.bulkDelete(queryInterface.dynamicSchema('tbl_user'));
}
};