baja-lite 1.0.33 → 1.0.35
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/{cjs/boot-remote.d.ts → boot-remote.d.ts} +1 -1
- package/{es/boot-remote.js → boot-remote.js} +2 -2
- package/{es/boot.d.ts → boot.d.ts} +1 -1
- package/{es/boot.js → boot.js} +2 -2
- package/{es/enum.js → enum.js} +1 -1
- package/{es/fn.js → fn.js} +2 -2
- package/index.d.ts +10 -0
- package/index.js +10 -0
- package/package.json +11 -12
- package/{cjs/sql.d.ts → sql.d.ts} +3 -3
- package/{es/sql.js → sql.js} +7 -7
- package/{es/sqlite.d.ts → sqlite.d.ts} +1 -1
- package/{es/sqlite.js → sqlite.js} +1 -1
- package/{es/test-mysql.js → test-mysql.js} +2 -2
- package/{es/test-postgresql.js → test-postgresql.js} +2 -2
- package/cjs/boot-remote.js +0 -49
- package/cjs/boot.d.ts +0 -2
- package/cjs/boot.js +0 -183
- package/cjs/code.js +0 -379
- package/cjs/convert-xml.js +0 -417
- package/cjs/enum.js +0 -64
- package/cjs/error.js +0 -16
- package/cjs/fn.js +0 -179
- package/cjs/index.d.ts +0 -10
- package/cjs/index.js +0 -26
- package/cjs/list.js +0 -60
- package/cjs/math.js +0 -474
- package/cjs/object.js +0 -242
- package/cjs/set-ex.js +0 -345
- package/cjs/sql.js +0 -5083
- package/cjs/sqlite.d.ts +0 -32
- package/cjs/sqlite.js +0 -169
- package/cjs/string.js +0 -119
- package/cjs/test-mysql.js +0 -138
- package/cjs/test-postgresql.js +0 -93
- package/cjs/test-sqlite.js +0 -91
- package/cjs/test-xml.js +0 -75
- package/cjs/test.js +0 -4
- package/es/boot-remote.d.ts +0 -2
- package/es/code.d.ts +0 -2
- package/es/convert-xml.d.ts +0 -10
- package/es/enum.d.ts +0 -18
- package/es/error.d.ts +0 -5
- package/es/fn.d.ts +0 -128
- package/es/index.d.ts +0 -10
- package/es/index.js +0 -10
- package/es/list.d.ts +0 -10
- package/es/math.d.ts +0 -83
- package/es/object.d.ts +0 -83
- package/es/set-ex.d.ts +0 -198
- package/es/sql.d.ts +0 -1858
- package/es/string.d.ts +0 -17
- package/es/test-mysql.d.ts +0 -2
- package/es/test-postgresql.d.ts +0 -2
- package/es/test-sqlite.d.ts +0 -1
- package/es/test-xml.d.ts +0 -1
- package/es/test.d.ts +0 -1
- package/src/boot-remote.ts +0 -46
- package/src/boot.ts +0 -153
- package/src/code.ts +0 -377
- package/src/convert-xml.ts +0 -460
- package/src/enum.ts +0 -71
- package/src/error.ts +0 -11
- package/src/fn.ts +0 -295
- package/src/index.ts +0 -10
- package/src/list.ts +0 -57
- package/src/math.ts +0 -405
- package/src/object.ts +0 -247
- package/src/set-ex.ts +0 -374
- package/src/sql.ts +0 -5281
- package/src/sqlite.ts +0 -161
- package/src/string.ts +0 -111
- package/src/test-mysql.ts +0 -126
- package/src/test-postgresql.ts +0 -79
- package/src/test-sqlite.ts +0 -80
- package/src/test-xml.ts +0 -70
- package/src/test.ts +0 -2
- /package/{cjs/code.d.ts → code.d.ts} +0 -0
- /package/{es/code.js → code.js} +0 -0
- /package/{cjs/convert-xml.d.ts → convert-xml.d.ts} +0 -0
- /package/{es/convert-xml.js → convert-xml.js} +0 -0
- /package/{cjs/enum.d.ts → enum.d.ts} +0 -0
- /package/{cjs/error.d.ts → error.d.ts} +0 -0
- /package/{es/error.js → error.js} +0 -0
- /package/{cjs/fn.d.ts → fn.d.ts} +0 -0
- /package/{cjs/list.d.ts → list.d.ts} +0 -0
- /package/{es/list.js → list.js} +0 -0
- /package/{cjs/math.d.ts → math.d.ts} +0 -0
- /package/{es/math.js → math.js} +0 -0
- /package/{cjs/object.d.ts → object.d.ts} +0 -0
- /package/{es/object.js → object.js} +0 -0
- /package/{cjs/set-ex.d.ts → set-ex.d.ts} +0 -0
- /package/{es/set-ex.js → set-ex.js} +0 -0
- /package/{cjs/string.d.ts → string.d.ts} +0 -0
- /package/{es/string.js → string.js} +0 -0
- /package/{cjs/test-mysql.d.ts → test-mysql.d.ts} +0 -0
- /package/{cjs/test-postgresql.d.ts → test-postgresql.d.ts} +0 -0
- /package/{cjs/test-sqlite.d.ts → test-sqlite.d.ts} +0 -0
- /package/{es/test-sqlite.js → test-sqlite.js} +0 -0
- /package/{cjs/test-xml.d.ts → test-xml.d.ts} +0 -0
- /package/{es/test-xml.js → test-xml.js} +0 -0
- /package/{cjs/test.d.ts → test.d.ts} +0 -0
- /package/{es/test.js → test.js} +0 -0
package/es/string.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 通过uri获取key
|
|
3
|
-
* @param uri
|
|
4
|
-
*/
|
|
5
|
-
export declare const getPicKey: (uri: string) => string;
|
|
6
|
-
export declare const emptyString: (source: any, skipEmptyString?: boolean) => boolean;
|
|
7
|
-
export declare const notEmptyString: (source: any, skipEmptyString?: boolean) => boolean;
|
|
8
|
-
export declare const safeString: (source?: string) => string;
|
|
9
|
-
export declare const trimObject: <T>(data: any) => T;
|
|
10
|
-
export declare const randomNumber: (len: number) => string;
|
|
11
|
-
export declare const randomString: (len: number) => string;
|
|
12
|
-
export declare const randomString2: (len: number) => string;
|
|
13
|
-
export declare const randomString3: (len: number) => string;
|
|
14
|
-
export declare const buildWxStr: (data: {
|
|
15
|
-
[key: string]: string;
|
|
16
|
-
}, maxLabelLength: number, ...titles: string[]) => string;
|
|
17
|
-
export declare const replaceChineseCode: (str: string) => string;
|
package/es/test-mysql.d.ts
DELETED
package/es/test-postgresql.d.ts
DELETED
package/es/test-sqlite.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
package/es/test-xml.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/es/test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/src/boot-remote.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { getEnums } from './enum';
|
|
2
|
-
import { _primaryDB, _dao, logger, DBType, _sqlCache, _GlobalSqlOption, GlobalSqlOptionForWeb, _defOption, SqlCache, SqliteRemote, _Hump, ColumnMode, _enums } from './sql';
|
|
3
|
-
|
|
4
|
-
export const BootRomote = async function (options: GlobalSqlOptionForWeb) {
|
|
5
|
-
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
6
|
-
globalThis[_Hump] = globalThis[_GlobalSqlOption].columnMode === ColumnMode.HUMP;
|
|
7
|
-
logger.level = options.log ?? 'info';
|
|
8
|
-
globalThis[_sqlCache] = new SqlCache();
|
|
9
|
-
if (options.sqlMap) {
|
|
10
|
-
await globalThis[_sqlCache].init(options);
|
|
11
|
-
}
|
|
12
|
-
globalThis[_dao] = {
|
|
13
|
-
[DBType.SqliteRemote]: {},
|
|
14
|
-
};
|
|
15
|
-
if (options.enums) {
|
|
16
|
-
globalThis[_enums] = getEnums(options.enums);
|
|
17
|
-
}
|
|
18
|
-
if (options.SqliteRemote && options.SqliteRemote.db) {
|
|
19
|
-
if (typeof options.SqliteRemote.db === 'string') {
|
|
20
|
-
options.SqliteRemote.service.initDB(options.SqliteRemote.db);
|
|
21
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
|
|
22
|
-
} else {
|
|
23
|
-
let flag = false;
|
|
24
|
-
for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
|
|
25
|
-
await options.SqliteRemote.service.initDB(fileName);
|
|
26
|
-
const db = new SqliteRemote(options.SqliteRemote.service, fileName);
|
|
27
|
-
if (flag === false) {
|
|
28
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
|
|
29
|
-
flag = true;
|
|
30
|
-
}
|
|
31
|
-
globalThis[_dao][DBType.SqliteRemote][key] = db;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// export const AppendRomote = async function (dbName: string) {
|
|
38
|
-
// if (!globalThis[_dao][DBType.SqliteRemote][dbName]) {
|
|
39
|
-
// globalThis[_GlobalSqlOption].SqliteRemote.service.initDB(dbName);
|
|
40
|
-
// const db = new SqliteRemote(globalThis[_GlobalSqlOption].SqliteRemote.service, dbName);
|
|
41
|
-
// if (globalThis[_dao][DBType.SqliteRemote][_primaryDB] === undefined) {
|
|
42
|
-
// globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
|
|
43
|
-
// }
|
|
44
|
-
// globalThis[_dao][DBType.SqliteRemote][dbName] = db;
|
|
45
|
-
// }
|
|
46
|
-
// }
|
package/src/boot.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { getEnums } from './enum';
|
|
2
|
-
import { _GlobalSqlOption, GlobalSqlOption, _enums, _EventBus, _defOption, logger, _sqlCache, SqlCache, _dao, DBType, _primaryDB, SqliteRemote, _fs, _path, Mysql, Sqlite, _Hump, ColumnMode, Postgresql } from './sql';
|
|
3
|
-
|
|
4
|
-
export const Boot = async function (options: GlobalSqlOption) {
|
|
5
|
-
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption, options);
|
|
6
|
-
globalThis[_Hump] = globalThis[_GlobalSqlOption].columnMode === ColumnMode.HUMP;
|
|
7
|
-
if (options.sqlDir) {
|
|
8
|
-
globalThis[_path] = await import('path');
|
|
9
|
-
globalThis[_fs] = await import('fs');
|
|
10
|
-
}
|
|
11
|
-
logger.level = options.log ?? 'info';
|
|
12
|
-
globalThis[_sqlCache] = new SqlCache();
|
|
13
|
-
if (options.sqlMap || options.sqlDir) {
|
|
14
|
-
await globalThis[_sqlCache].init(options);
|
|
15
|
-
}
|
|
16
|
-
globalThis[_dao] = {
|
|
17
|
-
[DBType.Mongo]: {},
|
|
18
|
-
[DBType.Mysql]: {},
|
|
19
|
-
[DBType.Postgresql]: {},
|
|
20
|
-
[DBType.Sqlite]: {},
|
|
21
|
-
[DBType.SqliteRemote]: {},
|
|
22
|
-
[DBType.Redis]: {}
|
|
23
|
-
};
|
|
24
|
-
if (options.enums) {
|
|
25
|
-
globalThis[_enums] = getEnums(options.enums);
|
|
26
|
-
}
|
|
27
|
-
if (options.Mysql) {
|
|
28
|
-
const { createPool } = await import('mysql2/promise');
|
|
29
|
-
if (options.Mysql['host']) {
|
|
30
|
-
globalThis[_dao][DBType.Mysql][_primaryDB] = new Mysql(createPool({
|
|
31
|
-
...options.Mysql,
|
|
32
|
-
multipleStatements: true,
|
|
33
|
-
decimalNumbers: true,
|
|
34
|
-
supportBigNumbers: true
|
|
35
|
-
}));
|
|
36
|
-
|
|
37
|
-
} else {
|
|
38
|
-
let flag = false;
|
|
39
|
-
for (const [key, option] of Object.entries(options.Mysql)) {
|
|
40
|
-
const db = new Mysql(createPool({
|
|
41
|
-
...option,
|
|
42
|
-
multipleStatements: true,
|
|
43
|
-
decimalNumbers: true,
|
|
44
|
-
supportBigNumbers: true
|
|
45
|
-
}));
|
|
46
|
-
|
|
47
|
-
if (flag === false) {
|
|
48
|
-
globalThis[_dao][DBType.Mysql][_primaryDB] = db;
|
|
49
|
-
flag = true;
|
|
50
|
-
}
|
|
51
|
-
globalThis[_dao][DBType.Mysql][key] = db;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (options.Sqlite) {
|
|
56
|
-
if (typeof options.Sqlite === 'string') {
|
|
57
|
-
globalThis[_dao][DBType.Sqlite][_primaryDB] = new Sqlite(new options.BetterSqlite3(options.Sqlite, { fileMustExist: false }));
|
|
58
|
-
} else {
|
|
59
|
-
let flag = false;
|
|
60
|
-
for (const [key, fileName] of Object.entries(options.Sqlite)) {
|
|
61
|
-
const db = new Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
|
|
62
|
-
if (flag === false) {
|
|
63
|
-
globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
|
|
64
|
-
flag = true;
|
|
65
|
-
}
|
|
66
|
-
globalThis[_dao][DBType.Sqlite][key] = db;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (options.SqliteRemote && options.SqliteRemote.db) {
|
|
71
|
-
if (typeof options.SqliteRemote.db === 'string') {
|
|
72
|
-
options.SqliteRemote.service.initDB(options.SqliteRemote.db);
|
|
73
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = new SqliteRemote(options.SqliteRemote.service, options.SqliteRemote.db);
|
|
74
|
-
} else {
|
|
75
|
-
let flag = false;
|
|
76
|
-
for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
|
|
77
|
-
options.SqliteRemote.service.initDB(fileName);
|
|
78
|
-
const db = new SqliteRemote(options.SqliteRemote.service, fileName);
|
|
79
|
-
if (flag === false) {
|
|
80
|
-
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
|
|
81
|
-
flag = true;
|
|
82
|
-
}
|
|
83
|
-
globalThis[_dao][DBType.SqliteRemote][key] = db;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (options.Redis) {
|
|
88
|
-
const { Redis } = await import('ioredis');
|
|
89
|
-
if (options.Redis['host']) {
|
|
90
|
-
globalThis[_dao][DBType.Redis][_primaryDB] = new Redis(options.Redis);
|
|
91
|
-
} else {
|
|
92
|
-
let flag = false;
|
|
93
|
-
for (const [key, option] of Object.entries(options.Redis)) {
|
|
94
|
-
const db = new Redis(option);
|
|
95
|
-
if (flag === false) {
|
|
96
|
-
globalThis[_dao][DBType.Redis][_primaryDB] = db;
|
|
97
|
-
flag = true;
|
|
98
|
-
}
|
|
99
|
-
globalThis[_dao][DBType.Redis][key] = db;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const clients = Object.values(globalThis[_dao][DBType.Redis]) as any;
|
|
103
|
-
const Redlock = await import('redlock');
|
|
104
|
-
globalThis[_dao][DBType.RedisLock] = new Redlock.default(
|
|
105
|
-
clients,
|
|
106
|
-
{
|
|
107
|
-
// The expected clock drift; for more details see:
|
|
108
|
-
// http://redis.io/topics/distlock
|
|
109
|
-
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
|
|
110
|
-
|
|
111
|
-
// The max number of times Redlock will attempt to lock a resource
|
|
112
|
-
// before erroring.
|
|
113
|
-
retryCount: 10,
|
|
114
|
-
|
|
115
|
-
// the time in ms between attempts
|
|
116
|
-
retryDelay: 200, // time in ms
|
|
117
|
-
|
|
118
|
-
// the max time in ms randomly added to retries
|
|
119
|
-
// to improve performance under high contention
|
|
120
|
-
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
|
|
121
|
-
retryJitter: 200, // time in ms
|
|
122
|
-
|
|
123
|
-
// The minimum remaining time on a lock before an extension is automatically
|
|
124
|
-
// attempted with the `using` API.
|
|
125
|
-
automaticExtensionThreshold: 500, // time in ms
|
|
126
|
-
}
|
|
127
|
-
);
|
|
128
|
-
const { EventEmitter } = await import('events');
|
|
129
|
-
const event = new EventEmitter({ captureRejections: true });
|
|
130
|
-
event.on('error', error => {
|
|
131
|
-
logger.error('event-bus', error);
|
|
132
|
-
});
|
|
133
|
-
globalThis[_EventBus] = event;
|
|
134
|
-
}
|
|
135
|
-
if (options.Postgresql) {
|
|
136
|
-
const Pool = await import('pg-pool');
|
|
137
|
-
if (options.Postgresql['host']) {
|
|
138
|
-
globalThis[_dao][DBType.Postgresql][_primaryDB] = new Postgresql(new Pool.default(options.Postgresql));
|
|
139
|
-
|
|
140
|
-
} else {
|
|
141
|
-
let flag = false;
|
|
142
|
-
for (const [key, option] of Object.entries(options.Postgresql)) {
|
|
143
|
-
const db = new Postgresql(new Pool.default(option));
|
|
144
|
-
|
|
145
|
-
if (flag === false) {
|
|
146
|
-
globalThis[_dao][DBType.Postgresql][_primaryDB] = db;
|
|
147
|
-
flag = true;
|
|
148
|
-
}
|
|
149
|
-
globalThis[_dao][DBType.Postgresql][key] = db;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
package/src/code.ts
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import { start } from 'repl';
|
|
5
|
-
import { createPool } from 'mysql2/promise';
|
|
6
|
-
import mustache from 'mustache';
|
|
7
|
-
import { mkdir } from 'shelljs';
|
|
8
|
-
|
|
9
|
-
const lxMap = {
|
|
10
|
-
tinyint: "number",
|
|
11
|
-
smallint: "number",
|
|
12
|
-
mediumint: "number",
|
|
13
|
-
int: "number",
|
|
14
|
-
integer: "number",
|
|
15
|
-
bigint: "BigInt",
|
|
16
|
-
bit: "boolean",
|
|
17
|
-
double: "number",
|
|
18
|
-
real: "number",
|
|
19
|
-
float: "number",
|
|
20
|
-
decimal: "number",
|
|
21
|
-
numeric: "number",
|
|
22
|
-
char: "string",
|
|
23
|
-
varchar: "string",
|
|
24
|
-
date: "Date",
|
|
25
|
-
time: "string",
|
|
26
|
-
year: "string",
|
|
27
|
-
timestamp: "BigInt",
|
|
28
|
-
datetime: "Date",
|
|
29
|
-
tinyblob: "string",
|
|
30
|
-
blob: "string",
|
|
31
|
-
mediumblob: "string",
|
|
32
|
-
longblob: "string",
|
|
33
|
-
tinytext: "string",
|
|
34
|
-
text: "string",
|
|
35
|
-
mediumtext: "string",
|
|
36
|
-
longtext: "string",
|
|
37
|
-
enum: "string",
|
|
38
|
-
set: "string",
|
|
39
|
-
binary: "string",
|
|
40
|
-
varbinary: "string",
|
|
41
|
-
point: "Object",
|
|
42
|
-
linestring: "Object",
|
|
43
|
-
polygon: "Object",
|
|
44
|
-
geometry: "string",
|
|
45
|
-
multipoint: "Object",
|
|
46
|
-
multilinestring: "Object",
|
|
47
|
-
multipolygon: "Object",
|
|
48
|
-
geometrycollection: "Object"
|
|
49
|
-
};
|
|
50
|
-
let force = false;
|
|
51
|
-
const basepath = path.join(__dirname, '..', '..', '..');
|
|
52
|
-
const config = path.join(basepath, 'baja.code.json');
|
|
53
|
-
const templatePath = path.join(basepath, 'code-template');
|
|
54
|
-
console.log(`
|
|
55
|
-
**********************-----------
|
|
56
|
-
配置文件:
|
|
57
|
-
请在项目根目录添加文件:baja.code.json!配置如下:
|
|
58
|
-
{
|
|
59
|
-
"host": "",
|
|
60
|
-
"port": "",
|
|
61
|
-
"user": "",
|
|
62
|
-
"password": "",
|
|
63
|
-
"database": "",
|
|
64
|
-
|
|
65
|
-
"command": {
|
|
66
|
-
"entity": "src/{vueName}/{vueName}.entity.ts",
|
|
67
|
-
"controller": "src/{vueName}/{vueName}.controller.ts",
|
|
68
|
-
"service": "src/{vueName}/{vueName}.service.ts",
|
|
69
|
-
"sql": "src/sql/{vueName}.mu",
|
|
70
|
-
"module": "src/{vueName}/{vueName}.module.ts"
|
|
71
|
-
},
|
|
72
|
-
"commands": {
|
|
73
|
-
"s": ["entity", "controller", "service", "sql", "module"]
|
|
74
|
-
}
|
|
75
|
-
"commands": {
|
|
76
|
-
"s": ["entity", "controller", "service", "sql", "module"]
|
|
77
|
-
},
|
|
78
|
-
"output": "{ClassName}Module",
|
|
79
|
-
"id"? : "uuidShort: true, notNull: true, uuid: true",
|
|
80
|
-
"logicDeleteK"?: "逻辑删除字段名",
|
|
81
|
-
"logicDeleteV"?: "逻辑删除值",如果是字符串需要这样 logicDeleteV: "'0'"
|
|
82
|
-
"NotlogicDeleteV"?: "未逻辑删除值",如果是字符串需要这样 NotlogicDeleteV: "'0'"
|
|
83
|
-
}
|
|
84
|
-
command是生成命令,这里声明命令同时定义文件生成路径:可用下面的变量替换.同时必须有同名模板.
|
|
85
|
-
路径是相对于项目根目录的
|
|
86
|
-
commands是组合命令,上面表示输入s,同时生成:"entity", "controller", "service", "sql", "module"
|
|
87
|
-
output 是生成后打印出的内容
|
|
88
|
-
模板转义:<%& 变量 %>
|
|
89
|
-
**********************-----------
|
|
90
|
-
**********************-----------
|
|
91
|
-
**********************-----------
|
|
92
|
-
模板文件
|
|
93
|
-
请在项目根目录的code-template添加模板文件, 按照mustache(标签定义成了[ '<%', '%>' ])进行格式化,变量:
|
|
94
|
-
title,
|
|
95
|
-
|
|
96
|
-
tableName, event_main_info
|
|
97
|
-
className, eventMainInfo
|
|
98
|
-
ClassName, EventMainInfo
|
|
99
|
-
vueName, event-main-info
|
|
100
|
-
splitName, event/main/info
|
|
101
|
-
SplitName, event/mainInfo
|
|
102
|
-
|
|
103
|
-
columns: [
|
|
104
|
-
comment:
|
|
105
|
-
name: sku_id
|
|
106
|
-
Name: skuId
|
|
107
|
-
NAME: SkuId
|
|
108
|
-
Field
|
|
109
|
-
Type
|
|
110
|
-
], Field(类型string),表示字段的注解, Type 表示JS类型
|
|
111
|
-
columnNames_join: 'sku_id, sku_name'
|
|
112
|
-
ColumnNames_join: 'sku_id skuId, sku_name skuName'
|
|
113
|
-
|
|
114
|
-
columns_no_id: [], 同columns,没有ID而已
|
|
115
|
-
columnNames_no_id: [],
|
|
116
|
-
|
|
117
|
-
ids: [], 参见 FieldOption,多一个参数Field,表示字段的注解, Type 表示JS类型
|
|
118
|
-
idNames: [], 主键字段数组
|
|
119
|
-
idNames_join: []
|
|
120
|
-
|
|
121
|
-
modelName: 模块名称,可能为空字符串
|
|
122
|
-
modelPath: 模块名称实际就影响访问路径,所以这里会直接拼好controller的模块访问路径,如果模块为空,则该属性就是空字符串,否则是 /模块名称/
|
|
123
|
-
|
|
124
|
-
logicDelete: 逻辑删除的查询条件,可以附加在sql条件的末尾,可能是空的
|
|
125
|
-
-----
|
|
126
|
-
命令 table1,table2,table3:模块名称
|
|
127
|
-
table=. 表示扫描全库表
|
|
128
|
-
:模块名称 可以省略
|
|
129
|
-
-----
|
|
130
|
-
force: 切换是否覆盖
|
|
131
|
-
`);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
const outputs = new Set<string>();
|
|
136
|
-
const _configData = fs.readFileSync(config, { encoding: 'utf-8' }).toString();
|
|
137
|
-
const configData = JSON.parse(_configData) as {
|
|
138
|
-
host: string;
|
|
139
|
-
port: number;
|
|
140
|
-
user: string;
|
|
141
|
-
password: string;
|
|
142
|
-
database: string;
|
|
143
|
-
command: Record<string, string>;
|
|
144
|
-
commands: Record<string, string[]>;
|
|
145
|
-
output: string;
|
|
146
|
-
id: string;
|
|
147
|
-
logicDeleteK: string;
|
|
148
|
-
logicDeleteV: number;
|
|
149
|
-
NotlogicDeleteV: number;
|
|
150
|
-
tables: string;
|
|
151
|
-
};
|
|
152
|
-
const templates = Object.fromEntries(fs.readdirSync(templatePath).map(r => [path.basename(r, '.mu'), fs.readFileSync(path.join(templatePath, r), { encoding: 'utf-8' }).toString()]));
|
|
153
|
-
const pool = createPool({
|
|
154
|
-
host: configData.host,
|
|
155
|
-
port: configData.port,
|
|
156
|
-
user: configData.user,
|
|
157
|
-
password: configData.password,
|
|
158
|
-
database: configData.database
|
|
159
|
-
});
|
|
160
|
-
async function getTables(tableName: string) {
|
|
161
|
-
const conn = await pool.getConnection();
|
|
162
|
-
const params: (string | string[])[] = [configData.database];
|
|
163
|
-
let sql = `
|
|
164
|
-
SELECT TABLE_NAME tableName, IFNULL(TABLE_COMMENT, TABLE_NAME) title FROM information_schema.TABLES
|
|
165
|
-
WHERE TABLE_SCHEMA= ? AND TABLE_TYPE = 'BASE TABLE'`;
|
|
166
|
-
if (tableName !== '.') {
|
|
167
|
-
sql += ` AND TABLE_NAME IN (?)`;
|
|
168
|
-
params.push(tableName.split(/,|\s/).map(r => r.trim()));
|
|
169
|
-
}
|
|
170
|
-
const [result] = await conn.query<any[]>(sql, params);
|
|
171
|
-
conn.release();
|
|
172
|
-
return result;
|
|
173
|
-
}
|
|
174
|
-
async function getColumns(tableName: string) {
|
|
175
|
-
const conn = await pool.getConnection();
|
|
176
|
-
const [result] = await conn.query<any[]>(`
|
|
177
|
-
SELECT
|
|
178
|
-
DATA_TYPE type,
|
|
179
|
-
COLUMN_NAME \`name\`,
|
|
180
|
-
IFNULL(IFNULL(CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION),DATETIME_PRECISION) \`length\`,
|
|
181
|
-
NUMERIC_SCALE scale,
|
|
182
|
-
COLUMN_DEFAULT def,
|
|
183
|
-
IF(COLUMN_KEY = 'PRI', 1, 0) id,
|
|
184
|
-
IF(IS_NULLABLE = 'NO', 1, 0) notNull,
|
|
185
|
-
COLUMN_COMMENT \`comment\`
|
|
186
|
-
FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME = ?;
|
|
187
|
-
`, [configData.database, tableName]);
|
|
188
|
-
let logicDelete = '';
|
|
189
|
-
const columns = result.map(r => {
|
|
190
|
-
if (r.id === 1) { r.id = true; }
|
|
191
|
-
else delete r.id;
|
|
192
|
-
if (r.notNull === 1) { r.notNull = true; }
|
|
193
|
-
else delete r.notNull;
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const fields = new Array<string>(`type:SqlType.${r.type}`);
|
|
197
|
-
if (r.length !== null) { fields.push(`length:${r.length}`); }
|
|
198
|
-
if (r.scale !== null) { fields.push(`scale:${r.scale}`); }
|
|
199
|
-
if (r.def !== null) {
|
|
200
|
-
r.def ??= '';
|
|
201
|
-
if (isNaN(r.def) || r.def === '') {
|
|
202
|
-
fields.push(`def:'${r.def}'`);
|
|
203
|
-
} else {
|
|
204
|
-
fields.push(`def:${r.def}`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (r.id === true) {
|
|
208
|
-
fields.push(`id:true`);
|
|
209
|
-
}
|
|
210
|
-
if (r.notNull === true) { fields.push(`notNull:true`); }
|
|
211
|
-
if (r.id === true && configData.id) {
|
|
212
|
-
fields.push(configData.id);
|
|
213
|
-
}
|
|
214
|
-
if (r.name === configData.logicDeleteK) {
|
|
215
|
-
fields.push(`logicDelete: ${configData.logicDeleteV}`);
|
|
216
|
-
logicDelete = `AND ${r.name} = ${configData.NotlogicDeleteV}`;
|
|
217
|
-
}
|
|
218
|
-
if (r.comment) { fields.push(`comment: '${r.comment}'`); }
|
|
219
|
-
r.comment = r.comment ?? '';
|
|
220
|
-
r.Type = lxMap[r.type];
|
|
221
|
-
r.Field = `@Field({${fields.join(',')}})`;
|
|
222
|
-
r.Name = r.name.replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
|
|
223
|
-
r.NAME = r.Name.replace(/\w/, (v: string) => v.toUpperCase());
|
|
224
|
-
return r;
|
|
225
|
-
});
|
|
226
|
-
conn.release();
|
|
227
|
-
return { columns, logicDelete };
|
|
228
|
-
}
|
|
229
|
-
async function excute(command: string, input: string, modelName: string) {
|
|
230
|
-
const tables = await getTables(input);
|
|
231
|
-
if (input !== '.') {
|
|
232
|
-
const checkTable = input.split(/,|\s/).map(r => r.trim())
|
|
233
|
-
if (checkTable.length !== tables.length) {
|
|
234
|
-
console.error(`[错误] 输入的表与数据库查询返回表不符,数据库返回${tables.length}个:${tables.map(i => i.tableName).join(',')}`);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
modelName ??= '';
|
|
239
|
-
const modelPath = modelName ? `/${modelName}/` : '';
|
|
240
|
-
for (const { tableName, title } of tables) {
|
|
241
|
-
const { columns, logicDelete } = await getColumns(tableName);
|
|
242
|
-
const className = tableName.replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
|
|
243
|
-
const ClassName = className.replace(/\w/, (v: string) => v.toUpperCase());
|
|
244
|
-
const vueName = tableName.replace(/_/g, '-');
|
|
245
|
-
const splitName = tableName.replace(/_/g, '/');
|
|
246
|
-
const SplitName = tableName.replace(/_/, '/').replace(/_(\w)/g, (a: string, b: string) => b.toUpperCase());
|
|
247
|
-
const data = {
|
|
248
|
-
title,
|
|
249
|
-
|
|
250
|
-
tableName,
|
|
251
|
-
className,
|
|
252
|
-
ClassName,
|
|
253
|
-
vueName,
|
|
254
|
-
splitName,
|
|
255
|
-
SplitName,
|
|
256
|
-
|
|
257
|
-
columns,
|
|
258
|
-
columnNames: columns?.map(i => i.name),
|
|
259
|
-
ColumnNames: columns?.map(i => i.Name),
|
|
260
|
-
columnNames_join: columns?.map(i => i.name).join(','),
|
|
261
|
-
ColumnNames_join: columns?.map(i => `${i.name} ${i.Name}`).join(','),
|
|
262
|
-
|
|
263
|
-
columns_no_id: columns?.filter(i => !i.id),
|
|
264
|
-
columnNames_no_id: columns?.filter(i => !i.id).map(i => i.name),
|
|
265
|
-
ColumnNames_no_id: columns?.filter(i => !i.id).map(i => i.Name),
|
|
266
|
-
columnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.name).join(','),
|
|
267
|
-
ColumnNames_no_id_join: columns?.filter(i => !i.id).map(i => i.Name).join(','),
|
|
268
|
-
|
|
269
|
-
ids: columns?.filter(i => i.id),
|
|
270
|
-
idNames: columns?.filter(i => i.id).map(i => i.name),
|
|
271
|
-
IdNames: columns?.filter(i => i.id).map(i => i.Name),
|
|
272
|
-
idNames_join: columns?.filter(i => i.id).map(i => i.name).join(','),
|
|
273
|
-
IdNames_join: columns?.filter(i => i.id).map(i => i.Name).join(','),
|
|
274
|
-
|
|
275
|
-
modelName,
|
|
276
|
-
modelPath,
|
|
277
|
-
|
|
278
|
-
logicDelete
|
|
279
|
-
};
|
|
280
|
-
const template = templates[command];
|
|
281
|
-
if (!template) {
|
|
282
|
-
console.error(`[错误] ${command} 未定义模板!`);
|
|
283
|
-
return;
|
|
284
|
-
}
|
|
285
|
-
const txt = mustache.render(template, data, {}, ['<%', '%>']);
|
|
286
|
-
const _fileName = configData.command[command]!.replace(/{([a-zA-Z]+)}/g, (a: string, b: string) => data[b]);
|
|
287
|
-
const fileNames = _fileName.split(',');
|
|
288
|
-
for(const fileName of fileNames){
|
|
289
|
-
const filePath = path.join(basepath, fileName);
|
|
290
|
-
const dirname = path.dirname(filePath);
|
|
291
|
-
mkdir('-p', dirname);
|
|
292
|
-
// try {
|
|
293
|
-
// fs.statSync(dirname);
|
|
294
|
-
// } catch (error) {
|
|
295
|
-
// fs.mkdirSync(dirname);
|
|
296
|
-
// console.info(`[生成] ${dirname}`);
|
|
297
|
-
// }
|
|
298
|
-
try {
|
|
299
|
-
fs.statSync(filePath);
|
|
300
|
-
if (force === false) {
|
|
301
|
-
console.warn(`[跳过] ${filePath}`);
|
|
302
|
-
return;
|
|
303
|
-
} else {
|
|
304
|
-
console.warn(`[覆盖] ${filePath}`);
|
|
305
|
-
}
|
|
306
|
-
} catch (error) {
|
|
307
|
-
console.info(`[生成] ${filePath}`);
|
|
308
|
-
}
|
|
309
|
-
if (configData.output) {
|
|
310
|
-
outputs.add(configData.output.replace(/{([a-zA-Z]+)}/g, (a: string, b: string) => data[b]));
|
|
311
|
-
}
|
|
312
|
-
fs.writeFileSync(path.join(basepath, fileName), txt);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
const replServer = start();
|
|
317
|
-
function defineCommand(command: string, comands?: string[]) {
|
|
318
|
-
if (comands) {
|
|
319
|
-
console.log(`[组合]${command}>${comands.join(',')}注册成功`);
|
|
320
|
-
} else {
|
|
321
|
-
console.log(`[命令]${command}注册成功`);
|
|
322
|
-
}
|
|
323
|
-
if (comands) {
|
|
324
|
-
replServer.defineCommand(command, async input => {
|
|
325
|
-
outputs.clear();
|
|
326
|
-
const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
|
|
327
|
-
if (inputs?.length !== 3) {
|
|
328
|
-
return console.error(`[错误]命令格式应为: table1,table2[:模块名]`);
|
|
329
|
-
}
|
|
330
|
-
const [_, tables, modelName] = inputs;
|
|
331
|
-
for (const c of comands) {
|
|
332
|
-
await excute(c, tables!, modelName ?? '');
|
|
333
|
-
}
|
|
334
|
-
console.info('执行完毕!下面打印生成的输出');
|
|
335
|
-
console.info(Array.from(outputs).join(','));
|
|
336
|
-
});
|
|
337
|
-
} else {
|
|
338
|
-
replServer.defineCommand(command, async input => {
|
|
339
|
-
outputs.clear();
|
|
340
|
-
const inputs = input.match(/([^:]+):{0,1}([a-zA-Z0-9]*)/);
|
|
341
|
-
if (inputs?.length !== 3) {
|
|
342
|
-
return console.error(`[错误]命令格式应为: table1,table2[:模块名]`);
|
|
343
|
-
}
|
|
344
|
-
const [_, tables, modelName] = inputs;
|
|
345
|
-
await excute(command, tables!, modelName ?? '');
|
|
346
|
-
console.info('执行完毕!下面打印生成的输出');
|
|
347
|
-
console.info(Array.from(outputs).join(','));
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
replServer.defineCommand('force', () => {
|
|
352
|
-
force = !force;
|
|
353
|
-
console.log(force ? '覆盖生成' : '不覆盖生成!');
|
|
354
|
-
});
|
|
355
|
-
if (configData.command) {
|
|
356
|
-
for (const command of Object.keys(configData.command)) {
|
|
357
|
-
if (!templates[command]) {
|
|
358
|
-
console.error(`命令:${command}没有定义模板,该命令不会生效`);
|
|
359
|
-
} else {
|
|
360
|
-
defineCommand(command);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
if (configData.commands) {
|
|
365
|
-
for (const [command, commands] of Object.entries(configData.commands)) {
|
|
366
|
-
const keys = commands as string[];
|
|
367
|
-
const error = keys.filter(k => !templates[k]).join(',');
|
|
368
|
-
if (error) {
|
|
369
|
-
console.error(`组合命令:${command}定义了${commands},但是${error}没有定义模板,该命令不会生效`);
|
|
370
|
-
} else {
|
|
371
|
-
defineCommand(command, keys);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
} catch (error) {
|
|
376
|
-
console.error(error);
|
|
377
|
-
}
|