@axiosleo/orm-mysql 0.9.1 → 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/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 +1 -2
- 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());
|
|
@@ -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
|
@@ -350,7 +350,7 @@ class ManageSQLBuilder extends Builder {
|
|
|
350
350
|
table: 'required|string',
|
|
351
351
|
name: 'required|string',
|
|
352
352
|
type: 'required|string',
|
|
353
|
-
length: '
|
|
353
|
+
length: 'integer',
|
|
354
354
|
unsigned: 'boolean',
|
|
355
355
|
allowNull: 'boolean',
|
|
356
356
|
default: 'string',
|
|
@@ -470,7 +470,6 @@ class ManageSQLBuilder extends Builder {
|
|
|
470
470
|
_validate(options, {
|
|
471
471
|
name: 'required|string',
|
|
472
472
|
type: 'required|string',
|
|
473
|
-
default: 'string',
|
|
474
473
|
onUpdate: 'string',
|
|
475
474
|
length: 'integer',
|
|
476
475
|
comment: 'string',
|
|
@@ -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';
|