@maiyunnet/kebab 9.29.5 → 9.29.6

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/sys/mod.d.ts CHANGED
@@ -11,8 +11,22 @@ import * as kebab from '#kebab/index.js';
11
11
  export type TOnlyProperties<T> = {
12
12
  [K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: T[K];
13
13
  };
14
+ /** --- 由 SELECT 决定字段的原生查询行 --- */
15
+ export type TModRow = Record<string, kebab.Json>;
16
+ /** --- 按条件查询单条记录的选项 --- */
17
+ export interface IModOneOptions {
18
+ 'ctr'?: sCtr.Ctr;
19
+ /** --- 加锁时确保 WHERE 条件命中索引,以免锁定过多记录 --- */
20
+ 'lock'?: boolean;
21
+ /** --- MySQL 表前缀或 PostgreSQL Schema 名 --- */
22
+ 'pre'?: string;
23
+ 'index'?: string | string[];
24
+ 'select'?: string | string[];
25
+ 'by'?: [string | string[], 'DESC' | 'ASC'];
26
+ 'hint'?: string;
27
+ }
14
28
  /** --- 一批游标查询结果,hasMore 表示扫描方向上还有记录 --- */
15
- export interface ICursorPage<T extends Record<string, unknown> = Record<string, unknown>> {
29
+ export interface ICursorPage<T extends object = Record<string, unknown>> {
16
30
  'list': T[];
17
31
  'hasMore': boolean;
18
32
  'next': lSql.TCursorValue[] | null;
@@ -26,8 +40,10 @@ export declare class Rows<T extends Mod> implements IRows<T> {
26
40
  get length(): number;
27
41
  /** --- 通过索引获取一个对象 --- */
28
42
  item(index: number): T;
29
- /** --- 转换为数组对象,获取的是新创建的数组 --- */
30
- toArray(): Array<Record<string, any>>;
43
+ /** --- 将模型转换为普通对象数组 --- */
44
+ toArray(): TModRow[];
45
+ /** --- 将模型转换为指定行类型的普通对象数组 --- */
46
+ toArray<TRow extends object>(): TRow[];
31
47
  /** --- 根据规则筛掉项,predicate 返回 true 代表保留 --- */
32
48
  filter(predicate: (value: T, index: number) => boolean): Rows<T>;
33
49
  /** --- 重塑对象内容为数组 --- */
@@ -227,7 +243,7 @@ export default class Mod {
227
243
  'list': string[];
228
244
  };
229
245
  'hint'?: string;
230
- }): T & Record<string, any>;
246
+ }): T;
231
247
  /**
232
248
  * --- 通过 where 条件获取模型 ---
233
249
  * @param db 数据库对象
@@ -244,7 +260,7 @@ export default class Mod {
244
260
  'list': string[];
245
261
  };
246
262
  'hint'?: string;
247
- }): T & Record<string, any>;
263
+ }): T;
248
264
  /**
249
265
  * --- 获取创建对象,通常用于新建数据库条目 ---
250
266
  * @param db 数据库对象
@@ -270,40 +286,28 @@ export default class Mod {
270
286
  'key'?: boolean;
271
287
  'hint'?: string;
272
288
  }): Promise<false | null | (T & Record<string, any>)>;
273
- static one(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: {
274
- 'ctr'?: sCtr.Ctr;
275
- 'lock'?: boolean;
276
- 'pre'?: string;
277
- 'index'?: string | string[];
278
- 'select'?: string | string[];
279
- 'by'?: [string | string[], 'DESC' | 'ASC'];
280
- 'hint'?: string;
289
+ /** --- 按条件读取一条原生记录,查询失败返回 false,未找到返回 null --- */
290
+ static one(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: IModOneOptions & {
281
291
  'array': true;
282
- }): Promise<false | null | Record<string, any>>;
283
- static one<T extends Mod>(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: {
284
- 'ctr'?: sCtr.Ctr;
285
- 'lock'?: boolean;
286
- 'pre'?: string;
287
- 'index'?: string | string[];
288
- 'select'?: string | string[];
289
- 'by'?: [string | string[], 'DESC' | 'ASC'];
290
- 'hint'?: string;
292
+ }): Promise<false | null | TModRow>;
293
+ /** --- 按条件读取一个模型实例,查询失败返回 false,未找到返回 null --- */
294
+ static one<T extends Mod>(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: IModOneOptions & {
291
295
  'array'?: false;
292
- }): Promise<false | null | (T & Record<string, any>)>;
296
+ }): Promise<false | null | (T & TModRow)>;
297
+ /** --- 按条件读取一条指定类型的原生记录,查询失败返回 false,未找到返回 null --- */
298
+ static one<TRow extends object>(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt: IModOneOptions & {
299
+ 'array': true;
300
+ }): Promise<false | null | TRow>;
293
301
  /**
294
302
  * --- 通过 where 条件筛选单条数据返回原生对象 ---
295
303
  * @param db 数据库对象
296
304
  * @param s 筛选条件数组或字符串
297
- * @param opt 选项(opt.pre: MySQL 表前缀/PostgreSQL Schema 名)
305
+ * @param opt 查询和加锁选项
306
+ * @returns 原生对象,查询失败返回 false,未找到返回 null
298
307
  */
