mm_sqlite 1.1.2 → 1.1.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 +46 -57
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -12,8 +12,7 @@ class Sqlite extends BaseService {
|
|
|
12
12
|
* 默认配置
|
|
13
13
|
*/
|
|
14
14
|
static default_config = {
|
|
15
|
-
|
|
16
|
-
port: 3306,
|
|
15
|
+
dir: "/db/".fullname(),
|
|
17
16
|
user: "root",
|
|
18
17
|
password: "",
|
|
19
18
|
database: "mm",
|
|
@@ -39,7 +38,7 @@ class Sqlite extends BaseService {
|
|
|
39
38
|
constructor(config) {
|
|
40
39
|
const merged_config = Object.assign({}, Sqlite.default_config, config || {});
|
|
41
40
|
super(merged_config);
|
|
42
|
-
|
|
41
|
+
|
|
43
42
|
this.config = merged_config;
|
|
44
43
|
this._connection = null;
|
|
45
44
|
this._pool = null;
|
|
@@ -58,7 +57,7 @@ class Sqlite extends BaseService {
|
|
|
58
57
|
* 初始化服务
|
|
59
58
|
* @returns {Promise<void>}
|
|
60
59
|
*/
|
|
61
|
-
Sqlite.prototype._initService = async function() {
|
|
60
|
+
Sqlite.prototype._initService = async function () {
|
|
62
61
|
if (this._is_inited) {
|
|
63
62
|
$.log.warn('SQLite服务已初始化');
|
|
64
63
|
return;
|
|
@@ -79,7 +78,7 @@ Sqlite.prototype._initService = async function() {
|
|
|
79
78
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
80
79
|
* @returns {Promise<boolean>}
|
|
81
80
|
*/
|
|
82
|
-
Sqlite.prototype.open = async function(timeout) {
|
|
81
|
+
Sqlite.prototype.open = async function (timeout) {
|
|
83
82
|
if (this._status === 'connected' || this._status === 'connecting') {
|
|
84
83
|
$.log.warn('数据库连接已存在或正在连接中');
|
|
85
84
|
return true;
|
|
@@ -89,14 +88,6 @@ Sqlite.prototype.open = async function(timeout) {
|
|
|
89
88
|
this._status = 'connecting';
|
|
90
89
|
|
|
91
90
|
try {
|
|
92
|
-
// $.log.debug(`[${this.constructor.name}] [open]`, '开始创建数据库连接', {
|
|
93
|
-
// host: this.config.host,
|
|
94
|
-
// port: this.config.port,
|
|
95
|
-
// database: this.config.database,
|
|
96
|
-
// user: this.config.user,
|
|
97
|
-
// timeout: timeout
|
|
98
|
-
// });
|
|
99
|
-
|
|
100
91
|
// 创建连接
|
|
101
92
|
const connection_promise = this._openInternal();
|
|
102
93
|
const timeout_promise = new Promise((_, reject) => {
|
|
@@ -111,8 +102,7 @@ Sqlite.prototype.open = async function(timeout) {
|
|
|
111
102
|
this._last_connect_time = Date.now();
|
|
112
103
|
this._status = 'connected';
|
|
113
104
|
$.log.info(`[${this.constructor.name}] [open]`, '数据库连接成功', {
|
|
114
|
-
|
|
115
|
-
port: this.config.port,
|
|
105
|
+
dir: this.config.dir,
|
|
116
106
|
database: this.config.database
|
|
117
107
|
});
|
|
118
108
|
return true;
|
|
@@ -122,7 +112,7 @@ Sqlite.prototype.open = async function(timeout) {
|
|
|
122
112
|
code: connection_err.code,
|
|
123
113
|
syscall: connection_err.syscall,
|
|
124
114
|
address: connection_err.address,
|
|
125
|
-
|
|
115
|
+
dir: connection_err.dir,
|
|
126
116
|
stack: connection_err.stack
|
|
127
117
|
});
|
|
128
118
|
throw connection_err;
|
|
@@ -131,8 +121,7 @@ Sqlite.prototype.open = async function(timeout) {
|
|
|
131
121
|
this._status = 'closed';
|
|
132
122
|
$.log.error(`[${this.constructor.name}] [open]`, '数据库连接失败', {
|
|
133
123
|
error: err.message,
|
|
134
|
-
|
|
135
|
-
port: this.config.port,
|
|
124
|
+
dir: this.config.dir,
|
|
136
125
|
database: this.config.database
|
|
137
126
|
});
|
|
138
127
|
throw err;
|
|
@@ -144,31 +133,31 @@ Sqlite.prototype.open = async function(timeout) {
|
|
|
144
133
|
* @private
|
|
145
134
|
* @returns {Promise<void>}
|
|
146
135
|
*/
|
|
147
|
-
Sqlite.prototype._openInternal = function() {
|
|
136
|
+
Sqlite.prototype._openInternal = function () {
|
|
148
137
|
return new Promise((resolve, reject) => {
|
|
149
138
|
try {
|
|
150
139
|
const fs = require('fs');
|
|
151
140
|
const path = require('path');
|
|
152
|
-
|
|
141
|
+
|
|
153
142
|
// 确保数据库目录存在
|
|
154
143
|
const db_path = this._getDatabasePath();
|
|
155
144
|
const dir = path.dirname(db_path);
|
|
156
|
-
|
|
145
|
+
|
|
157
146
|
if (!fs.existsSync(dir)) {
|
|
158
147
|
fs.mkdirSync(dir, { recursive: true });
|
|
159
148
|
}
|
|
160
|
-
|
|
149
|
+
|
|
161
150
|
// 创建数据库连接
|
|
162
151
|
this._db = new sqlite3.Database(db_path);
|
|
163
152
|
this._open = true;
|
|
164
153
|
this._conn_retry_count = 0;
|
|
165
|
-
|
|
154
|
+
|
|
166
155
|
// 设置连接错误处理
|
|
167
156
|
this._db.on('error', (err) => {
|
|
168
157
|
$.log.error('SQLite数据库连接错误:', err);
|
|
169
158
|
this._handleConnectionError(err);
|
|
170
159
|
});
|
|
171
|
-
|
|
160
|
+
|
|
172
161
|
$.log.debug(`SQLite数据库已打开: ${db_path}`);
|
|
173
162
|
resolve();
|
|
174
163
|
} catch (error) {
|
|
@@ -182,9 +171,9 @@ Sqlite.prototype._openInternal = function() {
|
|
|
182
171
|
* @private
|
|
183
172
|
* @returns {String}
|
|
184
173
|
*/
|
|
185
|
-
Sqlite.prototype._getDatabasePath = function() {
|
|
174
|
+
Sqlite.prototype._getDatabasePath = function () {
|
|
186
175
|
const db_name = this.config.database || 'mm';
|
|
187
|
-
const db_dir = this.config.
|
|
176
|
+
const db_dir = this.config.dir || '/db/';
|
|
188
177
|
return `${db_dir}${db_name}.db`;
|
|
189
178
|
};
|
|
190
179
|
|
|
@@ -192,7 +181,7 @@ Sqlite.prototype._getDatabasePath = function() {
|
|
|
192
181
|
* 关闭数据库连接
|
|
193
182
|
* @returns {Promise<boolean>}
|
|
194
183
|
*/
|
|
195
|
-
Sqlite.prototype.close = function() {
|
|
184
|
+
Sqlite.prototype.close = function () {
|
|
196
185
|
if (this._status !== 'connected') {
|
|
197
186
|
$.log.warn('数据库连接未建立');
|
|
198
187
|
return Promise.resolve(false);
|
|
@@ -230,7 +219,7 @@ Sqlite.prototype.close = function() {
|
|
|
230
219
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
231
220
|
* @returns {Promise<Object>}
|
|
232
221
|
*/
|
|
233
|
-
Sqlite.prototype.getConn = async function(timeout) {
|
|
222
|
+
Sqlite.prototype.getConn = async function (timeout) {
|
|
234
223
|
timeout = timeout || this.config.acquire_timeout || this.config.connect_timeout || 20000;
|
|
235
224
|
|
|
236
225
|
// $.log.debug(`[${this.constructor.name}] [getConn] 使用超时时间: ${timeout}ms`);
|
|
@@ -279,7 +268,7 @@ Sqlite.prototype.getConn = async function(timeout) {
|
|
|
279
268
|
* @private
|
|
280
269
|
* @param {Error} err - 错误对象
|
|
281
270
|
*/
|
|
282
|
-
Sqlite.prototype._handleConnectionError = function(err) {
|
|
271
|
+
Sqlite.prototype._handleConnectionError = function (err) {
|
|
283
272
|
if (this._reconnecting) {
|
|
284
273
|
$.log.debug('重连已在进行中');
|
|
285
274
|
return;
|
|
@@ -304,7 +293,7 @@ Sqlite.prototype._handleConnectionError = function(err) {
|
|
|
304
293
|
* @param {Number} max_retries - 最大重试次数
|
|
305
294
|
* @returns {Promise<boolean>}
|
|
306
295
|
*/
|
|
307
|
-
Sqlite.prototype._reconnect = async function(max_retries) {
|
|
296
|
+
Sqlite.prototype._reconnect = async function (max_retries) {
|
|
308
297
|
if (this._reconnecting) {
|
|
309
298
|
$.log.warn('重连已在进行中');
|
|
310
299
|
return false;
|
|
@@ -353,7 +342,7 @@ Sqlite.prototype._reconnect = async function(max_retries) {
|
|
|
353
342
|
* @param {Number} ms - 休眠时间(毫秒)
|
|
354
343
|
* @returns {Promise<void>}
|
|
355
344
|
*/
|
|
356
|
-
Sqlite.prototype._sleep = function(ms) {
|
|
345
|
+
Sqlite.prototype._sleep = function (ms) {
|
|
357
346
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
358
347
|
};
|
|
359
348
|
|
|
@@ -364,7 +353,7 @@ Sqlite.prototype._sleep = function(ms) {
|
|
|
364
353
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
365
354
|
* @returns {Promise<Object>}
|
|
366
355
|
*/
|
|
367
|
-
Sqlite.prototype.run = async function(sql, params, timeout) {
|
|
356
|
+
Sqlite.prototype.run = async function (sql, params, timeout) {
|
|
368
357
|
let conn = null;
|
|
369
358
|
timeout = timeout || this.config.query_timeout || 30000;
|
|
370
359
|
|
|
@@ -383,7 +372,7 @@ Sqlite.prototype.run = async function(sql, params, timeout) {
|
|
|
383
372
|
}
|
|
384
373
|
});
|
|
385
374
|
});
|
|
386
|
-
|
|
375
|
+
|
|
387
376
|
const timeout_promise = new Promise((_, reject) => {
|
|
388
377
|
setTimeout(() => {
|
|
389
378
|
reject(new Error(`SQL查询超时: ${timeout}ms`));
|
|
@@ -416,7 +405,7 @@ Sqlite.prototype.run = async function(sql, params, timeout) {
|
|
|
416
405
|
* @param {Number} timeout - 超时时间(毫秒)
|
|
417
406
|
* @returns {Promise<Object>}
|
|
418
407
|
*/
|
|
419
|
-
Sqlite.prototype.exec = async function(sql, params, timeout) {
|
|
408
|
+
Sqlite.prototype.exec = async function (sql, params, timeout) {
|
|
420
409
|
let conn = null;
|
|
421
410
|
timeout = timeout || this.config.query_timeout || 30000;
|
|
422
411
|
|
|
@@ -426,7 +415,7 @@ Sqlite.prototype.exec = async function(sql, params, timeout) {
|
|
|
426
415
|
|
|
427
416
|
// 直接在方法内部实现超时控制
|
|
428
417
|
const query_promise = new Promise((resolve, reject) => {
|
|
429
|
-
conn.run(sql, params || [], function(error) {
|
|
418
|
+
conn.run(sql, params || [], function (error) {
|
|
430
419
|
if (error) {
|
|
431
420
|
reject(error);
|
|
432
421
|
} else {
|
|
@@ -439,7 +428,7 @@ Sqlite.prototype.exec = async function(sql, params, timeout) {
|
|
|
439
428
|
}
|
|
440
429
|
});
|
|
441
430
|
});
|
|
442
|
-
|
|
431
|
+
|
|
443
432
|
const timeout_promise = new Promise((_, reject) => {
|
|
444
433
|
setTimeout(() => {
|
|
445
434
|
reject(new Error(`SQL执行超时: ${timeout}ms`));
|
|
@@ -472,7 +461,7 @@ Sqlite.prototype.exec = async function(sql, params, timeout) {
|
|
|
472
461
|
* @param {Object} options - 选项(order_by, limit, offset等)
|
|
473
462
|
* @returns {Promise<Array>}
|
|
474
463
|
*/
|
|
475
|
-
Sqlite.prototype.read = async function(table, condition, options) {
|
|
464
|
+
Sqlite.prototype.read = async function (table, condition, options) {
|
|
476
465
|
try {
|
|
477
466
|
// 检查连接状态
|
|
478
467
|
if (this._status !== 'connected') {
|
|
@@ -539,24 +528,24 @@ Sqlite.prototype.read = async function(table, condition, options) {
|
|
|
539
528
|
* @param {String} database - 数据库名称
|
|
540
529
|
* @returns {Object} 数据库操作实例
|
|
541
530
|
*/
|
|
542
|
-
Sqlite.prototype.db = function(database) {
|
|
531
|
+
Sqlite.prototype.db = function (database) {
|
|
543
532
|
if (this._status !== 'connected') {
|
|
544
533
|
throw new Error('数据库连接未建立');
|
|
545
534
|
}
|
|
546
|
-
|
|
535
|
+
|
|
547
536
|
// 优化:缓存DB实例,避免重复创建
|
|
548
537
|
if (!this._db_instance) {
|
|
549
538
|
this._db_instance = new DB(this);
|
|
550
|
-
|
|
539
|
+
|
|
551
540
|
// 为DB实例添加必要的方法,确保与MySQL接口兼容
|
|
552
541
|
this._db_instance.run = this.run.bind(this);
|
|
553
542
|
this._db_instance.exec = this.exec.bind(this);
|
|
554
543
|
}
|
|
555
|
-
|
|
544
|
+
|
|
556
545
|
if (database) {
|
|
557
546
|
this._db_instance.database = database;
|
|
558
547
|
}
|
|
559
|
-
|
|
548
|
+
|
|
560
549
|
return this._db_instance;
|
|
561
550
|
};
|
|
562
551
|
|
|
@@ -564,7 +553,7 @@ Sqlite.prototype.db = function(database) {
|
|
|
564
553
|
* 获取数据库管理器模型
|
|
565
554
|
* @returns {Object} 数据库管理器模型
|
|
566
555
|
*/
|
|
567
|
-
Sqlite.prototype.dbs = function() {
|
|
556
|
+
Sqlite.prototype.dbs = function () {
|
|
568
557
|
return this.db();
|
|
569
558
|
};
|
|
570
559
|
|
|
@@ -572,7 +561,7 @@ Sqlite.prototype.dbs = function() {
|
|
|
572
561
|
* 开始事务
|
|
573
562
|
* @returns {Promise<Object>} 事务连接对象
|
|
574
563
|
*/
|
|
575
|
-
Sqlite.prototype.beginTransaction = async function() {
|
|
564
|
+
Sqlite.prototype.beginTransaction = async function () {
|
|
576
565
|
try {
|
|
577
566
|
// 检查连接状态
|
|
578
567
|
if (this._status !== 'connected') {
|
|
@@ -631,7 +620,7 @@ Sqlite.prototype.beginTransaction = async function() {
|
|
|
631
620
|
exec: async (sql, params) => {
|
|
632
621
|
// 在事务中执行SQL
|
|
633
622
|
return new Promise((resolve, reject) => {
|
|
634
|
-
connection.run(sql, params || [], function(error) {
|
|
623
|
+
connection.run(sql, params || [], function (error) {
|
|
635
624
|
if (error) {
|
|
636
625
|
reject(error);
|
|
637
626
|
} else {
|
|
@@ -658,7 +647,7 @@ Sqlite.prototype.beginTransaction = async function() {
|
|
|
658
647
|
* @param {Function} callback - 包含事务操作的回调函数
|
|
659
648
|
* @returns {Promise<*>} 回调函数的返回值
|
|
660
649
|
*/
|
|
661
|
-
Sqlite.prototype.transaction = async function(callback) {
|
|
650
|
+
Sqlite.prototype.transaction = async function (callback) {
|
|
662
651
|
let transaction = null;
|
|
663
652
|
|
|
664
653
|
try {
|
|
@@ -689,10 +678,10 @@ Sqlite.prototype.transaction = async function(callback) {
|
|
|
689
678
|
* 确保连接池对象存在
|
|
690
679
|
*/
|
|
691
680
|
if (!$.pool) {
|
|
692
|
-
|
|
681
|
+
$.pool = {};
|
|
693
682
|
}
|
|
694
683
|
if (!$.pool.sqlite) {
|
|
695
|
-
|
|
684
|
+
$.pool.sqlite = {};
|
|
696
685
|
}
|
|
697
686
|
|
|
698
687
|
/**
|
|
@@ -702,15 +691,15 @@ if (!$.pool.sqlite) {
|
|
|
702
691
|
* @return {Object} 返回一个Sqlite类实例
|
|
703
692
|
*/
|
|
704
693
|
function sqliteAdmin(scope, config) {
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
694
|
+
if (!scope) {
|
|
695
|
+
scope = 'sys';
|
|
696
|
+
}
|
|
697
|
+
var obj = $.pool.sqlite[scope];
|
|
698
|
+
if (!obj) {
|
|
699
|
+
$.pool.sqlite[scope] = new Sqlite(config);
|
|
700
|
+
obj = $.pool.sqlite[scope];
|
|
701
|
+
}
|
|
702
|
+
return obj;
|
|
714
703
|
}
|
|
715
704
|
|
|
716
705
|
// 模块导出
|