dborm-mysql 2.2.1 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base/db.js +39 -28
- package/base/dbORM.js +1 -0
- package/index.d.ts +2 -1
- package/package.json +1 -1
package/base/db.js
CHANGED
|
@@ -11,16 +11,16 @@ function initMysqlPool(db, dbConfig) {
|
|
|
11
11
|
db.pool = mysql.createPool(dbConfig);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
let logSql = (connection, rows, sql, startTime, logExecuteTime) => {
|
|
14
|
+
let logSql = (connection, rows, sql, startTime, logExecuteTime, logger) => {
|
|
15
15
|
let insertIdLog = (rows && rows.insertId) ? `[insertId = ${rows.insertId}] ` : '';
|
|
16
16
|
|
|
17
17
|
let info = `[${connection.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}]`;
|
|
18
18
|
if(logExecuteTime){
|
|
19
19
|
const executeTime = (new Date()).getTime() - startTime.getTime();
|
|
20
|
-
info += `[execute time: ${executeTime}ms]
|
|
20
|
+
info += `[execute time: ${executeTime}ms]`;
|
|
21
21
|
}
|
|
22
22
|
info += `${insertIdLog} ${sql}`;
|
|
23
|
-
|
|
23
|
+
logger(info);
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
// 去掉报错信息行,只保留当前函数调用栈
|
|
@@ -29,32 +29,33 @@ let getCurrentStack = (currentStack) => {
|
|
|
29
29
|
return currentStack.split('\n').slice(1).join('\n');
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) => {
|
|
32
|
+
module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime, logger}) => {
|
|
33
33
|
let db = {
|
|
34
34
|
pool: null
|
|
35
35
|
};
|
|
36
36
|
initMysqlPool(db, dbConfig);
|
|
37
37
|
let reconnectionTime = 0;
|
|
38
38
|
//获取数据连接,将回调转换为promise
|
|
39
|
-
db.getConnection = function () {
|
|
39
|
+
db.getConnection = function (options = {}) {
|
|
40
40
|
return new Promise(function (resolve, reject) {
|
|
41
41
|
db.pool.getConnection(function (err, connection) {
|
|
42
42
|
if (err) {
|
|
43
43
|
if(err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT' || err.code === 'PROTOCOL_SEQUENCE_TIMEOUT'){
|
|
44
|
-
|
|
44
|
+
logger('mysql reconnect, reconnect time:', reconnectionTime++);
|
|
45
45
|
db.getConnection().then(resolve, reject);
|
|
46
46
|
}
|
|
47
47
|
reject(err);
|
|
48
48
|
} else {
|
|
49
|
-
connection.connectionLogId = shortUuid().new().slice(0, 6);
|
|
49
|
+
connection.connectionLogId = options.transId || shortUuid().new().slice(0, 6);
|
|
50
50
|
reconnectionTime = 0;
|
|
51
|
+
connection.logSql = options.transId || options.logSql || log || process.SQL_LOG;
|
|
51
52
|
resolve(connection);
|
|
52
53
|
}
|
|
53
54
|
});
|
|
54
55
|
});
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
db.wrapTransaction = function (fn, nth, timeout) {
|
|
58
|
+
db.wrapTransaction = function (fn, nth, timeout, options = {}) {
|
|
58
59
|
const Message = '等待事务超时';
|
|
59
60
|
return function () {
|
|
60
61
|
let ctx = this;
|
|
@@ -63,24 +64,35 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
63
64
|
return fn.apply(ctx, params);
|
|
64
65
|
} else {
|
|
65
66
|
return (co.wrap(function* (params) {
|
|
66
|
-
let
|
|
67
|
+
let newOptions = Object.assign({}, options);
|
|
68
|
+
if (options.transId && typeof options.transId === 'function'){
|
|
69
|
+
newOptions.transId = options.transId(params);
|
|
70
|
+
}
|
|
71
|
+
if (options.logSql && typeof options.logSql === 'function'){
|
|
72
|
+
newOptions.logSql = options.logSql(params);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let conn = yield db.beginTransaction(newOptions);
|
|
67
76
|
let result;
|
|
77
|
+
let timer;
|
|
68
78
|
try {
|
|
69
79
|
params[nth] = conn;
|
|
70
80
|
result = yield Promise.race([
|
|
71
81
|
fn.apply(ctx, params),
|
|
72
82
|
new Promise((res) => {
|
|
73
|
-
setTimeout(() => {
|
|
83
|
+
timer = setTimeout(() => {
|
|
74
84
|
res(Message);
|
|
75
85
|
}, timeout || 50000);
|
|
76
86
|
})
|
|
77
87
|
]);
|
|
88
|
+
if(timer) clearTimeout(timer);
|
|
78
89
|
if(result === Message){
|
|
79
90
|
throw new Error(result);
|
|
80
91
|
}
|
|
81
92
|
yield db.commitTransaction(conn);
|
|
82
93
|
conn.release();
|
|
83
94
|
} catch (err) {
|
|
95
|
+
if(timer) clearTimeout(timer);
|
|
84
96
|
yield db.rollbackTransaction(conn);
|
|
85
97
|
conn.release();
|
|
86
98
|
conn.destroy();
|
|
@@ -110,12 +122,12 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
110
122
|
|
|
111
123
|
if (connection) {
|
|
112
124
|
query = connection.query(sql, sqlParam, function (err, rows) {
|
|
113
|
-
if(
|
|
114
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
125
|
+
if(connection.logSql){
|
|
126
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
115
127
|
}
|
|
116
128
|
if (err) {
|
|
117
|
-
if(!
|
|
118
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
129
|
+
if (!connection.logSql){
|
|
130
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
119
131
|
}
|
|
120
132
|
err.code = dbCode;
|
|
121
133
|
err.stack = err.stack + getCurrentStack(currentStack);
|
|
@@ -127,13 +139,13 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
127
139
|
} else {
|
|
128
140
|
db.getConnection().then(function (connection) {
|
|
129
141
|
query = connection.query(sql, sqlParam, function (err, rows) {
|
|
130
|
-
if(
|
|
131
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
142
|
+
if(connection.logSql){
|
|
143
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
132
144
|
}
|
|
133
145
|
connection.release();
|
|
134
146
|
if (err) {
|
|
135
|
-
if(!
|
|
136
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
147
|
+
if (!connection.logSql){
|
|
148
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
137
149
|
}
|
|
138
150
|
connection.destroy();
|
|
139
151
|
err.code = dbCode;
|
|
@@ -150,11 +162,11 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
150
162
|
});
|
|
151
163
|
};
|
|
152
164
|
|
|
153
|
-
db.beginTransaction = function () {
|
|
165
|
+
db.beginTransaction = function (options) {
|
|
154
166
|
let p = new Promise(function (resolve, reject) {
|
|
155
|
-
db.getConnection().then(function (conn) {
|
|
156
|
-
if(
|
|
157
|
-
|
|
167
|
+
db.getConnection(options).then(function (conn) {
|
|
168
|
+
if(conn.logSql){
|
|
169
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] beginTransaction`);
|
|
158
170
|
}
|
|
159
171
|
conn.beginTransaction(function (err) {
|
|
160
172
|
if (err) {
|
|
@@ -178,8 +190,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
178
190
|
if (err) {
|
|
179
191
|
reject(err);
|
|
180
192
|
} else {
|
|
181
|
-
if(
|
|
182
|
-
|
|
193
|
+
if(conn.logSql){
|
|
194
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] commitTransaction`);
|
|
183
195
|
}
|
|
184
196
|
// conn.release();
|
|
185
197
|
resolve('success');
|
|
@@ -191,8 +203,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
191
203
|
db.rollbackTransaction = function (conn) {
|
|
192
204
|
return new Promise(function (resolve, reject) {
|
|
193
205
|
conn.rollback(function (err, suc) {
|
|
194
|
-
if(
|
|
195
|
-
|
|
206
|
+
if(conn.logSql){
|
|
207
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] rollbackTransaction`);
|
|
196
208
|
}
|
|
197
209
|
resolve();
|
|
198
210
|
});
|
|
@@ -200,5 +212,4 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
200
212
|
};
|
|
201
213
|
|
|
202
214
|
return db;
|
|
203
|
-
};
|
|
204
|
-
|
|
215
|
+
};
|
package/base/dbORM.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -51,6 +51,7 @@ interface options {
|
|
|
51
51
|
dbCode: number;
|
|
52
52
|
noConvertDbCodes: Array<number>,
|
|
53
53
|
logExecuteTime?: boolean,
|
|
54
|
+
logger?: any;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
|
|
@@ -76,7 +77,7 @@ declare namespace dbORM {
|
|
|
76
77
|
interface ORM_DB {
|
|
77
78
|
pool: mysql.Pool,
|
|
78
79
|
getConnection(): Promise<mysql.Connection>,
|
|
79
|
-
wrapTransaction(fn: Function, nth: number, timeout?: number): (...args: any[]) => Promise<any>,
|
|
80
|
+
wrapTransaction(fn: Function, nth: number, timeout?: number, options?: {transId?: any, logSql?: any}): (...args: any[]) => Promise<any>,
|
|
80
81
|
query(sql: string, params: Array<any>, connection?: mysql.Connection): Promise<any>,
|
|
81
82
|
beginTransaction(): Promise<mysql.Connection>,
|
|
82
83
|
commitTransaction(conn: mysql.Connection): Promise<any>,
|