299
- static oneArray(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: {
300
- 'ctr'?: sCtr.Ctr;
301
- 'lock'?: boolean;
302
- 'pre'?: string;
303
- 'index'?: string | string[];
304
- 'select'?: string | string[];
305
- 'hint'?: string;
306
- }): Promise<false | null | Record<string, any>>;
308
+ static oneArray(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: Omit<IModOneOptions, 'by'>): Promise<false | null | TModRow>;
309
+ /** --- 按条件读取一条指定类型的原生记录,查询失败返回 false,未找到返回 null --- */
310
+ static oneArray<TRow extends object>(db: lDb.Pool | lDb.Transaction, s: string | kebab.Json, opt?: Omit<IModOneOptions, 'by'>): Promise<false | null | TRow>;
307
311
  /**
308
312
  * --- 根据 where 条件获取主键值列表 ---
309
313
  * @param db 数据库对象
@@ -316,10 +320,13 @@ export default class Mod {
316
320
  'index'?: string;
317
321
  }): Promise<any[] | false>;
318
322
  /**
319
- * --- 将 key val 组成的数据列表转换为原生对象模式,获取的是新创建的数组 ---
320
- * @param obj 要转换的 kv 数据列表
323
+ * --- 将模型字典转换为普通对象字典,沿用原有键 ---
324
+ * @param obj 要转换的模型字典
325
+ * @returns 转换后的普通对象字典
321
326
  */
322
- static toArrayByRecord<T extends Mod>(obj: Record<string, T>): Record<string, Record<string, any>>;
327
+ static toArrayByRecord<T extends Mod>(obj: Record<string, T>): Record<string, TModRow>;
328
+ /** --- 将模型字典转换为指定行类型的普通对象字典 --- */
329
+ static toArrayByRecord<TRow extends object>(obj: Record<string, Mod>): Record<string, TRow>;
323
330
  /**
324
331
  * --- 构造包含主键和分区字段的 WHERE 条件 ---
325
332
  * @returns 主键及分区字段条件,缺少字段时返回 false
@@ -334,10 +341,13 @@ export default class Mod {
334
341
  set<T extends this, TK extends keyof T>(n: Record<TK, T[TK] | undefined>): void;
335
342
  set<T extends this, TK extends keyof T>(n: TK, v: T[TK]): void;
336
343
  /**
337
- * --- 获取一个字段值
344
+ * --- 获取一个已加载的字段值 ---
338
345
  * @param n 字段名
346
+ * @returns 字段值,未加载时返回 undefined
339
347
  */
340
- get(n: string): any;
348
+ get(n: string): kebab.Json;
349
+ /** --- 按指定类型获取一个已加载的字段值,未加载时返回 undefined --- */
350
+ get<T>(n: string): T | undefined;
341
351
  /**
342
352
  * --- 收集待写字段,保留模型内的原始 JSON 值 ---
343
353
  * @returns 独立的 SQL 数据对象
@@ -373,13 +383,32 @@ export default class Mod {
373
383
  * @returns 是否成功,缺少定位字段、记录不存在或删除失败返回 false
374
384
  */
375
385
  remove(): Promise<boolean>;
376
- first(lock: boolean, array: true): Promise<false | null | Record<string, any>>;
377
- first(lock?: boolean, array?: false): Promise<false | null | (this & Record<string, any>)>;
386
+ /** --- 获取第一条原生记录,查询失败返回 false,未找到返回 null --- */
387
+ first(lock: boolean, array: true): Promise<false | null | TModRow>;
388
+ /** --- 获取第一个模型实例,查询失败返回 false,未找到返回 null --- */
389
+ first(lock?: boolean, array?: false): Promise<false | null | (this & TModRow)>;
390
+ /**
391
+ * --- 获取指定类型的第一条原生记录 ---
392
+ * @param lock 是否加锁
393
+ * @param array 传入 true 返回原生记录
394
+ * @returns 原生记录,查询失败返回 false,未找到返回 null
395
+ */
396
+ first<TRow extends object>(lock: boolean, array: true): Promise<false | null | TRow>;
397
+ /**
398
+ * --- 获取第一个模型实例,并为查询附加的字段指定类型 ---
399
+ * @param lock 是否加锁
400
+ * @param array 省略或传入 false 返回模型实例
401
+ * @returns 模型实例,查询失败返回 false,未找到返回 null
402
+ */
403
+ first<TExtra extends object>(lock?: boolean, array?: false): Promise<false | null | (this & TExtra)>;
378
404
  /**
379
405
  * --- 获取数据库第一个原生对象 ---
380
406
  * @param lock 是否加锁
407
+ * @returns 原生对象,查询失败返回 false,未找到返回 null
381
408
  */
