@axiosleo/orm-mysql 0.12.3 → 0.13.1
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 +34 -13
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.1",
|
|
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
|
@@ -9,7 +9,7 @@ const { _execSQL } = require('./core');
|
|
|
9
9
|
const { _render } = require('@axiosleo/cli-tool/src/helper/str');
|
|
10
10
|
const { _foreach } = require('@axiosleo/cli-tool/src/helper/cmd');
|
|
11
11
|
const { _assign } = require('@axiosleo/cli-tool/src/helper/obj');
|
|
12
|
-
const { TransactionHandler } = require('
|
|
12
|
+
const { TransactionHandler } = require('./transaction');
|
|
13
13
|
const { ManageSQLBuilder, Builder } = require('./builder');
|
|
14
14
|
|
|
15
15
|
const migrationColumns = [
|
|
@@ -81,7 +81,13 @@ async function init(context) {
|
|
|
81
81
|
context.pool = conn;
|
|
82
82
|
|
|
83
83
|
handler = new QueryHandler(conn);
|
|
84
|
+
context.items = {};
|
|
84
85
|
if (await handler.existTable(context.task_key, database)) {
|
|
86
|
+
printer.yellow('table ' + context.task_key + ' already exists').println();
|
|
87
|
+
const items = await handler.table(context.task_key).select();
|
|
88
|
+
items.forEach(item => {
|
|
89
|
+
context.items[item.filename] = true;
|
|
90
|
+
});
|
|
85
91
|
conn.end();
|
|
86
92
|
return;
|
|
87
93
|
}
|
|
@@ -97,8 +103,7 @@ async function init(context) {
|
|
|
97
103
|
let res = await _execSQL(conn, builder.sql);
|
|
98
104
|
conn.end();
|
|
99
105
|
if (res.serverStatus !== 2 && res.serverStatus !== 16386) {
|
|
100
|
-
|
|
101
|
-
process.exit(1);
|
|
106
|
+
throw new Error('create migration table failed.');
|
|
102
107
|
}
|
|
103
108
|
}
|
|
104
109
|
|
|
@@ -221,12 +226,12 @@ function _initMigration(file, queries = {}) {
|
|
|
221
226
|
});
|
|
222
227
|
|
|
223
228
|
Object.defineProperty(migration, 'createForeignKey', {
|
|
224
|
-
value: function (options = {}) {
|
|
229
|
+
value: function (table, options = {}) {
|
|
225
230
|
_assign(options, {
|
|
226
231
|
operator: 'create',
|
|
227
232
|
target: 'foreignKey',
|
|
228
|
-
name: options.foreignKey ? options.foreignKey : 'fk_' +
|
|
229
|
-
table:
|
|
233
|
+
name: options.foreignKey ? options.foreignKey : 'fk_' + table + '_' + options.columnName,
|
|
234
|
+
table: table,
|
|
230
235
|
column: options.columnName,
|
|
231
236
|
reference: options.reference
|
|
232
237
|
});
|
|
@@ -247,7 +252,7 @@ function _initMigration(file, queries = {}) {
|
|
|
247
252
|
});
|
|
248
253
|
|
|
249
254
|
Object.defineProperty(migration, 'dropColumn', {
|
|
250
|
-
value: function (
|
|
255
|
+
value: function (table, name) {
|
|
251
256
|
const builder = new ManageSQLBuilder({
|
|
252
257
|
operator: 'drop',
|
|
253
258
|
target: 'column',
|
|
@@ -259,10 +264,22 @@ function _initMigration(file, queries = {}) {
|
|
|
259
264
|
});
|
|
260
265
|
|
|
261
266
|
Object.defineProperty(migration, 'dropIndex', {
|
|
262
|
-
value: function (
|
|
267
|
+
value: function (table, columns) {
|
|
263
268
|
const builder = new ManageSQLBuilder({
|
|
264
269
|
operator: 'drop',
|
|
265
270
|
target: 'index',
|
|
271
|
+
columns,
|
|
272
|
+
table
|
|
273
|
+
});
|
|
274
|
+
queries[file].push({ sql: builder.sql, values: builder.values });
|
|
275
|
+
}, ...baseAttr
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
Object.defineProperty(migration, 'dropIndexWithName', {
|
|
279
|
+
value: function (table, name) {
|
|
280
|
+
const builder = new ManageSQLBuilder({
|
|
281
|
+
operator: 'drop',
|
|
282
|
+
target: 'indexWithName',
|
|
266
283
|
name,
|
|
267
284
|
table
|
|
268
285
|
});
|
|
@@ -271,7 +288,7 @@ function _initMigration(file, queries = {}) {
|
|
|
271
288
|
});
|
|
272
289
|
|
|
273
290
|
Object.defineProperty(migration, 'dropForeignKey', {
|
|
274
|
-
value: function (
|
|
291
|
+
value: function (table, name) {
|
|
275
292
|
const builder = new ManageSQLBuilder({
|
|
276
293
|
operator: 'drop',
|
|
277
294
|
target: 'foreignKey',
|
|
@@ -310,6 +327,12 @@ async function run(context) {
|
|
|
310
327
|
const { files } = context;
|
|
311
328
|
const queries = {};
|
|
312
329
|
await _foreach(files, async (file) => {
|
|
330
|
+
if (context.action === 'up' && context.items[file]) {
|
|
331
|
+
printer.yellow(`Migration file "${file}" has been migrated.`).println();
|
|
332
|
+
return;
|
|
333
|
+
} else if (context.action === 'down' && !context.items[file]) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
313
336
|
const scriptPath = path.join(context.config.dir, file);
|
|
314
337
|
const script = require(scriptPath);
|
|
315
338
|
queries[file] = [];
|
|
@@ -319,16 +342,14 @@ async function run(context) {
|
|
|
319
342
|
switch (context.action) {
|
|
320
343
|
case 'up': {
|
|
321
344
|
if (typeof script.up !== 'function') {
|
|
322
|
-
|
|
323
|
-
process.exit(1);
|
|
345
|
+
throw new Error(`Migration file "${file}" must have a function named up.`);
|
|
324
346
|
}
|
|
325
347
|
await script.up(migration);
|
|
326
348
|
break;
|
|
327
349
|
}
|
|
328
350
|
case 'down': {
|
|
329
351
|
if (typeof script.down !== 'function') {
|
|
330
|
-
|
|
331
|
-
process.exit(1);
|
|
352
|
+
throw new Error(`Migration file "${file}" must have a function named down.`);
|
|
332
353
|
}
|
|
333
354
|
await script.down(migration);
|
|
334
355
|
break;
|