@maiyunnet/kebab 10.0.0 → 10.1.0
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/doc/kebab-rag.md +393 -255
- package/index.d.ts +3 -2
- package/index.js +1 -1
- package/lib/db/conn.d.ts +4 -2
- package/lib/db/pool.d.ts +17 -4
- package/lib/db/pool.js +41 -23
- package/lib/db/tran.d.ts +6 -4
- package/lib/db.d.ts +10 -5
- package/lib/db.js +36 -6
- package/lib/undici/response.d.ts +1 -1
- package/package.json +1 -1
- package/sys/cmd.js +0 -13
- package/sys/ctr.d.ts +2 -2
- package/sys/ctr.js +2 -2
- package/sys/mod.d.ts +16 -0
- package/sys/mod.js +33 -15
- package/www/example/ctr/test.d.ts +1 -1
- package/www/example/ctr/test.js +26 -7
- package/www/example/mod/test.js +0 -1
package/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type * as tls from 'node:tls';
|
|
7
7
|
/** --- 当前系统版本号 --- */
|
|
8
|
-
export declare const VER = "10.
|
|
8
|
+
export declare const VER = "10.1.0";
|
|
9
9
|
/** --- 框架根目录,以 / 结尾 --- */
|
|
10
10
|
export declare const ROOT_PATH: string;
|
|
11
11
|
/** --- 框架的 LIB,以 / 结尾 --- */
|
|
@@ -41,7 +41,8 @@ export interface IConfig {
|
|
|
41
41
|
'const': IConfigConst;
|
|
42
42
|
'db': Record<string, {
|
|
43
43
|
'default': IConfigDb;
|
|
44
|
-
|
|
44
|
+
/** --- 可选只读从库配置,支持单节点或节点数组;省略的字段自动继承 default --- */
|
|
45
|
+
'read'?: Partial<IConfigDb> | Array<Partial<IConfigDb>>;
|
|
45
46
|
}> & {
|
|
46
47
|
'default': 'MYSQL' | 'PGSQL';
|
|
47
48
|
};
|
package/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
5
5
|
*/
|
|
6
6
|
/** --- 当前系统版本号 --- */
|
|
7
|
-
export const VER = '10.
|
|
7
|
+
export const VER = '10.1.0';
|
|
8
8
|
// --- 服务端用的路径 ---
|
|
9
9
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
10
10
|
/** --- /xxx/xxx --- */
|
package/lib/db/conn.d.ts
CHANGED
|
@@ -76,17 +76,19 @@ export declare class Connection {
|
|
|
76
76
|
* --- 执行一条 SQL 并获得返回数据 ---
|
|
77
77
|
* @param sql 执行的 SQL 字符串
|
|
78
78
|
* @param values SQL 占位符绑定的值
|
|
79
|
+
* @param opt 查询选项(单连接模式下忽略)
|
|
79
80
|
* @returns 查询结果,result = -500 表示查询失败
|
|
80
81
|
*/
|
|
81
|
-
query(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData>;
|
|
82
|
+
query(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData>;
|
|
82
83
|
/**
|
|
83
84
|
* --- 执行一条 SQL,按指定类型返回数据行 ---
|
|
84
85
|
* @typeParam TRow SQL 返回的行结构,由调用方根据查询字段指定
|
|
85
86
|
* @param sql 执行的 SQL 字符串
|
|
86
87
|
* @param values SQL 占位符绑定的值
|
|
88
|
+
* @param opt 查询选项(单连接模式下忽略)
|
|
87
89
|
* @returns 查询结果,result = -500 表示查询失败
|
|
88
90
|
*/
|
|
89
|
-
query<TRow extends object>(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData<TRow>>;
|
|
91
|
+
query<TRow extends object>(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData<TRow>>;
|
|
90
92
|
/**
|
|
91
93
|
* --- 执行一条 SQL 并获得影响行数对象 packet ---
|
|
92
94
|
* @param sql 执行的 SQL 字符串
|
package/lib/db/pool.d.ts
CHANGED
|
@@ -25,11 +25,15 @@ export declare class Pool {
|
|
|
25
25
|
private _queries;
|
|
26
26
|
/** --- 当前服务商 --- */
|
|
27
27
|
private readonly _service;
|
|
28
|
-
/** --- 当前 Pool
|
|
28
|
+
/** --- 当前 Pool 对象的主库连接信息 --- */
|
|
29
29
|
private readonly _etc;
|
|
30
|
+
/** --- 当前 Pool 对象的只读从库连接列表 --- */
|
|
31
|
+
private readonly _read;
|
|
30
32
|
constructor(etc: kebab.IConfigDb, opt: {
|
|
31
33
|
/** --- 服务商 --- */
|
|
32
34
|
'service': lDb.ESERVICE;
|
|
35
|
+
/** --- 可选只读从库连接列表 --- */
|
|
36
|
+
'read'?: kebab.IConfigDb[];
|
|
33
37
|
});
|
|
34
38
|
/** --- 获取当前连接的服务商 --- */
|
|
35
39
|
getService(): lDb.ESERVICE;
|
|
@@ -39,17 +43,19 @@ export declare class Pool {
|
|
|
39
43
|
* --- 执行一条 SQL,无视顺序和相同连接,随用随取 ---
|
|
40
44
|
* @param sql 执行的 SQL 字符串
|
|
41
45
|
* @param values SQL 占位符绑定的值
|
|
46
|
+
* @param opt 查询选项,read 为 true 且已配置从库时路由到只读从库
|
|
42
47
|
* @returns 查询结果,result = -500 表示系统错误
|
|
43
48
|
*/
|
|
44
|
-
query(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData>;
|
|
49
|
+
query(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData>;
|
|
45
50
|
/**
|
|
46
51
|
* --- 执行一条 SQL,按指定类型返回数据行 ---
|
|
47
52
|
* @typeParam TRow SQL 返回的行结构,由调用方根据查询字段指定
|
|
48
53
|
* @param sql 执行的 SQL 字符串
|
|
49
54
|
* @param values SQL 占位符绑定的值
|
|
55
|
+
* @param opt 查询选项,read 为 true 且已配置从库时路由到只读从库
|
|
50
56
|
* @returns 查询结果,result = -500 表示系统错误
|
|
51
57
|
*/
|
|
52
|
-
query<TRow extends object>(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData<TRow>>;
|
|
58
|
+
query<TRow extends object>(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData<TRow>>;
|
|
53
59
|
/**
|
|
54
60
|
* --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
|
|
55
61
|
* @param sql 执行的 SQL 字符串
|
|
@@ -61,7 +67,14 @@ export declare class Pool {
|
|
|
61
67
|
*/
|
|
62
68
|
beginTransaction(ctr: sCtr.Ctr | null): Promise<Transaction | null>;
|
|
63
69
|
/**
|
|
64
|
-
* ---
|
|
70
|
+
* --- 按读写标志选择本次目标连接配置;未开启 read 或未配置从库时返回主库 ---
|
|
71
|
+
* @param read 是否优先使用只读从库
|
|
72
|
+
* @returns 目标数据库连接配置
|
|
73
|
+
*/
|
|
74
|
+
private _pickEtc;
|
|
75
|
+
/**
|
|
76
|
+
* --- 获取一个连接,自动变为 using 状态;连接失败会返回 null ---
|
|
77
|
+
* @param targetEtc 目标连接配置,默认使用主库
|
|
65
78
|
*/
|
|
66
79
|
private _getConnection;
|
|
67
80
|
/**
|
package/lib/db/pool.js
CHANGED
|
@@ -103,11 +103,14 @@ export class Pool {
|
|
|
103
103
|
_queries = 0;
|
|
104
104
|
/** --- 当前服务商 --- */
|
|
105
105
|
_service;
|
|
106
|
-
/** --- 当前 Pool
|
|
106
|
+
/** --- 当前 Pool 对象的主库连接信息 --- */
|
|
107
107
|
_etc;
|
|
108
|
+
/** --- 当前 Pool 对象的只读从库连接列表 --- */
|
|
109
|
+
_read;
|
|
108
110
|
constructor(etc, opt) {
|
|
109
111
|
this._etc = etc;
|
|
110
112
|
this._service = opt.service;
|
|
113
|
+
this._read = (opt.read ?? []).filter(item => Boolean(item.host));
|
|
111
114
|
}
|
|
112
115
|
/** --- 获取当前连接的服务商 --- */
|
|
113
116
|
getService() {
|
|
@@ -117,10 +120,10 @@ export class Pool {
|
|
|
117
120
|
getName() {
|
|
118
121
|
return this._etc.name ?? '';
|
|
119
122
|
}
|
|
120
|
-
async query(sql, values) {
|
|
123
|
+
async query(sql, values, opt = {}) {
|
|
121
124
|
++this._queries;
|
|
122
125
|
// --- 获取并自动 using ---
|
|
123
|
-
const conn = await this._getConnection();
|
|
126
|
+
const conn = await this._getConnection(this._pickEtc(opt.read));
|
|
124
127
|
if (!conn) {
|
|
125
128
|
return {
|
|
126
129
|
'rows': null,
|
|
@@ -177,17 +180,32 @@ export class Pool {
|
|
|
177
180
|
return null;
|
|
178
181
|
}
|
|
179
182
|
/**
|
|
180
|
-
* ---
|
|
183
|
+
* --- 按读写标志选择本次目标连接配置;未开启 read 或未配置从库时返回主库 ---
|
|
184
|
+
* @param read 是否优先使用只读从库
|
|
185
|
+
* @returns 目标数据库连接配置
|
|
186
|
+
*/
|
|
187
|
+
_pickEtc(read) {
|
|
188
|
+
if (!read || this._read.length === 0) {
|
|
189
|
+
return this._etc;
|
|
190
|
+
}
|
|
191
|
+
if (this._read.length === 1) {
|
|
192
|
+
return this._read[0];
|
|
193
|
+
}
|
|
194
|
+
return this._read[Math.floor(Math.random() * this._read.length)];
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* --- 获取一个连接,自动变为 using 状态;连接失败会返回 null ---
|
|
198
|
+
* @param targetEtc 目标连接配置,默认使用主库
|
|
181
199
|
*/
|
|
182
|
-
async _getConnection() {
|
|
200
|
+
async _getConnection(targetEtc = this._etc) {
|
|
183
201
|
let conn = null;
|
|
184
202
|
for (const connection of connections) {
|
|
185
203
|
const etc = connection.getEtc();
|
|
186
204
|
const service = connection.getService();
|
|
187
|
-
if ((etc.host !==
|
|
188
|
-
(etc.port !==
|
|
189
|
-
(etc.name !==
|
|
190
|
-
(etc.user !==
|
|
205
|
+
if ((etc.host !== targetEtc.host) ||
|
|
206
|
+
(etc.port !== targetEtc.port) ||
|
|
207
|
+
(etc.name !== targetEtc.name) ||
|
|
208
|
+
(etc.user !== targetEtc.user) ||
|
|
191
209
|
(service !== this._service)) {
|
|
192
210
|
// --- 配置项连接项不匹配 ---
|
|
193
211
|
continue;
|
|
@@ -208,12 +226,12 @@ export class Pool {
|
|
|
208
226
|
case lDb.ESERVICE.MYSQL: {
|
|
209
227
|
// --- MYSQL ---
|
|
210
228
|
const link = await mysql2.createConnection({
|
|
211
|
-
'host':
|
|
212
|
-
'port':
|
|
213
|
-
'charset':
|
|
214
|
-
'database':
|
|
215
|
-
'user':
|
|
216
|
-
'password':
|
|
229
|
+
'host': targetEtc.host,
|
|
230
|
+
'port': targetEtc.port,
|
|
231
|
+
'charset': targetEtc.charset,
|
|
232
|
+
'database': targetEtc.name,
|
|
233
|
+
'user': targetEtc.user,
|
|
234
|
+
'password': targetEtc.pwd,
|
|
217
235
|
'connectTimeout': 3_000,
|
|
218
236
|
typeCast: (field, next) => {
|
|
219
237
|
if (field.type !== 'GEOMETRY') {
|
|
@@ -232,7 +250,7 @@ export class Pool {
|
|
|
232
250
|
return geo[0];
|
|
233
251
|
},
|
|
234
252
|
});
|
|
235
|
-
const c = new Connection(
|
|
253
|
+
const c = new Connection(targetEtc, link);
|
|
236
254
|
c.using();
|
|
237
255
|
link.on('error', function (err) {
|
|
238
256
|
c.setLost();
|
|
@@ -253,15 +271,15 @@ export class Pool {
|
|
|
253
271
|
case lDb.ESERVICE.PGSQL: {
|
|
254
272
|
// --- PGSQL ---
|
|
255
273
|
const link = new pg.Client({
|
|
256
|
-
'host':
|
|
257
|
-
'port':
|
|
258
|
-
'database':
|
|
259
|
-
'user':
|
|
260
|
-
'password':
|
|
274
|
+
'host': targetEtc.host,
|
|
275
|
+
'port': targetEtc.port,
|
|
276
|
+
'database': targetEtc.name,
|
|
277
|
+
'user': targetEtc.user,
|
|
278
|
+
'password': targetEtc.pwd,
|
|
261
279
|
'connectionTimeoutMillis': 3_000,
|
|
262
280
|
});
|
|
263
281
|
await link.connect();
|
|
264
|
-
const c = new Connection(
|
|
282
|
+
const c = new Connection(targetEtc, link);
|
|
265
283
|
c.using();
|
|
266
284
|
link.on('error', err => {
|
|
267
285
|
c.setLost();
|
|
@@ -283,7 +301,7 @@ export class Pool {
|
|
|
283
301
|
await lCore.sleep(300);
|
|
284
302
|
continue;
|
|
285
303
|
}
|
|
286
|
-
const msg = `[DB][_getConnection][${lDb.ESERVICE[this._service]}] ${message}(${
|
|
304
|
+
const msg = `[DB][_getConnection][${lDb.ESERVICE[this._service]}] ${message}(${targetEtc.host}:${targetEtc.port})`;
|
|
287
305
|
lCore.debug(msg);
|
|
288
306
|
lCore.log({}, msg, '-error');
|
|
289
307
|
break;
|
package/lib/db/tran.d.ts
CHANGED
|
@@ -17,20 +17,22 @@ export declare class Transaction {
|
|
|
17
17
|
/** --- 获取当前连接的服务商 --- */
|
|
18
18
|
getService(): lDb.ESERVICE | null;
|
|
19
19
|
/**
|
|
20
|
-
* --- 在事务连接中执行一条 SQL ---
|
|
20
|
+
* --- 在事务连接中执行一条 SQL(事务内始终走当前主库连接,忽略 read 选项) ---
|
|
21
21
|
* @param sql 执行的 SQL 字符串
|
|
22
22
|
* @param values SQL 占位符绑定的值
|
|
23
|
+
* @param opt 查询选项(事务内忽略)
|
|
23
24
|
* @returns 查询结果,result = -500 表示事务已关闭或查询失败
|
|
24
25
|
*/
|
|
25
|
-
query(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData>;
|
|
26
|
+
query(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData>;
|
|
26
27
|
/**
|
|
27
|
-
* --- 在事务连接中执行一条 SQL
|
|
28
|
+
* --- 在事务连接中执行一条 SQL,按指定类型返回数据行(事务内始终走当前主库连接,忽略 read 选项) ---
|
|
28
29
|
* @typeParam TRow SQL 返回的行结构,由调用方根据查询字段指定
|
|
29
30
|
* @param sql 执行的 SQL 字符串
|
|
30
31
|
* @param values SQL 占位符绑定的值
|
|
32
|
+
* @param opt 查询选项(事务内忽略)
|
|
31
33
|
* @returns 查询结果,result = -500 表示事务已关闭或查询失败
|
|
32
34
|
*/
|
|
33
|
-
query<TRow extends object>(sql: string, values?: kebab.DbValue[]): Promise<lDb.IData<TRow>>;
|
|
35
|
+
query<TRow extends object>(sql: string, values?: kebab.DbValue[], opt?: lDb.IQueryOptions): Promise<lDb.IData<TRow>>;
|
|
34
36
|
/**
|
|
35
37
|
* --- 执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误 ---
|
|
36
38
|
* @param sql 执行的 SQL 字符串
|
package/lib/db.d.ts
CHANGED
|
@@ -51,16 +51,21 @@ export interface IPacket {
|
|
|
51
51
|
/** --- 1-正常,-500-服务器错误 --- */
|
|
52
52
|
'result': number;
|
|
53
53
|
}
|
|
54
|
+
/** --- query 查询选项 --- */
|
|
55
|
+
export interface IQueryOptions {
|
|
56
|
+
/** --- 是否优先使用只读从库,默认 false(走主库);未配置从库时自动走主库 --- */
|
|
57
|
+
'read'?: boolean;
|
|
58
|
+
}
|
|
54
59
|
/**
|
|
55
|
-
* --- 获取 Db Pool
|
|
56
|
-
* @param ctrEtc
|
|
60
|
+
* --- 获取 Db Pool 对象(单 Pool 内同时管理主库和可选只读从库) ---
|
|
61
|
+
* @param ctrEtc 控制器对象或主库连接配置
|
|
57
62
|
* @param opt 选项
|
|
58
63
|
* @returns Db Pool 对象
|
|
59
64
|
*/
|
|
60
65
|
export declare function get(ctrEtc: sCtr.Ctr | kebab.IConfigDb, opt?: {
|
|
61
|
-
/** ---
|
|
66
|
+
/** --- 服务商,未传时从控制器配置读取,直接传配置时默认 PGSQL --- */
|
|
62
67
|
'service'?: ESERVICE;
|
|
63
|
-
/** ---
|
|
64
|
-
'read'?:
|
|
68
|
+
/** --- 可选只读从库配置,传入时覆盖控制器配置中的 read --- */
|
|
69
|
+
'read'?: Partial<kebab.IConfigDb> | Array<Partial<kebab.IConfigDb>>;
|
|
65
70
|
}): Pool;
|
|
66
71
|
export { Connection, Pool, IConnectionInfo, getConnectionList, Transaction };
|
package/lib/db.js
CHANGED
|
@@ -14,8 +14,35 @@ export var ESERVICE;
|
|
|
14
14
|
ESERVICE[ESERVICE["PGSQL"] = 1] = "PGSQL";
|
|
15
15
|
})(ESERVICE || (ESERVICE = {}));
|
|
16
16
|
/**
|
|
17
|
-
* ---
|
|
18
|
-
* @param
|
|
17
|
+
* --- 将只读从库配置规范化为完整连接列表,省略字段自动继承主库 ---
|
|
18
|
+
* @param primary 主库连接配置
|
|
19
|
+
* @param read 只读从库配置(单对象或数组)
|
|
20
|
+
* @returns 有效的只读从库连接列表
|
|
21
|
+
*/
|
|
22
|
+
function normalizeReadConfigs(primary, read) {
|
|
23
|
+
if (!read) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
const items = Array.isArray(read) ? read : [read];
|
|
27
|
+
const list = [];
|
|
28
|
+
for (const item of items) {
|
|
29
|
+
if (!item?.host) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
list.push({
|
|
33
|
+
'host': item.host,
|
|
34
|
+
'port': item.port ?? primary.port,
|
|
35
|
+
'charset': item.charset ?? primary.charset,
|
|
36
|
+
'name': item.name ?? primary.name,
|
|
37
|
+
'user': item.user ?? primary.user,
|
|
38
|
+
'pwd': item.pwd ?? primary.pwd,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return list;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* --- 获取 Db Pool 对象(单 Pool 内同时管理主库和可选只读从库) ---
|
|
45
|
+
* @param ctrEtc 控制器对象或主库连接配置
|
|
19
46
|
* @param opt 选项
|
|
20
47
|
* @returns Db Pool 对象
|
|
21
48
|
*/
|
|
@@ -23,15 +50,18 @@ export function get(ctrEtc, opt = {}) {
|
|
|
23
50
|
if (ctrEtc instanceof sCtr.Ctr) {
|
|
24
51
|
// --- 从 ctr 中读取连接信息 ---
|
|
25
52
|
const config = ctrEtc.getPrototype('_config');
|
|
26
|
-
const service = opt.service ? ESERVICE[opt.service] : config.db.default;
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
return new Pool(
|
|
53
|
+
const service = opt.service !== undefined ? ESERVICE[opt.service] : config.db.default;
|
|
54
|
+
const primary = config.db[service].default;
|
|
55
|
+
const read = normalizeReadConfigs(primary, opt.read ?? config.db[service].read);
|
|
56
|
+
return new Pool(primary, {
|
|
30
57
|
'service': service === 'MYSQL' ? ESERVICE.MYSQL : ESERVICE.PGSQL,
|
|
58
|
+
'read': read,
|
|
31
59
|
});
|
|
32
60
|
}
|
|
61
|
+
const read = normalizeReadConfigs(ctrEtc, opt.read);
|
|
33
62
|
return new Pool(ctrEtc, {
|
|
34
63
|
'service': opt.service ?? ESERVICE.PGSQL,
|
|
64
|
+
'read': read,
|
|
35
65
|
});
|
|
36
66
|
}
|
|
37
67
|
export { Connection, Pool, getConnectionList, Transaction };
|
package/lib/undici/response.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ export declare class Response {
|
|
|
41
41
|
/**
|
|
42
42
|
* --- 获取响应读取流对象 ---
|
|
43
43
|
*/
|
|
44
|
-
getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) |
|
|
44
|
+
getStream(): zlib.BrotliDecompress | zlib.Gunzip | zlib.Inflate | (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | null;
|
|
45
45
|
/**
|
|
46
46
|
* --- 获取原生响应读取流对象 ---
|
|
47
47
|
*/
|
package/package.json
CHANGED
package/sys/cmd.js
CHANGED
|
@@ -293,13 +293,6 @@ async function run() {
|
|
|
293
293
|
config.db['MYSQL'].default.name ??= 'maiyun';
|
|
294
294
|
config.db['MYSQL'].default.user ??= 'root';
|
|
295
295
|
config.db['MYSQL'].default.pwd ??= 'DashAdmin';
|
|
296
|
-
config.db['MYSQL'].read ??= {};
|
|
297
|
-
config.db['MYSQL'].read.host ??= '127.0.0.1';
|
|
298
|
-
config.db['MYSQL'].read.port ??= 3306;
|
|
299
|
-
config.db['MYSQL'].read.charset ??= 'utf8mb4';
|
|
300
|
-
config.db['MYSQL'].read.name ??= 'maiyun';
|
|
301
|
-
config.db['MYSQL'].read.user ??= 'root';
|
|
302
|
-
config.db['MYSQL'].read.pwd ??= 'DashAdmin';
|
|
303
296
|
config.db['PGSQL'] ??= {};
|
|
304
297
|
config.db['PGSQL'].default ??= {};
|
|
305
298
|
config.db['PGSQL'].default.host ??= '127.0.0.1';
|
|
@@ -307,12 +300,6 @@ async function run() {
|
|
|
307
300
|
config.db['PGSQL'].default.name ??= 'maiyun';
|
|
308
301
|
config.db['PGSQL'].default.user ??= 'root';
|
|
309
302
|
config.db['PGSQL'].default.pwd ??= 'DashAdmin';
|
|
310
|
-
config.db['PGSQL'].read ??= {};
|
|
311
|
-
config.db['PGSQL'].read.host ??= '127.0.0.1';
|
|
312
|
-
config.db['PGSQL'].read.port ??= 5432;
|
|
313
|
-
config.db['PGSQL'].read.name ??= 'maiyun';
|
|
314
|
-
config.db['PGSQL'].read.user ??= 'root';
|
|
315
|
-
config.db['PGSQL'].read.pwd ??= 'DashAdmin';
|
|
316
303
|
// --- config - dns ---
|
|
317
304
|
config.dns ??= {};
|
|
318
305
|
config.dns['DNSPOD'] ??= {};
|
package/sys/ctr.d.ts
CHANGED
|
@@ -224,7 +224,7 @@ export declare class Ctr {
|
|
|
224
224
|
protected _getMemoryUsage(): number;
|
|
225
225
|
/**
|
|
226
226
|
* --- 加载视图 ---
|
|
227
|
-
* @param
|
|
227
|
+
* @param viewPath 视图目录下的文件路径(不含 .ejs 扩展名)
|
|
228
228
|
* @param data
|
|
229
229
|
*/
|
|
230
230
|
protected _loadView(viewPath: string, data?: kebab.Json): Promise<string>;
|
|
@@ -336,7 +336,7 @@ export declare class Ctr {
|
|
|
336
336
|
} | false;
|
|
337
337
|
/**
|
|
338
338
|
* --- 获取 data JSON 数据 ---
|
|
339
|
-
* @param
|
|
339
|
+
* @param dataPath data 目录下的文件路径(不含 .json 扩展名)
|
|
340
340
|
*/
|
|
341
341
|
protected _loadData<T>(dataPath: string): Promise<T | false | null>;
|
|
342
342
|
/**
|
package/sys/ctr.js
CHANGED
|
@@ -276,7 +276,7 @@ export class Ctr {
|
|
|
276
276
|
}
|
|
277
277
|
/**
|
|
278
278
|
* --- 加载视图 ---
|
|
279
|
-
* @param
|
|
279
|
+
* @param viewPath 视图目录下的文件路径(不含 .ejs 扩展名)
|
|
280
280
|
* @param data
|
|
281
281
|
*/
|
|
282
282
|
async _loadView(viewPath, data = {}) {
|
|
@@ -833,7 +833,7 @@ export class Ctr {
|
|
|
833
833
|
}
|
|
834
834
|
/**
|
|
835
835
|
* --- 获取 data JSON 数据 ---
|
|
836
|
-
* @param
|
|
836
|
+
* @param dataPath data 目录下的文件路径(不含 .json 扩展名)
|
|
837
837
|
*/
|
|
838
838
|
async _loadData(dataPath) {
|
|
839
839
|
const realPath = resolveInsideBase(this._config.const.dataPath, `${dataPath}.json`);
|
package/sys/mod.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ export interface IModOneOptions {
|
|
|
29
29
|
'by'?: [string | string[], 'DESC' | 'ASC'];
|
|
30
30
|
/** --- SQL 优化提示 --- */
|
|
31
31
|
'hint'?: string;
|
|
32
|
+
/** --- 是否优先使用只读从库,默认 false(走主库);加锁或事务内仍走主库 --- */
|
|
33
|
+
'read'?: boolean;
|
|
32
34
|
}
|
|
33
35
|
/** --- 一批游标查询结果,hasMore 表示扫描方向上还有记录 --- */
|
|
34
36
|
export interface ICursorPage<T extends object = Record<string, unknown>> {
|
|
@@ -99,6 +101,8 @@ export default class Mod {
|
|
|
99
101
|
protected _ctr?: sCtr.Ctr;
|
|
100
102
|
/** --- 主表筛选前缀,优先 alias,其次表名 --- */
|
|
101
103
|
protected _fieldPrefix: string;
|
|
104
|
+
/** --- 当前查询是否优先使用只读从库 --- */
|
|
105
|
+
protected _read: boolean;
|
|
102
106
|
/**
|
|
103
107
|
* --- 构造函数 ---
|
|
104
108
|
* @param opt 选项
|
|
@@ -120,6 +124,8 @@ export default class Mod {
|
|
|
120
124
|
'hint'?: string;
|
|
121
125
|
/** --- MySQL 表前缀或 PostgreSQL Schema 名,优先级:选项 > 类属性 > 配置 --- */
|
|
122
126
|
'pre'?: string;
|
|
127
|
+
/** --- 是否优先使用只读从库,默认 false(走主库) --- */
|
|
128
|
+
'read'?: boolean;
|
|
123
129
|
});
|
|
124
130
|
/** --- 创建字段对象 --- */
|
|
125
131
|
static column(field: string): {
|
|
@@ -248,6 +254,7 @@ export default class Mod {
|
|
|
248
254
|
'list': string[];
|
|
249
255
|
};
|
|
250
256
|
'hint'?: string;
|
|
257
|
+
'read'?: boolean;
|
|
251
258
|
}): T;
|
|
252
259
|
/**
|
|
253
260
|
* --- 通过 where 条件获取模型 ---
|
|
@@ -265,6 +272,7 @@ export default class Mod {
|
|
|
265
272
|
'list': string[];
|
|
266
273
|
};
|
|
267
274
|
'hint'?: string;
|
|
275
|
+
'read'?: boolean;
|
|
268
276
|
}): T;
|
|
269
277
|
/**
|
|
270
278
|
* --- 获取创建对象,通常用于新建数据库条目 ---
|
|
@@ -290,6 +298,8 @@ export default class Mod {
|
|
|
290
298
|
/** --- 通过 key 字段获取,默认为 false,即从主键获取 --- */
|
|
291
299
|
'key'?: boolean;
|
|
292
300
|
'hint'?: string;
|
|
301
|
+
/** --- 是否优先使用只读从库,默认 false(走主库);加锁或事务内仍走主库 --- */
|
|
302
|
+
'read'?: boolean;
|
|
293
303
|
}): Promise<false | null | (T & Record<string, any>)>;
|
|
294
304
|
/** --- 按条件读取一条原生记录,查询失败返回 false,未找到返回 null --- */
|
|
295
305
|
static one(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: IModOneOptions & {
|
|
@@ -323,6 +333,7 @@ export default class Mod {
|
|
|
323
333
|
'ctr'?: sCtr.Ctr;
|
|
324
334
|
'pre'?: string;
|
|
325
335
|
'index'?: string;
|
|
336
|
+
'read'?: boolean;
|
|
326
337
|
}): Promise<any[] | false>;
|
|
327
338
|
/**
|
|
328
339
|
* --- 将模型字典转换为普通对象字典,沿用原有键 ---
|
|
@@ -388,6 +399,11 @@ export default class Mod {
|
|
|
388
399
|
* @returns 是否成功,缺少定位字段、记录不存在或删除失败返回 false
|
|
389
400
|
*/
|
|
390
401
|
remove(): Promise<boolean>;
|
|
402
|
+
/**
|
|
403
|
+
* --- 设置当前查询是否优先使用只读从库(加锁查询或事务内仍走主库) ---
|
|
404
|
+
* @param enable 是否使用只读从库,默认 true
|
|
405
|
+
*/
|
|
406
|
+
read(enable?: boolean): this;
|
|
391
407
|
/** --- 获取第一条原生记录,查询失败返回 false,未找到返回 null --- */
|
|
392
408
|
first(lock: boolean, array: true): Promise<false | null | TModRow>;
|
|
393
409
|
/** --- 获取第一个模型实例,查询失败返回 false,未找到返回 null --- */
|