migrationjs v0.4.12
MigrationJS
MigrationJS is a database migration package for NodeJS. MigrationJS allows you to create migrations for your database and run them in a specific order. This way it is easy to introduce database changes to you application at ease.
Installation
npm i -g migrationjs
Commands
Commands are composed like this: migrationjs <command> <args>
The available commands are:
help
Gets a list of all the commandsmigrate
This makes a new batch of all the migrations which did not run yet and executes theupfunction of those migrations.rollback
This command executes thedownfunction of the migrations of the latest batch.make:migration \<migration name>
This command creates a migration file. The name has to start withcreateif you want to generate a create-table migration template, oralterfor an alter-table migration templatemake:config
This command creates amigrationjs.conf.jsonfile where you can configure your settings of MigrationJS.
Migration file
A migration file is a file which contains two functions: up and down. The up function is executed when you run migrationjs migrate and the down function is executed when you run migrationjs rollback.
In those methods you may use the Schema object to create tables, alter tables, drop tables, etc. one example is shown below.
import {Migration, Blueprint, Schema} from 'migrationjs';
export default class CreateTestTable extends Migration {
async up() {
await Schema.create('test', (table: Blueprint) => {
table.id();
table.string('name');
});
}
async down() {
await Schema.dropIfExists('test');
}
}To run the created migration you have to run migrationjs migrate in the terminal. This will create a new batch of migrations and execute the up function of the created migration.
To rollback the created migration you have to run migrationjs rollback in the terminal. This will execute the down function of the created migration.
Table functions
Create tables
To create a table you have to use the Schema.create function. This function takes two arguments: the name of the table and a callback function which takes a Blueprint object as argument. The Blueprint object contains all the functions to create a table.
await Schema.create('users', (table: Blueprint) => {
table.id();
table.string('name');
});Updating tables
To update a table you have to use the Schema.table function. This function takes two arguments: the name of the table and a callback function which takes a Blueprint object as argument. The Blueprint object contains all the functions to update a table.
await Schema.table('users', (table: Blueprint) => {
table.string('username');
});Renaming / Dropping tables
To rename a table you have to use the Schema.rename function. This function takes two arguments: the name of the table and the new name of the table.
await Schema.rename(from: string, to: string);To drop an existing table you have to use the Schema.drop function. This function takes one argument: the name of the table.
await Schema.dropIfExists('users'); Contributors
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago