dborm-mysql 2.2.1 → 2.2.2
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 +35 -27
- 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,7 +64,15 @@ 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;
|
|
68
77
|
try {
|
|
69
78
|
params[nth] = conn;
|
|
@@ -110,12 +119,12 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
110
119
|
|
|
111
120
|
if (connection) {
|
|
112
121
|
query = connection.query(sql, sqlParam, function (err, rows) {
|
|
113
|
-
if(
|
|
114
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
122
|
+
if(connection.logSql){
|
|
123
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
115
124
|
}
|
|
116
125
|
if (err) {
|
|
117
|
-
if(!
|
|
118
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
126
|
+
if (!connection.logSql){
|
|
127
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
119
128
|
}
|
|
120
129
|
err.code = dbCode;
|
|
121
130
|
err.stack = err.stack + getCurrentStack(currentStack);
|
|
@@ -127,13 +136,13 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
127
136
|
} else {
|
|
128
137
|
db.getConnection().then(function (connection) {
|
|
129
138
|
query = connection.query(sql, sqlParam, function (err, rows) {
|
|
130
|
-
if(
|
|
131
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
139
|
+
if(connection.logSql){
|
|
140
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
132
141
|
}
|
|
133
142
|
connection.release();
|
|
134
143
|
if (err) {
|
|
135
|
-
if(!
|
|
136
|
-
logSql(connection, rows, query.sql, startTime, logExecuteTime);
|
|
144
|
+
if (!connection.logSql){
|
|
145
|
+
logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
|
|
137
146
|
}
|
|
138
147
|
connection.destroy();
|
|
139
148
|
err.code = dbCode;
|
|
@@ -150,11 +159,11 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
150
159
|
});
|
|
151
160
|
};
|
|
152
161
|
|
|
153
|
-
db.beginTransaction = function () {
|
|
162
|
+
db.beginTransaction = function (options) {
|
|
154
163
|
let p = new Promise(function (resolve, reject) {
|
|
155
|
-
db.getConnection().then(function (conn) {
|
|
156
|
-
if(
|
|
157
|
-
|
|
164
|
+
db.getConnection(options).then(function (conn) {
|
|
165
|
+
if(conn.logSql){
|
|
166
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] beginTransaction`);
|
|
158
167
|
}
|
|
159
168
|
conn.beginTransaction(function (err) {
|
|
160
169
|
if (err) {
|
|
@@ -178,8 +187,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
178
187
|
if (err) {
|
|
179
188
|
reject(err);
|
|
180
189
|
} else {
|
|
181
|
-
if(
|
|
182
|
-
|
|
190
|
+
if(conn.logSql){
|
|
191
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] commitTransaction`);
|
|
183
192
|
}
|
|
184
193
|
// conn.release();
|
|
185
194
|
resolve('success');
|
|
@@ -191,8 +200,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
191
200
|
db.rollbackTransaction = function (conn) {
|
|
192
201
|
return new Promise(function (resolve, reject) {
|
|
193
202
|
conn.rollback(function (err, suc) {
|
|
194
|
-
if(
|
|
195
|
-
|
|
203
|
+
if(conn.logSql){
|
|
204
|
+
logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] rollbackTransaction`);
|
|
196
205
|
}
|
|
197
206
|
resolve();
|
|
198
207
|
});
|
|
@@ -200,5 +209,4 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
|
|
|
200
209
|
};
|
|
201
210
|
|
|
202
211
|
return db;
|
|
203
|
-
};
|
|
204
|
-
|
|
212
|
+
};
|
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>,
|