midway-fatcms 0.0.1-beta.75 → 0.0.1-beta.77
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/dist/controller/base/BaseApiController.js +6 -2
- package/dist/controller/manage/SystemInfoManageApi.js +2 -0
- package/dist/libs/crud-pro/CrudPro.js +3 -0
- package/dist/libs/crud-pro/interfaces.d.ts +10 -1
- package/dist/libs/crud-pro/models/Transaction.d.ts +21 -2
- package/dist/libs/crud-pro/models/Transaction.js +118 -15
- package/dist/libs/crud-pro/models/TransactionMySQL.d.ts +11 -20
- package/dist/libs/crud-pro/models/TransactionMySQL.js +33 -61
- package/dist/libs/crud-pro/models/TransactionPostgres.d.ts +11 -20
- package/dist/libs/crud-pro/models/TransactionPostgres.js +39 -60
- package/dist/libs/crud-pro/models/TransactionSqlServer.d.ts +1 -26
- package/dist/libs/crud-pro/models/TransactionSqlServer.js +42 -73
- package/dist/libs/crud-pro/services/CrudProCachedCfgService.js +3 -3
- package/dist/libs/crud-pro/services/CrudProExecuteSqlService.js +10 -19
- package/dist/libs/crud-pro/services/CrudProServiceBase.d.ts +1 -1
- package/dist/libs/crud-pro/services/CrudProTableMetaService.js +3 -3
- package/dist/libs/crud-pro/utils/CrudMonitor.d.ts +9 -0
- package/dist/libs/crud-pro/utils/CrudMonitor.js +12 -0
- package/dist/libs/crud-pro/utils/MixinUtils.d.ts +1 -0
- package/dist/libs/crud-pro/utils/MixinUtils.js +3 -0
- package/dist/libs/crud-pro/utils/sqlConvert/convertMix.d.ts +0 -3
- package/dist/libs/crud-pro/utils/sqlConvert/convertMix.js +24 -22
- package/dist/models/userSession.d.ts +1 -0
- package/dist/models/userSession.js +1 -0
- package/dist/schedule/anonymousContext.js +2 -1
- package/dist/service/AuthService.js +6 -1
- package/package.json +1 -1
- package/src/controller/base/BaseApiController.ts +7 -2
- package/src/controller/manage/SuperAdminManageApi.ts +6 -6
- package/src/controller/manage/SystemInfoManageApi.ts +2 -0
- package/src/libs/crud-pro/CrudPro.ts +5 -0
- package/src/libs/crud-pro/interfaces.ts +10 -1
- package/src/libs/crud-pro/models/Transaction.ts +141 -19
- package/src/libs/crud-pro/models/TransactionMySQL.ts +39 -60
- package/src/libs/crud-pro/models/TransactionPostgres.ts +49 -61
- package/src/libs/crud-pro/models/TransactionSqlServer.ts +52 -77
- package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +3 -3
- package/src/libs/crud-pro/services/CrudProExecuteSqlService.ts +13 -22
- package/src/libs/crud-pro/services/CrudProTableMetaService.ts +3 -3
- package/src/libs/crud-pro/utils/CrudMonitor.ts +13 -0
- package/src/libs/crud-pro/utils/MixinUtils.ts +3 -0
- package/src/libs/crud-pro/utils/sqlConvert/convertMix.ts +24 -24
- package/src/models/userSession.ts +2 -0
- package/src/schedule/anonymousContext.ts +2 -1
- package/src/service/AuthService.ts +7 -1
- package/dist/libs/crud-pro/utils/sqlConvert/convertPgType.d.ts +0 -2
- package/dist/libs/crud-pro/utils/sqlConvert/convertPgType.js +0 -128
- package/src/libs/crud-pro/utils/sqlConvert/convertPgType.ts +0 -127
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import * as _ from 'lodash';
|
|
2
|
-
import { PoolClient } from 'pg';
|
|
3
2
|
import { CrudProServiceBase } from './CrudProServiceBase';
|
|
4
3
|
import { SqlCfgModel } from '../models/SqlCfgModel';
|
|
5
4
|
import { KeyOfCrudTypes, KeysOfCustomSQL, KeysOfSqlResPicker, SqlDbType } from '../models/keys';
|
|
6
5
|
import { CommonException, Exceptions } from '../exceptions';
|
|
7
|
-
import { pickAndConvertRowsByMix } from '../utils/sqlConvert/convertMix';
|
|
8
6
|
import { replaceQuestionMarks } from '../utils/sqlConvert/convertPgSql';
|
|
9
7
|
import { replaceQuestionMarksForMssql } from '../utils/sqlConvert/convertMsSql';
|
|
10
8
|
import { ModelUtils } from '../utils/ModelUtils';
|
|
11
9
|
import { MixinUtils } from '../utils/MixinUtils';
|
|
12
10
|
import { FuncContext } from '../models/FuncContext';
|
|
13
11
|
import { ExecuteContext } from '../models/ExecuteContext';
|
|
12
|
+
import { IPoolConnectionClient, IPoolConnectionQueryResult } from "@/libs/crud-pro/interfaces";
|
|
14
13
|
|
|
15
14
|
const { checkFuncCfgValid } = ModelUtils;
|
|
16
15
|
const { isEmpty } = MixinUtils;
|
|
@@ -32,20 +31,15 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
32
31
|
this.logger.info('[CrudProExecuteSqlService] executeSqlCfgModel_before', sqlCfgModel.executeSql, sqlCfgModel.executeSqlArgs);
|
|
33
32
|
|
|
34
33
|
const exeCtx = this.getExecuteContext();
|
|
35
|
-
const connection = await this.getTxConnectionBySqlCfg(sqlCfgModel);
|
|
34
|
+
const connection: IPoolConnectionClient = await this.getTxConnectionBySqlCfg(sqlCfgModel);
|
|
36
35
|
const executeSqlArgs = this.handleExecuteSqlArgsByResModel(exeCtx, sqlCfgModel.executeSqlArgs || []);
|
|
37
36
|
|
|
38
|
-
let queryRes:
|
|
37
|
+
let queryRes: IPoolConnectionQueryResult;
|
|
39
38
|
|
|
40
39
|
if (sqlCfgModel.sqlDbType === SqlDbType.postgres) {
|
|
41
|
-
// import { PoolClient } from 'pg';
|
|
42
40
|
|
|
43
|
-
const pgClient: PoolClient = connection as any;
|
|
44
41
|
const pgSql = replaceQuestionMarks(sqlCfgModel.executeSql, sqlCfgModel.isNativeSQL);
|
|
45
|
-
queryRes = await
|
|
46
|
-
text: pgSql,
|
|
47
|
-
values: executeSqlArgs || [],
|
|
48
|
-
});
|
|
42
|
+
queryRes = await connection.query(pgSql, executeSqlArgs);
|
|
49
43
|
|
|
50
44
|
this.logger.debug('[CrudProExecuteSqlService] executeSqlCfgModel_postgres', pgSql, executeSqlArgs, sqlCfgModel.resPicker);
|
|
51
45
|
|
|
@@ -62,8 +56,7 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
62
56
|
this.logger.debug('[CrudProExecuteSqlService] executeSqlCfgModel_mysql', sqlCfgModel.executeSql, executeSqlArgs, sqlCfgModel.resPicker);
|
|
63
57
|
}
|
|
64
58
|
|
|
65
|
-
const
|
|
66
|
-
const resObject = this.toQueryResByResPicker(sqlRes, queryRes, sqlCfgModel);
|
|
59
|
+
const resObject = this.toQueryResByResPicker(queryRes.rows, queryRes.originRes, sqlCfgModel);
|
|
67
60
|
exeCtx.setResModelItem(sqlCfgModel.resName, resObject);
|
|
68
61
|
}
|
|
69
62
|
|
|
@@ -207,12 +200,11 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
207
200
|
return result === true;
|
|
208
201
|
}
|
|
209
202
|
|
|
210
|
-
private toQueryResByResPicker(
|
|
203
|
+
private toQueryResByResPicker(rows: any[], originRes: any, sqlCfgModel: SqlCfgModel) {
|
|
211
204
|
const resPicker = sqlCfgModel.resPicker;
|
|
212
205
|
|
|
213
206
|
// 返回第一行
|
|
214
207
|
if (KeysOfSqlResPicker.RESULT_FIRST_ROW === resPicker) {
|
|
215
|
-
const rows = sqlRes as any[];
|
|
216
208
|
if (rows && rows.length > 0) {
|
|
217
209
|
return rows[0];
|
|
218
210
|
}
|
|
@@ -221,7 +213,6 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
221
213
|
|
|
222
214
|
// $ResultSet[0].total_count , 只取第一行的的total_count
|
|
223
215
|
if (KeysOfSqlResPicker.RESULT_TOTAL_COUNT === resPicker) {
|
|
224
|
-
const rows = sqlRes as any[];
|
|
225
216
|
if (isEmpty(rows)) {
|
|
226
217
|
return 0;
|
|
227
218
|
}
|
|
@@ -233,31 +224,31 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
233
224
|
if (KeysOfSqlResPicker.UPDATE_RESULT === resPicker) {
|
|
234
225
|
if (sqlCfgModel.sqlDbType === SqlDbType.postgres) {
|
|
235
226
|
return {
|
|
236
|
-
insertId: _.get(
|
|
237
|
-
affectedRows: _.get(
|
|
227
|
+
insertId: _.get(originRes, 'rows[0].id'),
|
|
228
|
+
affectedRows: _.get(originRes, 'rowCount'),
|
|
238
229
|
};
|
|
239
230
|
}
|
|
240
231
|
|
|
241
232
|
if (sqlCfgModel.sqlDbType === SqlDbType.sqlserver) {
|
|
242
233
|
return {
|
|
243
|
-
insertId: _.get(
|
|
244
|
-
affectedRows: _.get(
|
|
234
|
+
insertId: _.get(originRes, 'recordset[0].id'),
|
|
235
|
+
affectedRows: _.get(originRes, 'rowsAffected[0]'),
|
|
245
236
|
};
|
|
246
237
|
}
|
|
247
238
|
|
|
248
|
-
return
|
|
239
|
+
return rows;
|
|
249
240
|
}
|
|
250
241
|
|
|
251
242
|
//其他配置:形如: sqlRes[0].total_count
|
|
252
243
|
if (typeof resPicker === 'string') {
|
|
253
244
|
if (resPicker.startsWith('sqlRes')) {
|
|
254
|
-
return _.get({ sqlRes }, resPicker);
|
|
245
|
+
return _.get({ sqlRes: rows }, resPicker);
|
|
255
246
|
} else {
|
|
256
247
|
throw new CommonException(Exceptions.RUN_EXECUTE_VALIDATE, 'resPicker必须是以sqlRes开头');
|
|
257
248
|
}
|
|
258
249
|
}
|
|
259
250
|
|
|
260
|
-
return
|
|
251
|
+
return rows;
|
|
261
252
|
}
|
|
262
253
|
}
|
|
263
254
|
|
|
@@ -2,7 +2,7 @@ import { CrudProServiceBase } from './CrudProServiceBase';
|
|
|
2
2
|
import { SqlCfgModel } from '../models/SqlCfgModel';
|
|
3
3
|
import { IExecuteUnsafeQueryCtx, ITableMeta } from '../interfaces';
|
|
4
4
|
import { SqlDbType } from '../models/keys';
|
|
5
|
-
import { pickAndConvertRowsByMix } from '../utils/sqlConvert/convertMix';
|
|
5
|
+
// import { pickAndConvertRowsByMix } from '../utils/sqlConvert/convertMix';
|
|
6
6
|
|
|
7
7
|
class CrudProTableMetaCache {
|
|
8
8
|
private cacheMap: Record<string, ITableMeta> = {};
|
|
@@ -58,7 +58,7 @@ class CrudProTableMetaService extends CrudProServiceBase {
|
|
|
58
58
|
private async loadTableColumnInfo(baseInfo: IExecuteUnsafeQueryCtx): Promise<string[]> {
|
|
59
59
|
if (baseInfo.sqlDbType === SqlDbType.mysql) {
|
|
60
60
|
const queryRes = await this.executeUnsafeQuery(baseInfo, 'describe ' + baseInfo.sqlTable);
|
|
61
|
-
const tableDescribe = pickAndConvertRowsByMix(queryRes, baseInfo.sqlDbType);
|
|
61
|
+
const tableDescribe = queryRes.rows || []; //pickAndConvertRowsByMix(queryRes, baseInfo.sqlDbType);
|
|
62
62
|
const tableDescribe2 = JSON.parse(JSON.stringify(tableDescribe));
|
|
63
63
|
return tableDescribe2.map(fieldObj => {
|
|
64
64
|
return fieldObj['Field'];
|
|
@@ -73,7 +73,7 @@ class CrudProTableMetaService extends CrudProServiceBase {
|
|
|
73
73
|
ORDER BY ordinal_position;
|
|
74
74
|
`.trim();
|
|
75
75
|
const queryRes = await this.executeUnsafeQuery(baseInfo, columnArraySql);
|
|
76
|
-
const tableDescribe =
|
|
76
|
+
const tableDescribe = queryRes.rows || [];
|
|
77
77
|
return tableDescribe.map(fieldObj => {
|
|
78
78
|
return fieldObj['column_name'];
|
|
79
79
|
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
const CrudMonitor = {
|
|
3
|
+
mysqlGetConnectionCount: 0,
|
|
4
|
+
mysqlReleaseConnectionCount: 0,
|
|
5
|
+
postgresGetConnectionCount: 0,
|
|
6
|
+
postgresReleaseConnectionCount: 0,
|
|
7
|
+
mssqlGetConnectionCount: 0,
|
|
8
|
+
mssqlReleaseConnectionCount: 0,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
CrudMonitor,
|
|
13
|
+
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { SqlDbType } from '../../models/keys';
|
|
2
|
-
import { pickAndConvertPgRows } from './convertPgType';
|
|
3
|
-
import * as _ from 'lodash';
|
|
4
|
-
|
|
5
|
-
function pickAndConvertRowsByMix(connectionQueryRes: any, sqlDbType: SqlDbType): any[] | any {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { pickAndConvertRowsByMix };
|
|
1
|
+
// import { SqlDbType } from '../../models/keys';
|
|
2
|
+
// import { pickAndConvertPgRows } from './convertPgType';
|
|
3
|
+
// import * as _ from 'lodash';
|
|
4
|
+
//
|
|
5
|
+
// function pickAndConvertRowsByMix(connectionQueryRes: any, sqlDbType: SqlDbType): any[] | any {
|
|
6
|
+
// if (!connectionQueryRes) {
|
|
7
|
+
// throw new Error('[pickAndConvertRowsByMix] error, connectionQueryRes is null ');
|
|
8
|
+
// }
|
|
9
|
+
//
|
|
10
|
+
// if (sqlDbType === SqlDbType.postgres) {
|
|
11
|
+
// return pickAndConvertPgRows(connectionQueryRes);
|
|
12
|
+
// }
|
|
13
|
+
//
|
|
14
|
+
// if (sqlDbType === SqlDbType.mysql) {
|
|
15
|
+
// return connectionQueryRes[0];
|
|
16
|
+
// }
|
|
17
|
+
//
|
|
18
|
+
// if (sqlDbType === SqlDbType.sqlserver) {
|
|
19
|
+
// return _.get(connectionQueryRes, 'recordsets[0]') || [];
|
|
20
|
+
// }
|
|
21
|
+
// throw new Error('[pickAndConvertRowsByMix] error sqlDbType, sqlDbType = ' + sqlDbType);
|
|
22
|
+
// }
|
|
23
|
+
//
|
|
24
|
+
// export { pickAndConvertRowsByMix };
|
|
@@ -28,6 +28,7 @@ interface ISessionInfo {
|
|
|
28
28
|
privateKey: string; // 私钥
|
|
29
29
|
bizExt?: any;
|
|
30
30
|
sessionIdCreatedAt: number
|
|
31
|
+
sessionSavedAt: number
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
interface IConsumerUserInfo {
|
|
@@ -63,6 +64,7 @@ function createEmptySessionInfo(superAdmin: boolean): ISessionInfo {
|
|
|
63
64
|
publicKey: '',
|
|
64
65
|
privateKey: '',
|
|
65
66
|
sessionIdCreatedAt: 0,
|
|
67
|
+
sessionSavedAt: 0
|
|
66
68
|
};
|
|
67
69
|
}
|
|
68
70
|
|
|
@@ -118,6 +118,7 @@ export class AuthService {
|
|
|
118
118
|
publicKey,
|
|
119
119
|
privateKey,
|
|
120
120
|
sessionIdCreatedAt: Date.now(),
|
|
121
|
+
sessionSavedAt: Date.now()
|
|
121
122
|
};
|
|
122
123
|
|
|
123
124
|
await this.userSessionService.saveUserSession(sessionInfo);
|
|
@@ -159,6 +160,7 @@ export class AuthService {
|
|
|
159
160
|
publicKey,
|
|
160
161
|
privateKey,
|
|
161
162
|
sessionIdCreatedAt: Date.now(),
|
|
163
|
+
sessionSavedAt: Date.now(),
|
|
162
164
|
};
|
|
163
165
|
|
|
164
166
|
if (bizExt && typeof bizExt === 'object') {
|
|
@@ -273,9 +275,13 @@ export class AuthService {
|
|
|
273
275
|
|
|
274
276
|
|
|
275
277
|
async refreshSession(sessionInfo: ISessionInfo): Promise<ISessionInfo> {
|
|
278
|
+
if (sessionInfo.sessionSavedAt && Date.now() - sessionInfo.sessionSavedAt < 1000 * 60 * 5) {
|
|
279
|
+
return sessionInfo;
|
|
280
|
+
}
|
|
281
|
+
|
|
276
282
|
const roleCodes = await this.queryUserRoleCodeList(sessionInfo.accountId);
|
|
277
283
|
const functionCodes = await this.queryFunctionCodeList(roleCodes);
|
|
278
|
-
const newSessionInfo: ISessionInfo = {...sessionInfo, roleCodes, functionCodes};
|
|
284
|
+
const newSessionInfo: ISessionInfo = {...sessionInfo, roleCodes, functionCodes, sessionSavedAt: Date.now()};
|
|
279
285
|
await this.userSessionService.saveUserSession(newSessionInfo);
|
|
280
286
|
return newSessionInfo;
|
|
281
287
|
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
//
|
|
3
|
-
// // 定义 PostgreSQL 数据类型 OID 映射
|
|
4
|
-
// const PG_TYPE_OIDS = {
|
|
5
|
-
// INT2: 21, // smallint
|
|
6
|
-
// INT4: 23, // integer
|
|
7
|
-
// INT8: 20, // bigint
|
|
8
|
-
// FLOAT4: 700, // real
|
|
9
|
-
// FLOAT8: 701, // double precision
|
|
10
|
-
// NUMERIC: 1700, // numeric
|
|
11
|
-
// MONEY: 790, // money
|
|
12
|
-
// BOOL: 16, // boolean
|
|
13
|
-
// DATE: 1082, // date
|
|
14
|
-
// TIMESTAMP: 1114, // timestamp without time zone
|
|
15
|
-
// TIMESTAMPTZ: 1184, // timestamp with time zone
|
|
16
|
-
// JSON: 114, // json
|
|
17
|
-
// JSONB: 3802, // jsonb
|
|
18
|
-
// };
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
// /**
|
|
23
|
-
// * 将 PostgreSQL 数据类型转换为 JavaScript 类型
|
|
24
|
-
// * @param {any} value - 原始值
|
|
25
|
-
// * @param {number} oid - 数据类型 OID
|
|
26
|
-
// * @returns {any} 转换后的值
|
|
27
|
-
// */
|
|
28
|
-
// function convertPgType(value: any, oid: any) {
|
|
29
|
-
// // 如果值为 null,直接返回
|
|
30
|
-
// if (value === null) {
|
|
31
|
-
// return null;
|
|
32
|
-
// }
|
|
33
|
-
//
|
|
34
|
-
// const typeOfValue = typeof value;
|
|
35
|
-
// if (typeOfValue === 'undefined') {
|
|
36
|
-
// return null;
|
|
37
|
-
// }
|
|
38
|
-
// if (typeOfValue === 'number' || typeOfValue === "boolean" || typeOfValue === "object") {
|
|
39
|
-
// return value;
|
|
40
|
-
// }
|
|
41
|
-
// if (typeOfValue === 'bigint') {
|
|
42
|
-
// return value.toString()
|
|
43
|
-
// }
|
|
44
|
-
//
|
|
45
|
-
// switch (oid) {
|
|
46
|
-
// // 整数类型(全部转为 Number)
|
|
47
|
-
// case PG_TYPE_OIDS.INT2: // smallint
|
|
48
|
-
// case PG_TYPE_OIDS.INT4: // integer
|
|
49
|
-
// return Number(value);
|
|
50
|
-
// case PG_TYPE_OIDS.INT8: // bigint
|
|
51
|
-
// return value;
|
|
52
|
-
//
|
|
53
|
-
// // 浮点类型
|
|
54
|
-
// case PG_TYPE_OIDS.FLOAT4: // real
|
|
55
|
-
// case PG_TYPE_OIDS.FLOAT8: // double precision
|
|
56
|
-
// case PG_TYPE_OIDS.NUMERIC: // numeric
|
|
57
|
-
// case PG_TYPE_OIDS.MONEY: // money
|
|
58
|
-
// return Number(value);
|
|
59
|
-
//
|
|
60
|
-
// // 布尔类型
|
|
61
|
-
// case PG_TYPE_OIDS.BOOL:
|
|
62
|
-
// if (typeof value === 'boolean') {
|
|
63
|
-
// return value;
|
|
64
|
-
// }
|
|
65
|
-
// return value === 't' || value === 'true' || value === true;
|
|
66
|
-
//
|
|
67
|
-
// // 日期/时间类型
|
|
68
|
-
// case PG_TYPE_OIDS.DATE:
|
|
69
|
-
// return new Date(value).toISOString().split('T')[0];
|
|
70
|
-
//
|
|
71
|
-
// case PG_TYPE_OIDS.TIMESTAMP:
|
|
72
|
-
// case PG_TYPE_OIDS.TIMESTAMPTZ:
|
|
73
|
-
// return new Date(value).toISOString();
|
|
74
|
-
//
|
|
75
|
-
// // JSON 类型
|
|
76
|
-
// case PG_TYPE_OIDS.JSON:
|
|
77
|
-
// case PG_TYPE_OIDS.JSONB:
|
|
78
|
-
// // try {
|
|
79
|
-
// // return JSON.parse(value);
|
|
80
|
-
// // } catch (e) {
|
|
81
|
-
// // return value; // 解析失败时返回原始值
|
|
82
|
-
// // }
|
|
83
|
-
//
|
|
84
|
-
// // 默认不转换
|
|
85
|
-
// default:
|
|
86
|
-
// return value;
|
|
87
|
-
// }
|
|
88
|
-
// }
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
// /**
|
|
92
|
-
// * 获取并转换PG查询结果的数据类型
|
|
93
|
-
// * @param result
|
|
94
|
-
// */
|
|
95
|
-
// function pickAndConvertPgRows(result: any): any[] {
|
|
96
|
-
// if (!result || !Array.isArray(result.rows)) {
|
|
97
|
-
// return [];
|
|
98
|
-
// }
|
|
99
|
-
//
|
|
100
|
-
// console.log('',convertPgType)
|
|
101
|
-
// try {
|
|
102
|
-
// // 获取字段类型 OID
|
|
103
|
-
// const fieldTypes = result.fields.map((field: any) => field.dataTypeID);
|
|
104
|
-
//
|
|
105
|
-
// // 转换每一行的数据类型
|
|
106
|
-
// return result.rows.map((row:any) => {
|
|
107
|
-
// const convertedRow = {};
|
|
108
|
-
// Object.keys(row).forEach((key, index) => {
|
|
109
|
-
// const value = row[key];
|
|
110
|
-
// const oid = fieldTypes[index];
|
|
111
|
-
// convertedRow[key] = convertPgType(value, oid);
|
|
112
|
-
// });
|
|
113
|
-
// return convertedRow;
|
|
114
|
-
// });
|
|
115
|
-
// } catch (e) {
|
|
116
|
-
// console.error('pickAndConvertPgRows', e);
|
|
117
|
-
// }
|
|
118
|
-
// return [];
|
|
119
|
-
// }
|
|
120
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
121
|
-
exports.pickAndConvertPgRows = void 0;
|
|
122
|
-
function pickAndConvertPgRows(result) {
|
|
123
|
-
if (!result || !Array.isArray(result.rows)) {
|
|
124
|
-
return [];
|
|
125
|
-
}
|
|
126
|
-
return result.rows;
|
|
127
|
-
}
|
|
128
|
-
exports.pickAndConvertPgRows = pickAndConvertPgRows;
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// // 定义 PostgreSQL 数据类型 OID 映射
|
|
3
|
-
// const PG_TYPE_OIDS = {
|
|
4
|
-
// INT2: 21, // smallint
|
|
5
|
-
// INT4: 23, // integer
|
|
6
|
-
// INT8: 20, // bigint
|
|
7
|
-
// FLOAT4: 700, // real
|
|
8
|
-
// FLOAT8: 701, // double precision
|
|
9
|
-
// NUMERIC: 1700, // numeric
|
|
10
|
-
// MONEY: 790, // money
|
|
11
|
-
// BOOL: 16, // boolean
|
|
12
|
-
// DATE: 1082, // date
|
|
13
|
-
// TIMESTAMP: 1114, // timestamp without time zone
|
|
14
|
-
// TIMESTAMPTZ: 1184, // timestamp with time zone
|
|
15
|
-
// JSON: 114, // json
|
|
16
|
-
// JSONB: 3802, // jsonb
|
|
17
|
-
// };
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
// /**
|
|
22
|
-
// * 将 PostgreSQL 数据类型转换为 JavaScript 类型
|
|
23
|
-
// * @param {any} value - 原始值
|
|
24
|
-
// * @param {number} oid - 数据类型 OID
|
|
25
|
-
// * @returns {any} 转换后的值
|
|
26
|
-
// */
|
|
27
|
-
// function convertPgType(value: any, oid: any) {
|
|
28
|
-
// // 如果值为 null,直接返回
|
|
29
|
-
// if (value === null) {
|
|
30
|
-
// return null;
|
|
31
|
-
// }
|
|
32
|
-
//
|
|
33
|
-
// const typeOfValue = typeof value;
|
|
34
|
-
// if (typeOfValue === 'undefined') {
|
|
35
|
-
// return null;
|
|
36
|
-
// }
|
|
37
|
-
// if (typeOfValue === 'number' || typeOfValue === "boolean" || typeOfValue === "object") {
|
|
38
|
-
// return value;
|
|
39
|
-
// }
|
|
40
|
-
// if (typeOfValue === 'bigint') {
|
|
41
|
-
// return value.toString()
|
|
42
|
-
// }
|
|
43
|
-
//
|
|
44
|
-
// switch (oid) {
|
|
45
|
-
// // 整数类型(全部转为 Number)
|
|
46
|
-
// case PG_TYPE_OIDS.INT2: // smallint
|
|
47
|
-
// case PG_TYPE_OIDS.INT4: // integer
|
|
48
|
-
// return Number(value);
|
|
49
|
-
// case PG_TYPE_OIDS.INT8: // bigint
|
|
50
|
-
// return value;
|
|
51
|
-
//
|
|
52
|
-
// // 浮点类型
|
|
53
|
-
// case PG_TYPE_OIDS.FLOAT4: // real
|
|
54
|
-
// case PG_TYPE_OIDS.FLOAT8: // double precision
|
|
55
|
-
// case PG_TYPE_OIDS.NUMERIC: // numeric
|
|
56
|
-
// case PG_TYPE_OIDS.MONEY: // money
|
|
57
|
-
// return Number(value);
|
|
58
|
-
//
|
|
59
|
-
// // 布尔类型
|
|
60
|
-
// case PG_TYPE_OIDS.BOOL:
|
|
61
|
-
// if (typeof value === 'boolean') {
|
|
62
|
-
// return value;
|
|
63
|
-
// }
|
|
64
|
-
// return value === 't' || value === 'true' || value === true;
|
|
65
|
-
//
|
|
66
|
-
// // 日期/时间类型
|
|
67
|
-
// case PG_TYPE_OIDS.DATE:
|
|
68
|
-
// return new Date(value).toISOString().split('T')[0];
|
|
69
|
-
//
|
|
70
|
-
// case PG_TYPE_OIDS.TIMESTAMP:
|
|
71
|
-
// case PG_TYPE_OIDS.TIMESTAMPTZ:
|
|
72
|
-
// return new Date(value).toISOString();
|
|
73
|
-
//
|
|
74
|
-
// // JSON 类型
|
|
75
|
-
// case PG_TYPE_OIDS.JSON:
|
|
76
|
-
// case PG_TYPE_OIDS.JSONB:
|
|
77
|
-
// // try {
|
|
78
|
-
// // return JSON.parse(value);
|
|
79
|
-
// // } catch (e) {
|
|
80
|
-
// // return value; // 解析失败时返回原始值
|
|
81
|
-
// // }
|
|
82
|
-
//
|
|
83
|
-
// // 默认不转换
|
|
84
|
-
// default:
|
|
85
|
-
// return value;
|
|
86
|
-
// }
|
|
87
|
-
// }
|
|
88
|
-
//
|
|
89
|
-
//
|
|
90
|
-
// /**
|
|
91
|
-
// * 获取并转换PG查询结果的数据类型
|
|
92
|
-
// * @param result
|
|
93
|
-
// */
|
|
94
|
-
// function pickAndConvertPgRows(result: any): any[] {
|
|
95
|
-
// if (!result || !Array.isArray(result.rows)) {
|
|
96
|
-
// return [];
|
|
97
|
-
// }
|
|
98
|
-
//
|
|
99
|
-
// console.log('',convertPgType)
|
|
100
|
-
// try {
|
|
101
|
-
// // 获取字段类型 OID
|
|
102
|
-
// const fieldTypes = result.fields.map((field: any) => field.dataTypeID);
|
|
103
|
-
//
|
|
104
|
-
// // 转换每一行的数据类型
|
|
105
|
-
// return result.rows.map((row:any) => {
|
|
106
|
-
// const convertedRow = {};
|
|
107
|
-
// Object.keys(row).forEach((key, index) => {
|
|
108
|
-
// const value = row[key];
|
|
109
|
-
// const oid = fieldTypes[index];
|
|
110
|
-
// convertedRow[key] = convertPgType(value, oid);
|
|
111
|
-
// });
|
|
112
|
-
// return convertedRow;
|
|
113
|
-
// });
|
|
114
|
-
// } catch (e) {
|
|
115
|
-
// console.error('pickAndConvertPgRows', e);
|
|
116
|
-
// }
|
|
117
|
-
// return [];
|
|
118
|
-
// }
|
|
119
|
-
|
|
120
|
-
function pickAndConvertPgRows(result: any): any[] {
|
|
121
|
-
if (!result || !Array.isArray(result.rows)) {
|
|
122
|
-
return [];
|
|
123
|
-
}
|
|
124
|
-
return result.rows;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export { pickAndConvertPgRows };
|