@axiosleo/orm-mysql 0.9.1 → 0.9.3
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/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/builder.js +28 -4
- package/src/query.js +2 -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());
|
|
@@ -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/index.d.ts
CHANGED
|
@@ -70,7 +70,9 @@ export type QueryOperatorOptions = QueryOperatorBaseOptions & {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export declare class Query {
|
|
73
|
-
|
|
73
|
+
options: QueryOperatorOptions;
|
|
74
|
+
|
|
75
|
+
constructor(operator?: OperatorType, alias?: string | null);
|
|
74
76
|
|
|
75
77
|
table(tableName: string, alias: string | null): this;
|
|
76
78
|
|
package/package.json
CHANGED
package/src/builder.js
CHANGED
|
@@ -32,7 +32,21 @@ class Builder {
|
|
|
32
32
|
}
|
|
33
33
|
// eslint-disable-next-line no-fallthrough
|
|
34
34
|
case 'select': {
|
|
35
|
-
|
|
35
|
+
options.attrs = options.attrs || [];
|
|
36
|
+
const attrs = options.attrs.map((attr) => {
|
|
37
|
+
if (attr instanceof Function) {
|
|
38
|
+
const query = attr();
|
|
39
|
+
const builder = new Builder(query.options);
|
|
40
|
+
let s = `(${builder.sql})`;
|
|
41
|
+
if (query.alias) {
|
|
42
|
+
return query.alias.indexOf(' ') > -1 ? s + ' ' + this._buildFieldKey(query.alias)
|
|
43
|
+
: s + ' AS ' + this._buildFieldKey(query.alias);
|
|
44
|
+
}
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
return attr;
|
|
48
|
+
});
|
|
49
|
+
emit(tmp, `SELECT ${attrs.length ? attrs.map((a) => this._buildFieldKey(a)).join(',') : '*'} FROM ${this._buildTables(options.tables)}`);
|
|
36
50
|
emit(tmp, this._buildJoins(options.joins));
|
|
37
51
|
emit(tmp, this._buildContidion(options.conditions));
|
|
38
52
|
emit(tmp, this._buildOrders(options.orders));
|
|
@@ -199,6 +213,11 @@ class Builder {
|
|
|
199
213
|
}
|
|
200
214
|
|
|
201
215
|
_buildConditionValues(val) {
|
|
216
|
+
if (is.string(val)) {
|
|
217
|
+
if (val.startsWith('`') && val.endsWith('`')) {
|
|
218
|
+
return val;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
202
221
|
if (val instanceof Query) {
|
|
203
222
|
const builder = new Builder(val.options);
|
|
204
223
|
this.values = this.values.concat(builder.values);
|
|
@@ -263,7 +282,13 @@ class Builder {
|
|
|
263
282
|
return `(${this._buildContidion(c.value, '')})`;
|
|
264
283
|
}
|
|
265
284
|
let res = this._buildConditionValues(c.value);
|
|
266
|
-
|
|
285
|
+
if (!is.empty(res)) {
|
|
286
|
+
if (res.startsWith('`') && res.endsWith('`')) {
|
|
287
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} ${res}`;
|
|
288
|
+
}
|
|
289
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} (${res})`;
|
|
290
|
+
}
|
|
291
|
+
return `${this._buildFieldKey(c.key)} ${c.opt} ?`;
|
|
267
292
|
}).join('')}`;
|
|
268
293
|
}
|
|
269
294
|
return sql;
|
|
@@ -350,7 +375,7 @@ class ManageSQLBuilder extends Builder {
|
|
|
350
375
|
table: 'required|string',
|
|
351
376
|
name: 'required|string',
|
|
352
377
|
type: 'required|string',
|
|
353
|
-
length: '
|
|
378
|
+
length: 'integer',
|
|
354
379
|
unsigned: 'boolean',
|
|
355
380
|
allowNull: 'boolean',
|
|
356
381
|
default: 'string',
|
|
@@ -470,7 +495,6 @@ class ManageSQLBuilder extends Builder {
|
|
|
470
495
|
_validate(options, {
|
|
471
496
|
name: 'required|string',
|
|
472
497
|
type: 'required|string',
|
|
473
|
-
default: 'string',
|
|
474
498
|
onUpdate: 'string',
|
|
475
499
|
length: 'integer',
|
|
476
500
|
comment: 'string',
|
package/src/query.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
class Query {
|
|
4
|
-
constructor(operator = 'select') {
|
|
4
|
+
constructor(operator = 'select', alias = null) {
|
|
5
5
|
this.options = {
|
|
6
6
|
driver: 'mysql',
|
|
7
7
|
queryHandler: null,
|
|
@@ -16,6 +16,7 @@ class Query {
|
|
|
16
16
|
suffix: null,
|
|
17
17
|
transaction: false
|
|
18
18
|
};
|
|
19
|
+
this.alias = alias || null;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
table(tableName, alias) {
|
|
@@ -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';
|