mm_sqlite 1.5.1 → 1.5.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/index.js +2 -0
- package/package.json +4 -4
- package/sql.js +53 -34
package/index.js
CHANGED
|
@@ -173,6 +173,7 @@ SQLite.prototype.run = async function (sql, params = [], timeout = 30000) {
|
|
|
173
173
|
throw new TypeError('timeout must be number');
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
// 注意:同 scope 下为单例共享,this.error/this.sql 并发时以最后一次写入为准
|
|
176
177
|
this.error = null;
|
|
177
178
|
this.sql = '';
|
|
178
179
|
let conn = null;
|
|
@@ -219,6 +220,7 @@ SQLite.prototype.exec = async function (sql, params = [], timeout) {
|
|
|
219
220
|
throw new TypeError('timeout must be number');
|
|
220
221
|
}
|
|
221
222
|
|
|
223
|
+
// 注意:同 scope 下为单例共享,this.error/this.sql 并发时以最后一次写入为准
|
|
222
224
|
this.error = null;
|
|
223
225
|
this.sql = '';
|
|
224
226
|
let conn = null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_sqlite",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "高性能SQLite数据库操作模块,提供与mm_mysql完全兼容的API接口,支持异步操作、事务处理和批量操作",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"better-sqlite3": "^13.0.3",
|
|
42
|
-
"mm_expand": "^2.5.
|
|
42
|
+
"mm_expand": "^2.5.3",
|
|
43
43
|
"sqlstring": "^2.3.3"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"eslint": "^10.8.1",
|
|
47
|
-
"eslint-plugin-jsdoc": "^64.0
|
|
48
|
-
"mm_eslint": "^1.7.
|
|
47
|
+
"eslint-plugin-jsdoc": "^64.2.0",
|
|
48
|
+
"mm_eslint": "^1.7.6"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/sql.js
CHANGED
|
@@ -66,7 +66,7 @@ class Sql extends Base {
|
|
|
66
66
|
}
|
|
67
67
|
};
|
|
68
68
|
/**
|
|
69
|
-
*
|
|
69
|
+
* 数据库管理器
|
|
70
70
|
* @param {Function} run 查询函数
|
|
71
71
|
* @param {Function} exec 更改函数
|
|
72
72
|
*/
|
|
@@ -153,7 +153,7 @@ class Sql extends Base {
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
|
-
*
|
|
156
|
+
* 清空查询条件
|
|
157
157
|
* @returns {object} 返回当前对象
|
|
158
158
|
*/
|
|
159
159
|
Sql.prototype.clear = function () {
|
|
@@ -171,7 +171,7 @@ Sql.prototype.clear = function () {
|
|
|
171
171
|
};
|
|
172
172
|
|
|
173
173
|
/**
|
|
174
|
-
*
|
|
174
|
+
* 过滤查询参数
|
|
175
175
|
* @param {object} query 查询参数
|
|
176
176
|
*/
|
|
177
177
|
Sql.prototype.filter = function (query) {
|
|
@@ -245,7 +245,7 @@ Sql.prototype.setSql = function (where, set) {
|
|
|
245
245
|
return this.exec(sql);
|
|
246
246
|
};
|
|
247
247
|
/**
|
|
248
|
-
*
|
|
248
|
+
* 查询数据
|
|
249
249
|
* @param {string} where 查询条件
|
|
250
250
|
* @param {string} sort 排序
|
|
251
251
|
* @param {string} view 显示的字段
|
|
@@ -257,7 +257,7 @@ Sql.prototype.getSql = function (where, sort, view) {
|
|
|
257
257
|
};
|
|
258
258
|
|
|
259
259
|
/**
|
|
260
|
-
*
|
|
260
|
+
* 添加或修改
|
|
261
261
|
* @param {string} where 查询条件
|
|
262
262
|
* @param {string} set 修改的键值
|
|
263
263
|
* @returns {Promise | object} 执行结果
|
|
@@ -285,7 +285,7 @@ Sql.prototype.addOrSetSql = async function (where, set) {
|
|
|
285
285
|
};
|
|
286
286
|
|
|
287
287
|
/**
|
|
288
|
-
*
|
|
288
|
+
* 查询符合结果总数
|
|
289
289
|
* @param {string} where 查询条件
|
|
290
290
|
* @returns {Promise | number} 返回结果总数
|
|
291
291
|
*/
|
|
@@ -303,7 +303,7 @@ Sql.prototype.countSql = async function (where) {
|
|
|
303
303
|
};
|
|
304
304
|
|
|
305
305
|
/**
|
|
306
|
-
*
|
|
306
|
+
* 查询数据并返回符合条件总数
|
|
307
307
|
* @param {string} where 查询条件
|
|
308
308
|
* @param {string} sort 排序
|
|
309
309
|
* @param {string} view 返回的字段
|
|
@@ -323,7 +323,7 @@ Sql.prototype.getCountSql = async function (where, sort, view) {
|
|
|
323
323
|
};
|
|
324
324
|
|
|
325
325
|
/**
|
|
326
|
-
*
|
|
326
|
+
* 统计学
|
|
327
327
|
* @param {string} where 查询条件
|
|
328
328
|
* @param {string} groupby 分组的字段
|
|
329
329
|
* @param {string} view 返回的字段
|
|
@@ -366,7 +366,7 @@ Sql.prototype.groupMathSql = async function (where, groupby, view, sort, method)
|
|
|
366
366
|
|
|
367
367
|
|
|
368
368
|
/**
|
|
369
|
-
*
|
|
369
|
+
* 分组求平均值
|
|
370
370
|
* @param {string} where 查询条件
|
|
371
371
|
* @param {string} groupby 分组的字段
|
|
372
372
|
* @param {string} view 返回的字段
|
|
@@ -378,7 +378,7 @@ Sql.prototype.groupAvgSql = async function (where, groupby, view, sort = '') {
|
|
|
378
378
|
};
|
|
379
379
|
|
|
380
380
|
/**
|
|
381
|
-
*
|
|
381
|
+
* 分组合计数值
|
|
382
382
|
* @param {string} where 查询条件
|
|
383
383
|
* @param {string} groupby 分组的字段
|
|
384
384
|
* @param {string} view 返回的字段
|
|
@@ -390,7 +390,7 @@ Sql.prototype.groupSumSql = async function (where, groupby, view, sort = '') {
|
|
|
390
390
|
};
|
|
391
391
|
|
|
392
392
|
/**
|
|
393
|
-
*
|
|
393
|
+
* 分组合计不同条数
|
|
394
394
|
* @param {string} where 查询条件
|
|
395
395
|
* @param {string} groupby 分组的字段
|
|
396
396
|
* @param {string} view 返回的字段
|
|
@@ -403,7 +403,7 @@ Sql.prototype.groupCountSql = async function (where, groupby, view, sort = '') {
|
|
|
403
403
|
|
|
404
404
|
|
|
405
405
|
/**
|
|
406
|
-
*
|
|
406
|
+
* 统计学
|
|
407
407
|
* @param {object} query 查询条件
|
|
408
408
|
* @param {string} groupby 分组的字段
|
|
409
409
|
* @param {string} view 返回的字段
|
|
@@ -417,7 +417,7 @@ Sql.prototype.groupMath = async function (query, groupby, view, sort, method) {
|
|
|
417
417
|
};
|
|
418
418
|
|
|
419
419
|
/**
|
|
420
|
-
*
|
|
420
|
+
* 分组求平均值
|
|
421
421
|
* @param {object} query 查询条件
|
|
422
422
|
* @param {string} groupby 分组的字段
|
|
423
423
|
* @param {string} view 返回的字段
|
|
@@ -429,7 +429,7 @@ Sql.prototype.groupAvg = async function (query, groupby, view, sort) {
|
|
|
429
429
|
};
|
|
430
430
|
|
|
431
431
|
/**
|
|
432
|
-
*
|
|
432
|
+
* 分组合计数值
|
|
433
433
|
* @param {object} query 查询条件
|
|
434
434
|
* @param {string} groupby 分组的字段
|
|
435
435
|
* @param {string} view 返回的字段
|
|
@@ -441,7 +441,7 @@ Sql.prototype.groupSum = async function (query, groupby, view, sort) {
|
|
|
441
441
|
};
|
|
442
442
|
|
|
443
443
|
/**
|
|
444
|
-
*
|
|
444
|
+
* 分组合计不同条数
|
|
445
445
|
* @param {object} query 查询条件
|
|
446
446
|
* @param {string} groupby 分组的字段
|
|
447
447
|
* @param {string} view 返回的字段
|
|
@@ -454,7 +454,7 @@ Sql.prototype.groupCount = async function (query, groupby, view, sort) {
|
|
|
454
454
|
|
|
455
455
|
/* === 聚合函数 === */
|
|
456
456
|
/**
|
|
457
|
-
*
|
|
457
|
+
* 求和函数
|
|
458
458
|
* @param {string} field 字段名
|
|
459
459
|
* @param {object} query 查询条件
|
|
460
460
|
* @returns {number} 求和结果
|
|
@@ -474,7 +474,7 @@ Sql.prototype.sum = async function (field, query) {
|
|
|
474
474
|
};
|
|
475
475
|
|
|
476
476
|
/**
|
|
477
|
-
*
|
|
477
|
+
* 最大值函数
|
|
478
478
|
* @param {string} field 字段名
|
|
479
479
|
* @param {object} query 查询条件
|
|
480
480
|
* @returns {number} 最大值结果
|
|
@@ -494,7 +494,7 @@ Sql.prototype.max = async function (field, query) {
|
|
|
494
494
|
};
|
|
495
495
|
|
|
496
496
|
/**
|
|
497
|
-
*
|
|
497
|
+
* 最小值函数
|
|
498
498
|
* @param {string} field 字段名
|
|
499
499
|
* @param {object} query 查询条件
|
|
500
500
|
* @returns {number} 最小值结果
|
|
@@ -514,7 +514,7 @@ Sql.prototype.min = async function (field, query) {
|
|
|
514
514
|
};
|
|
515
515
|
|
|
516
516
|
/**
|
|
517
|
-
*
|
|
517
|
+
* 平均值函数
|
|
518
518
|
* @param {string} field 字段名
|
|
519
519
|
* @param {object} query 查询条件
|
|
520
520
|
* @returns {number} 平均值结果
|
|
@@ -535,7 +535,7 @@ Sql.prototype.avg = async function (field, query) {
|
|
|
535
535
|
|
|
536
536
|
/* === 字符串操作函数 === */
|
|
537
537
|
/**
|
|
538
|
-
*
|
|
538
|
+
* 字符串连接函数
|
|
539
539
|
* @param {...string} args 要连接的字符串参数
|
|
540
540
|
* @returns {string} 连接后的字符串
|
|
541
541
|
*/
|
|
@@ -551,7 +551,7 @@ Sql.prototype.toStr = function (...args) {
|
|
|
551
551
|
|
|
552
552
|
/* === 备份恢复函数 === */
|
|
553
553
|
/**
|
|
554
|
-
*
|
|
554
|
+
* 备份数据库到 SQL 文件
|
|
555
555
|
* @param {string} file 备份文件路径
|
|
556
556
|
* @returns {Promise<boolean>} 备份是否成功
|
|
557
557
|
*/
|
|
@@ -601,7 +601,7 @@ Sql.prototype.backup = async function (file) {
|
|
|
601
601
|
};
|
|
602
602
|
|
|
603
603
|
/**
|
|
604
|
-
*
|
|
604
|
+
* 从 SQL 文件恢复数据库
|
|
605
605
|
* @param {string} file 恢复文件路径
|
|
606
606
|
* @returns {Promise<boolean>} 恢复是否成功
|
|
607
607
|
*/
|
|
@@ -645,7 +645,7 @@ Sql.prototype.restore = async function (file) {
|
|
|
645
645
|
};
|
|
646
646
|
|
|
647
647
|
/**
|
|
648
|
-
*
|
|
648
|
+
* 解析 SQL 语句,正确处理字符串中的分号
|
|
649
649
|
* @param {string} sql SQL 内容
|
|
650
650
|
* @returns {string[]} SQL 语句数组
|
|
651
651
|
* @private
|
|
@@ -692,7 +692,7 @@ Sql.prototype._parseSqlStatements = function (sql) {
|
|
|
692
692
|
|
|
693
693
|
/* === sql语句拼接函数 === */
|
|
694
694
|
/**
|
|
695
|
-
*
|
|
695
|
+
* 转为where语句
|
|
696
696
|
* @param {object} obj 用作拼接的对象
|
|
697
697
|
* @param {boolean} like 是否使用like匹配, 默认不使用
|
|
698
698
|
* @returns {string} where格式sql语句字符串
|
|
@@ -759,7 +759,7 @@ Sql.prototype.toWhere = function (obj, like) {
|
|
|
759
759
|
};
|
|
760
760
|
|
|
761
761
|
/**
|
|
762
|
-
*
|
|
762
|
+
* 转为set语句
|
|
763
763
|
* @param {object} obj 用作拼接的对象
|
|
764
764
|
* @returns {string} set格式sql语句字符串
|
|
765
765
|
*/
|
|
@@ -847,7 +847,7 @@ Sql.prototype.toSetSql = function (query, item, like) {
|
|
|
847
847
|
};
|
|
848
848
|
|
|
849
849
|
/**
|
|
850
|
-
*
|
|
850
|
+
* 转查询sql语句
|
|
851
851
|
* @param {object} query 查询键值集合
|
|
852
852
|
* @param {string} sort 排序规则
|
|
853
853
|
* @param {string} view 显示的字段
|
|
@@ -860,7 +860,7 @@ Sql.prototype.toGetSql = function (query, sort, view, like) {
|
|
|
860
860
|
};
|
|
861
861
|
/* === 传入对象操作 === */
|
|
862
862
|
/**
|
|
863
|
-
*
|
|
863
|
+
* 增加数据
|
|
864
864
|
* @param {object} body 添加的对象
|
|
865
865
|
* @returns {Promise | object} 执行结果
|
|
866
866
|
*/
|
|
@@ -875,7 +875,7 @@ Sql.prototype.add = async function (body) {
|
|
|
875
875
|
return bl;
|
|
876
876
|
};
|
|
877
877
|
/**
|
|
878
|
-
*
|
|
878
|
+
* 删除数据
|
|
879
879
|
* @param {object} query 查询条件集合
|
|
880
880
|
* @param {boolean} like 是否使用like匹配, 为空使用默认方式
|
|
881
881
|
* @returns {Promise | object} 执行结果
|
|
@@ -891,7 +891,7 @@ Sql.prototype.del = async function (query, like) {
|
|
|
891
891
|
};
|
|
892
892
|
|
|
893
893
|
/**
|
|
894
|
-
*
|
|
894
|
+
* 修改数据
|
|
895
895
|
* @param {object} query 查询条件集合
|
|
896
896
|
* @param {object} body 修改的键值集合
|
|
897
897
|
* @param {boolean} like 是否使用like匹配, 为空使用默认方式
|
|
@@ -908,7 +908,7 @@ Sql.prototype.set = async function (query, body, like) {
|
|
|
908
908
|
};
|
|
909
909
|
|
|
910
910
|
/**
|
|
911
|
-
*
|
|
911
|
+
* 查询数据(优化版)
|
|
912
912
|
* @param {object} query 查询条件
|
|
913
913
|
* @param {string} sort 排序
|
|
914
914
|
* @param {string} view 返回的字段
|
|
@@ -982,6 +982,25 @@ Sql.prototype.addOrSet = async function (where, set, like) {
|
|
|
982
982
|
}
|
|
983
983
|
};
|
|
984
984
|
|
|
985
|
+
/**
|
|
986
|
+
* 查询数据(优化版)
|
|
987
|
+
* @param {object} query 查询条件
|
|
988
|
+
* @param {string} sort 排序
|
|
989
|
+
* @param {string} view 返回的字段
|
|
990
|
+
* @param {boolean} like 是否使用like匹配, 为空使用默认方式
|
|
991
|
+
* @param {number} timeout 超时时间(毫秒),默认5000ms
|
|
992
|
+
* @returns {Promise|Array} 查询结果
|
|
993
|
+
*/
|
|
994
|
+
Sql.prototype.list = async function (query, sort, view, like, timeout = 20000) {
|
|
995
|
+
if (!this.table) {
|
|
996
|
+
throw new Error('表名未设置');
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
var sql = this.toGetSql(query, sort, view, like);
|
|
1000
|
+
var list = await this.run(sql, [], timeout);
|
|
1001
|
+
return list;
|
|
1002
|
+
};
|
|
1003
|
+
|
|
985
1004
|
/**
|
|
986
1005
|
* 统计记录数
|
|
987
1006
|
* @param {object} query 查询条件
|
|
@@ -1605,7 +1624,7 @@ Sql.prototype._splitSqlStatements = function (sql_content) {
|
|
|
1605
1624
|
};
|
|
1606
1625
|
|
|
1607
1626
|
/**
|
|
1608
|
-
*
|
|
1627
|
+
* 获取创建表的 SQL 语句
|
|
1609
1628
|
* @returns {Promise<string>} 创建表的 SQL 语句
|
|
1610
1629
|
* @private
|
|
1611
1630
|
*/
|
|
@@ -1627,7 +1646,7 @@ Sql.prototype._getCreateTableSql = async function () {
|
|
|
1627
1646
|
};
|
|
1628
1647
|
|
|
1629
1648
|
/**
|
|
1630
|
-
*
|
|
1649
|
+
* 备份数据库到 SQL 文件
|
|
1631
1650
|
* @param {string} file 备份文件路径
|
|
1632
1651
|
* @param {boolean} create 是否导出创建表语句,默认为false
|
|
1633
1652
|
* @returns {Promise<boolean>} 备份是否成功
|
|
@@ -1686,7 +1705,7 @@ Sql.prototype.backup = async function (file, create = false) {
|
|
|
1686
1705
|
};
|
|
1687
1706
|
|
|
1688
1707
|
/**
|
|
1689
|
-
*
|
|
1708
|
+
* 从 SQL 文件恢复数据库
|
|
1690
1709
|
* @param {string} file 备份文件路径
|
|
1691
1710
|
* @returns {Promise<boolean>} 恢复是否成功
|
|
1692
1711
|
*/
|
|
@@ -1722,7 +1741,7 @@ Sql.prototype.restore = async function (file) {
|
|
|
1722
1741
|
};
|
|
1723
1742
|
|
|
1724
1743
|
/**
|
|
1725
|
-
*
|
|
1744
|
+
* 解析 SQL 语句
|
|
1726
1745
|
* @param {string} sql SQL 内容
|
|
1727
1746
|
* @returns {string[]} SQL 语句数组
|
|
1728
1747
|
* @private
|