382
- firstArray(lock?: boolean): Promise<Record<string, any> | false | null>;
409
+ firstArray(lock?: boolean): Promise<false | null | TModRow>;
410
+ /** --- 获取指定类型的第一条原生记录,查询失败返回 false,未找到返回 null --- */
411
+ firstArray<TRow extends object>(lock?: boolean): Promise<false | null | TRow>;
383
412
  /**
384
413
  * --- 联合查询表数据 ---
385
414
  * @param f 要联合查询的表列表、单个表、sql 对象
@@ -391,10 +420,34 @@ export default class Mod {
391
420
  * @param f 要联合查询的表列表、单个表、sql 对象
392
421
  */
393
422
  unionAll(f: lSql.Sql | string | IModUnionItem | string[] | IModUnionItem[]): this;
394
- all(): Promise<false | Rows<this>>;
395
- all(key: string): Promise<false | Record<string, this>>;
396
- allArray(): Promise<false | Array<Record<string, any>>>;
397
- allArray(key: string): Promise<false | Record<string, Record<string, any>>>;
423
+ /** --- 查询模型列表,查询失败返回 false --- */
424
+ all(): Promise<false | Rows<this & TModRow>>;
425
+ /**
426
+ * --- 查询模型字典,查询失败返回 false ---
427
+ * @param key 用作字典键的结果字段
428
+ */
429
+ all(key: string): Promise<false | Record<string, this & TModRow>>;
430
+ /** --- 查询模型列表,并为查询附加的字段指定类型,查询失败返回 false --- */
431
+ all<TExtra extends object>(): Promise<false | Rows<this & TExtra>>;
432
+ /**
433
+ * --- 查询模型字典,并为查询附加的字段指定类型,查询失败返回 false ---
434
+ * @param key 用作字典键的结果字段
435
+ */
436
+ all<TExtra extends object>(key: string): Promise<false | Record<string, this & TExtra>>;
437
+ /** --- 查询原生行列表,查询失败返回 false --- */
438
+ allArray(): Promise<false | TModRow[]>;
439
+ /**
440
+ * --- 查询原生行字典,查询失败返回 false ---
441
+ * @param key 用作字典键的结果字段
442
+ */
443
+ allArray(key: string): Promise<false | Record<string, TModRow>>;
444
+ /** --- 查询指定行类型的原生行列表,查询失败返回 false --- */
445
+ allArray<TRow extends object>(): Promise<false | TRow[]>;
446
+ /**
447
+ * --- 查询指定行类型的原生行字典,查询失败返回 false ---
448
+ * @param key 用作字典键的结果字段
449
+ */
450
+ allArray<TRow extends object>(key: string): Promise<false | Record<string, TRow>>;
398
451
  explain(all?: false): Promise<false | string>;
399
452
  explain(all: true): Promise<false | Record<string, any>>;
400
453
  /** --- 获取 contain 补查时使用的 where key(联表且无前缀时自动补主表前缀) --- */
