From 3d387362daa6c0a9c514b3be287639634783afac Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 14:49:52 +0800 Subject: [PATCH 1/6] feat(migration): add migrate function for handling database migrations with improved options and refactor migration command to utilize new utility --- commands/migrate.js | 29 ++++------------------------- index.d.ts | 9 +++++++++ index.js | 38 +++++++++++++++++++++++++++++++++++--- package.json | 2 +- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/commands/migrate.js b/commands/migrate.js index b481194..0adbe8f 100644 --- a/commands/migrate.js +++ b/commands/migrate.js @@ -1,8 +1,7 @@ 'use strict'; -const { Command, debug, Workflow } = require('@axiosleo/cli-tool'); -const is = require('@axiosleo/cli-tool/src/helper/is'); -const migration = require('../src/migration'); +const { Command, debug } = require('@axiosleo/cli-tool'); +const { migrate } = require('../index'); class MigrateCommand extends Command { constructor() { @@ -26,31 +25,11 @@ class MigrateCommand extends Command { * @param {*} options */ async exec(args, options) { - const workflow = new Workflow(migration); try { - await workflow.start({ - task_key: 'migrate_logs', - action: args.action, - config: { - dir: args.dir - }, - connection: { - host: options.host, - port: is.number(options.port) ? - options.port : parseInt(options.port), - user: options.user, - password: options.pass, - database: options.db - }, - debug: options.debug - }); + await migrate(args.action, args.dir, options); process.exit(0); } catch (e) { - if (e.curr && e.curr.error) { - debug.error(e.curr.error); - } else { - debug.log(e); - } + debug.error(e); process.exit(1); } } diff --git a/index.d.ts b/index.d.ts index 5c7bd94..a64a8e2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -661,3 +661,12 @@ export declare class MigrationInterface { raw(sql: string, values: any[]): void; } + +export function migrate(action: 'up' | 'down' | 'UP' | 'DOWN', dir: string, options?: { + host?: string, + port?: number, + user?: string, + password?: string, + database?: string, + debug?: boolean +}): Promise; diff --git a/index.js b/index.js index caaed0a..37209b1 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,11 @@ 'use strict'; +const { Workflow } = require('@axiosleo/cli-tool'); +const is = require('@axiosleo/cli-tool/src/helper/is'); +const Hook = require('./src/hook'); +const { Builder } = require('./src/builder'); +const migration = require('./src/migration'); + const { QueryHandler, QueryOperator, @@ -21,8 +27,33 @@ const { MySQLClient } = require('./src/client'); -const Hook = require('./src/hook'); -const { Builder } = require('./src/builder'); +const _runMigration = async (action, dir, options = {}) => { + const workflow = new Workflow(migration); + try { + await workflow.start({ + task_key: 'migrate_logs', + action: action, + config: { + dir: dir + }, + connection: { + host: options.host, + port: is.number(options.port) ? + options.port : parseInt(options.port), + user: options.user, + password: options.pass, + database: options.db + }, + debug: options.debug + }); + } catch (e) { + if (e.curr && e.curr.error) { + throw e.curr.error; + } else { + throw e; + } + } +}; module.exports = { Hook, @@ -40,5 +71,6 @@ module.exports = { getClient, createPool, createClient, - createPromiseClient + createPromiseClient, + migrate: _runMigration }; diff --git a/package.json b/package.json index 49f9251..96f9a18 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "test": "mocha --reporter spec --timeout 3000 tests/*.tests.js", "test-cov": "nyc -r=lcov -r=html -r=text -r=json mocha -t 10000 -R spec tests/*.tests.js", "test-one": "mocha --reporter spec --timeout 3000 ", + "build": "DEPLOY_ENV=prod bun build ./index.js --compile --outfile dist/orm-mysql", "feature-test": "node tests/transaction.ft.js", "setup-feature-db": "node tests/setup-feature-db.js", "feature-test:local": "docker compose up -d && sleep 10 && npm run setup-feature-db && npm run feature-test && docker compose down", @@ -48,7 +49,6 @@ "mocha-sinon": "^2.1.2", "nyc": "^15.1.0", "pre-commit": "^1.2.2", - "sinon": "^17.0.1", "typescript": "^5.3.3" }, "pre-commit": { From a49d5bca48e68d57e1c67218bc0f7326fec410da Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 14:50:16 +0800 Subject: [PATCH 2/6] chore(workspace): add pnpm workspace configuration with ignored and only built dependencies --- pnpm-workspace.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 pnpm-workspace.yaml diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..dacb4a5 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,5 @@ +ignoredBuiltDependencies: + - pre-commit + +onlyBuiltDependencies: + - spawn-sync From 6d043218c23e2a1ea98310ba22068fdbcf29bc09 Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 14:51:44 +0800 Subject: [PATCH 3/6] feat(types): define MigrateAction and MigrateOptions types for improved migration function clarity and documentation --- index.d.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index a64a8e2..9d31983 100644 --- a/index.d.ts +++ b/index.d.ts @@ -662,11 +662,20 @@ export declare class MigrationInterface { raw(sql: string, values: any[]): void; } -export function migrate(action: 'up' | 'down' | 'UP' | 'DOWN', dir: string, options?: { +export type MigrateAction = 'up' | 'down' | 'UP' | 'DOWN'; +export type MigrateOptions = { host?: string, port?: number, user?: string, password?: string, database?: string, debug?: boolean -}): Promise; +}; + +/** + * migrate database + * @param action + * @param dir + * @param options + */ +export function migrate(action: MigrateAction, dir: string, options?: MigrateOptions): Promise; From 7f7c60fbd8fa7673dda8b15da5313db566d1637e Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 15:25:12 +0800 Subject: [PATCH 4/6] fix(migration): normalize action string to lowercase and update password/database options in migrate command --- commands/migrate.js | 9 ++++++++- index.js | 6 +++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/commands/migrate.js b/commands/migrate.js index 0adbe8f..d5fba01 100644 --- a/commands/migrate.js +++ b/commands/migrate.js @@ -26,7 +26,14 @@ class MigrateCommand extends Command { */ async exec(args, options) { try { - await migrate(args.action, args.dir, options); + await migrate(args.action, args.dir, { + host: options.host, + port: options.port, + user: options.user, + password: options.pass, + database: options.db, + debug: options.debug + }); process.exit(0); } catch (e) { debug.error(e); diff --git a/index.js b/index.js index 37209b1..7b0306c 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ const _runMigration = async (action, dir, options = {}) => { try { await workflow.start({ task_key: 'migrate_logs', - action: action, + action: action.toLowerCase(), config: { dir: dir }, @@ -41,8 +41,8 @@ const _runMigration = async (action, dir, options = {}) => { port: is.number(options.port) ? options.port : parseInt(options.port), user: options.user, - password: options.pass, - database: options.db + password: options.password, + database: options.database }, debug: options.debug }); From ef8dd68e98e23572d18aed26c79d968c4faa6aed Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 15:36:25 +0800 Subject: [PATCH 5/6] fix(migration): set default values for database connection options in migrate command --- commands/migrate.js | 4 +++- index.js | 12 +++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/commands/migrate.js b/commands/migrate.js index d5fba01..fda9cea 100644 --- a/commands/migrate.js +++ b/commands/migrate.js @@ -2,6 +2,7 @@ const { Command, debug } = require('@axiosleo/cli-tool'); const { migrate } = require('../index'); +const is = require('@axiosleo/cli-tool/src/helper/is'); class MigrateCommand extends Command { constructor() { @@ -28,7 +29,8 @@ class MigrateCommand extends Command { try { await migrate(args.action, args.dir, { host: options.host, - port: options.port, + port: is.string(options.port) ? + parseInt(options.port) : options.port || 3306, user: options.user, password: options.pass, database: options.db, diff --git a/index.js b/index.js index 7b0306c..ee81d35 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ 'use strict'; const { Workflow } = require('@axiosleo/cli-tool'); -const is = require('@axiosleo/cli-tool/src/helper/is'); const Hook = require('./src/hook'); const { Builder } = require('./src/builder'); const migration = require('./src/migration'); @@ -37,12 +36,11 @@ const _runMigration = async (action, dir, options = {}) => { dir: dir }, connection: { - host: options.host, - port: is.number(options.port) ? - options.port : parseInt(options.port), - user: options.user, - password: options.password, - database: options.database + host: options.host || 'localhost', + port: options.port || 3306, + user: options.user || 'root', + password: options.password || '', + database: options.database || '' }, debug: options.debug }); From 5b3457c40b990b8f592d7bbc378b31a33df88a03 Mon Sep 17 00:00:00 2001 From: axiosleo Date: Fri, 6 Feb 2026 15:49:56 +0800 Subject: [PATCH 6/6] refactor(migration): replace process.exit with error throwing for better error handling in migration functions --- src/migration.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/migration.js b/src/migration.js index 0635ec2..8f65925 100644 --- a/src/migration.js +++ b/src/migration.js @@ -97,8 +97,7 @@ async function init(context) { let res = await _execSQL(conn, builder.sql); conn.end(); if (res.serverStatus !== 2 && res.serverStatus !== 16386) { - printer.error('create migration table failed.'); - process.exit(1); + throw new Error('create migration table failed.'); } } @@ -319,16 +318,14 @@ async function run(context) { switch (context.action) { case 'up': { if (typeof script.up !== 'function') { - printer.error(`Migration file "${file}" must have a function named up.`); - process.exit(1); + throw new Error(`Migration file "${file}" must have a function named up.`); } await script.up(migration); break; } case 'down': { if (typeof script.down !== 'function') { - printer.error(`Migration file "${file}" must have a function named down.`); - process.exit(1); + throw new Error(`Migration file "${file}" must have a function named down.`); } await script.down(migration); break;