@axiosleo/orm-mysql 0.12.2 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/orm-mysql.js +1 -1
- package/commands/migrate.js +10 -22
- package/dist/orm-mysql +0 -0
- package/index.d.ts +43 -5
- package/index.js +33 -3
- package/package.json +2 -2
- package/pnpm-workspace.yaml +5 -0
- package/src/builder.js +9 -0
- package/src/migration.js +21 -12
package/bin/orm-mysql.js
CHANGED
package/commands/migrate.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { Command, debug
|
|
3
|
+
const { Command, debug } = require('@axiosleo/cli-tool');
|
|
4
|
+
const { migrate } = require('../index');
|
|
4
5
|
const is = require('@axiosleo/cli-tool/src/helper/is');
|
|
5
|
-
const migration = require('../src/migration');
|
|
6
6
|
|
|
7
7
|
class MigrateCommand extends Command {
|
|
8
8
|
constructor() {
|
|
@@ -26,31 +26,19 @@ class MigrateCommand extends Command {
|
|
|
26
26
|
* @param {*} options
|
|
27
27
|
*/
|
|
28
28
|
async exec(args, options) {
|
|
29
|
-
const workflow = new Workflow(migration);
|
|
30
29
|
try {
|
|
31
|
-
await
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
host: options.host,
|
|
39
|
-
port: is.number(options.port) ?
|
|
40
|
-
options.port : parseInt(options.port),
|
|
41
|
-
user: options.user,
|
|
42
|
-
password: options.pass,
|
|
43
|
-
database: options.db
|
|
44
|
-
},
|
|
30
|
+
await migrate(args.action, args.dir, {
|
|
31
|
+
host: options.host,
|
|
32
|
+
port: is.string(options.port) ?
|
|
33
|
+
parseInt(options.port) : options.port || 3306,
|
|
34
|
+
user: options.user,
|
|
35
|
+
password: options.pass,
|
|
36
|
+
database: options.db,
|
|
45
37
|
debug: options.debug
|
|
46
38
|
});
|
|
47
39
|
process.exit(0);
|
|
48
40
|
} catch (e) {
|
|
49
|
-
|
|
50
|
-
debug.error(e.curr.error);
|
|
51
|
-
} else {
|
|
52
|
-
debug.log(e);
|
|
53
|
-
}
|
|
41
|
+
debug.error(e);
|
|
54
42
|
process.exit(1);
|
|
55
43
|
}
|
|
56
44
|
}
|
package/dist/orm-mysql
ADDED
|
Binary file
|
package/index.d.ts
CHANGED
|
@@ -637,9 +637,8 @@ export declare class MigrationInterface {
|
|
|
637
637
|
spatial?: boolean
|
|
638
638
|
}): void;
|
|
639
639
|
|
|
640
|
-
createForeignKey(options: {
|
|
640
|
+
createForeignKey(table: string, options: {
|
|
641
641
|
foreignKey?: string,
|
|
642
|
-
tableName: string,
|
|
643
642
|
columnName: string,
|
|
644
643
|
reference: {
|
|
645
644
|
tableName: string,
|
|
@@ -649,15 +648,54 @@ export declare class MigrationInterface {
|
|
|
649
648
|
}
|
|
650
649
|
}): void;
|
|
651
650
|
|
|
651
|
+
/**
|
|
652
|
+
* drop table from database
|
|
653
|
+
* @param tableName
|
|
654
|
+
*/
|
|
652
655
|
dropTable(tableName: string): void;
|
|
653
656
|
|
|
654
|
-
|
|
657
|
+
/**
|
|
658
|
+
* drop column from table
|
|
659
|
+
* @param columnName
|
|
660
|
+
* @param tableName
|
|
661
|
+
*/
|
|
662
|
+
dropColumn(tableName: string, columnName: string): void;
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* drop index from table
|
|
666
|
+
* @param indexName
|
|
667
|
+
* @param tableName
|
|
668
|
+
*/
|
|
669
|
+
dropIndex(tableName: string, columns: string[]): void;
|
|
655
670
|
|
|
656
|
-
|
|
671
|
+
dropIndexWithName(tableName: string, indexName: string): void;
|
|
657
672
|
|
|
658
|
-
|
|
673
|
+
/**
|
|
674
|
+
* drop foreign key from table
|
|
675
|
+
* @param foreign_key
|
|
676
|
+
* @param tableName
|
|
677
|
+
*/
|
|
678
|
+
dropForeignKey(tableName: string, foreign_key: string): void;
|
|
659
679
|
|
|
660
680
|
insertData(table: string, data: any[]): void;
|
|
661
681
|
|
|
662
682
|
raw(sql: string, values: any[]): void;
|
|
663
683
|
}
|
|
684
|
+
|
|
685
|
+
export type MigrateAction = 'up' | 'down' | 'UP' | 'DOWN';
|
|
686
|
+
export type MigrateOptions = {
|
|
687
|
+
host?: string,
|
|
688
|
+
port?: number,
|
|
689
|
+
user?: string,
|
|
690
|
+
password?: string,
|
|
691
|
+
database?: string,
|
|
692
|
+
debug?: boolean
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* migrate database
|
|
697
|
+
* @param action
|
|
698
|
+
* @param dir
|
|
699
|
+
* @param options
|
|
700
|
+
*/
|
|
701
|
+
export function migrate(action: MigrateAction, dir: string, options?: MigrateOptions): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { Workflow } = require('@axiosleo/cli-tool');
|
|
4
|
+
const Hook = require('./src/hook');
|
|
5
|
+
const { Builder } = require('./src/builder');
|
|
6
|
+
const migration = require('./src/migration');
|
|
7
|
+
|
|
3
8
|
const {
|
|
4
9
|
QueryHandler,
|
|
5
10
|
QueryOperator,
|
|
@@ -21,8 +26,32 @@ const {
|
|
|
21
26
|
MySQLClient
|
|
22
27
|
} = require('./src/client');
|
|
23
28
|
|
|
24
|
-
const
|
|
25
|
-
const
|
|
29
|
+
const _runMigration = async (action, dir, options = {}) => {
|
|
30
|
+
const workflow = new Workflow(migration);
|
|
31
|
+
try {
|
|
32
|
+
await workflow.start({
|
|
33
|
+
task_key: 'migrate_logs',
|
|
34
|
+
action: action.toLowerCase(),
|
|
35
|
+
config: {
|
|
36
|
+
dir: dir
|
|
37
|
+
},
|
|
38
|
+
connection: {
|
|
39
|
+
host: options.host || 'localhost',
|
|
40
|
+
port: options.port || 3306,
|
|
41
|
+
user: options.user || 'root',
|
|
42
|
+
password: options.password || '',
|
|
43
|
+
database: options.database || ''
|
|
44
|
+
},
|
|
45
|
+
debug: options.debug
|
|
46
|
+
});
|
|
47
|
+
} catch (e) {
|
|
48
|
+
if (e.curr && e.curr.error) {
|
|
49
|
+
throw e.curr.error;
|
|
50
|
+
} else {
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
26
55
|
|
|
27
56
|
module.exports = {
|
|
28
57
|
Hook,
|
|
@@ -40,5 +69,6 @@ module.exports = {
|
|
|
40
69
|
getClient,
|
|
41
70
|
createPool,
|
|
42
71
|
createClient,
|
|
43
|
-
createPromiseClient
|
|
72
|
+
createPromiseClient,
|
|
73
|
+
migrate: _runMigration
|
|
44
74
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiosleo/orm-mysql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "MySQL ORM tool",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mysql",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"test": "mocha --reporter spec --timeout 3000 tests/*.tests.js",
|
|
19
19
|
"test-cov": "nyc -r=lcov -r=html -r=text -r=json mocha -t 10000 -R spec tests/*.tests.js",
|
|
20
20
|
"test-one": "mocha --reporter spec --timeout 3000 ",
|
|
21
|
+
"build": "DEPLOY_ENV=prod bun build ./index.js --compile --outfile dist/orm-mysql",
|
|
21
22
|
"feature-test": "node tests/transaction.ft.js",
|
|
22
23
|
"setup-feature-db": "node tests/setup-feature-db.js",
|
|
23
24
|
"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 @@
|
|
|
48
49
|
"mocha-sinon": "^2.1.2",
|
|
49
50
|
"nyc": "^15.1.0",
|
|
50
51
|
"pre-commit": "^1.2.2",
|
|
51
|
-
"sinon": "^17.0.1",
|
|
52
52
|
"typescript": "^5.3.3"
|
|
53
53
|
},
|
|
54
54
|
"pre-commit": {
|
package/src/builder.js
CHANGED
|
@@ -622,6 +622,15 @@ class ManageSQLBuilder extends Builder {
|
|
|
622
622
|
}
|
|
623
623
|
|
|
624
624
|
dropIndex(options) {
|
|
625
|
+
_validate(options, {
|
|
626
|
+
columns: 'required|array',
|
|
627
|
+
table: 'required|string',
|
|
628
|
+
});
|
|
629
|
+
options.name = 'idx_' + options.table + '_' + options.columns.join('_');
|
|
630
|
+
return _render('DROP INDEX `${name}` ON `${table}`', options);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
dropIndexWithName(options) {
|
|
625
634
|
_validate(options, {
|
|
626
635
|
name: 'required|string',
|
|
627
636
|
table: 'required|string',
|
package/src/migration.js
CHANGED
|
@@ -97,8 +97,7 @@ async function init(context) {
|
|
|
97
97
|
let res = await _execSQL(conn, builder.sql);
|
|
98
98
|
conn.end();
|
|
99
99
|
if (res.serverStatus !== 2 && res.serverStatus !== 16386) {
|
|
100
|
-
|
|
101
|
-
process.exit(1);
|
|
100
|
+
throw new Error('create migration table failed.');
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
103
|
|
|
@@ -221,12 +220,12 @@ function _initMigration(file, queries = {}) {
|
|
|
221
220
|
});
|
|
222
221
|
|
|
223
222
|
Object.defineProperty(migration, 'createForeignKey', {
|
|
224
|
-
value: function (options = {}) {
|
|
223
|
+
value: function (table, options = {}) {
|
|
225
224
|
_assign(options, {
|
|
226
225
|
operator: 'create',
|
|
227
226
|
target: 'foreignKey',
|
|
228
|
-
name: options.foreignKey ? options.foreignKey : 'fk_' +
|
|
229
|
-
table:
|
|
227
|
+
name: options.foreignKey ? options.foreignKey : 'fk_' + table + '_' + options.columnName,
|
|
228
|
+
table: table,
|
|
230
229
|
column: options.columnName,
|
|
231
230
|
reference: options.reference
|
|
232
231
|
});
|
|
@@ -247,7 +246,7 @@ function _initMigration(file, queries = {}) {
|
|
|
247
246
|
});
|
|
248
247
|
|
|
249
248
|
Object.defineProperty(migration, 'dropColumn', {
|
|
250
|
-
value: function (
|
|
249
|
+
value: function (table, name) {
|
|
251
250
|
const builder = new ManageSQLBuilder({
|
|
252
251
|
operator: 'drop',
|
|
253
252
|
target: 'column',
|
|
@@ -259,10 +258,22 @@ function _initMigration(file, queries = {}) {
|
|
|
259
258
|
});
|
|
260
259
|
|
|
261
260
|
Object.defineProperty(migration, 'dropIndex', {
|
|
262
|
-
value: function (
|
|
261
|
+
value: function (table, columns) {
|
|
263
262
|
const builder = new ManageSQLBuilder({
|
|
264
263
|
operator: 'drop',
|
|
265
264
|
target: 'index',
|
|
265
|
+
columns,
|
|
266
|
+
table
|
|
267
|
+
});
|
|
268
|
+
queries[file].push({ sql: builder.sql, values: builder.values });
|
|
269
|
+
}, ...baseAttr
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
Object.defineProperty(migration, 'dropIndexWithName', {
|
|
273
|
+
value: function (table, name) {
|
|
274
|
+
const builder = new ManageSQLBuilder({
|
|
275
|
+
operator: 'drop',
|
|
276
|
+
target: 'indexWithName',
|
|
266
277
|
name,
|
|
267
278
|
table
|
|
268
279
|
});
|
|
@@ -271,7 +282,7 @@ function _initMigration(file, queries = {}) {
|
|
|
271
282
|
});
|
|
272
283
|
|
|
273
284
|
Object.defineProperty(migration, 'dropForeignKey', {
|
|
274
|
-
value: function (
|
|
285
|
+
value: function (table, name) {
|
|
275
286
|
const builder = new ManageSQLBuilder({
|
|
276
287
|
operator: 'drop',
|
|
277
288
|
target: 'foreignKey',
|
|
@@ -319,16 +330,14 @@ async function run(context) {
|
|
|
319
330
|
switch (context.action) {
|
|
320
331
|
case 'up': {
|
|
321
332
|
if (typeof script.up !== 'function') {
|
|
322
|
-
|
|
323
|
-
process.exit(1);
|
|
333
|
+
throw new Error(`Migration file "${file}" must have a function named up.`);
|
|
324
334
|
}
|
|
325
335
|
await script.up(migration);
|
|
326
336
|
break;
|
|
327
337
|
}
|
|
328
338
|
case 'down': {
|
|
329
339
|
if (typeof script.down !== 'function') {
|
|
330
|
-
|
|
331
|
-
process.exit(1);
|
|
340
|
+
throw new Error(`Migration file "${file}" must have a function named down.`);
|
|
332
341
|
}
|
|
333
342
|
await script.down(migration);
|
|
334
343
|
break;
|