mm_mysql 2.3.6 → 2.3.8
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/db.js +17 -17
- package/package.json +2 -2
package/db.js
CHANGED
|
@@ -27,7 +27,7 @@ class DB extends Sql {
|
|
|
27
27
|
* 获取上级
|
|
28
28
|
* @returns {object} 返回上级管理器
|
|
29
29
|
*/
|
|
30
|
-
this.
|
|
30
|
+
this.getParent = function () {
|
|
31
31
|
return mysql;
|
|
32
32
|
};
|
|
33
33
|
}
|
|
@@ -53,7 +53,7 @@ DB.prototype._createTimeoutPromise = function (timeout, message) {
|
|
|
53
53
|
* @returns {string} 数据库
|
|
54
54
|
*/
|
|
55
55
|
DB.prototype.database = function () {
|
|
56
|
-
return this.
|
|
56
|
+
return this.getParent().config.database;
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -62,12 +62,12 @@ DB.prototype.database = function () {
|
|
|
62
62
|
* @param {string} key 键名
|
|
63
63
|
* @returns {object} 返回管理器
|
|
64
64
|
*/
|
|
65
|
-
DB.prototype.new = function (table, key) {
|
|
66
|
-
const db = this.
|
|
65
|
+
DB.prototype.new = function (table = '', key = '') {
|
|
66
|
+
const db = this.getParent().db();
|
|
67
67
|
db.table = table;
|
|
68
68
|
if (key) {
|
|
69
69
|
db.key = key;
|
|
70
|
-
} else {
|
|
70
|
+
} else if (table) {
|
|
71
71
|
const arr = table.split('_');
|
|
72
72
|
db.key = arr[arr.length - 1] + '_id';
|
|
73
73
|
}
|
|
@@ -78,9 +78,9 @@ DB.prototype.new = function (table, key) {
|
|
|
78
78
|
* 获取数据库连接
|
|
79
79
|
* @returns {Promise<object>}
|
|
80
80
|
*/
|
|
81
|
-
DB.prototype.getConn = async function() {
|
|
81
|
+
DB.prototype.getConn = async function () {
|
|
82
82
|
try {
|
|
83
|
-
return await this.
|
|
83
|
+
return await this.getParent().getConn();
|
|
84
84
|
} catch (error) {
|
|
85
85
|
this.log('error', '获取连接失败', error);
|
|
86
86
|
// 返回null作为默认值,保持返回值类型一致
|
|
@@ -92,9 +92,9 @@ DB.prototype.getConn = async function() {
|
|
|
92
92
|
* 开始事务
|
|
93
93
|
* @returns {Promise<object>}
|
|
94
94
|
*/
|
|
95
|
-
DB.prototype.start = async function() {
|
|
95
|
+
DB.prototype.start = async function () {
|
|
96
96
|
try {
|
|
97
|
-
return await this.
|
|
97
|
+
return await this.getParent().beginTransaction();
|
|
98
98
|
} catch (error) {
|
|
99
99
|
this.log('error', '开始事务失败', error);
|
|
100
100
|
// 返回null作为默认值,保持返回值类型一致
|
|
@@ -108,12 +108,12 @@ DB.prototype.start = async function() {
|
|
|
108
108
|
* @returns {Promise<void>}
|
|
109
109
|
* @throws {TypeError} 当transaction参数无效时
|
|
110
110
|
*/
|
|
111
|
-
DB.prototype.commit = async function(transaction) {
|
|
111
|
+
DB.prototype.commit = async function (transaction) {
|
|
112
112
|
// 参数校验
|
|
113
113
|
if (!transaction || typeof transaction !== 'object') {
|
|
114
114
|
throw new TypeError('transaction must be object');
|
|
115
115
|
}
|
|
116
|
-
|
|
116
|
+
|
|
117
117
|
try {
|
|
118
118
|
await transaction.commit();
|
|
119
119
|
} catch (error) {
|
|
@@ -127,12 +127,12 @@ DB.prototype.commit = async function(transaction) {
|
|
|
127
127
|
* @returns {Promise<void>}
|
|
128
128
|
* @throws {TypeError} 当transaction参数无效时
|
|
129
129
|
*/
|
|
130
|
-
DB.prototype.back = async function(transaction) {
|
|
130
|
+
DB.prototype.back = async function (transaction) {
|
|
131
131
|
// 参数校验
|
|
132
132
|
if (!transaction || typeof transaction !== 'object') {
|
|
133
133
|
throw new TypeError('transaction must be object');
|
|
134
134
|
}
|
|
135
|
-
|
|
135
|
+
|
|
136
136
|
try {
|
|
137
137
|
await transaction.rollback();
|
|
138
138
|
} catch (error) {
|
|
@@ -146,14 +146,14 @@ DB.prototype.back = async function(transaction) {
|
|
|
146
146
|
* @returns {Promise<*>}
|
|
147
147
|
* @throws {TypeError} 当callback参数无效时
|
|
148
148
|
*/
|
|
149
|
-
DB.prototype.transaction = async function(callback) {
|
|
149
|
+
DB.prototype.transaction = async function (callback) {
|
|
150
150
|
// 参数校验
|
|
151
151
|
if (typeof callback !== 'function') {
|
|
152
152
|
throw new TypeError('callback must be function');
|
|
153
153
|
}
|
|
154
|
-
|
|
154
|
+
|
|
155
155
|
try {
|
|
156
|
-
return await this.
|
|
156
|
+
return await this.getParent().transaction(callback);
|
|
157
157
|
} catch (error) {
|
|
158
158
|
this.log('error', '事务执行失败', error);
|
|
159
159
|
// 返回null作为默认值,保持返回值类型一致
|
|
@@ -205,7 +205,7 @@ DB.prototype.fields = async function (table, field_name, timeout = 15000) {
|
|
|
205
205
|
}
|
|
206
206
|
const field = 'COLUMN_NAME as `name`,ORDINAL_POSITION as `cid`,COLUMN_DEFAULT as `default_value`,IS_NULLABLE as `not_null`,COLUMN_TYPE as `type`,COLUMN_KEY as `pk`,EXTRA as `auto`,COLUMN_COMMENT as `note`';
|
|
207
207
|
let sql = 'select ' + field + " from information_schema.COLUMNS where `table_name` = '" + targetTable +
|
|
208
|
-
|
|
208
|
+
"' and `table_schema` = '" + this.database() + "'";
|
|
209
209
|
if (field_name) {
|
|
210
210
|
sql += " AND COLUMN_NAME='" + field_name + "'";
|
|
211
211
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.8",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"mm_expand": "^2.
|
|
7
|
+
"mm_expand": "^2.2.8",
|
|
8
8
|
"mysql2": "^3.22.0",
|
|
9
9
|
"sqlstring": "^2.3.3"
|
|
10
10
|
},
|