mm_sqlite 1.1.9 → 1.2.1
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 +4 -52
- package/index.js +2 -16
- package/package.json +1 -1
- package/sql.js +0 -1
package/db.js
CHANGED
|
@@ -71,54 +71,6 @@ DB.prototype.new = function (table, key) {
|
|
|
71
71
|
return db;
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
-
/**
|
|
75
|
-
* 执行SQL查询
|
|
76
|
-
* @param {string} sql SQL语句
|
|
77
|
-
* @param {Array} params 参数数组
|
|
78
|
-
* @param {Number} timeout 超时时间(毫秒)
|
|
79
|
-
* @returns {Promise<Object>} 查询结果
|
|
80
|
-
*/
|
|
81
|
-
DB.prototype.run = async function (sql, params = [], timeout = 30000) {
|
|
82
|
-
if (!this._mysql) {
|
|
83
|
-
throw new Error('SQLite实例未初始化');
|
|
84
|
-
}
|
|
85
|
-
try {
|
|
86
|
-
// 使用Promise.race实现超时控制
|
|
87
|
-
return await Promise.race([
|
|
88
|
-
this._mysql.run(sql, params, timeout),
|
|
89
|
-
this._createTimeoutPromise(timeout, `查询执行超时: ${sql.substring(0, 100)}...`)
|
|
90
|
-
]);
|
|
91
|
-
} catch (error) {
|
|
92
|
-
this.logger('error', '查询执行失败', error);
|
|
93
|
-
// 返回空数组作为默认值,保持返回值类型一致
|
|
94
|
-
return [];
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 执行SQL命令
|
|
100
|
-
* @param {string} sql SQL语句
|
|
101
|
-
* @param {Array} params 参数数组
|
|
102
|
-
* @param {Number} timeout 超时时间(毫秒)
|
|
103
|
-
* @returns {Promise<Object>} 执行结果
|
|
104
|
-
*/
|
|
105
|
-
DB.prototype.exec = async function (sql, params = [], timeout = 30000) {
|
|
106
|
-
if (!this._mysql) {
|
|
107
|
-
throw new Error('SQLite实例未初始化');
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
// 使用Promise.race实现超时控制
|
|
111
|
-
return await Promise.race([
|
|
112
|
-
this._mysql.exec(sql, params, timeout),
|
|
113
|
-
this._createTimeoutPromise(timeout, `命令执行超时: ${sql.substring(0, 100)}...`)
|
|
114
|
-
]);
|
|
115
|
-
} catch (error) {
|
|
116
|
-
this.logger('error', '命令执行失败', error);
|
|
117
|
-
// 返回默认操作结果对象,保持返回值类型一致
|
|
118
|
-
return 0;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
|
|
122
74
|
/**
|
|
123
75
|
* 获取数据库连接
|
|
124
76
|
* @param {Number} timeout 超时时间(毫秒)
|
|
@@ -882,12 +834,12 @@ DB.prototype.backup = function (table, backup, timeout = 60000) {
|
|
|
882
834
|
* @return {Promise|Number} 操作结果
|
|
883
835
|
*/
|
|
884
836
|
DB.prototype.createTable = function (table, model, key = 'id', timeout = 15000) {
|
|
885
|
-
if (!table || !model
|
|
886
|
-
throw new TypeError('table, model
|
|
837
|
+
if (!table || !model) {
|
|
838
|
+
throw new TypeError('table, model must be specified');
|
|
887
839
|
}
|
|
888
840
|
var fields = '';
|
|
889
|
-
for (const field in model
|
|
890
|
-
const value = model
|
|
841
|
+
for (const field in model) {
|
|
842
|
+
const value = model[field];
|
|
891
843
|
let type = '';
|
|
892
844
|
if (field === key) {
|
|
893
845
|
type = 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
package/index.js
CHANGED
|
@@ -345,13 +345,6 @@ Sqlite.prototype.run = async function (sql, params, timeout) {
|
|
|
345
345
|
if (err.code && (err.code === 'ECONNRESET' || err.code === 'ETIMEDOUT')) {
|
|
346
346
|
this._handleConnectionError(err);
|
|
347
347
|
}
|
|
348
|
-
this.sql = sql;
|
|
349
|
-
// 记录错误信息
|
|
350
|
-
this.error = {
|
|
351
|
-
code: err.code,
|
|
352
|
-
message: err.message
|
|
353
|
-
};
|
|
354
|
-
|
|
355
348
|
// 返回空数组作为默认值,保持返回值类型一致
|
|
356
349
|
return [];
|
|
357
350
|
}
|
|
@@ -387,9 +380,9 @@ Sqlite.prototype.exec = async function (sql, params, timeout) {
|
|
|
387
380
|
var _this = this;
|
|
388
381
|
// 直接在方法内部实现超时控制
|
|
389
382
|
const query_promise = new Promise((resolve, reject) => {
|
|
390
|
-
conn.run(sql, params || [], function (error
|
|
383
|
+
conn.run(sql, params || [], function (error) {
|
|
391
384
|
if (error) {
|
|
392
|
-
_this.sql =
|
|
385
|
+
_this.sql = sql;
|
|
393
386
|
_this.error = {
|
|
394
387
|
code: error.errno,
|
|
395
388
|
message: error.message
|
|
@@ -429,13 +422,6 @@ Sqlite.prototype.exec = async function (sql, params, timeout) {
|
|
|
429
422
|
if (err.code && (err.code === 'ECONNRESET' || err.code === 'ETIMEDOUT')) {
|
|
430
423
|
this._handleConnectionError(err);
|
|
431
424
|
}
|
|
432
|
-
this.sql = sql;
|
|
433
|
-
// 记录错误信息
|
|
434
|
-
this.error = {
|
|
435
|
-
code: err.code,
|
|
436
|
-
message: err.message
|
|
437
|
-
};
|
|
438
|
-
|
|
439
425
|
// 返回默认操作结果对象,保持返回值类型一致
|
|
440
426
|
return 0;
|
|
441
427
|
}
|
package/package.json
CHANGED
package/sql.js
CHANGED