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
|
@@ -151,18 +151,22 @@ let BaseApiController = class BaseApiController extends BaseService_1.BaseServic
|
|
|
151
151
|
async decodeBodyBySession(body) {
|
|
152
152
|
var _a;
|
|
153
153
|
if (!((_a = this.ctx.userSession) === null || _a === void 0 ? void 0 : _a.isLogin())) {
|
|
154
|
-
throw new Error('
|
|
154
|
+
throw new Error('身份安全校验不通过, 用户必须先登录');
|
|
155
155
|
}
|
|
156
156
|
const sessionInfo = this.getUserSessionInfo();
|
|
157
157
|
const secretBody0 = body;
|
|
158
158
|
if (!secretBody0.payload1 || !secretBody0.payload2 || !secretBody0.payload3 || !secretBody0.payload4) {
|
|
159
159
|
return body;
|
|
160
160
|
}
|
|
161
|
+
const { privateKey, publicKey, accountId } = sessionInfo;
|
|
162
|
+
const serverPayload1 = md5(publicKey + accountId);
|
|
163
|
+
if (serverPayload1 !== secretBody0.payload1) {
|
|
164
|
+
throw new Error('身份安全校验不通过,请重新登录');
|
|
165
|
+
}
|
|
161
166
|
const secretBody = {
|
|
162
167
|
secretPayload: secretBody0.payload2,
|
|
163
168
|
secretKey: secretBody0.payload4,
|
|
164
169
|
};
|
|
165
|
-
const { privateKey, publicKey } = sessionInfo;
|
|
166
170
|
const secretPayload = secretBody.secretPayload;
|
|
167
171
|
const secretKey = secretBody.secretKey; // 使用RSA加密的AES密钥
|
|
168
172
|
const aesKey = await AsymmetricCrypto_1.AsymmetricCrypto.decrypt(privateKey, secretKey); // 解密AES密钥
|
|
@@ -16,6 +16,7 @@ const BaseApiController_1 = require("../base/BaseApiController");
|
|
|
16
16
|
const permission_middleware_1 = require("../../middleware/permission.middleware");
|
|
17
17
|
const SystemPerm_1 = require("../../models/SystemPerm");
|
|
18
18
|
const common_dto_1 = require("../../libs/utils/common-dto");
|
|
19
|
+
const CrudMonitor_1 = require("../../libs/crud-pro/utils/CrudMonitor");
|
|
19
20
|
let SystemInfoManageApi = class SystemInfoManageApi extends BaseApiController_1.BaseApiController {
|
|
20
21
|
/**
|
|
21
22
|
* 预设系统内置功能点数据
|
|
@@ -49,6 +50,7 @@ let SystemInfoManageApi = class SystemInfoManageApi extends BaseApiController_1.
|
|
|
49
50
|
timeInfo,
|
|
50
51
|
envInfo,
|
|
51
52
|
networkInfo,
|
|
53
|
+
crudMonitorInfo: CrudMonitor_1.CrudMonitor
|
|
52
54
|
});
|
|
53
55
|
}
|
|
54
56
|
};
|
|
@@ -100,6 +100,9 @@ class CrudPro {
|
|
|
100
100
|
await this.executeSQLList();
|
|
101
101
|
// 执行SQL执行之后的的处理
|
|
102
102
|
await this.afterExecuteSQLList();
|
|
103
|
+
// 再次尝试释放一下资源。避免因为不正确的使用方式导致,连接未释放。
|
|
104
|
+
const txObj = this.executeContext.getTransaction();
|
|
105
|
+
await txObj.releaseTxOnlyReleased();
|
|
103
106
|
return exeCtx;
|
|
104
107
|
}
|
|
105
108
|
async getCachedCfgByMethod(method, isEnableCache) {
|
|
@@ -17,8 +17,17 @@ export interface IConnectionPool {
|
|
|
17
17
|
poolName: string;
|
|
18
18
|
poolInstance: any;
|
|
19
19
|
}
|
|
20
|
+
export interface IPoolConnectionQueryResult {
|
|
21
|
+
rows: any[];
|
|
22
|
+
originRes: any;
|
|
23
|
+
}
|
|
20
24
|
export interface IPoolConnectionClient {
|
|
21
|
-
query: (sql: string, values: any[]) => Promise<
|
|
25
|
+
query: (sql: string, values: any[]) => Promise<IPoolConnectionQueryResult>;
|
|
26
|
+
release: () => Promise<any>;
|
|
27
|
+
commit: () => Promise<any>;
|
|
28
|
+
rollback: () => Promise<any>;
|
|
29
|
+
beginTransaction: () => Promise<any>;
|
|
30
|
+
originConnection: any;
|
|
22
31
|
}
|
|
23
32
|
/**
|
|
24
33
|
* 用户请求
|
|
@@ -1,15 +1,33 @@
|
|
|
1
|
-
import { IConnectionPool, IPoolConnectionClient } from '../interfaces';
|
|
1
|
+
import { IConnectionPool, IPoolConnectionClient, IPoolConnectionQueryResult } from '../interfaces';
|
|
2
|
+
declare class TransactionConnection implements IPoolConnectionClient {
|
|
3
|
+
originConnection: IPoolConnectionClient;
|
|
4
|
+
private isFinished;
|
|
5
|
+
private isTxBegan;
|
|
6
|
+
private isTxCommitted;
|
|
7
|
+
private isTxRollback;
|
|
8
|
+
private isReleased;
|
|
9
|
+
constructor(originConnection: IPoolConnectionClient);
|
|
10
|
+
query(sql: string, values: any[]): Promise<IPoolConnectionQueryResult>;
|
|
11
|
+
beginTransaction(): Promise<void>;
|
|
12
|
+
commit(): Promise<void>;
|
|
13
|
+
rollback(): Promise<void>;
|
|
14
|
+
release(): Promise<void>;
|
|
15
|
+
releaseOnlyFinished(): Promise<void>;
|
|
16
|
+
}
|
|
2
17
|
declare class Transaction {
|
|
3
18
|
private transactionMySQL;
|
|
4
19
|
private transactionPostgres;
|
|
5
20
|
private transactionSqlServer;
|
|
21
|
+
private isBeginTransaction;
|
|
22
|
+
private connectionMap;
|
|
23
|
+
private connectionList;
|
|
6
24
|
private isReleased;
|
|
7
25
|
private getTxObj;
|
|
8
26
|
/**
|
|
9
27
|
* 获取链接对象
|
|
10
28
|
* @param pool
|
|
11
29
|
*/
|
|
12
|
-
getTxConnection(pool: IConnectionPool): Promise<
|
|
30
|
+
getTxConnection(pool: IConnectionPool): Promise<TransactionConnection>;
|
|
13
31
|
/**
|
|
14
32
|
* 开始事务
|
|
15
33
|
*/
|
|
@@ -26,5 +44,6 @@ declare class Transaction {
|
|
|
26
44
|
* 释放连接
|
|
27
45
|
*/
|
|
28
46
|
releaseTx(): Promise<void>;
|
|
47
|
+
releaseTxOnlyReleased(): Promise<void>;
|
|
29
48
|
}
|
|
30
49
|
export { Transaction };
|
|
@@ -5,11 +5,68 @@ const keys_1 = require("./keys");
|
|
|
5
5
|
const TransactionMySQL_1 = require("./TransactionMySQL");
|
|
6
6
|
const TransactionPostgres_1 = require("./TransactionPostgres");
|
|
7
7
|
const TransactionSqlServer_1 = require("./TransactionSqlServer");
|
|
8
|
+
class TransactionConnection {
|
|
9
|
+
constructor(originConnection) {
|
|
10
|
+
this.isFinished = false;
|
|
11
|
+
this.isTxBegan = false;
|
|
12
|
+
this.isTxCommitted = false;
|
|
13
|
+
this.isTxRollback = false;
|
|
14
|
+
this.isReleased = false;
|
|
15
|
+
this.originConnection = originConnection;
|
|
16
|
+
}
|
|
17
|
+
async query(sql, values) {
|
|
18
|
+
try {
|
|
19
|
+
return await this.originConnection.query(sql, values);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.error('[crud-pro]TransactionConnection', error);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
this.isFinished = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async beginTransaction() {
|
|
30
|
+
this.isTxBegan = true;
|
|
31
|
+
await this.originConnection.beginTransaction();
|
|
32
|
+
}
|
|
33
|
+
async commit() {
|
|
34
|
+
this.isTxCommitted = true;
|
|
35
|
+
await this.originConnection.commit();
|
|
36
|
+
}
|
|
37
|
+
async rollback() {
|
|
38
|
+
this.isTxRollback = true;
|
|
39
|
+
await this.originConnection.commit();
|
|
40
|
+
}
|
|
41
|
+
async release() {
|
|
42
|
+
if (this.isReleased) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.isReleased = true;
|
|
46
|
+
try {
|
|
47
|
+
if (this.isTxBegan && !this.isTxRollback && !this.isTxCommitted) {
|
|
48
|
+
await this.commit();
|
|
49
|
+
}
|
|
50
|
+
await this.originConnection.release();
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
console.log('[crud-pro] MySqlConnectionClient release ', e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async releaseOnlyFinished() {
|
|
57
|
+
if (this.isFinished) {
|
|
58
|
+
await this.release();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
8
62
|
class Transaction {
|
|
9
63
|
constructor() {
|
|
10
64
|
this.transactionMySQL = new TransactionMySQL_1.TransactionMySQL();
|
|
11
65
|
this.transactionPostgres = new TransactionPostgres_1.TransactionPostgres();
|
|
12
66
|
this.transactionSqlServer = new TransactionSqlServer_1.TransactionSqlServer();
|
|
67
|
+
this.isBeginTransaction = false;
|
|
68
|
+
this.connectionMap = {};
|
|
69
|
+
this.connectionList = [];
|
|
13
70
|
this.isReleased = false;
|
|
14
71
|
}
|
|
15
72
|
getTxObj(pool) {
|
|
@@ -30,45 +87,91 @@ class Transaction {
|
|
|
30
87
|
*/
|
|
31
88
|
async getTxConnection(pool) {
|
|
32
89
|
if (this.isReleased) {
|
|
33
|
-
const msg = '[Transaction] getTxConnection error, the txObject is isReleased, please check your code . DB request must be await';
|
|
90
|
+
const msg = '[crud-pro][Transaction] getTxConnection error, the txObject is isReleased, please check your code . DB request must be await';
|
|
34
91
|
console.error(msg);
|
|
35
92
|
throw new Error(msg);
|
|
36
93
|
}
|
|
37
|
-
|
|
38
|
-
|
|
94
|
+
// 尽量复用同一个连接。但是如果并行请求,也允许使用多个链接。
|
|
95
|
+
// 复用一个链接:可以使用事务。
|
|
96
|
+
// 使用多个链接:不能使用事务。
|
|
97
|
+
let connection = this.connectionMap[pool.poolName];
|
|
98
|
+
if (!connection) {
|
|
99
|
+
const txObj = this.getTxObj(pool);
|
|
100
|
+
const connection0 = await txObj.getTxConnection(pool);
|
|
101
|
+
connection = new TransactionConnection(connection0);
|
|
102
|
+
this.connectionMap[pool.poolName] = connection;
|
|
103
|
+
this.connectionList.push(connection);
|
|
104
|
+
// 开启了事务
|
|
105
|
+
if (this.isBeginTransaction) {
|
|
106
|
+
await connection.beginTransaction();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return connection;
|
|
39
110
|
}
|
|
40
111
|
/**
|
|
41
112
|
* 开始事务
|
|
42
113
|
*/
|
|
43
114
|
beginTx() {
|
|
44
|
-
this.
|
|
45
|
-
this.transactionPostgres.beginTx();
|
|
46
|
-
this.transactionSqlServer.beginTx();
|
|
115
|
+
this.isBeginTransaction = true;
|
|
47
116
|
}
|
|
48
117
|
/**
|
|
49
118
|
* 提交事务
|
|
50
119
|
*/
|
|
51
120
|
async commitTx() {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
121
|
+
if (!this.isBeginTransaction) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const connections = this.connectionList;
|
|
125
|
+
for (let i = 0; i < connections.length; i++) {
|
|
126
|
+
const connection = connections[i];
|
|
127
|
+
await connection.commit();
|
|
128
|
+
}
|
|
55
129
|
}
|
|
56
130
|
/**
|
|
57
131
|
* 事务回滚
|
|
58
132
|
*/
|
|
59
133
|
async rollbackTx() {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
134
|
+
if (!this.isBeginTransaction) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const connections = this.connectionList;
|
|
138
|
+
for (let i = 0; i < connections.length; i++) {
|
|
139
|
+
const connection = connections[i];
|
|
140
|
+
await connection.rollback();
|
|
141
|
+
}
|
|
63
142
|
}
|
|
64
143
|
/**
|
|
65
144
|
* 释放连接
|
|
66
145
|
*/
|
|
67
146
|
async releaseTx() {
|
|
68
|
-
await this.transactionMySQL.releaseTx();
|
|
69
|
-
await this.transactionPostgres.releaseTx();
|
|
70
|
-
await this.transactionSqlServer.releaseTx();
|
|
71
147
|
this.isReleased = true;
|
|
148
|
+
// 不管有没有开启事务,都要释放连接。
|
|
149
|
+
const connections = this.connectionList;
|
|
150
|
+
for (let i = 0; i < connections.length; i++) {
|
|
151
|
+
const connection = connections[i];
|
|
152
|
+
try {
|
|
153
|
+
await connection.releaseOnlyFinished();
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
console.error(e);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async releaseTxOnlyReleased() {
|
|
161
|
+
if (!this.isReleased) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// 不管有没有开启事务,都要释放连接。
|
|
165
|
+
const connections = this.connectionList;
|
|
166
|
+
for (let i = 0; i < connections.length; i++) {
|
|
167
|
+
const connection = connections[i];
|
|
168
|
+
try {
|
|
169
|
+
await connection.releaseOnlyFinished();
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
console.error(e);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
72
175
|
}
|
|
73
176
|
}
|
|
74
177
|
exports.Transaction = Transaction;
|
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
import { PoolConnection } from 'mysql2/promise';
|
|
2
|
-
import { IConnectionPool } from '../interfaces';
|
|
2
|
+
import { IConnectionPool, IPoolConnectionClient, IPoolConnectionQueryResult } from '../interfaces';
|
|
3
|
+
declare class MySqlConnectionClient implements IPoolConnectionClient {
|
|
4
|
+
originConnection: PoolConnection;
|
|
5
|
+
constructor(originConnection: PoolConnection);
|
|
6
|
+
query(sql: string, values: any[]): Promise<IPoolConnectionQueryResult>;
|
|
7
|
+
beginTransaction(): Promise<void>;
|
|
8
|
+
commit(): Promise<void>;
|
|
9
|
+
rollback(): Promise<void>;
|
|
10
|
+
release(): Promise<void>;
|
|
11
|
+
}
|
|
3
12
|
declare class TransactionMySQL {
|
|
4
|
-
private isBeginTransaction;
|
|
5
|
-
private connectionMap;
|
|
6
13
|
/**
|
|
7
14
|
* 获取链接对象
|
|
8
15
|
* @param pool
|
|
9
16
|
*/
|
|
10
|
-
getTxConnection(pool: IConnectionPool): Promise<
|
|
11
|
-
/**
|
|
12
|
-
* 开始事务
|
|
13
|
-
*/
|
|
14
|
-
beginTx(): void;
|
|
15
|
-
/**
|
|
16
|
-
* 提交事务
|
|
17
|
-
*/
|
|
18
|
-
commitTx(): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* 事务回滚
|
|
21
|
-
*/
|
|
22
|
-
rollbackTx(): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* 释放连接
|
|
25
|
-
*/
|
|
26
|
-
releaseTx(): Promise<void>;
|
|
17
|
+
getTxConnection(pool: IConnectionPool): Promise<MySqlConnectionClient>;
|
|
27
18
|
}
|
|
28
19
|
export { TransactionMySQL };
|
|
@@ -1,76 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TransactionMySQL = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.
|
|
4
|
+
const CrudMonitor_1 = require("../../../libs/crud-pro/utils/CrudMonitor");
|
|
5
|
+
class MySqlConnectionClient {
|
|
6
|
+
constructor(originConnection) {
|
|
7
|
+
this.originConnection = originConnection;
|
|
8
|
+
CrudMonitor_1.CrudMonitor.mysqlGetConnectionCount++;
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const poolInstance = pool.poolInstance;
|
|
17
|
-
connection = await poolInstance.getConnection();
|
|
18
|
-
this.connectionMap[pool.poolName] = connection;
|
|
19
|
-
// 开启了事务
|
|
20
|
-
if (this.isBeginTransaction) {
|
|
21
|
-
await connection.beginTransaction();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return connection;
|
|
10
|
+
async query(sql, values) {
|
|
11
|
+
const res = await this.originConnection.query(sql, values);
|
|
12
|
+
const rows = res[0];
|
|
13
|
+
return {
|
|
14
|
+
rows,
|
|
15
|
+
originRes: res
|
|
16
|
+
};
|
|
25
17
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
29
|
-
beginTx() {
|
|
30
|
-
this.isBeginTransaction = true;
|
|
18
|
+
async beginTransaction() {
|
|
19
|
+
await this.originConnection.beginTransaction();
|
|
31
20
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*/
|
|
35
|
-
async commitTx() {
|
|
36
|
-
if (!this.isBeginTransaction) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
const connections = Object.values(this.connectionMap);
|
|
40
|
-
for (let i = 0; i < connections.length; i++) {
|
|
41
|
-
const connection = connections[i];
|
|
42
|
-
await connection.commit();
|
|
43
|
-
}
|
|
21
|
+
async commit() {
|
|
22
|
+
await this.originConnection.commit();
|
|
44
23
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
async
|
|
49
|
-
|
|
50
|
-
|
|
24
|
+
async rollback() {
|
|
25
|
+
await this.originConnection.commit();
|
|
26
|
+
}
|
|
27
|
+
async release() {
|
|
28
|
+
try {
|
|
29
|
+
CrudMonitor_1.CrudMonitor.mysqlReleaseConnectionCount++;
|
|
30
|
+
await this.originConnection.release();
|
|
51
31
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const connection = connections[i];
|
|
55
|
-
await connection.rollback();
|
|
32
|
+
catch (e) {
|
|
33
|
+
console.log('[crud-pro] MySqlConnectionClient release ', e);
|
|
56
34
|
}
|
|
57
35
|
}
|
|
36
|
+
}
|
|
37
|
+
class TransactionMySQL {
|
|
58
38
|
/**
|
|
59
|
-
*
|
|
39
|
+
* 获取链接对象
|
|
40
|
+
* @param pool
|
|
60
41
|
*/
|
|
61
|
-
async
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const connection = connections[i];
|
|
66
|
-
try {
|
|
67
|
-
await connection.release();
|
|
68
|
-
}
|
|
69
|
-
catch (e) {
|
|
70
|
-
console.error(e);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
this.connectionMap = {};
|
|
42
|
+
async getTxConnection(pool) {
|
|
43
|
+
const poolInstance = pool.poolInstance;
|
|
44
|
+
const originConnection = await poolInstance.getConnection();
|
|
45
|
+
return new MySqlConnectionClient(originConnection);
|
|
74
46
|
}
|
|
75
47
|
}
|
|
76
48
|
exports.TransactionMySQL = TransactionMySQL;
|
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
import { PoolClient } from 'pg';
|
|
2
|
-
import { IConnectionPool } from '../interfaces';
|
|
2
|
+
import { IConnectionPool, IPoolConnectionClient, IPoolConnectionQueryResult } from '../interfaces';
|
|
3
|
+
declare class PostgresConnectionClient implements IPoolConnectionClient {
|
|
4
|
+
originConnection: PoolClient;
|
|
5
|
+
constructor(originConnection: PoolClient);
|
|
6
|
+
query(sql: string, values: any[]): Promise<IPoolConnectionQueryResult>;
|
|
7
|
+
beginTransaction(): Promise<void>;
|
|
8
|
+
commit(): Promise<void>;
|
|
9
|
+
rollback(): Promise<void>;
|
|
10
|
+
release(): Promise<void>;
|
|
11
|
+
}
|
|
3
12
|
declare class TransactionPostgres {
|
|
4
|
-
private isBeginTransaction;
|
|
5
|
-
private connectionMap;
|
|
6
13
|
/**
|
|
7
14
|
* 获取链接对象
|
|
8
15
|
* @param pool
|
|
9
16
|
*/
|
|
10
|
-
getTxConnection(pool: IConnectionPool): Promise<
|
|
11
|
-
/**
|
|
12
|
-
* 开始事务
|
|
13
|
-
*/
|
|
14
|
-
beginTx(): void;
|
|
15
|
-
/**
|
|
16
|
-
* 提交事务
|
|
17
|
-
*/
|
|
18
|
-
commitTx(): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* 事务回滚
|
|
21
|
-
*/
|
|
22
|
-
rollbackTx(): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* 释放连接
|
|
25
|
-
*/
|
|
26
|
-
releaseTx(): Promise<void>;
|
|
17
|
+
getTxConnection(pool: IConnectionPool): Promise<PostgresConnectionClient>;
|
|
27
18
|
}
|
|
28
19
|
export { TransactionPostgres };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TransactionPostgres = void 0;
|
|
4
|
+
const CrudMonitor_1 = require("../../../libs/crud-pro/utils/CrudMonitor");
|
|
4
5
|
async function beginTransaction(connection) {
|
|
5
6
|
return await connection.query('BEGIN');
|
|
6
7
|
}
|
|
@@ -10,76 +11,54 @@ async function commit(connection) {
|
|
|
10
11
|
async function rollback(connection) {
|
|
11
12
|
return await connection.query('ROLLBACK');
|
|
12
13
|
}
|
|
13
|
-
class
|
|
14
|
-
constructor() {
|
|
15
|
-
this.
|
|
16
|
-
|
|
14
|
+
class PostgresConnectionClient {
|
|
15
|
+
constructor(originConnection) {
|
|
16
|
+
this.originConnection = originConnection;
|
|
17
|
+
CrudMonitor_1.CrudMonitor.postgresGetConnectionCount++;
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
this.connectionMap[pool.poolName] = connection;
|
|
28
|
-
// 开启了事务
|
|
29
|
-
if (this.isBeginTransaction) {
|
|
30
|
-
await beginTransaction(connection);
|
|
31
|
-
}
|
|
19
|
+
async query(sql, values) {
|
|
20
|
+
const pgClient = this.originConnection;
|
|
21
|
+
const result = await pgClient.query({
|
|
22
|
+
text: sql,
|
|
23
|
+
values: values || [],
|
|
24
|
+
});
|
|
25
|
+
let rows = [];
|
|
26
|
+
if (result && Array.isArray(result.rows)) {
|
|
27
|
+
rows = result.rows;
|
|
32
28
|
}
|
|
33
|
-
return
|
|
29
|
+
return {
|
|
30
|
+
rows,
|
|
31
|
+
originRes: result
|
|
32
|
+
};
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*/
|
|
38
|
-
beginTx() {
|
|
39
|
-
this.isBeginTransaction = true;
|
|
34
|
+
async beginTransaction() {
|
|
35
|
+
await beginTransaction(this.originConnection);
|
|
40
36
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*/
|
|
44
|
-
async commitTx() {
|
|
45
|
-
if (!this.isBeginTransaction) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const connections = Object.values(this.connectionMap);
|
|
49
|
-
for (let i = 0; i < connections.length; i++) {
|
|
50
|
-
const connection = connections[i];
|
|
51
|
-
await commit(connection);
|
|
52
|
-
}
|
|
37
|
+
async commit() {
|
|
38
|
+
await commit(this.originConnection);
|
|
53
39
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
async
|
|
58
|
-
|
|
59
|
-
|
|
40
|
+
async rollback() {
|
|
41
|
+
await rollback(this.originConnection);
|
|
42
|
+
}
|
|
43
|
+
async release() {
|
|
44
|
+
try {
|
|
45
|
+
CrudMonitor_1.CrudMonitor.postgresReleaseConnectionCount++;
|
|
46
|
+
await this.originConnection.release();
|
|
60
47
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const connection = connections[i];
|
|
64
|
-
await rollback(connection);
|
|
48
|
+
catch (e) {
|
|
49
|
+
console.log('[crud-pro] PostgresConnectionClient release ', e);
|
|
65
50
|
}
|
|
66
51
|
}
|
|
52
|
+
}
|
|
53
|
+
class TransactionPostgres {
|
|
67
54
|
/**
|
|
68
|
-
*
|
|
55
|
+
* 获取链接对象
|
|
56
|
+
* @param pool
|
|
69
57
|
*/
|
|
70
|
-
async
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
const connection = connections[i];
|
|
75
|
-
try {
|
|
76
|
-
await connection.release();
|
|
77
|
-
}
|
|
78
|
-
catch (e) {
|
|
79
|
-
console.error(e);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
this.connectionMap = {};
|
|
58
|
+
async getTxConnection(pool) {
|
|
59
|
+
const poolInstance = pool.poolInstance;
|
|
60
|
+
const originConnection = await poolInstance.connect();
|
|
61
|
+
return new PostgresConnectionClient(originConnection);
|
|
83
62
|
}
|
|
84
63
|
}
|
|
85
64
|
exports.TransactionPostgres = TransactionPostgres;
|
|
@@ -1,34 +1,9 @@
|
|
|
1
|
-
import { ConnectionPool } from 'mssql';
|
|
2
1
|
import { IConnectionPool, IPoolConnectionClient } from '../interfaces';
|
|
3
|
-
interface ISqlServerConnection extends IPoolConnectionClient {
|
|
4
|
-
originConnection: ConnectionPool;
|
|
5
|
-
}
|
|
6
2
|
declare class TransactionSqlServer {
|
|
7
|
-
private isBeginTransaction;
|
|
8
|
-
private connectionMap;
|
|
9
|
-
private transactionMap;
|
|
10
3
|
/**
|
|
11
4
|
* 获取链接对象
|
|
12
5
|
* @param pool
|
|
13
6
|
*/
|
|
14
|
-
getTxConnection(pool: IConnectionPool): Promise<
|
|
15
|
-
private connectAndCreateConnection;
|
|
16
|
-
/**
|
|
17
|
-
* 开始事务
|
|
18
|
-
*/
|
|
19
|
-
beginTx(): void;
|
|
20
|
-
/**
|
|
21
|
-
* 提交事务
|
|
22
|
-
*/
|
|
23
|
-
commitTx(): Promise<void>;
|
|
24
|
-
/**
|
|
25
|
-
* 事务回滚
|
|
26
|
-
*/
|
|
27
|
-
rollbackTx(): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* 释放连接
|
|
30
|
-
*/
|
|
31
|
-
releaseTx(): Promise<void>;
|
|
32
|
-
private beginTransaction;
|
|
7
|
+
getTxConnection(pool: IConnectionPool): Promise<IPoolConnectionClient>;
|
|
33
8
|
}
|
|
34
9
|
export { TransactionSqlServer };
|