@maiyunnet/kebab 6.2.2 → 7.0.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/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/scan.d.ts +5 -3
- package/lib/scan.js +19 -7
- package/lib/session.js +5 -2
- package/lib/sql.d.ts +10 -5
- package/lib/sql.js +20 -8
- package/package.json +1 -1
- package/sys/cmd.js +1 -1
- package/sys/mod.js +27 -9
- package/www/example/ctr/test.js +10 -8
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '
|
|
9
|
+
export const VER = '7.0.1';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
package/lib/scan.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2022-09-24 15:23:25
|
|
4
|
-
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05
|
|
4
|
+
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05, 2025-12-3 11:05:38
|
|
5
5
|
*/
|
|
6
6
|
import * as lDb from '#kebab/lib/db.js';
|
|
7
7
|
import * as lKv from '#kebab/lib/kv.js';
|
|
@@ -9,12 +9,14 @@ import * as sCtr from '#kebab/sys/ctr.js';
|
|
|
9
9
|
/** --- Scan 设置的选项 --- */
|
|
10
10
|
export interface IOptions {
|
|
11
11
|
'ttl'?: number;
|
|
12
|
-
'
|
|
12
|
+
'ctr'?: sCtr.Ctr;
|
|
13
|
+
'pre'?: string;
|
|
13
14
|
'name'?: string;
|
|
14
15
|
}
|
|
15
16
|
/** --- scanned 函数的选项 --- */
|
|
16
17
|
export interface IStaticOptions {
|
|
17
|
-
'
|
|
18
|
+
'ctr'?: sCtr.Ctr;
|
|
19
|
+
'pre'?: string;
|
|
18
20
|
'name'?: string;
|
|
19
21
|
}
|
|
20
22
|
export declare class Scan {
|
package/lib/scan.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Project: Kebab, User: JianSuoQiYue
|
|
3
3
|
* Date: 2022-09-24 15:23:25
|
|
4
|
-
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05
|
|
4
|
+
* Last: 2022-09-24 15:23:25, 2022-9-26 12:37:01, 2022-12-29 00:11:16, 2025-11-6 16:32:05, 2025-12-3 11:05:38
|
|
5
5
|
*/
|
|
6
6
|
/*
|
|
7
7
|
CREATE TABLE IF NOT EXISTS `scan` (
|
|
@@ -41,7 +41,11 @@ export class Scan {
|
|
|
41
41
|
}
|
|
42
42
|
this._link = link;
|
|
43
43
|
if (link instanceof lDb.Pool) {
|
|
44
|
-
this._sql = lSql.get(
|
|
44
|
+
this._sql = lSql.get({
|
|
45
|
+
'service': link.getService(),
|
|
46
|
+
'ctr': opt.ctr,
|
|
47
|
+
'pre': opt.pre,
|
|
48
|
+
});
|
|
45
49
|
}
|
|
46
50
|
if (token) {
|
|
47
51
|
this._token = token;
|
|
@@ -227,15 +231,19 @@ export async function scanned(link, token, opt = {}) {
|
|
|
227
231
|
const name = opt.name ?? 'scan';
|
|
228
232
|
if (link instanceof lDb.Pool) {
|
|
229
233
|
// --- Db ---
|
|
230
|
-
const sql = lSql.get(
|
|
234
|
+
const sql = lSql.get({
|
|
235
|
+
'service': link.getService(),
|
|
236
|
+
'ctr': opt.ctr,
|
|
237
|
+
'pre': opt.pre,
|
|
238
|
+
});
|
|
231
239
|
sql.update(name, {
|
|
232
|
-
'time_update': time
|
|
240
|
+
'time_update': time,
|
|
233
241
|
}).where([
|
|
234
242
|
{
|
|
235
243
|
'token': token,
|
|
236
244
|
'time_update': '0'
|
|
237
245
|
},
|
|
238
|
-
['time_exp', '>', time]
|
|
246
|
+
['time_exp', '>', time],
|
|
239
247
|
]);
|
|
240
248
|
const r = await link.execute(sql.getSql(), sql.getData());
|
|
241
249
|
if (r.error) {
|
|
@@ -281,7 +289,11 @@ export async function setData(link, token, data, opt = {}) {
|
|
|
281
289
|
const name = opt.name ?? 'scan';
|
|
282
290
|
if (link instanceof lDb.Pool) {
|
|
283
291
|
// --- Db ---
|
|
284
|
-
const sql = lSql.get(
|
|
292
|
+
const sql = lSql.get({
|
|
293
|
+
'service': link.getService(),
|
|
294
|
+
'ctr': opt.ctr,
|
|
295
|
+
'pre': opt.pre,
|
|
296
|
+
});
|
|
285
297
|
sql.update(name, {
|
|
286
298
|
'data': lText.stringifyJson(data)
|
|
287
299
|
}).where([
|
|
@@ -289,7 +301,7 @@ export async function setData(link, token, data, opt = {}) {
|
|
|
289
301
|
'token': token,
|
|
290
302
|
},
|
|
291
303
|
['time_update', '>', '0'],
|
|
292
|
-
['time_exp', '>', time]
|
|
304
|
+
['time_exp', '>', time],
|
|
293
305
|
]);
|
|
294
306
|
const r = await link.execute(sql.getSql(), sql.getData());
|
|
295
307
|
if (r.error) {
|
package/lib/session.js
CHANGED
|
@@ -41,7 +41,6 @@ export class Session {
|
|
|
41
41
|
async init(ctr, link, auth = false, opt = {}) {
|
|
42
42
|
const config = ctr.getPrototype('_config');
|
|
43
43
|
const ssl = opt.ssl ?? config.session.ssl ?? false;
|
|
44
|
-
const pre = opt.sqlPre ?? null;
|
|
45
44
|
this._name = opt.name ?? config.session.name;
|
|
46
45
|
this._ttl = opt.ttl ?? config.session.ttl ?? 172800;
|
|
47
46
|
const tim = time.stamp();
|
|
@@ -58,7 +57,11 @@ export class Session {
|
|
|
58
57
|
}
|
|
59
58
|
this._link = link;
|
|
60
59
|
if (link instanceof db.Pool) {
|
|
61
|
-
this._sql = sql.get(
|
|
60
|
+
this._sql = sql.get({
|
|
61
|
+
'service': link.getService(),
|
|
62
|
+
'ctr': ctr,
|
|
63
|
+
'pre': opt.sqlPre,
|
|
64
|
+
});
|
|
62
65
|
await this._gc(); // --- 执行 gc ---
|
|
63
66
|
}
|
|
64
67
|
// --- 初始化 Session 数组 ---
|
package/lib/sql.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export declare enum ESERVICE {
|
|
|
11
11
|
'PGSQL' = 1
|
|
12
12
|
}
|
|
13
13
|
export declare class Sql {
|
|
14
|
+
/** --- ctr 对象 --- */
|
|
15
|
+
private readonly _ctr?;
|
|
14
16
|
/** --- 前置 --- */
|
|
15
17
|
private readonly _pre;
|
|
16
18
|
/** --- 服务商 --- */
|
|
@@ -23,10 +25,12 @@ export declare class Sql {
|
|
|
23
25
|
private readonly _alias;
|
|
24
26
|
/** --- PostgreSQL 占位符计数器 --- */
|
|
25
27
|
private _placeholderCounter;
|
|
26
|
-
constructor(
|
|
28
|
+
constructor(opt: {
|
|
29
|
+
'service': ESERVICE;
|
|
30
|
+
'ctr'?: ctr.Ctr;
|
|
31
|
+
'pre'?: string;
|
|
27
32
|
'data'?: kebab.DbValue[];
|
|
28
33
|
'sql'?: string[];
|
|
29
|
-
'service'?: ESERVICE;
|
|
30
34
|
});
|
|
31
35
|
/**
|
|
32
36
|
* --- 插入数据前导 ---
|
|
@@ -215,13 +219,14 @@ export declare class Sql {
|
|
|
215
219
|
}
|
|
216
220
|
/**
|
|
217
221
|
* --- 创建 sql 对象 ---
|
|
218
|
-
* @param ctrPre ctr 对象或 pre 表前缀
|
|
219
222
|
* @param opt 参数
|
|
220
223
|
*/
|
|
221
|
-
export declare function get(
|
|
224
|
+
export declare function get(opt: {
|
|
225
|
+
'service': ESERVICE;
|
|
226
|
+
'ctr'?: ctr.Ctr;
|
|
227
|
+
'pre'?: string;
|
|
222
228
|
'data'?: kebab.DbValue[];
|
|
223
229
|
'sql'?: string[];
|
|
224
|
-
'service'?: ESERVICE;
|
|
225
230
|
}): Sql;
|
|
226
231
|
/**
|
|
227
232
|
* --- 返回代入后的完整 SQL 字符串,这并不安全不能直接执行,只是用来调试打印 sql 语句 ---
|
package/lib/sql.js
CHANGED
|
@@ -2,8 +2,6 @@ import * as lText from '#kebab/lib/text.js';
|
|
|
2
2
|
import * as lCore from '#kebab/lib/core.js';
|
|
3
3
|
// --- 第三方 ---
|
|
4
4
|
import * as mysql2 from 'mysql2/promise';
|
|
5
|
-
// --- 库和定义 ---
|
|
6
|
-
import * as ctr from '#kebab/sys/ctr.js';
|
|
7
5
|
/** --- 服务商定义 --- */
|
|
8
6
|
export var ESERVICE;
|
|
9
7
|
(function (ESERVICE) {
|
|
@@ -14,7 +12,7 @@ export var ESERVICE;
|
|
|
14
12
|
let columnToken = '';
|
|
15
13
|
export class Sql {
|
|
16
14
|
// --- 实例化 ---
|
|
17
|
-
constructor(
|
|
15
|
+
constructor(opt) {
|
|
18
16
|
/** --- 前置 --- */
|
|
19
17
|
this._pre = '';
|
|
20
18
|
/** --- 预拼装 Sql 数组 --- */
|
|
@@ -27,7 +25,8 @@ export class Sql {
|
|
|
27
25
|
this._placeholderCounter = 1;
|
|
28
26
|
/** --- where 的 data 的开始处和结束处 --- */
|
|
29
27
|
this._whereDataPosition = [0, 0];
|
|
30
|
-
this.
|
|
28
|
+
this._ctr = opt.ctr;
|
|
29
|
+
this._pre = opt.pre ?? '';
|
|
31
30
|
this._service = opt.service ?? ESERVICE.MYSQL;
|
|
32
31
|
if (opt.data) {
|
|
33
32
|
this._data = opt.data;
|
|
@@ -646,7 +645,10 @@ export class Sql {
|
|
|
646
645
|
}
|
|
647
646
|
sql[0] = sql[0].replace(/FROM [`\w, ]+/, 'FROM ' + table);
|
|
648
647
|
}
|
|
649
|
-
return get(
|
|
648
|
+
return get({
|
|
649
|
+
'service': this._service,
|
|
650
|
+
'ctr': this._ctr,
|
|
651
|
+
'pre': this._pre,
|
|
650
652
|
'data': data,
|
|
651
653
|
'sql': sql,
|
|
652
654
|
});
|
|
@@ -934,11 +936,21 @@ export class Sql {
|
|
|
934
936
|
}
|
|
935
937
|
/**
|
|
936
938
|
* --- 创建 sql 对象 ---
|
|
937
|
-
* @param ctrPre ctr 对象或 pre 表前缀
|
|
938
939
|
* @param opt 参数
|
|
939
940
|
*/
|
|
940
|
-
export function get(
|
|
941
|
-
|
|
941
|
+
export function get(opt) {
|
|
942
|
+
let pre = opt.pre ?? opt.ctr?.getPrototype('_config').sql.pre;
|
|
943
|
+
if (pre) {
|
|
944
|
+
if (opt.service === ESERVICE.MYSQL) {
|
|
945
|
+
if (!pre.endsWith('_')) {
|
|
946
|
+
pre += '_';
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return new Sql({
|
|
951
|
+
...opt,
|
|
952
|
+
'pre': pre,
|
|
953
|
+
});
|
|
942
954
|
}
|
|
943
955
|
/**
|
|
944
956
|
* --- 返回代入后的完整 SQL 字符串,这并不安全不能直接执行,只是用来调试打印 sql 语句 ---
|
package/package.json
CHANGED
package/sys/cmd.js
CHANGED
package/sys/mod.js
CHANGED
|
@@ -79,8 +79,10 @@ export default class Mod {
|
|
|
79
79
|
/** --- 导入数据库连接 --- */
|
|
80
80
|
this._db = opt.db;
|
|
81
81
|
/** --- 新建 sql 对象 --- */
|
|
82
|
-
this._sql = lSql.get(
|
|
82
|
+
this._sql = lSql.get({
|
|
83
83
|
'service': this._db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
84
|
+
'ctr': opt.ctr,
|
|
85
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
84
86
|
});
|
|
85
87
|
if (opt.index) {
|
|
86
88
|
this._index = typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)];
|
|
@@ -118,8 +120,10 @@ export default class Mod {
|
|
|
118
120
|
* @param opt 选项
|
|
119
121
|
*/
|
|
120
122
|
static async insert(db, cs, vs, opt = {}) {
|
|
121
|
-
const sq = lSql.get(
|
|
123
|
+
const sq = lSql.get({
|
|
122
124
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
125
|
+
'ctr': opt.ctr,
|
|
126
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
123
127
|
});
|
|
124
128
|
if (!vs) {
|
|
125
129
|
// --- 单行 ---
|
|
@@ -165,8 +169,10 @@ export default class Mod {
|
|
|
165
169
|
* @param opt 选项
|
|
166
170
|
*/
|
|
167
171
|
static insertSql(db, cs, vs, opt = {}) {
|
|
168
|
-
const sq = lSql.get(
|
|
172
|
+
const sq = lSql.get({
|
|
169
173
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
174
|
+
'ctr': opt.ctr,
|
|
175
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
170
176
|
});
|
|
171
177
|
sq.insert(this._$table + (opt.index ? ('_' + opt.index) : '')).values(cs, vs);
|
|
172
178
|
return sq.format();
|
|
@@ -181,8 +187,10 @@ export default class Mod {
|
|
|
181
187
|
const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
|
|
182
188
|
let ar = 0;
|
|
183
189
|
for (const index of indexs) {
|
|
184
|
-
const sq = lSql.get(
|
|
190
|
+
const sq = lSql.get({
|
|
185
191
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
192
|
+
'ctr': opt.ctr,
|
|
193
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
186
194
|
});
|
|
187
195
|
sq.delete(this._$table + (index ? ('_' + index) : '')).where(where);
|
|
188
196
|
if (opt.by) {
|
|
@@ -209,8 +217,10 @@ export default class Mod {
|
|
|
209
217
|
* @param opt 选项
|
|
210
218
|
*/
|
|
211
219
|
static removeByWhereSql(db, where, opt = {}) {
|
|
212
|
-
const sq = lSql.get(
|
|
220
|
+
const sq = lSql.get({
|
|
213
221
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
222
|
+
'ctr': opt.ctr,
|
|
223
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
214
224
|
});
|
|
215
225
|
sq.delete(this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
216
226
|
if (opt.by) {
|
|
@@ -232,8 +242,10 @@ export default class Mod {
|
|
|
232
242
|
const indexs = opt.index ? (typeof opt.index === 'string' ? [opt.index] : [...new Set(opt.index)]) : [''];
|
|
233
243
|
let ar = 0;
|
|
234
244
|
for (const index of indexs) {
|
|
235
|
-
const sq = lSql.get(
|
|
245
|
+
const sq = lSql.get({
|
|
236
246
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
247
|
+
'ctr': opt.ctr,
|
|
248
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
237
249
|
});
|
|
238
250
|
sq.update(this._$table + (index ? ('_' + index) : ''), data).where(where);
|
|
239
251
|
if (opt.by) {
|
|
@@ -261,8 +273,10 @@ export default class Mod {
|
|
|
261
273
|
* @param opt 选项
|
|
262
274
|
*/
|
|
263
275
|
static updateByWhereSql(db, data, where, opt = {}) {
|
|
264
|
-
const sq = lSql.get(
|
|
276
|
+
const sq = lSql.get({
|
|
265
277
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
278
|
+
'ctr': opt.ctr,
|
|
279
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
266
280
|
});
|
|
267
281
|
sq.update(this._$table + (opt.index ? ('_' + opt.index) : ''), data).where(where);
|
|
268
282
|
if (opt.by) {
|
|
@@ -307,8 +321,10 @@ export default class Mod {
|
|
|
307
321
|
batches.push(data.slice(i, i + batchSize));
|
|
308
322
|
}
|
|
309
323
|
for (const batch of batches) {
|
|
310
|
-
const sq = lSql.get(
|
|
324
|
+
const sq = lSql.get({
|
|
311
325
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
326
|
+
'ctr': opt.ctr,
|
|
327
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
312
328
|
});
|
|
313
329
|
const updates = {};
|
|
314
330
|
const keys = [];
|
|
@@ -475,8 +491,10 @@ export default class Mod {
|
|
|
475
491
|
* @param opt 选项
|
|
476
492
|
*/
|
|
477
493
|
static async primarys(db, where = '', opt = {}) {
|
|
478
|
-
const sq = lSql.get(
|
|
494
|
+
const sq = lSql.get({
|
|
479
495
|
'service': db.getService() ?? lDb.ESERVICE.PGSQL,
|
|
496
|
+
'ctr': opt.ctr,
|
|
497
|
+
'pre': opt.pre ?? this.constructor._$pre,
|
|
480
498
|
});
|
|
481
499
|
sq.select(this._$primary, this._$table + (opt.index ? ('_' + opt.index) : '')).where(where);
|
|
482
500
|
const r = await db.query(sq.getSql(), sq.getData());
|
package/www/example/ctr/test.js
CHANGED
|
@@ -2098,9 +2098,9 @@ error: <pre>${JSON.stringify(res.error, null, 4)}</pre>`);
|
|
|
2098
2098
|
}
|
|
2099
2099
|
const s = this._get['s'] ?? 'db';
|
|
2100
2100
|
const echo = [];
|
|
2101
|
-
const scan = await lScan.get(link, undefined, { 'ttl': 30, '
|
|
2101
|
+
const scan = await lScan.get(link, undefined, { 'ttl': 30, 'ctr': this });
|
|
2102
2102
|
const token = scan.getToken();
|
|
2103
|
-
echo.push(`<pre>const scan = await lScan.get(this, link, undefined, { 'ttl': 30, '
|
|
2103
|
+
echo.push(`<pre>const scan = await lScan.get(this, link, undefined, { 'ttl': 30, 'ctr': this });
|
|
2104
2104
|
const token = scan.getToken();</pre>
|
|
2105
2105
|
token: ${token ?? 'null'}<br><br>
|
|
2106
2106
|
Scan status: <b id="status" style="color: red;">Waiting...</b><br>
|
|
@@ -2189,7 +2189,7 @@ function confirm() {
|
|
|
2189
2189
|
return [0, 'Failed, link can not be connected.'];
|
|
2190
2190
|
}
|
|
2191
2191
|
const scan = await lScan.get(link, this._post['token'], {
|
|
2192
|
-
'
|
|
2192
|
+
'ctr': this
|
|
2193
2193
|
});
|
|
2194
2194
|
const rtn = await scan.poll();
|
|
2195
2195
|
switch (rtn) {
|
|
@@ -2214,7 +2214,7 @@ function confirm() {
|
|
|
2214
2214
|
return [0, 'Failed, link can not be connected.'];
|
|
2215
2215
|
}
|
|
2216
2216
|
if (!await lScan.scanned(link, this._post['token'], {
|
|
2217
|
-
'
|
|
2217
|
+
'ctr': this
|
|
2218
2218
|
})) {
|
|
2219
2219
|
return [0, 'Token has expired.'];
|
|
2220
2220
|
}
|
|
@@ -2228,7 +2228,7 @@ function confirm() {
|
|
|
2228
2228
|
if (!await lScan.setData(link, this._post['token'], {
|
|
2229
2229
|
'uid': '5'
|
|
2230
2230
|
}, {
|
|
2231
|
-
'
|
|
2231
|
+
'ctr': this
|
|
2232
2232
|
})) {
|
|
2233
2233
|
return [0, 'Token has expired.'];
|
|
2234
2234
|
}
|
|
@@ -2315,9 +2315,11 @@ Result:<pre id="result">Nothing.</pre>`);
|
|
|
2315
2315
|
}
|
|
2316
2316
|
sql() {
|
|
2317
2317
|
const echo = [];
|
|
2318
|
-
let sql =
|
|
2319
|
-
'service': lSql.ESERVICE.PGSQL,
|
|
2320
|
-
|
|
2318
|
+
let sql = lSql.get({
|
|
2319
|
+
'service': this._get['s'] === 'pgsql' ? lSql.ESERVICE.PGSQL : lSql.ESERVICE.MYSQL,
|
|
2320
|
+
'ctr': this,
|
|
2321
|
+
'pre': 'test'
|
|
2322
|
+
});
|
|
2321
2323
|
switch (this._get['type']) {
|
|
2322
2324
|
case 'insert': {
|
|
2323
2325
|
let s = sql.insert('user').values(['name', 'age'], [
|