@axiosleo/orm-mysql 0.9.0 → 0.9.2
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/README.md +66 -2
- package/bin/orm-mysql.js +1 -1
- package/commands/generate.js +2 -1
- package/commands/migrate.js +2 -1
- package/examples/base_column.js +28 -0
- package/examples/migration/.connect.js +12 -5
- package/examples/migration/1706764048.init.js +118 -0
- package/package.json +1 -1
- package/src/builder.js +9 -3
- package/src/core.js +4 -0
- package/src/migration.js +3 -1
- package/examples/migration/20240101.create.table.js +0 -28
- package/examples/migration/20240102.1.create.column.js +0 -14
- package/examples/migration/20240102.2.create.index.js +0 -1
package/README.md
CHANGED
|
@@ -197,6 +197,10 @@ try {
|
|
|
197
197
|
|
|
198
198
|
### Migration
|
|
199
199
|
|
|
200
|
+
> [Migration examples](./examples/migration/).
|
|
201
|
+
|
|
202
|
+
- Migration script example
|
|
203
|
+
|
|
200
204
|
```javascript
|
|
201
205
|
'use strict';
|
|
202
206
|
|
|
@@ -204,14 +208,30 @@ try {
|
|
|
204
208
|
* @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
|
|
205
209
|
*/
|
|
206
210
|
function up(migration) {
|
|
207
|
-
migration.
|
|
211
|
+
migration.createTable('table1', {
|
|
212
|
+
field1: {
|
|
213
|
+
type: 'varchar',
|
|
214
|
+
length: 64,
|
|
215
|
+
allowNull: false,
|
|
216
|
+
uniqIndex: true
|
|
217
|
+
},
|
|
218
|
+
field2: {
|
|
219
|
+
type: 'VARCHAR',
|
|
220
|
+
allowNull: false
|
|
221
|
+
},
|
|
222
|
+
field3: {
|
|
223
|
+
type: 'VARCHAR',
|
|
224
|
+
comment: 'comment',
|
|
225
|
+
allowNull: false
|
|
226
|
+
},
|
|
227
|
+
});
|
|
208
228
|
}
|
|
209
229
|
|
|
210
230
|
/**
|
|
211
231
|
* @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
|
|
212
232
|
*/
|
|
213
233
|
function down(migration) {
|
|
214
|
-
migration.
|
|
234
|
+
migration.dropTable('table1');
|
|
215
235
|
}
|
|
216
236
|
|
|
217
237
|
module.exports = {
|
|
@@ -220,6 +240,50 @@ module.exports = {
|
|
|
220
240
|
};
|
|
221
241
|
```
|
|
222
242
|
|
|
243
|
+
- Generate migration script
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
orm-mysql generate -h
|
|
247
|
+
|
|
248
|
+
Usage:
|
|
249
|
+
|
|
250
|
+
generate [--] [name] <dir>
|
|
251
|
+
gen
|
|
252
|
+
|
|
253
|
+
Arguments:
|
|
254
|
+
|
|
255
|
+
*name Migration name
|
|
256
|
+
dir Migration scripts directory
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
- Run migration
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
orm-mysql migrate -h
|
|
263
|
+
|
|
264
|
+
Description:
|
|
265
|
+
|
|
266
|
+
Migrate database
|
|
267
|
+
|
|
268
|
+
Usage:
|
|
269
|
+
|
|
270
|
+
migrate [options] [--] [action] <dir>
|
|
271
|
+
|
|
272
|
+
Options:
|
|
273
|
+
|
|
274
|
+
-d, --debug [false] debug mode
|
|
275
|
+
--host [localhost] mysql host
|
|
276
|
+
--port [3306] port number to connect to the database
|
|
277
|
+
--user [root] username for connect to the database
|
|
278
|
+
--pass password to connect to the database
|
|
279
|
+
--db database name
|
|
280
|
+
|
|
281
|
+
Arguments:
|
|
282
|
+
|
|
283
|
+
*action up or down
|
|
284
|
+
dir migration directory
|
|
285
|
+
```
|
|
286
|
+
|
|
223
287
|
### Custom query driver
|
|
224
288
|
|
|
225
289
|
```javascript
|
package/bin/orm-mysql.js
CHANGED
package/commands/generate.js
CHANGED
|
@@ -9,7 +9,8 @@ class GenerateCommand extends Command {
|
|
|
9
9
|
constructor() {
|
|
10
10
|
super({
|
|
11
11
|
name: 'generate',
|
|
12
|
-
desc: ''
|
|
12
|
+
desc: '',
|
|
13
|
+
alias: ['gen']
|
|
13
14
|
});
|
|
14
15
|
this.addArgument('name', 'Migration name', 'required', '');
|
|
15
16
|
this.addArgument('dir', 'Migration scripts directory', 'optional', process.cwd());
|
package/commands/migrate.js
CHANGED
|
@@ -15,9 +15,9 @@ class MigrateCommand extends Command {
|
|
|
15
15
|
|
|
16
16
|
this.addOption('debug', 'd', '[false] debug mode', 'optional', false);
|
|
17
17
|
this.addOption('host', null, '[localhost] mysql host', 'optional', 'localhost');
|
|
18
|
+
this.addOption('port', null, '[3306] port number to connect to the database', 'optional', 3306);
|
|
18
19
|
this.addOption('user', null, '[root] username for connect to the database', 'optional', 'root');
|
|
19
20
|
this.addOption('pass', null, 'password to connect to the database', 'optional', '');
|
|
20
|
-
this.addOption('port', null, '[3306] port number to connect to the database', 'optional', 3306);
|
|
21
21
|
this.addOption('db', null, 'database name', 'optional', '');
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -29,6 +29,7 @@ class MigrateCommand extends Command {
|
|
|
29
29
|
const workflow = new Workflow(migration);
|
|
30
30
|
try {
|
|
31
31
|
await workflow.start({
|
|
32
|
+
task_key: 'migrate_logs',
|
|
32
33
|
action: args.action,
|
|
33
34
|
config: {
|
|
34
35
|
dir: args.dir
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @returns {Record<string, import('@axiosleo/orm-mysql').ColumnItem>}
|
|
5
|
+
*/
|
|
6
|
+
module.exports = {
|
|
7
|
+
id: {
|
|
8
|
+
type: 'int',
|
|
9
|
+
allowNull: false,
|
|
10
|
+
primaryKey: true,
|
|
11
|
+
autoIncrement: true
|
|
12
|
+
},
|
|
13
|
+
created_at: {
|
|
14
|
+
type: 'TIMESTAMP',
|
|
15
|
+
allowNull: false,
|
|
16
|
+
default: 'CURRENT_TIMESTAMP'
|
|
17
|
+
},
|
|
18
|
+
updated_at: {
|
|
19
|
+
type: 'TIMESTAMP',
|
|
20
|
+
allowNull: false,
|
|
21
|
+
default: 'CURRENT_TIMESTAMP',
|
|
22
|
+
onUpdate: 'CURRENT_TIMESTAMP'
|
|
23
|
+
},
|
|
24
|
+
disabled_at: {
|
|
25
|
+
type: 'TIMESTAMP',
|
|
26
|
+
allowNull: true
|
|
27
|
+
}
|
|
28
|
+
};
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const dotenv = require('dotenv');
|
|
6
|
+
dotenv.config({
|
|
7
|
+
path: path.join(__dirname, '../../../.env')
|
|
8
|
+
});
|
|
9
|
+
|
|
3
10
|
module.exports = {
|
|
4
|
-
host:
|
|
5
|
-
port:
|
|
6
|
-
user:
|
|
7
|
-
password:
|
|
8
|
-
database:
|
|
11
|
+
host: process.env.MYSQL_HOST,
|
|
12
|
+
port: process.env.MYSQL_PORT,
|
|
13
|
+
user: process.env.MYSQL_USER,
|
|
14
|
+
password: process.env.MYSQL_PASS,
|
|
15
|
+
database: process.env.MYSQL_DB
|
|
9
16
|
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const baseColumn = require('../base_column');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
|
|
7
|
+
*/
|
|
8
|
+
function up(migration) {
|
|
9
|
+
migration.createTable('orgs', {
|
|
10
|
+
...baseColumn,
|
|
11
|
+
code: {
|
|
12
|
+
type: 'varchar',
|
|
13
|
+
length: 64,
|
|
14
|
+
allowNull: false,
|
|
15
|
+
uniqIndex: true
|
|
16
|
+
},
|
|
17
|
+
name: {
|
|
18
|
+
type: 'VARCHAR',
|
|
19
|
+
allowNull: false
|
|
20
|
+
},
|
|
21
|
+
type: {
|
|
22
|
+
type: 'VARCHAR',
|
|
23
|
+
comment: '集团 group;公司 company;',
|
|
24
|
+
allowNull: false
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
migration.createTable('account', {
|
|
29
|
+
...baseColumn,
|
|
30
|
+
uuid: {
|
|
31
|
+
type: 'VARCHAR',
|
|
32
|
+
length: 36,
|
|
33
|
+
allowNull: false,
|
|
34
|
+
uniqIndex: true,
|
|
35
|
+
comment: 'uuid'
|
|
36
|
+
},
|
|
37
|
+
username: {
|
|
38
|
+
type: 'varchar',
|
|
39
|
+
length: 64,
|
|
40
|
+
allowNull: false,
|
|
41
|
+
},
|
|
42
|
+
name: {
|
|
43
|
+
type: 'VARCHAR',
|
|
44
|
+
length: 100,
|
|
45
|
+
allowNull: false,
|
|
46
|
+
comment: '姓名',
|
|
47
|
+
},
|
|
48
|
+
avatar: {
|
|
49
|
+
type: 'VARCHAR',
|
|
50
|
+
allowNull: true,
|
|
51
|
+
comment: '头像文件索引'
|
|
52
|
+
},
|
|
53
|
+
password: {
|
|
54
|
+
type: 'varchar',
|
|
55
|
+
length: 64,
|
|
56
|
+
allowNull: false
|
|
57
|
+
},
|
|
58
|
+
last_token: {
|
|
59
|
+
type: 'VARCHAR',
|
|
60
|
+
length: 36,
|
|
61
|
+
allowNull: true,
|
|
62
|
+
comment: '最后一次登录的token'
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
migration.createIndex('account', ['username asc']);
|
|
66
|
+
|
|
67
|
+
migration.createTable('account_orgs', {
|
|
68
|
+
id: baseColumn.id,
|
|
69
|
+
account_id: {
|
|
70
|
+
type: 'int',
|
|
71
|
+
allowNull: false,
|
|
72
|
+
},
|
|
73
|
+
org_id: {
|
|
74
|
+
type: 'int',
|
|
75
|
+
allowNull: false,
|
|
76
|
+
},
|
|
77
|
+
type: {
|
|
78
|
+
type: 'varchar',
|
|
79
|
+
length: 32,
|
|
80
|
+
allowNull: false,
|
|
81
|
+
comment: '组织创建者creator;组织管理员admin;组织用户user;',
|
|
82
|
+
default: 'user'
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
migration.createColumn('created_at', 'TIMESTAMP', 'account_orgs');
|
|
86
|
+
migration.createForeignKey({
|
|
87
|
+
tableName: 'account_orgs',
|
|
88
|
+
columnName: 'account_id',
|
|
89
|
+
reference: {
|
|
90
|
+
tableName: 'account',
|
|
91
|
+
columnName: 'id',
|
|
92
|
+
onDelete: 'CASCADE'
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
migration.createForeignKey({
|
|
96
|
+
tableName: 'account_orgs',
|
|
97
|
+
columnName: 'org_id',
|
|
98
|
+
reference: {
|
|
99
|
+
tableName: 'orgs',
|
|
100
|
+
columnName: 'id',
|
|
101
|
+
onDelete: 'CASCADE'
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {import('@axiosleo/orm-mysql').MigrationInterface} migration
|
|
108
|
+
*/
|
|
109
|
+
function down(migration) {
|
|
110
|
+
migration.dropTable('account_orgs');
|
|
111
|
+
migration.dropTable('account');
|
|
112
|
+
migration.dropTable('orgs');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {
|
|
116
|
+
up,
|
|
117
|
+
down
|
|
118
|
+
};
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -273,6 +273,9 @@ class Builder {
|
|
|
273
273
|
if (key === null) {
|
|
274
274
|
return '';
|
|
275
275
|
}
|
|
276
|
+
if (typeof key === 'undefined') {
|
|
277
|
+
throw new Error('Field key is required');
|
|
278
|
+
}
|
|
276
279
|
if (key.indexOf('(') !== -1 && key.indexOf(')') !== -1) {
|
|
277
280
|
let field = key.substring(key.indexOf('(') + 1, key.indexOf(')'));
|
|
278
281
|
key = key.substring(0, key.indexOf('(')) + '(' + this._buildFieldWithTableName(field) + ')' + key.substring(key.indexOf(')') + 1);
|
|
@@ -347,7 +350,7 @@ class ManageSQLBuilder extends Builder {
|
|
|
347
350
|
table: 'required|string',
|
|
348
351
|
name: 'required|string',
|
|
349
352
|
type: 'required|string',
|
|
350
|
-
length: '
|
|
353
|
+
length: 'integer',
|
|
351
354
|
unsigned: 'boolean',
|
|
352
355
|
allowNull: 'boolean',
|
|
353
356
|
default: 'string',
|
|
@@ -467,7 +470,6 @@ class ManageSQLBuilder extends Builder {
|
|
|
467
470
|
_validate(options, {
|
|
468
471
|
name: 'required|string',
|
|
469
472
|
type: 'required|string',
|
|
470
|
-
default: 'string',
|
|
471
473
|
onUpdate: 'string',
|
|
472
474
|
length: 'integer',
|
|
473
475
|
comment: 'string',
|
|
@@ -501,9 +503,13 @@ class ManageSQLBuilder extends Builder {
|
|
|
501
503
|
}
|
|
502
504
|
if (options.default === null) {
|
|
503
505
|
str += ' DEFAULT NULL';
|
|
504
|
-
} else if (options.default === 'timestamp') {
|
|
506
|
+
} else if (options.default === 'timestamp' || options.default === 'TIMESTAMP') {
|
|
505
507
|
str += ' DEFAULT CURRENT_TIMESTAMP';
|
|
508
|
+
} else if (options.default === 'CURRENT_TIMESTAMP') {
|
|
509
|
+
str += ` DEFAULT ${options.default}`;
|
|
506
510
|
} else if (is.string(options.default)) {
|
|
511
|
+
str += ` DEFAULT '${options.default}'`;
|
|
512
|
+
} else {
|
|
507
513
|
str += ` DEFAULT ${options.default}`;
|
|
508
514
|
}
|
|
509
515
|
}
|
package/src/core.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { Builder } = require('./builder');
|
|
4
|
+
const is = require('@axiosleo/cli-tool/src/helper/is');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
*
|
|
@@ -10,6 +11,9 @@ const { Builder } = require('./builder');
|
|
|
10
11
|
* @returns
|
|
11
12
|
*/
|
|
12
13
|
const _query = (conn, options, opt = null) => {
|
|
14
|
+
if (is.empty(options)) {
|
|
15
|
+
options = { driver: 'mysql' };
|
|
16
|
+
}
|
|
13
17
|
switch (options.driver) {
|
|
14
18
|
case 'mysql': {
|
|
15
19
|
if (opt === null) {
|
package/src/migration.js
CHANGED
|
@@ -58,7 +58,9 @@ async function init(context) {
|
|
|
58
58
|
const connect = require(connectPath);
|
|
59
59
|
_assign(context.connection, connect);
|
|
60
60
|
}
|
|
61
|
-
context.task_key
|
|
61
|
+
if (!context.task_key) {
|
|
62
|
+
context.task_key = 'migrate_' + context.connection.database;
|
|
63
|
+
}
|
|
62
64
|
let globalConn = createClient({
|
|
63
65
|
...context.connection,
|
|
64
66
|
database: 'mysql'
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @param {import('../../').MigrationInterface} migration
|
|
5
|
-
*/
|
|
6
|
-
function up(migration) {
|
|
7
|
-
migration.createTable({
|
|
8
|
-
table_name: 'organization',
|
|
9
|
-
columns: [{
|
|
10
|
-
column_name: 'id',
|
|
11
|
-
type: 'int(11)',
|
|
12
|
-
not_null: true,
|
|
13
|
-
is_primary_key: true,
|
|
14
|
-
}]
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @param {import('../../').MigrationInterface} migration
|
|
20
|
-
*/
|
|
21
|
-
function down(migration) {
|
|
22
|
-
migration.dropTable({ table_name: 'table1' });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = {
|
|
26
|
-
up,
|
|
27
|
-
down
|
|
28
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
'use strict';
|