mm_sqlite 1.0.5 → 1.0.6
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/LICENSE +201 -0
- package/README.md +2 -0
- package/config.json +8 -0
- package/db/mm.db +0 -0
- package/db.js +344 -94
- package/index.js +38 -8
- package/index_/345/217/202/350/200/203.js +299 -0
- package/link_model.js +132 -0
- package/package.json +38 -36
- package/sql.js +275 -51
- package/sql.json +56 -0
- package/test.js +594 -82
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview Mysql帮助类函数
|
|
3
|
+
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
|
+
* @version 1.2
|
|
5
|
+
*/
|
|
6
|
+
const {
|
|
7
|
+
createPool
|
|
8
|
+
} = require('mysql');
|
|
9
|
+
const {
|
|
10
|
+
DB
|
|
11
|
+
} = require('./db');
|
|
12
|
+
|
|
13
|
+
const Link_model = require('./link_model');
|
|
14
|
+
|
|
15
|
+
var pool = {};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @description 数据库封装
|
|
19
|
+
*/
|
|
20
|
+
class Mysql {
|
|
21
|
+
/**
|
|
22
|
+
* @description 创建Mysql帮助类函数 (构造函数)
|
|
23
|
+
* @param {String} scope 作用域
|
|
24
|
+
* @param {String} dir 当前路径
|
|
25
|
+
* @constructor
|
|
26
|
+
*/
|
|
27
|
+
constructor(scope, dir) {
|
|
28
|
+
// 作用域
|
|
29
|
+
this.scope;
|
|
30
|
+
if (scope) {
|
|
31
|
+
this.scope = scope;
|
|
32
|
+
} else {
|
|
33
|
+
this.scope = $.val.scope + '';
|
|
34
|
+
}
|
|
35
|
+
// 当前目录
|
|
36
|
+
this.dir = __dirname;
|
|
37
|
+
if (dir) {
|
|
38
|
+
this.dir = dir;
|
|
39
|
+
}
|
|
40
|
+
// 错误提示
|
|
41
|
+
this.error;
|
|
42
|
+
/**
|
|
43
|
+
* sql语句
|
|
44
|
+
*/
|
|
45
|
+
this.sql = "";
|
|
46
|
+
// 连接器
|
|
47
|
+
this.conn;
|
|
48
|
+
|
|
49
|
+
// 数据库配置参数
|
|
50
|
+
this.config = {
|
|
51
|
+
// 服务器地址
|
|
52
|
+
host: "127.0.0.1",
|
|
53
|
+
// 端口号
|
|
54
|
+
port: 3306,
|
|
55
|
+
// 连接用户名
|
|
56
|
+
user: "root",
|
|
57
|
+
// 连接密码
|
|
58
|
+
password: "asd123",
|
|
59
|
+
// 数据库
|
|
60
|
+
database: "mm",
|
|
61
|
+
// 是否支持多个sql语句同时操作
|
|
62
|
+
multipleStatements: false,
|
|
63
|
+
// 打印SQL
|
|
64
|
+
log: false
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// 唯一标识符
|
|
68
|
+
this.identifier = this.config.host + "/" + this.config.database;
|
|
69
|
+
|
|
70
|
+
// 定义当前类, 用于数据库实例化访问
|
|
71
|
+
var $this = this;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @description 查询sql
|
|
75
|
+
* @param {String} sql 查询参
|
|
76
|
+
* @param {Array} val 替换值
|
|
77
|
+
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
78
|
+
*/
|
|
79
|
+
this.run = function(sql, val) {
|
|
80
|
+
var _this = this;
|
|
81
|
+
this.sql = sql;
|
|
82
|
+
if ($this.config.log) {
|
|
83
|
+
$.log.debug("SQL:", sql);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 返回一个 Promise
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
$this.conn.getConnection(function(err, db) {
|
|
89
|
+
if (err) {
|
|
90
|
+
_this.error = {
|
|
91
|
+
code: 2003,
|
|
92
|
+
message: $.info(err).between('Error: ', '\n')
|
|
93
|
+
};
|
|
94
|
+
$.log.error("SQL error:", err);
|
|
95
|
+
resolve([]);
|
|
96
|
+
reject(err);
|
|
97
|
+
} else {
|
|
98
|
+
db.query(sql, val, (err, rows) => {
|
|
99
|
+
// 结束会话
|
|
100
|
+
db.release();
|
|
101
|
+
if (err) {
|
|
102
|
+
_this.error = {
|
|
103
|
+
code: err.errno,
|
|
104
|
+
message: err.sqlMessage
|
|
105
|
+
};
|
|
106
|
+
$.log.error("SQL error:", err);
|
|
107
|
+
resolve([]);
|
|
108
|
+
reject(err);
|
|
109
|
+
} else {
|
|
110
|
+
_this.error = undefined;
|
|
111
|
+
resolve(rows);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @description 增删改sql
|
|
121
|
+
* @param {String} sql 查询参
|
|
122
|
+
* @param {Array} val 替换值
|
|
123
|
+
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
124
|
+
*/
|
|
125
|
+
this.exec = function(sql, val) {
|
|
126
|
+
var _this = this;
|
|
127
|
+
if (this.task) {
|
|
128
|
+
this.task_sql += sql + "\r\n";
|
|
129
|
+
if (this.task === 1) {
|
|
130
|
+
return this.task_sql;
|
|
131
|
+
} else if (this.task === 2) {
|
|
132
|
+
this.task = 0;
|
|
133
|
+
sql = this.task_sql.trim();
|
|
134
|
+
this.task_sql = '';
|
|
135
|
+
} else {
|
|
136
|
+
this.task = 0;
|
|
137
|
+
this.task_sql = '';
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this.sql = sql;
|
|
142
|
+
if ($this.config.log) {
|
|
143
|
+
$.log.debug("SQL:", sql);
|
|
144
|
+
}
|
|
145
|
+
// $this.conn.config.connectionConfig = false;
|
|
146
|
+
// console.log("测试", $this.conn.config.connectionConfig);
|
|
147
|
+
// 返回一个 Promise
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
$this.conn.getConnection(function(err, db) {
|
|
150
|
+
if (err) {
|
|
151
|
+
_this.error = {
|
|
152
|
+
code: 41000,
|
|
153
|
+
message: $.info(err).between('Error: ', '\n')
|
|
154
|
+
};
|
|
155
|
+
resolve(-1);
|
|
156
|
+
reject(err);
|
|
157
|
+
$.log.error("SQL error:", err);
|
|
158
|
+
} else {
|
|
159
|
+
db.query(sql, val, (err, o) => {
|
|
160
|
+
if (err) {
|
|
161
|
+
_this.error = {
|
|
162
|
+
code: err.errno,
|
|
163
|
+
message: err.sqlMessage
|
|
164
|
+
};
|
|
165
|
+
resolve(-1);
|
|
166
|
+
reject(err);
|
|
167
|
+
$.log.error("SQL error:", err);
|
|
168
|
+
} else {
|
|
169
|
+
_this.error = undefined;
|
|
170
|
+
if (o.constructor == Array) {
|
|
171
|
+
if (o.length > 0) {
|
|
172
|
+
var num = 0;
|
|
173
|
+
o.map(function(item) {
|
|
174
|
+
num += item['affectedRows'];
|
|
175
|
+
});
|
|
176
|
+
if (num === 0) {
|
|
177
|
+
resolve(o.length);
|
|
178
|
+
} else {
|
|
179
|
+
resolve(num);
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
resolve(1);
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
var num = o['affectedRows'];
|
|
186
|
+
if (num === 0) {
|
|
187
|
+
resolve(1);
|
|
188
|
+
} else {
|
|
189
|
+
resolve(num);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// 结束会话
|
|
194
|
+
db.release();
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @description 获取数据库管理器
|
|
203
|
+
*/
|
|
204
|
+
this.db = function() {
|
|
205
|
+
return new DB($this);
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 获取数据库管理器
|
|
212
|
+
* @param {String} key 主键
|
|
213
|
+
* @param {String|Number} value 对象值
|
|
214
|
+
* @param {Boolean} clear_prefix 清除前缀
|
|
215
|
+
* @param {Array} arr_table 关联的数据表
|
|
216
|
+
* @return {Object} 管理模型
|
|
217
|
+
*/
|
|
218
|
+
Mysql.prototype.dbs = async function(key, value, clear_prefix, ...arr_table) {
|
|
219
|
+
var lm = new Link_model({
|
|
220
|
+
key,
|
|
221
|
+
value,
|
|
222
|
+
clear_prefix
|
|
223
|
+
});
|
|
224
|
+
lm.sql = this;
|
|
225
|
+
for (var i = 0; i < arr_table.length; i++) {
|
|
226
|
+
await lm.add(arr_table[i]);
|
|
227
|
+
}
|
|
228
|
+
return lm;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 设置配置参数
|
|
233
|
+
* @param {Object} cg 配置对象或配置路径
|
|
234
|
+
*/
|
|
235
|
+
Mysql.prototype.setConfig = function(cg) {
|
|
236
|
+
var obj;
|
|
237
|
+
if (typeof(cg) === "string") {
|
|
238
|
+
obj = cg.loadJson(this.dir);
|
|
239
|
+
} else {
|
|
240
|
+
obj = cg;
|
|
241
|
+
}
|
|
242
|
+
$.push(this.config, obj);
|
|
243
|
+
this.identifier = this.config.host + "/" + this.config.database;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @description 打开数据库, 如果没有则建立数据库连接再打开
|
|
248
|
+
*/
|
|
249
|
+
Mysql.prototype.open = function() {
|
|
250
|
+
if (!pool[this.identifier]) {
|
|
251
|
+
pool[this.identifier] = createPool(this.config);
|
|
252
|
+
}
|
|
253
|
+
this.conn = pool[this.identifier];
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @description 关闭连接
|
|
258
|
+
*/
|
|
259
|
+
Mysql.prototype.close = function() {
|
|
260
|
+
if (pool[this.identifier]) {
|
|
261
|
+
pool[this.identifier] = null;
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @description 导出Mysql帮助类
|
|
267
|
+
*/
|
|
268
|
+
exports.Mysql = Mysql;
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @description mysql连接池
|
|
273
|
+
*/
|
|
274
|
+
if (!$.pool.mysql) {
|
|
275
|
+
$.pool.mysql = {};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @description 缓存管理器,用于创建缓存
|
|
280
|
+
* @param {String} scope 作用域
|
|
281
|
+
* @param {String} dir 当前路径
|
|
282
|
+
* @return {Object} 返回一个缓存类
|
|
283
|
+
*/
|
|
284
|
+
function mysql_admin(scope, dir) {
|
|
285
|
+
if (!scope) {
|
|
286
|
+
scope = $.val.scope
|
|
287
|
+
}
|
|
288
|
+
var obj = $.pool.mysql[scope];
|
|
289
|
+
if (!obj) {
|
|
290
|
+
$.pool.mysql[scope] = new Mysql(scope, dir);
|
|
291
|
+
obj = $.pool.mysql[scope];
|
|
292
|
+
}
|
|
293
|
+
return obj;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @description 导出Mysql管理函数
|
|
298
|
+
*/
|
|
299
|
+
exports.mysql_admin = mysql_admin;
|
package/link_model.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 连接数据库模型类
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
class Link_model {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {Object} config 配置参数
|
|
9
|
+
*/
|
|
10
|
+
constructor(config, sql) {
|
|
11
|
+
/**
|
|
12
|
+
* 配置参数
|
|
13
|
+
*/
|
|
14
|
+
this.config = Object.assign({
|
|
15
|
+
key: "user_id",
|
|
16
|
+
value: 0,
|
|
17
|
+
clear_prefix: true
|
|
18
|
+
}, config);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 模型集合
|
|
22
|
+
*/
|
|
23
|
+
this.model = {};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 数据库集合
|
|
27
|
+
*/
|
|
28
|
+
this.dbs = {};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 数据库集合
|
|
32
|
+
*/
|
|
33
|
+
this.sql = sql;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 添加对象
|
|
39
|
+
* @param {String} table 表名
|
|
40
|
+
* @return {Object}
|
|
41
|
+
*/
|
|
42
|
+
Link_model.prototype.add = async function(table) {
|
|
43
|
+
var {
|
|
44
|
+
key,
|
|
45
|
+
value,
|
|
46
|
+
clear_prefix
|
|
47
|
+
} = this.config;
|
|
48
|
+
var db = this.sql.db();
|
|
49
|
+
db.table = table;
|
|
50
|
+
db.key = key;
|
|
51
|
+
db.size = 1;
|
|
52
|
+
db.page = 1;
|
|
53
|
+
var query = {};
|
|
54
|
+
query[key] = value;
|
|
55
|
+
var obj = await db.getObj(query);
|
|
56
|
+
if (!obj) {
|
|
57
|
+
var n = await db.add(query);
|
|
58
|
+
if (n) {
|
|
59
|
+
obj = await db.getObj(query);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var name = table;
|
|
64
|
+
// 是否去除表前缀
|
|
65
|
+
if (clear_prefix) {
|
|
66
|
+
var arr = table.split("_");
|
|
67
|
+
if (arr.length > 1) {
|
|
68
|
+
name = table.right("_");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.model[name] = obj;
|
|
72
|
+
this.dbs[table] = db;
|
|
73
|
+
return this;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 初始化
|
|
78
|
+
* @param {Object} sql 数据管理器
|
|
79
|
+
* @return {Object} 当前对象
|
|
80
|
+
*/
|
|
81
|
+
Link_model.prototype.init = function(sql) {
|
|
82
|
+
this.sql = sql;
|
|
83
|
+
return this;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 更新键值
|
|
88
|
+
* @param {Object} key
|
|
89
|
+
* @param {Object} value
|
|
90
|
+
* @return {Object} 当前对象
|
|
91
|
+
*/
|
|
92
|
+
Link_model.prototype.update = function(key, value) {
|
|
93
|
+
var m = this.model;
|
|
94
|
+
for (var k in m) {
|
|
95
|
+
var o = m[k];
|
|
96
|
+
if (o[key] !== undefined) {
|
|
97
|
+
o[key] = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return this;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 合并属性
|
|
105
|
+
* @param {Array} arr_name 当前对象
|
|
106
|
+
* @return {Object}
|
|
107
|
+
*/
|
|
108
|
+
Link_model.prototype.merge = function(...arr_name) {
|
|
109
|
+
var model = {};
|
|
110
|
+
if (arr_name.length === 0) {
|
|
111
|
+
arr_name = Object.keys(this.model);
|
|
112
|
+
}
|
|
113
|
+
for (var i = 0; i < arr_name.length; i++) {
|
|
114
|
+
var name = arr_name[i];
|
|
115
|
+
var o = this.model[name];
|
|
116
|
+
if (o) {
|
|
117
|
+
for (var k in o) {
|
|
118
|
+
model[k] = o[k]
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
var _this = this;
|
|
123
|
+
return new Proxy(model, {
|
|
124
|
+
set: function(obj, prop, value) {
|
|
125
|
+
_this.update(prop, value);
|
|
126
|
+
obj[prop] = value;
|
|
127
|
+
return obj;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
module.exports = Link_model;
|
package/package.json
CHANGED
|
@@ -1,36 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_sqlite",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "这是超级美眉sqlite帮助函数模块,用于便捷操作sqlite,使用await方式,可以避免嵌套函数",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"dev": "nodemon test.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_sqlite.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"sqlite",
|
|
16
|
+
"async",
|
|
17
|
+
"await",
|
|
18
|
+
"promise",
|
|
19
|
+
"add",
|
|
20
|
+
"del",
|
|
21
|
+
"set",
|
|
22
|
+
"get",
|
|
23
|
+
"query",
|
|
24
|
+
"run",
|
|
25
|
+
"exec"
|
|
26
|
+
],
|
|
27
|
+
"author": "邱文武",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://gitee.com/qiuwenwu91/mm_sqlite/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_sqlite#readme",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"mm_logs": "^1.2.0",
|
|
35
|
+
"sqlite3": "^5.1.7",
|
|
36
|
+
"sqlstring": "^2.3.3"
|
|
37
|
+
}
|
|
38
|
+
}
|