baja-lite 1.4.6 → 1.4.8
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/boot.js +11 -8
- package/object.d.ts +2 -2
- package/object.js +25 -3
- package/package.json +1 -1
- package/sql.d.ts +78 -2
- package/sql.js +135 -87
package/boot.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { _Hump, DBType, getEnums } from 'baja-lite-field';
|
|
2
2
|
import events from 'events';
|
|
3
|
-
import { _dao, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _path, _primaryDB, _sqlCache, ColumnMode, logger, Mysql, Postgresql, SqlCache, Sqlite, SqliteRemote } from './sql.js';
|
|
3
|
+
import { _dao, _DataConvert, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _path, _primaryDB, _sqlCache, ColumnMode, logger, Mysql, Postgresql, SqlCache, Sqlite, SqliteRemote } from './sql.js';
|
|
4
4
|
export const Boot = async function (options) {
|
|
5
5
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption);
|
|
6
6
|
if (options.skipEmptyString !== undefined) {
|
|
@@ -16,12 +16,12 @@ export const Boot = async function (options) {
|
|
|
16
16
|
globalThis[_GlobalSqlOption].maxDeal = options.maxDeal;
|
|
17
17
|
}
|
|
18
18
|
globalThis[_Hump] = options.columnMode === ColumnMode.HUMP;
|
|
19
|
+
logger.level = options.log ?? 'info';
|
|
20
|
+
globalThis[_sqlCache] = new SqlCache();
|
|
19
21
|
if (options.sqlDir) {
|
|
20
22
|
globalThis[_path] = await import('path');
|
|
21
23
|
globalThis[_fs] = await import('fs');
|
|
22
24
|
}
|
|
23
|
-
logger.level = options.log ?? 'info';
|
|
24
|
-
globalThis[_sqlCache] = new SqlCache();
|
|
25
25
|
if (options.sqlMap || options.sqlDir) {
|
|
26
26
|
await globalThis[_sqlCache].init(options);
|
|
27
27
|
}
|
|
@@ -36,6 +36,9 @@ export const Boot = async function (options) {
|
|
|
36
36
|
if (options.enums) {
|
|
37
37
|
globalThis[_enum] = getEnums(options.enums);
|
|
38
38
|
}
|
|
39
|
+
if (options.dataConvert) {
|
|
40
|
+
globalThis[_DataConvert] = options.dataConvert;
|
|
41
|
+
}
|
|
39
42
|
if (options.Mysql) {
|
|
40
43
|
const { createPool } = await import('mysql2/promise');
|
|
41
44
|
if (options.Mysql['host']) {
|
|
@@ -55,7 +58,7 @@ export const Boot = async function (options) {
|
|
|
55
58
|
decimalNumbers: true,
|
|
56
59
|
supportBigNumbers: true
|
|
57
60
|
}));
|
|
58
|
-
if (flag
|
|
61
|
+
if (!flag) {
|
|
59
62
|
globalThis[_dao][DBType.Mysql][_primaryDB] = db;
|
|
60
63
|
flag = true;
|
|
61
64
|
}
|
|
@@ -71,7 +74,7 @@ export const Boot = async function (options) {
|
|
|
71
74
|
let flag = false;
|
|
72
75
|
for (const [key, fileName] of Object.entries(options.Sqlite)) {
|
|
73
76
|
const db = new Sqlite(new options.BetterSqlite3(fileName, { fileMustExist: false }));
|
|
74
|
-
if (flag
|
|
77
|
+
if (!flag) {
|
|
75
78
|
globalThis[_dao][DBType.Sqlite][_primaryDB] = db;
|
|
76
79
|
flag = true;
|
|
77
80
|
}
|
|
@@ -89,7 +92,7 @@ export const Boot = async function (options) {
|
|
|
89
92
|
for (const [key, fileName] of Object.entries(options.SqliteRemote.db)) {
|
|
90
93
|
options.SqliteRemote.service.initDB(fileName);
|
|
91
94
|
const db = new SqliteRemote(options.SqliteRemote.service, fileName);
|
|
92
|
-
if (flag
|
|
95
|
+
if (!flag) {
|
|
93
96
|
globalThis[_dao][DBType.SqliteRemote][_primaryDB] = db;
|
|
94
97
|
flag = true;
|
|
95
98
|
}
|
|
@@ -107,7 +110,7 @@ export const Boot = async function (options) {
|
|
|
107
110
|
let flag = false;
|
|
108
111
|
for (const [key, option] of Object.entries(options.Redis)) {
|
|
109
112
|
const db = new Redis(option);
|
|
110
|
-
if (flag
|
|
113
|
+
if (!flag) {
|
|
111
114
|
globalThis[_dao][DBType.Redis][_primaryDB] = db;
|
|
112
115
|
flag = true;
|
|
113
116
|
}
|
|
@@ -149,7 +152,7 @@ export const Boot = async function (options) {
|
|
|
149
152
|
let flag = false;
|
|
150
153
|
for (const [key, option] of Object.entries(options.Postgresql)) {
|
|
151
154
|
const db = new Postgresql(new Pool.default(option));
|
|
152
|
-
if (flag
|
|
155
|
+
if (!flag) {
|
|
153
156
|
globalThis[_dao][DBType.Postgresql][_primaryDB] = db;
|
|
154
157
|
flag = true;
|
|
155
158
|
}
|
package/object.d.ts
CHANGED
|
@@ -77,8 +77,8 @@ export declare const arraySplit: <T = any>(datas: T[], { everyLength, groupCount
|
|
|
77
77
|
}) => T[][];
|
|
78
78
|
export declare const P2C: (pro: string, IF?: boolean) => string;
|
|
79
79
|
export declare const C2P: (pro: string, IF?: boolean) => string;
|
|
80
|
-
export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
|
|
81
|
-
export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L): T;
|
|
80
|
+
export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L[], hump?: boolean, convert?: Record<string, (data: any) => any>): T[];
|
|
81
|
+
export declare function C2P2<T extends Object = any, L extends Object = T>(datas: L, hump?: boolean, convert?: Record<string, (data: any) => any>): T;
|
|
82
82
|
export declare function P2C2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
|
|
83
83
|
export declare function P2C2<T extends Object = any, L extends Object = T>(datas: L): T;
|
|
84
84
|
export declare function fillArrayToMinLength<T>(arr: T[], minLength: number, fillValue: T): T[];
|
package/object.js
CHANGED
|
@@ -198,12 +198,34 @@ const P2CEX = /[A-Z]/g;
|
|
|
198
198
|
export const P2C = (pro, IF = true) => IF ? pro.replace(P2CEX, (a) => `_${a.toLowerCase()}`) : pro;
|
|
199
199
|
const C2PEX = /_([a-z])/g;
|
|
200
200
|
export const C2P = (pro, IF = true) => IF ? pro.replace(C2PEX, (a, b) => `${b.toUpperCase()}`) : pro;
|
|
201
|
-
export function C2P2(datas) {
|
|
201
|
+
export function C2P2(datas, hump, convert) {
|
|
202
202
|
if (datas instanceof Array) {
|
|
203
|
-
return iterate(datas)
|
|
203
|
+
return iterate(datas)
|
|
204
|
+
.map((data) => Object.fromEntries(Object.entries(data).map(([K, V]) => {
|
|
205
|
+
if (hump) {
|
|
206
|
+
K = C2P(K);
|
|
207
|
+
}
|
|
208
|
+
if (convert && convert[K]) {
|
|
209
|
+
return [K, convert[K](V)];
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
return [K, V];
|
|
213
|
+
}
|
|
214
|
+
})))
|
|
215
|
+
.toArray();
|
|
204
216
|
}
|
|
205
217
|
else if (datas) {
|
|
206
|
-
return Object.fromEntries(Object.entries(datas).map(([K, V]) =>
|
|
218
|
+
return Object.fromEntries(Object.entries(datas).map(([K, V]) => {
|
|
219
|
+
if (hump) {
|
|
220
|
+
K = C2P(K);
|
|
221
|
+
}
|
|
222
|
+
if (convert && convert[K]) {
|
|
223
|
+
return [K, convert[K](V)];
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
return [K, V];
|
|
227
|
+
}
|
|
228
|
+
}));
|
|
207
229
|
}
|
|
208
230
|
else {
|
|
209
231
|
return datas;
|
package/package.json
CHANGED
package/sql.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ declare const _inTransaction: unique symbol;
|
|
|
20
20
|
declare const _daoDB: unique symbol;
|
|
21
21
|
declare const _sqliteRemoteName: unique symbol;
|
|
22
22
|
declare const _SqlOption: unique symbol;
|
|
23
|
+
export declare const _DataConvert: unique symbol;
|
|
23
24
|
export declare const _enum: unique symbol;
|
|
24
25
|
export declare const _GlobalSqlOption: unique symbol;
|
|
25
26
|
export declare const _EventBus: unique symbol;
|
|
@@ -247,6 +248,17 @@ export interface GlobalSqlOptionForWeb {
|
|
|
247
248
|
columnMode?: ColumnMode;
|
|
248
249
|
/** 对于WEB模式,默认为SqliteRemote */
|
|
249
250
|
dbType?: DBType;
|
|
251
|
+
/**
|
|
252
|
+
* * 数据转换器,用于service的select、page方法,以及stream查询
|
|
253
|
+
* * 例如
|
|
254
|
+
* ```
|
|
255
|
+
* dataConvert: {
|
|
256
|
+
* // 使用时,传入qiniu和列名,将根据列名对应的值生成URL
|
|
257
|
+
* qiniu: data => qiniuConvertUrl(data)
|
|
258
|
+
* }
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
dataConvert?: Record<string, (data: any) => any>;
|
|
250
262
|
}
|
|
251
263
|
/**
|
|
252
264
|
# 全局行为配置文件
|
|
@@ -398,6 +410,19 @@ export interface GlobalSqlOption extends GlobalSqlOptionForWeb {
|
|
|
398
410
|
## 文件名就是模板名
|
|
399
411
|
*/
|
|
400
412
|
sqlFNDir?: string;
|
|
413
|
+
/**
|
|
414
|
+
* #TODO 未完成读取 sqlMapDir
|
|
415
|
+
* # sqlMapDir 目录定义如下,
|
|
416
|
+
* `test.ts`
|
|
417
|
+
* ```
|
|
418
|
+
* export const dict:SqlMappers = [
|
|
419
|
+
* {columnName: 'dit_id', mapNames: ['DTID'], def: 0, convert: 转换函数}, // 列名ditid,对应属性DTID,如果没有值,将返回默认值0,其中默认值0是可选的
|
|
420
|
+
* {columnName: 'event_id', mapNames: ['eventMainInfo', 'id'], def: 0, convert: 转换函数},// 列名event_id对应属性eventMainInfo.id,这种方式将返回嵌套的json对象,其中默认值null是可选的
|
|
421
|
+
* ]
|
|
422
|
+
* ```
|
|
423
|
+
* 将得到 test.dict 这个map
|
|
424
|
+
*/
|
|
425
|
+
sqlMapDir?: string;
|
|
401
426
|
/**
|
|
402
427
|
## 映射数据为对象,文件名就是模板名
|
|
403
428
|
```
|
|
@@ -596,7 +621,12 @@ export declare class SqliteRemote implements Dao {
|
|
|
596
621
|
restore(sync: SyncMode.Sync, importPath: string): void;
|
|
597
622
|
restore(sync: SyncMode.Async, importPath: string): Promise<void>;
|
|
598
623
|
}
|
|
599
|
-
export type SqlMapper =
|
|
624
|
+
export type SqlMapper = {
|
|
625
|
+
columnName: string;
|
|
626
|
+
mapNames: string[];
|
|
627
|
+
def?: any;
|
|
628
|
+
convert?: (data: any) => any;
|
|
629
|
+
}[];
|
|
600
630
|
export type SqlMappers = Record<string, SqlMapper>;
|
|
601
631
|
export type SqlModel = Record<string, string | ((options: {
|
|
602
632
|
ctx?: any;
|
|
@@ -1084,7 +1114,33 @@ export declare class SqlService<T extends object> {
|
|
|
1084
1114
|
9. `dao`: 永远不需要传入该值
|
|
1085
1115
|
10. `hump`: 是否将列名改为驼峰写法?默认情况下按照全局配置
|
|
1086
1116
|
11. `mapper`: 列名-属性 映射工具,优先级高于hump
|
|
1087
|
-
|
|
1117
|
+
```
|
|
1118
|
+
// 该属性支持传入mybatis.xml中定义的resultMap块ID,或者读取sqlMapDir目录下的JSON文件.
|
|
1119
|
+
// 注意:resultMap块ID与sql语句逻辑一致,同样是 目录.ID
|
|
1120
|
+
// 或者自定义Mapper,自定义Mapper格式如下:
|
|
1121
|
+
[
|
|
1122
|
+
{columnName: 'dit_id', mapNames: ['DTID'], def: 0, convert: 转换函数}, // 列名ditid,对应属性DTID,如果没有值,将返回默认值0,其中默认值0是可选的
|
|
1123
|
+
{columnName: 'event_id', mapNames: ['eventMainInfo', 'id'], def: 0, convert: 转换函数},// 列名event_id对应属性eventMainInfo.id,这种方式将返回嵌套的json对象,其中默认值null是可选的
|
|
1124
|
+
]
|
|
1125
|
+
```
|
|
1126
|
+
#TODO 未完成读取 sqlMapDir
|
|
1127
|
+
#### sqlMapDir 目录定义如下,
|
|
1128
|
+
`test.ts`
|
|
1129
|
+
```
|
|
1130
|
+
export const dict:SqlMappers = [
|
|
1131
|
+
{columnName: 'dit_id', mapNames: ['DTID'], def: 0, convert: 转换函数}, // 列名ditid,对应属性DTID,如果没有值,将返回默认值0,其中默认值0是可选的
|
|
1132
|
+
{columnName: 'event_id', mapNames: ['eventMainInfo', 'id'], def: 0, convert: 转换函数},// 列名event_id对应属性eventMainInfo.id,这种方式将返回嵌套的json对象,其中默认值null是可选的
|
|
1133
|
+
]
|
|
1134
|
+
```
|
|
1135
|
+
将得到 test.dict 这个map
|
|
1136
|
+
12. dataConvert 数据转换器
|
|
1137
|
+
```
|
|
1138
|
+
dataConvert: {
|
|
1139
|
+
fileName: 'qiniu'
|
|
1140
|
+
}
|
|
1141
|
+
// 表示列 fileName 按 qiniu的函数格式化
|
|
1142
|
+
// qiniu 在开始时定义
|
|
1143
|
+
```
|
|
1088
1144
|
*/
|
|
1089
1145
|
select<L = T>(option: MethodOption & {
|
|
1090
1146
|
sync?: SyncMode.Async;
|
|
@@ -1099,6 +1155,7 @@ export declare class SqlService<T extends object> {
|
|
|
1099
1155
|
hump?: boolean;
|
|
1100
1156
|
mapper?: string | SqlMapper;
|
|
1101
1157
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1158
|
+
dataConvert?: Record<string, string>;
|
|
1102
1159
|
}): Promise<L[]>;
|
|
1103
1160
|
select<L = T>(option: MethodOption & {
|
|
1104
1161
|
sync?: SyncMode.Async;
|
|
@@ -1113,6 +1170,7 @@ export declare class SqlService<T extends object> {
|
|
|
1113
1170
|
hump?: boolean;
|
|
1114
1171
|
mapper?: string | SqlMapper;
|
|
1115
1172
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1173
|
+
dataConvert?: Record<string, string>;
|
|
1116
1174
|
}): Promise<ArrayList<L>>;
|
|
1117
1175
|
select<L = T>(option: MethodOption & {
|
|
1118
1176
|
sync?: SyncMode.Async;
|
|
@@ -1127,6 +1185,7 @@ export declare class SqlService<T extends object> {
|
|
|
1127
1185
|
hump?: boolean;
|
|
1128
1186
|
mapper?: string | SqlMapper;
|
|
1129
1187
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1188
|
+
dataConvert?: Record<string, string>;
|
|
1130
1189
|
}): Promise<L>;
|
|
1131
1190
|
select<L = T>(option: MethodOption & {
|
|
1132
1191
|
sync?: SyncMode.Async;
|
|
@@ -1141,6 +1200,7 @@ export declare class SqlService<T extends object> {
|
|
|
1141
1200
|
hump?: boolean;
|
|
1142
1201
|
mapper?: string | SqlMapper;
|
|
1143
1202
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1203
|
+
dataConvert?: Record<string, string>;
|
|
1144
1204
|
}): Promise<L | null>;
|
|
1145
1205
|
select<L = T>(option: MethodOption & {
|
|
1146
1206
|
sync: SyncMode.Sync;
|
|
@@ -1155,6 +1215,7 @@ export declare class SqlService<T extends object> {
|
|
|
1155
1215
|
hump?: boolean;
|
|
1156
1216
|
mapper?: string | SqlMapper;
|
|
1157
1217
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1218
|
+
dataConvert?: Record<string, string>;
|
|
1158
1219
|
}): L[];
|
|
1159
1220
|
select<L = T>(option: MethodOption & {
|
|
1160
1221
|
sync: SyncMode.Sync;
|
|
@@ -1169,6 +1230,7 @@ export declare class SqlService<T extends object> {
|
|
|
1169
1230
|
hump?: boolean;
|
|
1170
1231
|
mapper?: string | SqlMapper;
|
|
1171
1232
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1233
|
+
dataConvert?: Record<string, string>;
|
|
1172
1234
|
}): ArrayList<L>;
|
|
1173
1235
|
select<L = T>(option: MethodOption & {
|
|
1174
1236
|
sync: SyncMode.Sync;
|
|
@@ -1183,6 +1245,7 @@ export declare class SqlService<T extends object> {
|
|
|
1183
1245
|
hump?: boolean;
|
|
1184
1246
|
mapper?: string | SqlMapper;
|
|
1185
1247
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1248
|
+
dataConvert?: Record<string, string>;
|
|
1186
1249
|
}): L;
|
|
1187
1250
|
select<L = T>(option: MethodOption & {
|
|
1188
1251
|
sync: SyncMode.Sync;
|
|
@@ -1197,6 +1260,7 @@ export declare class SqlService<T extends object> {
|
|
|
1197
1260
|
hump?: boolean;
|
|
1198
1261
|
mapper?: string | SqlMapper;
|
|
1199
1262
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1263
|
+
dataConvert?: Record<string, string>;
|
|
1200
1264
|
}): L | null;
|
|
1201
1265
|
/**
|
|
1202
1266
|
# 自由执行sql
|
|
@@ -1268,6 +1332,7 @@ export declare class SqlService<T extends object> {
|
|
|
1268
1332
|
hump?: boolean;
|
|
1269
1333
|
mapper?: string | SqlMapper;
|
|
1270
1334
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1335
|
+
dataConvert?: Record<string, string>;
|
|
1271
1336
|
}): Promise<PageQuery<L>>;
|
|
1272
1337
|
page<L = T>(option: MethodOption & {
|
|
1273
1338
|
sync: SyncMode.Sync;
|
|
@@ -1284,6 +1349,7 @@ export declare class SqlService<T extends object> {
|
|
|
1284
1349
|
hump?: boolean;
|
|
1285
1350
|
mapper?: string | SqlMapper;
|
|
1286
1351
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1352
|
+
dataConvert?: Record<string, string>;
|
|
1287
1353
|
}): PageQuery<L>;
|
|
1288
1354
|
/**
|
|
1289
1355
|
* 导出数据,可以为EJS-EXCEL直接使用
|
|
@@ -1689,6 +1755,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1689
1755
|
update(key: keyof T, value: T[keyof T]): this;
|
|
1690
1756
|
update2(sql: string, param?: Record<string, any>): this;
|
|
1691
1757
|
updateT(t: Partial<T>): this;
|
|
1758
|
+
/** SET key = REPLACE(key, :valueToFind, :valueToReplace) */
|
|
1692
1759
|
replace(key: keyof T, valueToFind: T[keyof T], valueToReplace: T[keyof T]): this;
|
|
1693
1760
|
excuteSelect<L = T>(option?: MethodOption & {
|
|
1694
1761
|
sync?: SyncMode.Async;
|
|
@@ -1696,6 +1763,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1696
1763
|
hump?: boolean;
|
|
1697
1764
|
mapper?: string | SqlMapper;
|
|
1698
1765
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1766
|
+
dataConvert?: Record<string, string>;
|
|
1699
1767
|
}): Promise<L[]>;
|
|
1700
1768
|
excuteSelect<L = T>(option?: MethodOption & {
|
|
1701
1769
|
sync?: SyncMode.Async;
|
|
@@ -1703,6 +1771,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1703
1771
|
hump?: boolean;
|
|
1704
1772
|
mapper?: string | SqlMapper;
|
|
1705
1773
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1774
|
+
dataConvert?: Record<string, string>;
|
|
1706
1775
|
}): Promise<ArrayList<L>>;
|
|
1707
1776
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1708
1777
|
sync?: SyncMode.Async;
|
|
@@ -1711,6 +1780,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1711
1780
|
hump?: boolean;
|
|
1712
1781
|
mapper?: string | SqlMapper;
|
|
1713
1782
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1783
|
+
dataConvert?: Record<string, string>;
|
|
1714
1784
|
}): Promise<L>;
|
|
1715
1785
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1716
1786
|
sync?: SyncMode.Async;
|
|
@@ -1718,6 +1788,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1718
1788
|
hump?: boolean;
|
|
1719
1789
|
mapper?: string | SqlMapper;
|
|
1720
1790
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1791
|
+
dataConvert?: Record<string, string>;
|
|
1721
1792
|
}): Promise<L | null>;
|
|
1722
1793
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1723
1794
|
sync: SyncMode.Sync;
|
|
@@ -1725,6 +1796,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1725
1796
|
hump?: boolean;
|
|
1726
1797
|
mapper?: string | SqlMapper;
|
|
1727
1798
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1799
|
+
dataConvert?: Record<string, string>;
|
|
1728
1800
|
}): L[];
|
|
1729
1801
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1730
1802
|
sync: SyncMode.Sync;
|
|
@@ -1732,6 +1804,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1732
1804
|
hump?: boolean;
|
|
1733
1805
|
mapper?: string | SqlMapper;
|
|
1734
1806
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1807
|
+
dataConvert?: Record<string, string>;
|
|
1735
1808
|
}): ArrayList<L>;
|
|
1736
1809
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1737
1810
|
sync: SyncMode.Sync;
|
|
@@ -1740,6 +1813,7 @@ declare class StreamQuery<T extends object> {
|
|
|
1740
1813
|
hump?: boolean;
|
|
1741
1814
|
mapper?: string | SqlMapper;
|
|
1742
1815
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1816
|
+
dataConvert?: Record<string, string>;
|
|
1743
1817
|
}): L;
|
|
1744
1818
|
excuteSelect<L = T>(option: MethodOption & {
|
|
1745
1819
|
sync: SyncMode.Sync;
|
|
@@ -1747,12 +1821,14 @@ declare class StreamQuery<T extends object> {
|
|
|
1747
1821
|
hump?: boolean;
|
|
1748
1822
|
mapper?: string | SqlMapper;
|
|
1749
1823
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1824
|
+
dataConvert?: Record<string, string>;
|
|
1750
1825
|
}): L | null;
|
|
1751
1826
|
excutePage<L = T>(option?: MethodOption & {
|
|
1752
1827
|
sync?: SyncMode;
|
|
1753
1828
|
hump?: boolean;
|
|
1754
1829
|
mapper?: string | SqlMapper;
|
|
1755
1830
|
mapperIfUndefined?: MapperIfUndefined;
|
|
1831
|
+
dataConvert?: Record<string, string>;
|
|
1756
1832
|
}): PageQuery<L> | Promise<PageQuery<L>>;
|
|
1757
1833
|
excuteUpdate(option?: MethodOption & {
|
|
1758
1834
|
sync?: SyncMode.Async;
|
package/sql.js
CHANGED
|
@@ -70,6 +70,7 @@ const _inTransaction = Symbol('inTransaction');
|
|
|
70
70
|
const _daoDB = Symbol('daoDB');
|
|
71
71
|
const _sqliteRemoteName = Symbol('sqliteRemoteName');
|
|
72
72
|
const _SqlOption = Symbol('SqlOption');
|
|
73
|
+
export const _DataConvert = Symbol('DataConvert');
|
|
73
74
|
const _resultMap = Symbol('resultMap');
|
|
74
75
|
const _resultMap_SQLID = Symbol('resultMap_SQLID');
|
|
75
76
|
export const _enum = Symbol('_enum');
|
|
@@ -449,7 +450,7 @@ export class Mysql {
|
|
|
449
450
|
conn[_inTransaction] = true;
|
|
450
451
|
try {
|
|
451
452
|
const result = await fn(conn);
|
|
452
|
-
if (needCommit
|
|
453
|
+
if (needCommit) {
|
|
453
454
|
logger.debug('commit begin!');
|
|
454
455
|
await conn[_daoConnection].commit();
|
|
455
456
|
conn[_inTransaction] = false;
|
|
@@ -467,10 +468,10 @@ export class Mysql {
|
|
|
467
468
|
}
|
|
468
469
|
finally {
|
|
469
470
|
try {
|
|
470
|
-
if (needCommit
|
|
471
|
+
if (needCommit) {
|
|
471
472
|
conn[_inTransaction] = false;
|
|
472
473
|
}
|
|
473
|
-
if (newConn
|
|
474
|
+
if (newConn) {
|
|
474
475
|
logger.debug('release begin!');
|
|
475
476
|
conn[_daoConnection].release();
|
|
476
477
|
logger.debug('release end!');
|
|
@@ -748,7 +749,7 @@ export class Postgresql {
|
|
|
748
749
|
conn[_inTransaction] = true;
|
|
749
750
|
try {
|
|
750
751
|
const result = await fn(conn);
|
|
751
|
-
if (needCommit
|
|
752
|
+
if (needCommit) {
|
|
752
753
|
logger.debug('commit begin!');
|
|
753
754
|
await conn[_daoConnection].query('COMMIT');
|
|
754
755
|
conn[_inTransaction] = false;
|
|
@@ -766,10 +767,10 @@ export class Postgresql {
|
|
|
766
767
|
}
|
|
767
768
|
finally {
|
|
768
769
|
try {
|
|
769
|
-
if (needCommit
|
|
770
|
+
if (needCommit) {
|
|
770
771
|
conn[_inTransaction] = false;
|
|
771
772
|
}
|
|
772
|
-
if (newConn
|
|
773
|
+
if (newConn) {
|
|
773
774
|
logger.debug('release begin!');
|
|
774
775
|
conn[_daoConnection].release();
|
|
775
776
|
logger.debug('release end!');
|
|
@@ -1228,7 +1229,7 @@ class Build {
|
|
|
1228
1229
|
*/
|
|
1229
1230
|
page() {
|
|
1230
1231
|
return (text, render) => {
|
|
1231
|
-
if (this.isCount
|
|
1232
|
+
if (this.isCount) {
|
|
1232
1233
|
return Build.page;
|
|
1233
1234
|
}
|
|
1234
1235
|
else if (this.isSum !== true) {
|
|
@@ -1260,7 +1261,7 @@ class Build {
|
|
|
1260
1261
|
*/
|
|
1261
1262
|
notPage() {
|
|
1262
1263
|
return (text, render) => {
|
|
1263
|
-
if (this.isCount
|
|
1264
|
+
if (this.isCount || this.isSum) {
|
|
1264
1265
|
return '';
|
|
1265
1266
|
}
|
|
1266
1267
|
else {
|
|
@@ -1351,7 +1352,7 @@ class Build {
|
|
|
1351
1352
|
*/
|
|
1352
1353
|
order() {
|
|
1353
1354
|
return (text, render) => {
|
|
1354
|
-
if (this.isCount
|
|
1355
|
+
if (this.isCount || this.isSum) {
|
|
1355
1356
|
return '';
|
|
1356
1357
|
}
|
|
1357
1358
|
else {
|
|
@@ -1381,7 +1382,7 @@ class Build {
|
|
|
1381
1382
|
*/
|
|
1382
1383
|
group() {
|
|
1383
1384
|
return (text, render) => {
|
|
1384
|
-
if (this.isCount
|
|
1385
|
+
if (this.isCount || this.isSum) {
|
|
1385
1386
|
return '';
|
|
1386
1387
|
}
|
|
1387
1388
|
else {
|
|
@@ -1463,7 +1464,7 @@ class Build {
|
|
|
1463
1464
|
* * t.problemtype = 列名
|
|
1464
1465
|
*
|
|
1465
1466
|
* ```
|
|
1466
|
-
* {{#
|
|
1467
|
+
* {{#enumTag}} PROBLEM_TYPE(t.problemtype) {{/enumTag}}
|
|
1467
1468
|
* ```
|
|
1468
1469
|
*/
|
|
1469
1470
|
enum() {
|
|
@@ -1508,7 +1509,7 @@ function replaceCdata(rawText) {
|
|
|
1508
1509
|
}
|
|
1509
1510
|
return rawText;
|
|
1510
1511
|
}
|
|
1511
|
-
function _flatData(result, i, length, keys, V) {
|
|
1512
|
+
function _flatData(result, i, length, keys, V, convert) {
|
|
1512
1513
|
var _f;
|
|
1513
1514
|
const key = keys[i];
|
|
1514
1515
|
if (i < length) {
|
|
@@ -1517,7 +1518,12 @@ function _flatData(result, i, length, keys, V) {
|
|
|
1517
1518
|
_flatData(result[key], i, length, keys, V);
|
|
1518
1519
|
}
|
|
1519
1520
|
else {
|
|
1520
|
-
|
|
1521
|
+
if (convert) {
|
|
1522
|
+
result[key] = convert(V);
|
|
1523
|
+
}
|
|
1524
|
+
else {
|
|
1525
|
+
result[key] = V;
|
|
1526
|
+
}
|
|
1521
1527
|
}
|
|
1522
1528
|
}
|
|
1523
1529
|
/**
|
|
@@ -1532,7 +1538,7 @@ export function flatData(options) {
|
|
|
1532
1538
|
options.mapperIfUndefined ?? (options.mapperIfUndefined = MapperIfUndefined.Skip);
|
|
1533
1539
|
options.mapper = options.mapper;
|
|
1534
1540
|
const result = {};
|
|
1535
|
-
for (const
|
|
1541
|
+
for (const { columnName, mapNames, def, convert } of options.mapper) {
|
|
1536
1542
|
let V = options.data[columnName];
|
|
1537
1543
|
if (V === undefined) {
|
|
1538
1544
|
if (options.mapperIfUndefined === MapperIfUndefined.Null) {
|
|
@@ -1551,7 +1557,7 @@ export function flatData(options) {
|
|
|
1551
1557
|
continue;
|
|
1552
1558
|
}
|
|
1553
1559
|
}
|
|
1554
|
-
_flatData(result, 0,
|
|
1560
|
+
_flatData(result, 0, mapNames.length - 1, mapNames, V, convert);
|
|
1555
1561
|
}
|
|
1556
1562
|
return result;
|
|
1557
1563
|
}
|
|
@@ -1584,7 +1590,7 @@ export class SqlCache {
|
|
|
1584
1590
|
}
|
|
1585
1591
|
logger.debug(`sql: ${file} explain over[${ct}]!`);
|
|
1586
1592
|
}
|
|
1587
|
-
else if (jsMode
|
|
1593
|
+
else if (jsMode && extname === '.js') {
|
|
1588
1594
|
logger.debug(`sql: ${file} start explain!`);
|
|
1589
1595
|
const obj = (await import(globalThis[_path].join(sqlDir, modeName))).default;
|
|
1590
1596
|
for (const [key, fn] of Object.entries(obj)) {
|
|
@@ -1610,8 +1616,8 @@ export class SqlCache {
|
|
|
1610
1616
|
globalThis[_resultMap] ?? (globalThis[_resultMap] = {});
|
|
1611
1617
|
const keys = [];
|
|
1612
1618
|
this.readResultMap(am.children, keys, []);
|
|
1613
|
-
globalThis[_resultMap][am.id] = keys;
|
|
1614
|
-
logger.debug(`sql_resultMap: ${am.id} found!`);
|
|
1619
|
+
globalThis[_resultMap][`${rootName || name}.${am.id}`] = keys;
|
|
1620
|
+
logger.debug(`sql_resultMap: ${`${rootName || name}.${am.id}`} found!`);
|
|
1615
1621
|
}
|
|
1616
1622
|
else {
|
|
1617
1623
|
this.sqlMap[`${rootName || name}.${am.id}`] = am.children;
|
|
@@ -1648,7 +1654,10 @@ export class SqlCache {
|
|
|
1648
1654
|
for (const am of ams) {
|
|
1649
1655
|
if (am.type === 'tag') {
|
|
1650
1656
|
if (am.name === 'result' || am.name === 'id') {
|
|
1651
|
-
keys.push(
|
|
1657
|
+
keys.push({
|
|
1658
|
+
columnName: am.attrs['column'],
|
|
1659
|
+
mapNames: [...key, am.attrs['property']]
|
|
1660
|
+
});
|
|
1652
1661
|
}
|
|
1653
1662
|
else {
|
|
1654
1663
|
this.readResultMap(am.children, keys, [...key, am.attrs['property']]);
|
|
@@ -1783,12 +1792,7 @@ function P(skipConn = false) {
|
|
|
1783
1792
|
return result;
|
|
1784
1793
|
}
|
|
1785
1794
|
catch (error) {
|
|
1786
|
-
|
|
1787
|
-
console.error(`service ${propertyKey} have an error:${error}`);
|
|
1788
|
-
}
|
|
1789
|
-
catch (error) {
|
|
1790
|
-
console.error(`${option.sqlId ?? option.tableName} service ${propertyKey} have an error:${error}`);
|
|
1791
|
-
}
|
|
1795
|
+
console.error(`${option.sqlId ?? option.tableName} service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1792
1796
|
throw error;
|
|
1793
1797
|
}
|
|
1794
1798
|
finally {
|
|
@@ -1827,7 +1831,6 @@ function P(skipConn = false) {
|
|
|
1827
1831
|
}
|
|
1828
1832
|
catch (error) {
|
|
1829
1833
|
console.error(`${option.sqlId ?? option.tableName} service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1830
|
-
// console.error(`${(option as any).sqlId ?? option!.tableName} service ${propertyKey} have an error:${error}`)
|
|
1831
1834
|
reject(error);
|
|
1832
1835
|
}
|
|
1833
1836
|
finally {
|
|
@@ -1858,7 +1861,6 @@ function P(skipConn = false) {
|
|
|
1858
1861
|
}
|
|
1859
1862
|
catch (error) {
|
|
1860
1863
|
console.error(`${option.sqlId ?? option.tableName} service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1861
|
-
// console.error(`${(option as any).sqlId ?? option!.tableName} service ${propertyKey} have an error:${error}`)
|
|
1862
1864
|
reject(error);
|
|
1863
1865
|
}
|
|
1864
1866
|
finally {
|
|
@@ -1889,7 +1891,6 @@ function P(skipConn = false) {
|
|
|
1889
1891
|
}
|
|
1890
1892
|
catch (error) {
|
|
1891
1893
|
console.error(`${option.sqlId ?? option.tableName} service ${propertyKey} have an error:${error}, it's argumens: ${JSON.stringify(args.filter(i => typeof i !== 'object' || (typeof i === 'object' && !i.insert)))}`);
|
|
1892
|
-
// console.error(`${(option as any).sqlId ?? option!.tableName} service ${propertyKey} have an error:${error}`)
|
|
1893
1894
|
reject(error);
|
|
1894
1895
|
}
|
|
1895
1896
|
finally {
|
|
@@ -1910,7 +1911,7 @@ const FieldFilter = (K, V, def, uuidColumn, option) => {
|
|
|
1910
1911
|
let ret = 0;
|
|
1911
1912
|
// 如果是插入操作且字段是UUID,则不进行空值检查
|
|
1912
1913
|
// 只有在非插入或者非UUID时,进行空置检查
|
|
1913
|
-
if (option?.insert
|
|
1914
|
+
if (option?.insert && uuidColumn) {
|
|
1914
1915
|
ret = 1;
|
|
1915
1916
|
if (V === undefined || emptyString(`${V ?? ''}`)) {
|
|
1916
1917
|
V = null;
|
|
@@ -1920,19 +1921,19 @@ const FieldFilter = (K, V, def, uuidColumn, option) => {
|
|
|
1920
1921
|
if (V === null) {
|
|
1921
1922
|
if (option?.skipNull !== true) {
|
|
1922
1923
|
ret = 1;
|
|
1923
|
-
V = option?.insert
|
|
1924
|
+
V = option?.insert && def && def.hasOwnProperty(K) ? def[K] : null;
|
|
1924
1925
|
}
|
|
1925
1926
|
}
|
|
1926
1927
|
else if (V === undefined) {
|
|
1927
1928
|
if (option?.skipUndefined !== true) {
|
|
1928
1929
|
ret = 1;
|
|
1929
|
-
V = option?.insert
|
|
1930
|
+
V = option?.insert && def && def.hasOwnProperty(K) ? def[K] : null;
|
|
1930
1931
|
}
|
|
1931
1932
|
}
|
|
1932
1933
|
else if (emptyString(`${V ?? ''}`)) {
|
|
1933
1934
|
if (option?.skipEmptyString !== true) {
|
|
1934
1935
|
ret = 1;
|
|
1935
|
-
V = option?.insert
|
|
1936
|
+
V = option?.insert && def && def.hasOwnProperty(K) ? def[K] : '';
|
|
1936
1937
|
}
|
|
1937
1938
|
}
|
|
1938
1939
|
else {
|
|
@@ -1989,8 +1990,8 @@ export const DB = (config) => {
|
|
|
1989
1990
|
this[_x] = __stateFileName;
|
|
1990
1991
|
this[_y] = __deleteState;
|
|
1991
1992
|
this[_z] = (data, option) => {
|
|
1992
|
-
return Object.fromEntries(iterate(option?.skipId
|
|
1993
|
-
.map(K => [K, FieldFilter(K, data[K], __def, __fields[K].uuid
|
|
1993
|
+
return Object.fromEntries(iterate(option?.skipId ? __columnsNoId : __columns)
|
|
1994
|
+
.map(K => [K, FieldFilter(K, data[K], __def, __fields[K].uuid || __fields[K].uuidShort, option)])
|
|
1994
1995
|
.filter(data => {
|
|
1995
1996
|
if (data[1][0] === 1) {
|
|
1996
1997
|
if (__fields[data[0]].Data2SQL) {
|
|
@@ -2365,13 +2366,13 @@ export class SqlService {
|
|
|
2365
2366
|
}
|
|
2366
2367
|
}
|
|
2367
2368
|
return result;
|
|
2368
|
-
}, { everyLength: option?.every
|
|
2369
|
+
}, { everyLength: option?.every ? 1 : option?.maxDeal });
|
|
2369
2370
|
if (isArray)
|
|
2370
2371
|
return result;
|
|
2371
2372
|
else
|
|
2372
2373
|
return result[0];
|
|
2373
2374
|
};
|
|
2374
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2375
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2375
2376
|
return fn();
|
|
2376
2377
|
}
|
|
2377
2378
|
else {
|
|
@@ -2390,10 +2391,10 @@ export class SqlService {
|
|
|
2390
2391
|
}
|
|
2391
2392
|
}
|
|
2392
2393
|
return result;
|
|
2393
|
-
}, { everyLength: option?.every
|
|
2394
|
+
}, { everyLength: option?.every ? 1 : option?.maxDeal });
|
|
2394
2395
|
return result;
|
|
2395
2396
|
};
|
|
2396
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2397
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2397
2398
|
return fn();
|
|
2398
2399
|
}
|
|
2399
2400
|
else {
|
|
@@ -2415,7 +2416,7 @@ export class SqlService {
|
|
|
2415
2416
|
}, { everyLength: 1 });
|
|
2416
2417
|
return result[0];
|
|
2417
2418
|
};
|
|
2418
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2419
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2419
2420
|
return fn();
|
|
2420
2421
|
}
|
|
2421
2422
|
else {
|
|
@@ -2476,7 +2477,7 @@ export class SqlService {
|
|
|
2476
2477
|
}, { everyLength: option?.maxDeal });
|
|
2477
2478
|
return result.length > 0 ? result.reduce((a, b) => a + b) : 0;
|
|
2478
2479
|
};
|
|
2479
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2480
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2480
2481
|
return fn();
|
|
2481
2482
|
}
|
|
2482
2483
|
else {
|
|
@@ -2498,7 +2499,7 @@ export class SqlService {
|
|
|
2498
2499
|
}, { everyLength: option?.maxDeal });
|
|
2499
2500
|
return result.length > 0 ? result.reduce((a, b) => a + b) : 0;
|
|
2500
2501
|
};
|
|
2501
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2502
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2502
2503
|
return fn();
|
|
2503
2504
|
}
|
|
2504
2505
|
else {
|
|
@@ -2597,7 +2598,7 @@ export class SqlService {
|
|
|
2597
2598
|
}
|
|
2598
2599
|
return result;
|
|
2599
2600
|
};
|
|
2600
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2601
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2601
2602
|
return fn();
|
|
2602
2603
|
}
|
|
2603
2604
|
else {
|
|
@@ -2613,7 +2614,7 @@ export class SqlService {
|
|
|
2613
2614
|
}
|
|
2614
2615
|
return result;
|
|
2615
2616
|
};
|
|
2616
|
-
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]
|
|
2617
|
+
if (option.dbType === DBType.SqliteRemote || option?.conn?.[_inTransaction]) {
|
|
2617
2618
|
return fn();
|
|
2618
2619
|
}
|
|
2619
2620
|
else {
|
|
@@ -2715,10 +2716,17 @@ export class SqlService {
|
|
|
2715
2716
|
});
|
|
2716
2717
|
}
|
|
2717
2718
|
}
|
|
2718
|
-
_select(templateResult, result, def, errorMsg, hump, mapper, mapperIfUndefined) {
|
|
2719
|
+
_select(templateResult, result, def, errorMsg, hump, mapper, mapperIfUndefined, dataConvert) {
|
|
2719
2720
|
switch (templateResult) {
|
|
2720
2721
|
case SelectResult.R_C_NotSure: {
|
|
2721
2722
|
try {
|
|
2723
|
+
if (dataConvert) {
|
|
2724
|
+
const key = Object.keys(result[0])[0];
|
|
2725
|
+
const value = Object.values(result[0])[0];
|
|
2726
|
+
if (key && dataConvert[key] && globalThis[_DataConvert][dataConvert[key]]) {
|
|
2727
|
+
return globalThis[_DataConvert][dataConvert[key]](value);
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2722
2730
|
return Object.values(result[0])[0];
|
|
2723
2731
|
}
|
|
2724
2732
|
catch (error) {
|
|
@@ -2727,6 +2735,13 @@ export class SqlService {
|
|
|
2727
2735
|
}
|
|
2728
2736
|
case SelectResult.R_C_Assert: {
|
|
2729
2737
|
try {
|
|
2738
|
+
if (dataConvert) {
|
|
2739
|
+
const key = Object.keys(result[0])[0];
|
|
2740
|
+
const value = Object.values(result[0])[0];
|
|
2741
|
+
if (key && dataConvert[key] && globalThis[_DataConvert][dataConvert[key]]) {
|
|
2742
|
+
return globalThis[_DataConvert][dataConvert[key]](value);
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2730
2745
|
return Object.values(result[0])[0];
|
|
2731
2746
|
}
|
|
2732
2747
|
catch (error) {
|
|
@@ -2739,11 +2754,15 @@ export class SqlService {
|
|
|
2739
2754
|
if (mapper) {
|
|
2740
2755
|
return flatData({ data: result[0], mapper, mapperIfUndefined });
|
|
2741
2756
|
}
|
|
2742
|
-
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2743
|
-
return C2P2(result[0]) ?? null;
|
|
2744
|
-
}
|
|
2745
2757
|
else {
|
|
2746
|
-
|
|
2758
|
+
hump = hump === true || (hump === undefined && globalThis[_Hump] === true);
|
|
2759
|
+
const __dataConvert = dataConvert ? Object.fromEntries(Object.entries(dataConvert).map(([k, v]) => [k, globalThis[_DataConvert][v]])) : undefined;
|
|
2760
|
+
if (hump || __dataConvert) {
|
|
2761
|
+
return C2P2(result[0], hump, __dataConvert) ?? null;
|
|
2762
|
+
}
|
|
2763
|
+
else {
|
|
2764
|
+
return result[0] ?? null;
|
|
2765
|
+
}
|
|
2747
2766
|
}
|
|
2748
2767
|
}
|
|
2749
2768
|
case SelectResult.R_CS_Assert: {
|
|
@@ -2752,15 +2771,25 @@ export class SqlService {
|
|
|
2752
2771
|
if (mapper) {
|
|
2753
2772
|
return flatData({ data, mapper, mapperIfUndefined });
|
|
2754
2773
|
}
|
|
2755
|
-
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2756
|
-
return C2P2(data) ?? null;
|
|
2757
|
-
}
|
|
2758
2774
|
else {
|
|
2759
|
-
|
|
2775
|
+
hump = hump === true || (hump === undefined && globalThis[_Hump] === true);
|
|
2776
|
+
const __dataConvert = dataConvert ? Object.fromEntries(Object.entries(dataConvert).map(([k, v]) => [k, globalThis[_DataConvert][v]])) : undefined;
|
|
2777
|
+
if (hump || __dataConvert) {
|
|
2778
|
+
return C2P2(data, hump, __dataConvert) ?? null;
|
|
2779
|
+
}
|
|
2780
|
+
else {
|
|
2781
|
+
return data;
|
|
2782
|
+
}
|
|
2760
2783
|
}
|
|
2761
2784
|
}
|
|
2762
2785
|
case SelectResult.RS_C: {
|
|
2763
2786
|
try {
|
|
2787
|
+
if (dataConvert) {
|
|
2788
|
+
const key = Object.keys(result[0])[0];
|
|
2789
|
+
if (key && dataConvert[key] && globalThis[_DataConvert][dataConvert[key]]) {
|
|
2790
|
+
return result.map((r) => globalThis[_DataConvert][dataConvert[key]](Object.values(r)[0]));
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2764
2793
|
return result.map((r) => Object.values(r)[0]);
|
|
2765
2794
|
}
|
|
2766
2795
|
catch (error) {
|
|
@@ -2771,15 +2800,25 @@ export class SqlService {
|
|
|
2771
2800
|
if (mapper) {
|
|
2772
2801
|
return iterate(result).map((data) => flatData({ data, mapper, mapperIfUndefined })).toArray();
|
|
2773
2802
|
}
|
|
2774
|
-
else if (hump === true || (hump === undefined && globalThis[_Hump] === true)) {
|
|
2775
|
-
return iterate(result).map((r) => C2P2(r)).toArray();
|
|
2776
|
-
}
|
|
2777
2803
|
else {
|
|
2778
|
-
|
|
2804
|
+
hump = hump === true || (hump === undefined && globalThis[_Hump] === true);
|
|
2805
|
+
const __dataConvert = dataConvert ? Object.fromEntries(Object.entries(dataConvert).map(([k, v]) => [k, globalThis[_DataConvert][v]])) : undefined;
|
|
2806
|
+
if (hump || __dataConvert) {
|
|
2807
|
+
return iterate(result).map((r) => C2P2(r, hump, __dataConvert)).toArray();
|
|
2808
|
+
}
|
|
2809
|
+
else {
|
|
2810
|
+
return result;
|
|
2811
|
+
}
|
|
2779
2812
|
}
|
|
2780
2813
|
}
|
|
2781
2814
|
case SelectResult.RS_C_List: {
|
|
2782
2815
|
try {
|
|
2816
|
+
if (dataConvert) {
|
|
2817
|
+
const key = Object.keys(result[0])[0];
|
|
2818
|
+
if (key && dataConvert[key] && globalThis[_DataConvert][dataConvert[key]]) {
|
|
2819
|
+
return new ArrayList(result.map((r) => globalThis[_DataConvert][dataConvert[key]](Object.values(r)[0])));
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2783
2822
|
return new ArrayList(result.map((r) => Object.values(r)[0]));
|
|
2784
2823
|
}
|
|
2785
2824
|
catch (error) {
|
|
@@ -2788,13 +2827,21 @@ export class SqlService {
|
|
|
2788
2827
|
}
|
|
2789
2828
|
case SelectResult.RS_CS_List: {
|
|
2790
2829
|
if (mapper) {
|
|
2791
|
-
return new ArrayList(iterate(result).map((data) => flatData({
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2830
|
+
return new ArrayList(iterate(result).map((data) => flatData({
|
|
2831
|
+
data,
|
|
2832
|
+
mapper,
|
|
2833
|
+
mapperIfUndefined
|
|
2834
|
+
})).toArray());
|
|
2795
2835
|
}
|
|
2796
2836
|
else {
|
|
2797
|
-
|
|
2837
|
+
hump = hump === true || (hump === undefined && globalThis[_Hump] === true);
|
|
2838
|
+
const __dataConvert = dataConvert ? Object.fromEntries(Object.entries(dataConvert).map(([k, v]) => [k, globalThis[_DataConvert][v]])) : undefined;
|
|
2839
|
+
if (hump || __dataConvert) {
|
|
2840
|
+
return new ArrayList(iterate(result).map((r) => C2P2(r, hump, __dataConvert)).toArray());
|
|
2841
|
+
}
|
|
2842
|
+
else {
|
|
2843
|
+
return new ArrayList();
|
|
2844
|
+
}
|
|
2798
2845
|
}
|
|
2799
2846
|
}
|
|
2800
2847
|
}
|
|
@@ -2811,13 +2858,13 @@ export class SqlService {
|
|
|
2811
2858
|
const { sql, params } = this._generSql(option.dbType, option.sql, _params);
|
|
2812
2859
|
if (option.sync === SyncMode.Sync) {
|
|
2813
2860
|
const result = option.conn.query(SyncMode.Sync, sql, params);
|
|
2814
|
-
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined);
|
|
2861
|
+
return this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert);
|
|
2815
2862
|
}
|
|
2816
2863
|
else {
|
|
2817
2864
|
return new Promise(async (resolve, reject) => {
|
|
2818
2865
|
try {
|
|
2819
2866
|
const result = await option.conn.query(SyncMode.Async, sql, params);
|
|
2820
|
-
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined));
|
|
2867
|
+
resolve(this._select(option.selectResult, result, option.defValue, option.errorMsg, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert));
|
|
2821
2868
|
}
|
|
2822
2869
|
catch (error) {
|
|
2823
2870
|
reject(error);
|
|
@@ -2874,7 +2921,7 @@ export class SqlService {
|
|
|
2874
2921
|
};
|
|
2875
2922
|
option.pageNumber ?? (option.pageNumber = 1);
|
|
2876
2923
|
option.pageSize ?? (option.pageSize = 0);
|
|
2877
|
-
if (option.hump
|
|
2924
|
+
if (option.hump || (option.hump === undefined && globalThis[_Hump]) && option.sortName) {
|
|
2878
2925
|
option.sortName = P2C(option.sortName);
|
|
2879
2926
|
}
|
|
2880
2927
|
Object.assign(option.params, {
|
|
@@ -2886,7 +2933,7 @@ export class SqlService {
|
|
|
2886
2933
|
let sql = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: false, ...option.params });
|
|
2887
2934
|
let sqlSum = '';
|
|
2888
2935
|
let sqlCount = '';
|
|
2889
|
-
if (option.sumSelf
|
|
2936
|
+
if (option.sumSelf) {
|
|
2890
2937
|
sqlSum = globalThis[_sqlCache].load(this._matchSqlid(option.sqlId), { ctx: option.context, isCount: false, isSum: true, ...option.params });
|
|
2891
2938
|
}
|
|
2892
2939
|
if (option.limitSelf !== true) {
|
|
@@ -2897,7 +2944,7 @@ export class SqlService {
|
|
|
2897
2944
|
}
|
|
2898
2945
|
}
|
|
2899
2946
|
if (option.pageSize > 0) {
|
|
2900
|
-
if (option.countSelf
|
|
2947
|
+
if (option.countSelf) {
|
|
2901
2948
|
sqlCount = globalThis[_sqlCache].load(this._matchSqlid(`${option.sqlId}_count`), { ctx: option.context, isCount: true, isSum: false, ...option.params });
|
|
2902
2949
|
}
|
|
2903
2950
|
else {
|
|
@@ -3193,7 +3240,7 @@ export class SqlService {
|
|
|
3193
3240
|
tableName = tableName ?? this[_tableName];
|
|
3194
3241
|
switch (dbType) {
|
|
3195
3242
|
case DBType.Mysql: {
|
|
3196
|
-
let sql = formatDialect(`CREATE ${temp
|
|
3243
|
+
let sql = formatDialect(`CREATE ${temp ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3197
3244
|
${columns.map(K => this[_fields][K][DBType.Mysql]()).join(',')}
|
|
3198
3245
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) USING BTREE ` : ''}
|
|
3199
3246
|
${indexs && indexs.length ? `,${indexs.map(i => `KEY ${this[_fields][i]?.C2()} (${this[_fields][i]?.C2()})`).join(',')} ` : ''}
|
|
@@ -3217,7 +3264,7 @@ export class SqlService {
|
|
|
3217
3264
|
}
|
|
3218
3265
|
case DBType.Sqlite:
|
|
3219
3266
|
case DBType.SqliteRemote: {
|
|
3220
|
-
let sql = formatDialect(`CREATE ${temp
|
|
3267
|
+
let sql = formatDialect(`CREATE ${temp ? 'TEMPORARY' : ''} TABLE IF NOT EXISTS ${tableName}(
|
|
3221
3268
|
${columns.map(K => this[_fields][K][DBType.Sqlite]()).join(',')}
|
|
3222
3269
|
${ids && ids.length ? `,PRIMARY KEY (${ids.map(i => this[_fields][i]?.C2()).join(',')}) ` : ''}
|
|
3223
3270
|
);`, { dialect: sqlite });
|
|
@@ -3351,7 +3398,7 @@ const IF_PROCEED = function () {
|
|
|
3351
3398
|
return function (_target, _propertyKey, descriptor) {
|
|
3352
3399
|
const fn = descriptor.value;
|
|
3353
3400
|
descriptor.value = function () {
|
|
3354
|
-
if (this.if_proceed
|
|
3401
|
+
if (this.if_proceed) {
|
|
3355
3402
|
// eslint-disable-next-line prefer-rest-params
|
|
3356
3403
|
const args = Array.from(arguments);
|
|
3357
3404
|
fn.call(this, ...args);
|
|
@@ -3368,7 +3415,7 @@ const IF_EXEC = function (def) {
|
|
|
3368
3415
|
return function (_target, _propertyKey, descriptor) {
|
|
3369
3416
|
const fn = descriptor.value;
|
|
3370
3417
|
descriptor.value = async function () {
|
|
3371
|
-
if (this.if_proceed
|
|
3418
|
+
if (this.if_proceed && this.if_exec) {
|
|
3372
3419
|
// eslint-disable-next-line prefer-rest-params
|
|
3373
3420
|
const args = Array.from(arguments);
|
|
3374
3421
|
return await fn.call(this, ...args);
|
|
@@ -3462,7 +3509,7 @@ class StreamQuery {
|
|
|
3462
3509
|
exe = true;
|
|
3463
3510
|
}
|
|
3464
3511
|
}
|
|
3465
|
-
if (breakExcuteIfSkip
|
|
3512
|
+
if (breakExcuteIfSkip && exe === false) {
|
|
3466
3513
|
this.if_exec = false;
|
|
3467
3514
|
}
|
|
3468
3515
|
return this;
|
|
@@ -3649,6 +3696,7 @@ class StreamQuery {
|
|
|
3649
3696
|
Object.assign(this._updates, t);
|
|
3650
3697
|
return this;
|
|
3651
3698
|
}
|
|
3699
|
+
/** SET key = REPLACE(key, :valueToFind, :valueToReplace) */
|
|
3652
3700
|
replace(key, valueToFind, valueToReplace) {
|
|
3653
3701
|
const [pkey1, pkey2] = [`p${this._prefix}${this._index++}`, `p${this._prefix}${this._index++}`];
|
|
3654
3702
|
this._updateColumns.push(` ${this[_fields][String(key)]?.C2()} = REPLACE(${this[_fields][String(key)]?.C2()}, :${pkey1}, :${pkey2}) `);
|
|
@@ -3857,8 +3905,8 @@ class StreamQuery {
|
|
|
3857
3905
|
_(key, value, op, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3858
3906
|
if (value === null
|
|
3859
3907
|
|| value === undefined
|
|
3860
|
-
|| (emptyString(`${value ?? ''}`) && skipEmptyString
|
|
3861
|
-
if (breakExcuteIfSkip
|
|
3908
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString)) {
|
|
3909
|
+
if (breakExcuteIfSkip) {
|
|
3862
3910
|
this.if_exec = false;
|
|
3863
3911
|
}
|
|
3864
3912
|
return this;
|
|
@@ -3879,8 +3927,8 @@ class StreamQuery {
|
|
|
3879
3927
|
__(keys, value, op, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3880
3928
|
if (value === null
|
|
3881
3929
|
|| value === undefined
|
|
3882
|
-
|| (emptyString(`${value ?? ''}`) && skipEmptyString
|
|
3883
|
-
if (breakExcuteIfSkip
|
|
3930
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString)) {
|
|
3931
|
+
if (breakExcuteIfSkip) {
|
|
3884
3932
|
this.if_exec = false;
|
|
3885
3933
|
}
|
|
3886
3934
|
return this;
|
|
@@ -3909,11 +3957,11 @@ class StreamQuery {
|
|
|
3909
3957
|
_between(key, value1, value2, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3910
3958
|
if (value1 === null
|
|
3911
3959
|
|| value1 === undefined
|
|
3912
|
-
|| (emptyString(`${value1 ?? ''}`) && skipEmptyString
|
|
3960
|
+
|| (emptyString(`${value1 ?? ''}`) && skipEmptyString)
|
|
3913
3961
|
|| value2 === null
|
|
3914
3962
|
|| value2 === undefined
|
|
3915
|
-
|| (emptyString(`${value2 ?? ''}`) && skipEmptyString
|
|
3916
|
-
if (breakExcuteIfSkip
|
|
3963
|
+
|| (emptyString(`${value2 ?? ''}`) && skipEmptyString)) {
|
|
3964
|
+
if (breakExcuteIfSkip) {
|
|
3917
3965
|
this.if_exec = false;
|
|
3918
3966
|
}
|
|
3919
3967
|
return this;
|
|
@@ -3934,7 +3982,7 @@ class StreamQuery {
|
|
|
3934
3982
|
return this;
|
|
3935
3983
|
}
|
|
3936
3984
|
_in(key, value, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3937
|
-
if (value && value.length > 0 && skipEmptyString
|
|
3985
|
+
if (value && value.length > 0 && skipEmptyString) {
|
|
3938
3986
|
value = value.filter(v => !emptyString(`${v ?? ''}`));
|
|
3939
3987
|
}
|
|
3940
3988
|
if (value && value.length > 0) {
|
|
@@ -3956,7 +4004,7 @@ class StreamQuery {
|
|
|
3956
4004
|
return this;
|
|
3957
4005
|
}
|
|
3958
4006
|
_in2(key, value, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
3959
|
-
const skip = emptyString(`${value ?? ''}`) && skipEmptyString
|
|
4007
|
+
const skip = emptyString(`${value ?? ''}`) && skipEmptyString;
|
|
3960
4008
|
if (!skip) {
|
|
3961
4009
|
if (paramName !== undefined && this._paramKeys.hasOwnProperty(paramName)) {
|
|
3962
4010
|
this._param[this._paramKeys[paramName]] = value;
|
|
@@ -3979,7 +4027,7 @@ class StreamQuery {
|
|
|
3979
4027
|
if (value === null
|
|
3980
4028
|
|| value === undefined
|
|
3981
4029
|
|| emptyString(`${value ?? ''}`)) {
|
|
3982
|
-
if (breakExcuteIfSkip
|
|
4030
|
+
if (breakExcuteIfSkip) {
|
|
3983
4031
|
this.if_exec = false;
|
|
3984
4032
|
}
|
|
3985
4033
|
return this;
|
|
@@ -4001,7 +4049,7 @@ class StreamQuery {
|
|
|
4001
4049
|
if (value === null
|
|
4002
4050
|
|| value === undefined
|
|
4003
4051
|
|| emptyString(`${value ?? ''}`)) {
|
|
4004
|
-
if (breakExcuteIfSkip
|
|
4052
|
+
if (breakExcuteIfSkip) {
|
|
4005
4053
|
this.if_exec = false;
|
|
4006
4054
|
}
|
|
4007
4055
|
return this;
|
|
@@ -4023,7 +4071,7 @@ class StreamQuery {
|
|
|
4023
4071
|
if (value === null
|
|
4024
4072
|
|| value === undefined
|
|
4025
4073
|
|| emptyString(`${value ?? ''}`)) {
|
|
4026
|
-
if (breakExcuteIfSkip
|
|
4074
|
+
if (breakExcuteIfSkip) {
|
|
4027
4075
|
this.if_exec = false;
|
|
4028
4076
|
}
|
|
4029
4077
|
return this;
|
|
@@ -4044,8 +4092,8 @@ class StreamQuery {
|
|
|
4044
4092
|
_like(key, value, { not = '', left = '', right = '', paramName = '', op = 'LIKE', skipEmptyString = true, breakExcuteIfSkip = false } = {}) {
|
|
4045
4093
|
if (value === null
|
|
4046
4094
|
|| value === undefined
|
|
4047
|
-
|| (emptyString(`${value ?? ''}`) && skipEmptyString
|
|
4048
|
-
if (breakExcuteIfSkip
|
|
4095
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString)) {
|
|
4096
|
+
if (breakExcuteIfSkip) {
|
|
4049
4097
|
this.if_exec = false;
|
|
4050
4098
|
}
|
|
4051
4099
|
return this;
|
|
@@ -4066,8 +4114,8 @@ class StreamQuery {
|
|
|
4066
4114
|
_includes(key, value, { not = '', paramName = '', skipEmptyString = true, breakExcuteIfSkip = true } = {}) {
|
|
4067
4115
|
if (value === null
|
|
4068
4116
|
|| value === undefined
|
|
4069
|
-
|| (emptyString(`${value ?? ''}`) && skipEmptyString
|
|
4070
|
-
if (breakExcuteIfSkip
|
|
4117
|
+
|| (emptyString(`${value ?? ''}`) && skipEmptyString)) {
|
|
4118
|
+
if (breakExcuteIfSkip) {
|
|
4071
4119
|
this.if_exec = false;
|
|
4072
4120
|
}
|
|
4073
4121
|
return this;
|