@@ -422,7 +475,7 @@ export default class Mod {
422
475
  * @param opt.before 是否读取边界前一批,须提供 cursor
423
476
  * @returns 列表、首末行游标和扫描方向上的 hasMore,数据库错误返回 false
424
477
  */
425
- allCursor<T extends Record<string, unknown> = Record<string, unknown>>(count: number, opt: {
478
+ allCursor<T extends object = Record<string, unknown>>(count: number, opt: {
426
479
  'by': string | string[];
427
480
  'keys'?: string[];
428
481
  'order'?: 'DESC' | 'ASC';
@@ -576,10 +629,12 @@ export default class Mod {
576
629
  * @param data 数据
577
630
  */
578
631
  format(sql?: string, data?: any[]): string;
579
- /**
580
- * --- 获取值对象,获取的是新创建的数组 ---
581
- */
582
- toArray<TC extends abstract new (...args: any) => any>(): TOnlyProperties<InstanceType<TC>> & Record<string, any>;
632
+ /** --- 将已加载和已设置的字段复制为一个普通对象 --- */
633
+ toArray(): TModRow;
634
+ /** --- 将字段复制为普通对象,按指定构造器的实例属性提供类型 --- */
635
+ toArray<TC extends abstract new (...args: never[]) => object>(): TOnlyProperties<InstanceType<TC>> & TModRow;
636
+ /** --- 将字段复制为指定类型的普通对象 --- */
637
+ toArray<TRow extends object>(): TRow;
583
638
  /**
584
639
  * --- 获取当前设置要提交的数据 ---
585
640
  */
package/sys/mod.js CHANGED
@@ -22,7 +22,6 @@ export class Rows {
22
22
  item(index) {
23
23
  return this._items[index];
24
24
  }
25
- /** --- 转换为数组对象,获取的是新创建的数组 --- */
26
25
  toArray() {
27
26
  return this._items.map(i => i.toArray());
28
27
  }
@@ -464,12 +463,6 @@ export default class Mod {
464
463
  'hint': opt.hint,
465
464
  }).first(opt.lock);
466
465
  }
467
- /**
468
- * --- 通过 where 条件筛选单条数据 ---
469
- * @param db 数据库对象
470
- * @param s 筛选条件数组或字符串
471
- * @param opt 选项(opt.pre: MySQL 表前缀/PostgreSQL Schema 名;lock 需确保 where 条件命中索引,否则可能退化为表锁)
472
- */
473
466
  static async one(db, s, opt = {}) {
474
467
  if (!opt.index) {
475
468
  // --- 无 index ---
@@ -511,15 +504,10 @@ export default class Mod {
511
504
  }
512
505
  return null;
513
506
  }
514
- /**
515
- * --- 通过 where 条件筛选单条数据返回原生对象 ---
516
- * @param db 数据库对象
517
- * @param s 筛选条件数组或字符串
518
- * @param opt 选项(opt.pre: MySQL 表前缀/PostgreSQL Schema 名)
519
- */
520
507
  static async oneArray(db, s, opt = {}) {
521
- opt.array = true;
522
- return this.one(db, s, opt);
508
+ const queryOptions = opt;
509
+ queryOptions.array = true;
510
+ return this.one(db, s, queryOptions);
523
511
  }
524
512
  /**
525
513
  * --- 根据 where 条件获取主键值列表 ---
@@ -545,10 +533,6 @@ export default class Mod {
545
533
  }
546
534
  return primarys;
547
535
  }
548
- /**
549
- * --- 将 key val 组成的数据列表转换为原生对象模式,获取的是新创建的数组 ---
550
- * @param obj 要转换的 kv 数据列表
551
- */
552
536
  static toArrayByRecord(obj) {
553
537
  const rtn = {};
554
538
  for (const key in obj) {
@@ -622,10 +606,6 @@ export default class Mod {
622
606
  this._setProperty(n, v);
623
607
  }
624
608
  }
625
- /**
626
- * --- 获取一个字段值
627
- * @param n 字段名
628
- */
629
609
  get(n) {
630
610
  return this._data[n];
631
611
  }
@@ -895,11 +875,6 @@ export default class Mod {
895
875
  return false;
896
876
  }
897
877
  }
898
- /**
899
- * --- 获取数据库第一个对象 ---
900
- * @param lock 是否加锁
901
- * @param array 是否返回原生对象
902
- */
903
878
  async first(lock = false, array = false) {
904
879
  this._sql.limit(1);
905
880
  if (lock) {
@@ -923,10 +898,6 @@ export default class Mod {
923
898
  }
924
899
  return this;
925
900
  }
926
- /**
927
- * --- 获取数据库第一个原生对象 ---
928
- * @param lock 是否加锁
929
- */
930
901
  async firstArray(lock = false) {
931
902
  return this.first(lock, true);
932
903
  }
@@ -989,10 +960,6 @@ export default class Mod {
989
960
  }
990
961
  return this;
991
962
  }
992
- /**
993
- * --- 获取列表 ---
994
- * @param key 是否以某个字段为主键
995
- */
996
963
  async all(key) {
997
964
  this._total.length = 0;
998
965
  if (this._index && this._index.length > 1) {
@@ -1153,10 +1120,6 @@ export default class Mod {
1153
1120
  }
1154
1121
  return new Rows(list);
1155
1122
  }
1156
- /**
1157
- * --- 获取列表(得到的为原生对象或数组,不是模型) ---
1158
- * @param key 是否以某个字段为主键
1159
- */
1160
1123
  async allArray(key) {
1161
1124
  this._total.length = 0;
1162
1125
  if (this._index && this._index.length > 1) {
@@ -1648,9 +1611,6 @@ export default class Mod {
1648
1611
  format(sql, data) {
1649
1612
  return this._sql.format(sql, data);
1650
1613
  }
1651
- /**
1652
- * --- 获取值对象,获取的是新创建的数组 ---
1653
- */
1654
1614
  toArray() {
1655
1615
  return { ...this._data };
1656
1616
  }