https://github.com/helloakn/mysql-migrator
npm data migration and data seeding package for mysql, mariadb
https://github.com/helloakn/mysql-migrator
data-seeding migration mysql-data-seeder mysql-migrations nodejs nodejs-migration
Last synced: about 1 month ago
JSON representation
npm data migration and data seeding package for mysql, mariadb
- Host: GitHub
- URL: https://github.com/helloakn/mysql-migrator
- Owner: helloakn
- License: mit
- Created: 2022-03-30T15:34:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-08T21:13:09.000Z (about 3 years ago)
- Last Synced: 2025-04-09T22:51:10.767Z (about 1 month ago)
- Topics: data-seeding, migration, mysql-data-seeder, mysql-migrations, nodejs, nodejs-migration
- Language: JavaScript
- Homepage:
- Size: 1 MB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mysql-migrator
npm migration package for mysql, mariadb[](https://github.com/helloakn/mysql-migrator) [](https://github.com/helloakn/mysql-migrator) [](https://github.com/helloakn/mysql-migrator)
[](https://github.com/feross/standard)## Table Of contents
- Installation
- Setup
- FOR ES6
- FOR CommonJs
- Update package.json
- Table Migration
- create migration
- code sample for table migration file
- run migration file
- up
- rollback
- Data Seeding
- create seeding
- code sample for data seeding file
- run seeding file
- up
- rollback### Installation
```shell
npm install mysql-migrator
```
### Setup
create **migrator.js** with the following code.
#### FOR ES6
```javascript
import { Migrator, Output } from 'mysql-migrator'import path from 'path'
import { fileURLToPath } from 'url'const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)const dbConfig = {
host: 'localhost',
user: 'your-user-name',
port: 3306,
password: 'your-password',
database: 'your-database'
}
const migrationsPath = __dirname + '/migrations'
const migrator = new Migrator(dbConfig, migrationsPath)
const result = await migrator.init()
Output(result)
process.exit()```
#### FOR CommonJs
```javascript
const { Migrator, Output } = require('mysql-migrator')const dbConfig = {
host: 'localhost',
user: 'your-user-name',
port: 3306,
password: 'your-password',
database: 'your-database'
}
const migrationsPath = __dirname + '/migrations'
const migrator = new Migrator(dbConfig, migrationsPath)
const result = await migrator.init()
Output(result)
process.exit()```
#### Update package.json
```
...
"scripts": {
...
"migrate": "node migrator.js"
},
...
```## Table Migration
### create migration file
```shell
node migrator.js migration:create create_table_user
```
or
```shell
npm run migrate migration:create create_table_user
```
### code sample for table migration file
there are two function **up** and **rollback** functions.
- **up** is for upgrading
- **rollback** is for downgrading. to use this function when you need to roll back to previous batch.
```shell
//write sql statement to create or modify
module.exports = {
up: async (tbl) => {
return await tbl.create('tblUser', {
id: 'int NOT NULL PRIMARY KEY AUTO_INCREMENT',
name: 'varchar(254) NOT NULL'
})
},
rollback: async (tbl) => {
return await tbl.dropTable('tblUser')
}
}```
### run migration file
#### up
```shell
node migrator.js migration:up
```
or
```shell
npm run migrate migration:up
```
#### rollback
```shell
node migrator.js migration:rollback
```
or
```shell
npm run migrate migration:rollback
```## Data Seeding
### create seeding file
```shell
node migrator.js seeding:create user_data_seeding
```
or```shell
npm run migrate seeding:create user_data_seeding
```
### code sample for data seeding file
there are two function **up** and **rollback** functions.
- **up** is for upgrading
- **rollback** is for downgrading. to use this function when you need to roll back to previous batch.
```javascript
//write sql statement to create or modify
module.exports = {
up: async (_query) => {
const queryString = 'INSERT INTO tblUser VALUES(1,"hello")'
await _query(queryString)
},
rollback: async (_query) => {
const queryString = 'TRUNCATE TABLE tblUser'
await _query(queryString)
}
}```
### run seeding file
#### up
```shell
node migrator.js seeding:up
```
or
```shell
npm run migrate seeding:up
```
#### rollback
```shell
node migrator.js seeding:rollback
```
or
```shell
npm run migrate seeding:rollback
```Thank You for Visiting to my repo. :)