dborm-mysql 2.2.0 → 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 CHANGED
@@ -11,44 +11,51 @@ 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
- console.log(info);
23
+ logger(info);
24
24
  };
25
25
 
26
- module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) => {
26
+ // 去掉报错信息行,只保留当前函数调用栈
27
+ let getCurrentStack = (currentStack) => {
28
+ // new Error().stack的格式是:Error: 错误信息\n at 函数名 (文件路径:行号:列号)... 去掉第一行即可获取当前函数的调用栈
29
+ return currentStack.split('\n').slice(1).join('\n');
30
+ };
31
+
32
+ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime, logger}) => {
27
33
  let db = {
28
34
  pool: null
29
35
  };
30
36
  initMysqlPool(db, dbConfig);
31
37
  let reconnectionTime = 0;
32
38
  //获取数据连接,将回调转换为promise
33
- db.getConnection = function () {
39
+ db.getConnection = function (options = {}) {
34
40
  return new Promise(function (resolve, reject) {
35
41
  db.pool.getConnection(function (err, connection) {
36
42
  if (err) {
37
43
  if(err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT' || err.code === 'PROTOCOL_SEQUENCE_TIMEOUT'){
38
- console.log('mysql reconnect, reconnect time:', reconnectionTime++);
44
+ logger('mysql reconnect, reconnect time:', reconnectionTime++);
39
45
  db.getConnection().then(resolve, reject);
40
46
  }
41
47
  reject(err);
42
48
  } else {
43
- connection.connectionLogId = shortUuid().new().slice(0, 6);
49
+ connection.connectionLogId = options.transId || shortUuid().new().slice(0, 6);
44
50
  reconnectionTime = 0;
51
+ connection.logSql = options.transId || options.logSql || log || process.SQL_LOG;
45
52
  resolve(connection);
46
53
  }
47
54
  });
48
55
  });
49
56
  };
50
57
 
51
- db.wrapTransaction = function (fn, nth, timeout) {
58
+ db.wrapTransaction = function (fn, nth, timeout, options = {}) {
52
59
  const Message = '等待事务超时';
53
60
  return function () {
54
61
  let ctx = this;
@@ -57,7 +64,15 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
57
64
  return fn.apply(ctx, params);
58
65
  } else {
59
66
  return (co.wrap(function* (params) {
60
- let conn = yield db.beginTransaction();
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);
61
76
  let result;
62
77
  try {
63
78
  params[nth] = conn;
@@ -90,6 +105,7 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
90
105
  };
91
106
 
92
107
  db.query = function (sql, sqlParam, connection) {
108
+ let currentStack = new Error().stack;
93
109
  let query;
94
110
  return new Promise(function (resolve, reject) {
95
111
  if(process.MYSQL_READ_ONLY && !sql.toLowerCase().trimLeft().startsWith('select')){
@@ -103,14 +119,15 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
103
119
 
104
120
  if (connection) {
105
121
  query = connection.query(sql, sqlParam, function (err, rows) {
106
- if(log || process.SQL_LOG){
107
- logSql(connection, rows, query.sql, startTime, logExecuteTime);
122
+ if(connection.logSql){
123
+ logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
108
124
  }
109
125
  if (err) {
110
- if(!log&&!process.SQL_LOG){
111
- logSql(connection, rows, query.sql, startTime, logExecuteTime);
126
+ if (!connection.logSql){
127
+ logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
112
128
  }
113
129
  err.code = dbCode;
130
+ err.stack = err.stack + getCurrentStack(currentStack);
114
131
  reject(err);
115
132
  } else {
116
133
  resolve(rows);
@@ -119,13 +136,13 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
119
136
  } else {
120
137
  db.getConnection().then(function (connection) {
121
138
  query = connection.query(sql, sqlParam, function (err, rows) {
122
- if(log || process.SQL_LOG){
123
- logSql(connection, rows, query.sql, startTime, logExecuteTime);
139
+ if(connection.logSql){
140
+ logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
124
141
  }
125
142
  connection.release();
126
143
  if (err) {
127
- if(!log&&!process.SQL_LOG){
128
- logSql(connection, rows, query.sql, startTime, logExecuteTime);
144
+ if (!connection.logSql){
145
+ logSql(connection, rows, query.sql, startTime, logExecuteTime, logger);
129
146
  }
130
147
  connection.destroy();
131
148
  err.code = dbCode;
@@ -135,17 +152,18 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
135
152
  }
136
153
  });
137
154
  }).catch(function (err) {
155
+ err.stack = err.stack + getCurrentStack(currentStack);
138
156
  reject(err);
139
157
  });
140
158
  }
141
159
  });
142
160
  };
143
161
 
144
- db.beginTransaction = function () {
162
+ db.beginTransaction = function (options) {
145
163
  let p = new Promise(function (resolve, reject) {
146
- db.getConnection().then(function (conn) {
147
- if(log || process.SQL_LOG){
148
- console.log(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] beginTransaction`);
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`);
149
167
  }
150
168
  conn.beginTransaction(function (err) {
151
169
  if (err) {
@@ -169,8 +187,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
169
187
  if (err) {
170
188
  reject(err);
171
189
  } else {
172
- if(log || process.SQL_LOG){
173
- console.log(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] commitTransaction`);
190
+ if(conn.logSql){
191
+ logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] commitTransaction`);
174
192
  }
175
193
  // conn.release();
176
194
  resolve('success');
@@ -182,8 +200,8 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
182
200
  db.rollbackTransaction = function (conn) {
183
201
  return new Promise(function (resolve, reject) {
184
202
  conn.rollback(function (err, suc) {
185
- if(log || process.SQL_LOG){
186
- console.log(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] rollbackTransaction`);
203
+ if(conn.logSql){
204
+ logger(`[${conn.connectionLogId}] [${moment().format('YYYY-MM-DD HH:mm:ss.mm.SSS')}] rollbackTransaction`);
187
205
  }
188
206
  resolve();
189
207
  });
@@ -191,5 +209,4 @@ module.exports = (dbConfig, {log, noConvertDbCodes, dbCode, logExecuteTime}) =>
191
209
  };
192
210
 
193
211
  return db;
194
- };
195
-
212
+ };
package/base/dbORM.js CHANGED
@@ -10,6 +10,7 @@ let dbFunss = (config) => {
10
10
  let exportsObj = {};
11
11
  let db = dbOrigin(dbConfig, {
12
12
  log: options.log || log,
13
+ logger: options.logger || console.log,
13
14
  noConvertDbCodes,
14
15
  dbCode,
15
16
  logExecuteTime //打印 sql 执行时间,默认开启
package/base/err.js CHANGED
@@ -6,4 +6,5 @@ module.exports = function (message, code, value){
6
6
  this.message = message;
7
7
  this.code = code || 500;
8
8
  this.value = value || '';
9
+ Error.captureStackTrace(this);
9
10
  };
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>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dborm-mysql",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "a NodeJs ORM for mysql",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",