1.0.3 • Published 4 years ago

my-migration-sql v1.0.3

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

my-migration-sql

Latest release Latest tag Created Updated License NPM Version

Table of content

Installation

NodeJS module to automatically load migrations and other queries using MySQL node instance Install NodeJS from the official website by downloading the version based on your O/S Then in your project workspace console run this command

npm install my-migration-sql

You'll install the package

Basic

const { migrations } = require('my-migration-sql');

let Migration = new migrations({
  Connection: MySQLConnection,
  saveOptions: {
    type: 'mysql',

  },
  path: 'migrations'
});

Migration.up();

This code'll use a MySQL instance to load all the migrations files in the folder called migrations using up method and save the migrated ones in a table in MySQL

Options

Costructor

my-migration-sql

OptionTypeExplanation
ConnectionMySQL instanceInstance of MySQL using createConnection method
pathStringPath to the migration folder, can be helpful using Node Path
saveOptionssaveOptions instance or objectOptions used to manage (save or remove) already migrated files

saveOptions

OptionTypeExplanation
typeStringType of saving method of migrated files.Available type:'mysql'Default: 'mysql'
ConnectionMySQL instanceSame instance of MySQL from the migrations, used to make queries to the database
table_metaStringonly mysql type, assign custom name to the table where saving migrations.Default: 'mysql-meta'

Methods

my-migration-sql

MethodReturn typeAccepted parameterWhat does it do?
upPromisemigration_name or [migration_names]Execute and load migration_name or migration_names present in the migration path to the database and save the changes. If none migration_name used the method'll load all pending migrations
downnonemigration_nameDowngrade and unload migration_name or migration_names present in the database. If none migration_name used the method'll unload all migrations (TODO)
pendingnonenoneGet the migrations that are still not loaded to the database

saveOptions

MethodReturn typeAccepted parameterWhat does it do?
getAllMigratedPromisenoneReturn all migrated files to the database
loadnonemigration_nameSave the migration_name as migrated to the storage option
unloadnonemigration_nameRemove the migration_name from the storage option
isValidBooleannoneReturn true if the storageOption is valid to operate

Migration generate tool

It's also available a tool that can generate a new migration file, by running

node node_modules/my-migration-sql/src/generateMigration.js

you'll create a new migration file called default_name in the folder migrations. To customize the options of the file you can use add some args to the command | Args | What does? | |---------------------|---------------------| | name | Customize the name of the migration. Default: 'default_name' | | path | Customize the path where save the migration. Default: 'migrations' |

For example:

node node_modules/my-migration-sql/src/generateMigration.js --name=ExampleName --path=./example/test_migration 

Create a new migration called ExampleName and save into the folder ./example/test_migration

This is just a demo for this feature because it would be much better have a much cleaner command to run.

IMPORTANT Due to the requirement of node_modules folder if you have a dockerized application you have to run npm install anyway to run the use command

REMEMBER For now in the down and in the up variable is just allowed use a String to declare the query

Other information

https://nodei.co/npm/my-migration-sql.png?downloads=true&downloadRank=true&stars=true

Issues and Feature

If you have an idea for a new feature or you found a bug please report it in the issues section

Contributors

Contributors