{"id":21523998,"url":"https://github.com/helloakn/mysql-migrator","last_synced_at":"2025-04-09T22:51:12.980Z","repository":{"id":57687626,"uuid":"475943141","full_name":"helloakn/mysql-migrator","owner":"helloakn","description":"npm data migration and data seeding package for mysql, mariadb","archived":false,"fork":false,"pushed_at":"2022-05-08T21:13:09.000Z","size":1050,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T22:51:10.767Z","etag":null,"topics":["data-seeding","migration","mysql-data-seeder","mysql-migrations","nodejs","nodejs-migration"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/helloakn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-30T15:34:33.000Z","updated_at":"2024-01-19T18:49:03.000Z","dependencies_parsed_at":"2022-08-25T20:21:45.908Z","dependency_job_id":null,"html_url":"https://github.com/helloakn/mysql-migrator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloakn%2Fmysql-migrator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloakn%2Fmysql-migrator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloakn%2Fmysql-migrator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloakn%2Fmysql-migrator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helloakn","download_url":"https://codeload.github.com/helloakn/mysql-migrator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125643,"owners_count":21051766,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-seeding","migration","mysql-data-seeder","mysql-migrations","nodejs","nodejs-migration"],"created_at":"2024-11-24T01:20:14.683Z","updated_at":"2025-04-09T22:51:12.960Z","avatar_url":"https://github.com/helloakn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv id=\"top\"\u003e\u003c/div\u003e \n\n# mysql-migrator\nnpm migration package for mysql, mariadb  \n\n[![Star Count](https://img.shields.io/badge/dynamic/json?color=brightgreen\u0026label=Star\u0026query=stargazers_count\u0026url=https%3A%2F%2Fapi.github.com%2Frepos%2Fhelloakn%2Fmysql-migrator)](https://github.com/helloakn/mysql-migrator) [![Licence](https://img.shields.io/badge/dynamic/json?color=informational\u0026label=LICENCE\u0026query=license.name\u0026url=https%3A%2F%2Fapi.github.com%2Frepos%2Fhelloakn%2Fmysql-migrator)](https://github.com/helloakn/mysql-migrator) [![Language](https://img.shields.io/badge/dynamic/json?color=blueviolet\u0026label=Language\u0026query=language\u0026url=https%3A%2F%2Fapi.github.com%2Frepos%2Fhelloakn%2Fmysql-migrator)](https://github.com/helloakn/mysql-migrator)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)\n\n## Table Of contents\n- Installation \n- Setup\n  - FOR ES6\n  - FOR CommonJs\n  - Update package.json\n- Table Migration\n\t- create migration \n\t- code sample for table migration file\n\t- run migration file\n\t\t- up\n\t\t- rollback\n- Data Seeding\n\t- create seeding \n\t- code sample for data seeding file\n\t- run seeding file\n\t\t- up\n\t\t- rollback\n\n### Installation\n```shell\nnpm install mysql-migrator\n```\n### Setup\ncreate **migrator.js** with the following code.\n#### FOR ES6\n```javascript\nimport { Migrator, Output } from 'mysql-migrator'\n\nimport path from 'path'\nimport { fileURLToPath } from 'url'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = path.dirname(__filename)\n\nconst dbConfig = {\n  host: 'localhost',\n  user: 'your-user-name',\n  port: 3306,\n  password: 'your-password',\n  database: 'your-database'\n}\nconst migrationsPath = __dirname + '/migrations'\nconst migrator = new Migrator(dbConfig, migrationsPath)\nconst result = await migrator.init()\nOutput(result)\nprocess.exit()\n\n```\n#### FOR CommonJs\n```javascript\nconst { Migrator, Output } = require('mysql-migrator')\n\nconst dbConfig = {\n  host: 'localhost',\n  user: 'your-user-name',\n  port: 3306,\n  password: 'your-password',\n  database: 'your-database'\n}\nconst migrationsPath = __dirname + '/migrations'\nconst migrator = new Migrator(dbConfig, migrationsPath)\nconst result = await migrator.init()\nOutput(result)\nprocess.exit()\n\n```\n#### Update package.json\n```\n...\n  \"scripts\": {\n    ...\n    \"migrate\": \"node migrator.js\"\n  },\n...\n```\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n## Table Migration\n### create migration file\n```shell\nnode migrator.js migration:create create_table_user\n```\nor\n```shell\nnpm run migrate migration:create create_table_user\n```\n### code sample for table migration file\nthere are two function **up** and **rollback** functions.\n- **up** is for upgrading\n- **rollback** is for downgrading. to use this function when you need to roll back to previous batch.\n ```shell\n//write sql statement to create or modify\nmodule.exports = {\n  up: async (tbl) =\u003e {\n    return await tbl.create('tblUser', {\n      id: 'int NOT NULL PRIMARY KEY AUTO_INCREMENT',\n      name: 'varchar(254) NOT NULL'\n    })\n  },\n  rollback: async (tbl) =\u003e {\n    return await tbl.dropTable('tblUser')\n  }\n}\n\n```\n### run migration file\n#### up\n```shell\nnode migrator.js migration:up\n```\nor\n```shell\nnpm run migrate migration:up\n```\n#### rollback\n```shell\nnode migrator.js migration:rollback\n```\nor\n```shell\nnpm run migrate migration:rollback\n```\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\n## Data Seeding \n### create seeding file\n```shell\nnode migrator.js seeding:create user_data_seeding\n```\nor\n\n```shell\nnpm run migrate seeding:create user_data_seeding\n```\n### code sample for data seeding file\nthere are two function **up** and **rollback** functions.\n- **up** is for upgrading\n- **rollback** is for downgrading. to use this function when you need to roll back to previous batch.\n ```javascript\n//write sql statement to create or modify\nmodule.exports = {\n  up: async (_query) =\u003e {\n    const queryString = 'INSERT INTO tblUser VALUES(1,\"hello\")'\n    await _query(queryString)\n  },\n  rollback: async (_query) =\u003e {\n    const queryString = 'TRUNCATE TABLE tblUser'\n    await _query(queryString)\n  }\n}\n\n```\n\n### run seeding file\n#### up\n```shell\nnode migrator.js seeding:up\n```\nor\n```shell\nnpm run migrate seeding:up\n```\n#### rollback\n```shell\nnode migrator.js seeding:rollback\n```\nor\n```shell\nnpm run migrate seeding:rollback\n```\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\nThank You for Visiting to my repo. :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelloakn%2Fmysql-migrator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelloakn%2Fmysql-migrator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelloakn%2Fmysql-migrator/lists"}