imodel 0.16.2 → 0.17.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.mts CHANGED
@@ -1,14 +1,14 @@
1
1
  /*!
2
- * imodel v0.16.2
2
+ * imodel v0.17.1
3
3
  * (c) 2019-2026 undefined
4
4
  * @license undefined
5
5
  */
6
6
 
7
7
  declare const now: unique symbol;
8
- /** @type {typeof now} now */
9
- declare const uuid: symbol;
10
- /** @type {typeof uuid} uuid */
11
- declare const increment$1: typeof uuid;
8
+ /** @typeof {typeof now} now */
9
+ declare const uuid: unique symbol;
10
+ /** @typeof {typeof uuid} uuid */
11
+ declare const increment$1: unique symbol;
12
12
  declare const decrement$1: unique symbol;
13
13
  declare const multiply$1: unique symbol;
14
14
  declare const divide$1: unique symbol;
@@ -21,878 +21,16 @@ declare namespace values {
21
21
  }
22
22
 
23
23
  /**
24
- * @param {WhereValue[]?} [items]
25
- * @returns {WhereValue[]?}
26
- */
27
- declare function toNot(items?: WhereValue[] | null): WhereValue[] | null;
28
- type WhereLike = {
29
- toWhereValue(): WhereValue[][];
30
- };
31
- type Wheres = WhereLike | Record<string, any> | (WhereLike | Record<string, any>)[];
32
- type WhereItem = {
33
- where?: null | undefined;
34
- or?: null | undefined;
35
- field: string | {
36
- table?: string;
37
- field: string;
38
- } | (string | {
39
- table?: string;
40
- field: string;
41
- })[];
42
- value: any;
43
- not?: boolean | undefined;
44
- operator?: string | null | undefined;
45
- };
46
- type WhereRaw = {
47
- field?: null | undefined;
48
- or?: null | undefined;
49
- where: readonly string[];
50
- values: readonly any[];
51
- not?: boolean | undefined;
52
- };
53
- type WhereOr = {
54
- where?: null | undefined;
55
- field?: null | undefined;
56
- or: WhereValue[][];
57
- not?: boolean | undefined;
58
- };
59
- type WhereValue = WhereItem | WhereRaw | WhereOr;
60
- declare class Where {
61
- /**
62
- * @overload
63
- * @param {Wheres} matches
64
- * @returns {WhereValue[]?}
65
- */
66
- static parse(matches: Wheres): WhereValue[] | null;
67
- /**
68
- * @overload
69
- * @param {string | string[]} field
70
- * @param {any} value
71
- * @returns {WhereValue[]?}
72
- */
73
- static parse(field: string | string[], value: any): WhereValue[] | null;
74
- /**
75
- * @overload
76
- * @param {string | string[]} field
77
- * @param {string} operator
78
- * @param {any} value
79
- * @param {boolean} [not]
80
- * @returns {WhereValue[]?}
81
- */
82
- static parse(field: string | string[], operator: string, value: any, not?: boolean | undefined): WhereValue[] | null;
83
- /**
84
- * @overload
85
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
86
- * @returns {WhereValue[]?}
87
- */
88
- static parse(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): WhereValue[] | null;
89
- /**
90
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
91
- * @returns {WhereValue[]?}
92
- */
93
- static parseParams(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): WhereValue[] | null;
94
- /**
95
- * @overload
96
- * @param {Wheres} matches
97
- * @returns {Where}
98
- */
99
- static and(matches: Wheres): Where;
100
- /**
101
- *
102
- * @overload
103
- * @param {TemplateStringsArray} where
104
- * @param {...any} values
105
- * @returns {Where}
106
- */
107
- static and(where: TemplateStringsArray, ...values: any[]): Where;
108
- /**
109
- * @overload
110
- * @param {string} field
111
- * @param {any} value
112
- * @returns {Where}
113
- */
114
- static and(field: string, value: any): Where;
115
- /**
116
- * @overload
117
- * @param {string[]} field
118
- * @param {any[]} value
119
- * @returns {Where}
120
- */
121
- static and(field: string[], value: any[]): Where;
122
- /**
123
- * @overload
124
- * @param {string} field
125
- * @param {string} operator
126
- * @param {any} value
127
- * @param {boolean} [not]
128
- * @returns {Where}
129
- */
130
- static and(field: string, operator: string, value: any, not?: boolean | undefined): Where;
131
- /**
132
- * @overload
133
- * @param {string[]} field
134
- * @param {string} operator
135
- * @param {any[]} value
136
- * @param {boolean} [not]
137
- * @returns {Where}
138
- */
139
- static and(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
140
- /**
141
- * @overload
142
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
143
- * @returns {Where}
144
- */
145
- static and(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
146
- /**
147
- * @overload
148
- * @param {Wheres} matches
149
- * @returns {Where}
150
- */
151
- static or(matches: Wheres): Where;
152
- /**
153
- *
154
- * @overload
155
- * @param {TemplateStringsArray} where
156
- * @param {...any} values
157
- * @returns {Where}
158
- */
159
- static or(where: TemplateStringsArray, ...values: any[]): Where;
160
- /**
161
- * @overload
162
- * @param {string} field
163
- * @param {any} value
164
- * @returns {Where}
165
- */
166
- static or(field: string, value: any): Where;
167
- /**
168
- * @overload
169
- * @param {string[]} field
170
- * @param {any[]} value
171
- * @returns {Where}
172
- */
173
- static or(field: string[], value: any[]): Where;
174
- /**
175
- * @overload
176
- * @param {string} field
177
- * @param {string} operator
178
- * @param {any} value
179
- * @param {boolean} [not]
180
- * @returns {Where}
181
- */
182
- static or(field: string, operator: string, value: any, not?: boolean | undefined): Where;
183
- /**
184
- * @overload
185
- * @param {string[]} field
186
- * @param {string} operator
187
- * @param {any[]} value
188
- * @param {boolean} [not]
189
- * @returns {Where}
190
- */
191
- static or(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
192
- /**
193
- * @overload
194
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
195
- * @returns {Where}
196
- */
197
- static or(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
198
- /**
199
- * @overload
200
- * @param {Wheres} matches
201
- * @returns {Where}
202
- */
203
- static not(matches: Wheres): Where;
204
- /**
205
- *
206
- * @overload
207
- * @param {TemplateStringsArray} where
208
- * @param {...any} values
209
- * @returns {Where}
210
- */
211
- static not(where: TemplateStringsArray, ...values: any[]): Where;
212
- /**
213
- * @overload
214
- * @param {string} field
215
- * @param {any} value
216
- * @returns {Where}
217
- */
218
- static not(field: string, value: any): Where;
219
- /**
220
- * @overload
221
- * @param {string[]} field
222
- * @param {any[]} value
223
- * @returns {Where}
224
- */
225
- static not(field: string[], value: any[]): Where;
226
- /**
227
- * @overload
228
- * @param {string} field
229
- * @param {string} operator
230
- * @param {any} value
231
- * @param {boolean} [not]
232
- * @returns {Where}
233
- */
234
- static not(field: string, operator: string, value: any, not?: boolean | undefined): Where;
235
- /**
236
- * @overload
237
- * @param {string[]} field
238
- * @param {string} operator
239
- * @param {any[]} value
240
- * @param {boolean} [not]
241
- * @returns {Where}
242
- */
243
- static not(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
244
- /**
245
- * @overload
246
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
247
- * @returns {Where}
248
- */
249
- static not(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
250
- /**
251
- * @overload
252
- * @param {Wheres} matches
253
- * @returns {Where}
254
- */
255
- static orNot(matches: Wheres): Where;
256
- /**
257
- *
258
- * @overload
259
- * @param {TemplateStringsArray} where
260
- * @param {...any} values
261
- * @returns {Where}
262
- */
263
- static orNot(where: TemplateStringsArray, ...values: any[]): Where;
264
- /**
265
- * @overload
266
- * @param {string} field
267
- * @param {any} value
268
- * @returns {Where}
269
- */
270
- static orNot(field: string, value: any): Where;
271
- /**
272
- * @overload
273
- * @param {string[]} field
274
- * @param {any[]} value
275
- * @returns {Where}
276
- */
277
- static orNot(field: string[], value: any[]): Where;
278
- /**
279
- * @overload
280
- * @param {string} field
281
- * @param {string} operator
282
- * @param {any} value
283
- * @param {boolean} [not]
284
- * @returns {Where}
285
- */
286
- static orNot(field: string, operator: string, value: any, not?: boolean | undefined): Where;
287
- /**
288
- * @overload
289
- * @param {string[]} field
290
- * @param {string} operator
291
- * @param {any[]} value
292
- * @param {boolean} [not]
293
- * @returns {Where}
294
- */
295
- static orNot(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
296
- /**
297
- * @overload
298
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
299
- * @returns {Where}
300
- */
301
- static orNot(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
302
- toWhereValue(): WhereValue[][];
303
- /**
304
- * @overload
305
- * @param {Wheres} matches
306
- * @returns {this}
307
- */
308
- and(matches: Wheres): this;
309
- /**
310
- *
311
- * @overload
312
- * @param {TemplateStringsArray} where
313
- * @param {...any} values
314
- * @returns {this}
315
- */
316
- and(where: TemplateStringsArray, ...values: any[]): this;
317
- /**
318
- * @overload
319
- * @param {string} field
320
- * @param {any} value
321
- * @returns {this}
322
- */
323
- and(field: string, value: any): this;
324
- /**
325
- * @overload
326
- * @param {string[]} field
327
- * @param {any[]} value
328
- * @returns {this}
329
- */
330
- and(field: string[], value: any[]): this;
331
- /**
332
- * @overload
333
- * @param {string} field
334
- * @param {string} operator
335
- * @param {any} value
336
- * @param {boolean} [not]
337
- * @returns {this}
338
- */
339
- and(field: string, operator: string, value: any, not?: boolean | undefined): this;
340
- /**
341
- * @overload
342
- * @param {string[]} field
343
- * @param {string} operator
344
- * @param {any[]} value
345
- * @param {boolean} [not]
346
- * @returns {this}
347
- */
348
- and(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
349
- /**
350
- * @overload
351
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
352
- * @returns {this}
353
- */
354
- and(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
355
- /**
356
- * @overload
357
- * @param {Wheres} matches
358
- * @returns {this}
359
- */
360
- or(matches: Wheres): this;
361
- /**
362
- *
363
- * @overload
364
- * @param {TemplateStringsArray} where
365
- * @param {...any} values
366
- * @returns {this}
367
- */
368
- or(where: TemplateStringsArray, ...values: any[]): this;
369
- /**
370
- * @overload
371
- * @param {string} field
372
- * @param {any} value
373
- * @returns {this}
374
- */
375
- or(field: string, value: any): this;
376
- /**
377
- * @overload
378
- * @param {string[]} field
379
- * @param {any[]} value
380
- * @returns {this}
381
- */
382
- or(field: string[], value: any[]): this;
383
- /**
384
- * @overload
385
- * @param {string} field
386
- * @param {string} operator
387
- * @param {any} value
388
- * @param {boolean} [not]
389
- * @returns {this}
390
- */
391
- or(field: string, operator: string, value: any, not?: boolean | undefined): this;
392
- /**
393
- * @overload
394
- * @param {string[]} field
395
- * @param {string} operator
396
- * @param {any[]} value
397
- * @param {boolean} [not]
398
- * @returns {this}
399
- */
400
- or(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
401
- /**
402
- * @overload
403
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
404
- * @returns {this}
405
- */
406
- or(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
407
- /**
408
- * @overload
409
- * @param {Wheres} matches
410
- * @returns {this}
411
- */
412
- not(matches: Wheres): this;
413
- /**
414
- *
415
- * @overload
416
- * @param {TemplateStringsArray} where
417
- * @param {...any} values
418
- * @returns {this}
419
- */
420
- not(where: TemplateStringsArray, ...values: any[]): this;
421
- /**
422
- * @overload
423
- * @param {string} field
424
- * @param {any} value
425
- * @returns {this}
426
- */
427
- not(field: string, value: any): this;
428
- /**
429
- * @overload
430
- * @param {string[]} field
431
- * @param {any[]} value
432
- * @returns {this}
433
- */
434
- not(field: string[], value: any[]): this;
435
- /**
436
- * @overload
437
- * @param {string} field
438
- * @param {string} operator
439
- * @param {any} value
440
- * @param {boolean} [not]
441
- * @returns {this}
442
- */
443
- not(field: string, operator: string, value: any, not?: boolean | undefined): this;
444
- /**
445
- * @overload
446
- * @param {string[]} field
447
- * @param {string} operator
448
- * @param {any[]} value
449
- * @param {boolean} [not]
450
- * @returns {this}
451
- */
452
- not(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
453
- /**
454
- * @overload
455
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
456
- * @returns {this}
457
- */
458
- not(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
459
- /**
460
- * @overload
461
- * @param {Wheres} matches
462
- * @returns {this}
463
- */
464
- orNot(matches: Wheres): this;
465
- /**
466
- *
467
- * @overload
468
- * @param {TemplateStringsArray} where
469
- * @param {...any} values
470
- * @returns {this}
471
- */
472
- orNot(where: TemplateStringsArray, ...values: any[]): this;
473
- /**
474
- * @overload
475
- * @param {string} field
476
- * @param {any} value
477
- * @returns {this}
478
- */
479
- orNot(field: string, value: any): this;
480
- /**
481
- * @overload
482
- * @param {string[]} field
483
- * @param {any[]} value
484
- * @returns {this}
485
- */
486
- orNot(field: string[], value: any[]): this;
487
- /**
488
- * @overload
489
- * @param {string} field
490
- * @param {string} operator
491
- * @param {any} value
492
- * @param {boolean} [not]
493
- * @returns {this}
494
- */
495
- orNot(field: string, operator: string, value: any, not?: boolean | undefined): this;
496
- /**
497
- * @overload
498
- * @param {string[]} field
499
- * @param {string} operator
500
- * @param {any[]} value
501
- * @param {boolean} [not]
502
- * @returns {this}
503
- */
504
- orNot(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
505
- /**
506
- * @overload
507
- * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
508
- * @returns {this}
509
- */
510
- orNot(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
511
- #private;
512
- }
513
-
514
- interface Environment<T extends object> {
515
- transaction: T | null;
516
- isDevelopment: boolean;
517
- values: typeof values;
518
- }
519
-
520
- /** @typedef {SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | SetValue.Coefficient | string | number | bigint | boolean | null | typeof now | typeof uuid | (string | number | bigint | boolean)[]} SetValue */
521
- /** @typedef {{[values.increment]: number | bigint}} SetValue.Increment */
522
- /** @typedef {{[values.decrement]: number | bigint}} SetValue.Decrement */
523
- /** @typedef {{[values.multiply]: number | bigint}} SetValue.Multiply */
524
- /** @typedef {{[values.divide]: number | bigint}} SetValue.Divide */
525
- /** @typedef {{[values.coefficient]: (number | bigint)[]}} SetValue.Coefficient */
526
- /**
527
- * @param {number | bigint} value
528
- * @returns {SetValue.Increment}
529
- */
530
- declare function increment(value: number | bigint): SetValue.Increment;
531
- /**
532
- * @param {number | bigint} value
533
- * @returns {SetValue.Decrement}
534
- */
535
- declare function decrement(value: number | bigint): SetValue.Decrement;
536
- /**
537
- * @param {number | bigint} value
538
- * @returns {SetValue.Multiply}
539
- */
540
- declare function multiply(value: number | bigint): SetValue.Multiply;
541
- /**
542
- * @param {number | bigint} value
543
- * @returns {SetValue.Divide}
544
- */
545
- declare function divide(value: number | bigint): SetValue.Divide;
546
- /**
547
- * @param {...number | bigint} value
548
- * @returns {SetValue.Coefficient}
549
- */
550
- declare function coefficient(...value: (number | bigint)[]): SetValue.Coefficient;
551
- type SetValue = SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | SetValue.Coefficient | string | number | bigint | boolean | null | typeof now | typeof uuid | (string | number | bigint | boolean)[];
552
- declare namespace SetValue {
553
- type Increment = {};
554
- type Decrement = {
555
- [decrement$1]: number | bigint;
556
- };
557
- type Multiply = {
558
- [multiply$1]: number | bigint;
559
- };
560
- type Divide = {
561
- [divide$1]: number | bigint;
562
- };
563
- type Coefficient = {
564
- [coefficient$1]: (number | bigint)[];
565
- };
566
- }
567
-
568
- interface VirtualTable<E extends object = object> {
569
- insert(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, data: object[], key: string[], ignoreConflict?: boolean): PromiseLike<any[]>;
570
- upsert(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, data: object[], key: string[], conflict: true | string[], conflictSet?: Record<string, SetValue>): PromiseLike<any[]>;
571
- update(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<number>;
572
- updateReturn(environment: Environment<E>, conn: IConnection<E>, returning: string[], columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<any[]>;
573
- updateMany(environment: Environment<E>, conn: IConnection<E>, fields: Fields<keyof FieldType>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[]): PromiseLike<number>;
574
- updateManyReturn(environment: Environment<E>, conn: IConnection<E>, fields: Fields<keyof FieldType>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[], returning: string[]): PromiseLike<any[]>;
575
- count(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, where: WhereValue[]): PromiseLike<number>;
576
- find(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, argv: FindArg): PromiseLike<any[]>;
577
- select(environment: Environment<E>, conn: IConnection<E>, select: SelectItem[]): PromiseLike<any[]>;
578
- delete(environment: Environment<E>, conn: IConnection<E>, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
579
- deleteReturn(environment: Environment<E>, conn: IConnection<E>, returning: string[], columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<any[]>;
580
- }
581
-
582
- /** 字段基本类型 */
583
- interface FieldType {
584
- i16: number;
585
- i32: number;
586
- i64: bigint;
587
- f32: number;
588
- f64: number;
589
- decimal: number;
590
- numeric: number;
591
- money: number;
592
- /** 文本类型(含字符串类型) */
593
- string: string;
594
- text: string;
595
- char: string;
596
- uuid: string;
597
- ip: string;
598
- ipv4: string;
599
- ipv6: string;
600
- ipnet: string;
601
- ipnetv4: string;
602
- ipnetv6: string;
603
- mac48: string;
604
- mac64: string;
605
- /** 布尔型 */
606
- bool: boolean;
607
- /** 日期类型 */
608
- date: Date;
609
- /** 时间类型 */
610
- time: Date;
611
- /** 时间类型 */
612
- timetz: Date;
613
- /** 日期时间类型 */
614
- datetime: Date;
615
- /** 时间戳类型 */
616
- timestamp: Date;
617
- interval: number | bigint;
618
- /** 对象类型 */
619
- object: Record<string, unknown>;
620
- /** JSON 类型 */
621
- json: any;
622
- }
623
- interface Constraint {
624
- noField?: boolean;
625
- field?: string;
626
- value?: any;
627
- writeOnly?: boolean;
628
- }
629
- type MainFieldType = keyof FieldType | TableDefine | null;
630
- type ToFieldType<T> = T extends keyof FieldType ? FieldType[T] : unknown;
631
- type FieldSpecificValue = (string | number | bigint | boolean)[] | string | number | bigint | boolean | null | typeof now | typeof uuid;
632
- type FieldSpecific = FieldSpecificValue | {
633
- value?: FieldSpecificValue;
634
- computed?(): PromiseLike<FieldSpecificValue> | FieldSpecificValue;
635
- };
636
- interface FieldDefineOption {
637
- column?: string;
638
- /** 缩放比 */
639
- scale?: number;
640
- /** 占用空间大小 */
641
- size?: number;
642
- /** 是否为主键 */
643
- primary?: number;
644
- /** 唯一索引? */
645
- unique?: boolean;
646
- /** 索引信息 */
647
- index?: string | boolean;
648
- /** 创建时,自定义数据 */
649
- creating?: FieldSpecific;
650
- /** 更新时,自定义数据 */
651
- updating?: FieldSpecific;
652
- /** 伪删除时,自定义数据 */
653
- deleting?: FieldSpecific;
654
- /** 是否为可创建 */
655
- uncreatable?: boolean;
656
- /** 是否为固定值 */
657
- immutable?: boolean;
658
- /** 固定值 */
659
- fixedValue?: any;
660
- /** 文本查询 */
661
- text?: boolean;
662
- /** 排序顺序,绝对值表示在排序列表中的顺序,负数表示逆序排序, 0 表示不排序 */
663
- sort?: number;
664
- constraints?: Record<string, Constraint>;
665
- }
666
- /** 字段类型信息 */
667
- interface FieldDefine<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> extends FieldTypeDefine<T, A, N>, FieldDefineOption {
668
- /** 默认值 */
669
- default?: ToType<T, A, N>;
670
- }
671
- interface FieldTypeDefine<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> {
672
- /** 字段的基本类型 */
673
- type: T;
674
- /** 是否可为空 */
675
- nullable: N;
676
- /** 是否为数组 */
677
- array: A;
678
- }
679
- type ToType<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> = N extends false ? A extends false ? ToFieldType<T> : A extends true ? ToFieldType<T>[] : ToFieldType<T> | ToFieldType<T>[] : A extends false ? ToFieldType<T> | null : A extends true ? ToFieldType<T>[] | null : ToFieldType<T> | ToFieldType<T>[] | null;
680
- type FieldDefineType<F extends FieldTypeDefine> = F extends FieldTypeDefine<infer T, infer A, infer N> ? ToType<T, A, N> : never;
681
- type Fields<T extends MainFieldType = MainFieldType> = Record<string, FieldDefine<T>>;
682
- type FieldValue<T extends Fields> = {
683
- [K in keyof T]: FieldDefineType<T[K]>;
684
- };
685
- interface IndexOptions {
686
- unique?: boolean;
687
- name?: string;
688
- includes?: string[];
689
- }
690
- interface IndexInfo extends IndexOptions {
691
- fields: string[];
692
- }
693
- interface Index extends IndexInfo {
694
- name: string;
695
- }
696
- interface Hooks<T extends Record<string, any> = Record<string, any>> {
697
- where(conn: Connection, model: TableDefine): PromiseLike<void | Where> | void;
698
- beforeCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void | Record<string, any>[]> | void | Record<string, any>[];
699
- afterCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void> | void;
700
- beforeCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
701
- afterCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void> | void;
702
- beforeUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, set: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
703
- afterUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, oldRecord: Record<string, any>): PromiseLike<void> | void;
704
- beforeDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
705
- afterDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
706
- }
707
- interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | undefined = string | VirtualTable | undefined> {
708
- /** 字段信息 */
709
- readonly fields: T;
710
- /** 表名 */
711
- readonly table: TT;
712
- /** 是否启用软删除 */
713
- readonly pseudo?: string;
714
- /** 索引信息 */
715
- readonly indexes?: readonly IndexInfo[];
716
- /** 钩子 */
717
- readonly hooks?: Partial<Hooks>;
718
- }
719
-
720
- type deleted = typeof deleted;
721
- declare const deleted: unique symbol;
722
- type undeleted = typeof undeleted;
723
- declare const undeleted: unique symbol;
724
- type withDeleted = typeof withDeleted;
725
- declare const withDeleted: unique symbol;
726
- type FindRange = deleted | undeleted | withDeleted;
727
-
728
- type Select = {
729
- field: string;
730
- table?: string;
731
- fn?: string;
732
- };
733
- declare namespace Select {
734
- type Param = boolean | string | Select;
735
- }
736
- type JoinType = 'left' | 'right' | 'inner' | 'full';
737
- interface Join<T extends TableDefine = TableDefine> {
738
- type: JoinType;
739
- table: T;
740
- /** 关联项目 { [this table field]: base table field } */
741
- on: {
742
- [k: string]: string;
743
- };
744
- }
745
- interface Options {
746
- range?: FindRange;
747
- select?: Record<string, Select> | null;
748
- /** 排序配置,true 表示逆序 */
749
- sort?: [field: string, desc: boolean][];
750
- /** 匹配信息 */
751
- where: WhereValue[];
752
- /** 偏移量 */
753
- offset?: number;
754
- /** 最大返回数 */
755
- limit?: number;
756
- update?: Record<string, SetValue>;
757
- alias?: string;
758
- group?: string[];
759
- }
760
- interface Queryable<T extends Fields = Fields> extends TableDefine<T> {
761
- readonly options: Options;
762
- }
763
-
764
- interface ColumnOptions<T = unknown> extends FieldDefineOption {
765
- /**
766
- * 是否可为空
767
- * @default true
768
- */
769
- nullable?: boolean;
770
- /** 是否为数组格式 */
771
- array?: boolean;
772
- /** 默认值 */
773
- default?: T extends never ? never : T;
774
- }
775
- interface Column<T> extends ColumnOptions<T> {
776
- column: string;
777
- type: string;
778
- }
779
-
780
- type TableType = '' | 'table' | 'view';
781
- interface FindArg {
782
- /** 排序配置,true 表示逆序 */
783
- sort?: [field: string, desc: boolean][];
784
- /** 匹配信息 */
785
- where: WhereValue[];
786
- select: [string, Select][];
787
- /** 偏移量 */
788
- offset?: number;
789
- /** 最大返回数 */
790
- limit?: number;
791
- }
792
- interface JOIN {
793
- type?: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL' | 'CROSS';
794
- table: string;
795
- columns: Record<string, DBColumn>;
796
- alias?: string;
797
- condition?: WhereValue[];
798
- }
799
- interface SelectItem {
800
- distinct?: boolean;
801
- select: [string, Select][];
802
- table: string;
803
- columns: Record<string, DBColumn>;
804
- alias?: string;
805
- /** 匹配信息 */
806
- where: WhereValue[];
807
- group: string[];
808
- having: WhereValue[];
809
- /** 排序配置,true 表示逆序 */
810
- sort: [field: string, desc: boolean][];
811
- /** 偏移量 */
812
- offset?: number;
813
- /** 最大返回数 */
814
- limit?: number;
815
- }
816
- type GetName = (table?: string) => string;
817
- interface DBColumn {
818
- type: string;
819
- size?: number;
820
- scale?: number;
821
- default?: any;
822
- nullable?: boolean;
823
- array?: boolean;
824
- primary?: number;
825
- }
826
- interface DBIndex {
827
- fields: string[];
828
- includes?: string[];
829
- unique?: boolean;
830
- name?: string;
831
- }
832
- interface DBTable {
833
- table: string;
834
- fields: Record<string, DBColumn>;
835
- indexes?: DBIndex[];
836
- primary?: string;
837
- }
838
- interface IConnection<E extends object = object> {
839
- /** 数据库类型 */
840
- readonly dbType: string;
841
- /** 数据库版本 */
842
- dbVersion(environment: Environment<E>): PromiseLike<string>;
843
- exec(environment: Environment<E>, ...values: any): Promise<number>;
844
- /**
845
- * 执行一条语句
846
- * @param sql 要执行的 SQL 语句
847
- */
848
- query(environment: Environment<E>, ...values: any): PromiseLike<any[]>;
849
- type(environment: Environment<E>, table: string): PromiseLike<TableType | null>;
850
- insert(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, data: object[], key: string[], ignoreConflict?: boolean): PromiseLike<any[]>;
851
- upsert(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, data: object[], key: string[], conflict: true | string[], conflictSet?: Record<string, SetValue>): PromiseLike<any[]>;
852
- update(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<number>;
853
- updateReturn(environment: Environment<E>, returning: string[], table: string, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<any[]>;
854
- updateMany(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[]): PromiseLike<number>;
855
- updateManyReturn(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[], returning: string[]): PromiseLike<any[]>;
856
- count(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
857
- find(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, argv: FindArg): PromiseLike<any[]>;
858
- select(environment: Environment<E>, select: SelectItem[]): PromiseLike<any[]>;
859
- delete(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
860
- deleteReturn(environment: Environment<E>, returning: string[], table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<any[]>;
861
- createTable(environment: Environment<E>, table: string, columns: Column<any>[]): PromiseLike<number>;
862
- dropTable(environment: Environment<E>, table: string): PromiseLike<number>;
863
- renameTable(environment: Environment<E>, table: string, newName: string): PromiseLike<number>;
864
- addColumn(environment: Environment<E>, table: string, column: string, type: string, options: ColumnOptions<any>): PromiseLike<number>;
865
- changeColumn(environment: Environment<E>, table: string, column: string, type: string, options: ColumnOptions<any>): PromiseLike<number>;
866
- changeColumnNull(environment: Environment<E>, table: string, column: string, nullable: boolean): PromiseLike<number>;
867
- dropColumn(environment: Environment<E>, table: string, column: string): PromiseLike<number>;
868
- renameColumn(environment: Environment<E>, table: string, column: string, newName: string): PromiseLike<number>;
869
- addIndex(environment: Environment<E>, table: string, name: string, columns: string[], options: IndexOptions): PromiseLike<number>;
870
- dropIndex(environment: Environment<E>, table: string, name: string): PromiseLike<number>;
871
- loadTables(environment: Environment<E>, tables: string[]): PromiseLike<DBTable[]>;
872
- syncTables(environment: Environment<E>, tables: DBTable[]): PromiseLike<void>;
873
- transaction<R>(environment: Environment<E>, fn: (t: E) => R | PromiseLike<R>): PromiseLike<R>;
874
- }
875
- interface Skip {
876
- uncreatable?: boolean;
877
- immutable?: boolean;
878
- specific?: boolean;
879
- where?: boolean;
880
- }
881
- interface TransactionFn {
882
- <T>(fn: (t: Connection) => PromiseLike<T> | T): Promise<T>;
883
- }
884
-
885
- /**
886
- * @template {keyof FieldType} T
887
- * @overload
888
- * @param {T} type
889
- * @param {{
890
- * nullable?: false;
891
- * array?: false;
892
- * default?: ToType<T, false, false> | symbol;
893
- * [k: string]: any;
894
- * } & FieldDefineOption} [options]
895
- * @returns {FieldDecorator<ToType<T, false, false>>}
24
+ * @template {keyof FieldType} T
25
+ * @overload
26
+ * @param {T} type
27
+ * @param {{
28
+ * nullable?: false;
29
+ * array?: false;
30
+ * default?: ToType<T, false, false> | symbol;
31
+ * [k: string]: any;
32
+ * } & FieldDefineOption} [options]
33
+ * @returns {FieldDecorator<ToType<T, false, false>>}
896
34
  */
897
35
  declare function field$1<T extends keyof FieldType>(type: T, options?: ({
898
36
  [k: string]: any;
@@ -1313,67 +451,937 @@ declare function model<T extends Record<string, any>>(type: TableDefine, constra
1313
451
  */
1314
452
  declare function model<T extends Record<string, any>, A extends boolean = boolean>(type: TableDefine, constraints: Record<string, Constraint>, array: A, options?: FieldDefineOption | undefined): FieldDecorator<A extends false ? T : A extends true ? T[] : T | T[]>;
1315
453
 
1316
- /**
1317
- * @param {number} [size]
1318
- * @returns {FieldDecorator<Date?>}
1319
- */
1320
- declare function creating(size?: number): FieldDecorator<Date | null>;
1321
- /**
1322
- * @param {number} [size]
1323
- * @returns {FieldDecorator<Date?>}
1324
- */
1325
- declare function updating(size?: number): FieldDecorator<Date | null>;
1326
- /**
1327
- * @param {number} [size]
1328
- * @returns {FieldDecorator<Date?>}
1329
- */
1330
- declare function deleting(size?: number): FieldDecorator<Date | null>;
454
+ /**
455
+ * @param {number} [size]
456
+ * @returns {FieldDecorator<Date?>}
457
+ */
458
+ declare function creating(size?: number): FieldDecorator<Date | null>;
459
+ /**
460
+ * @param {number} [size]
461
+ * @returns {FieldDecorator<Date?>}
462
+ */
463
+ declare function updating(size?: number): FieldDecorator<Date | null>;
464
+ /**
465
+ * @param {number} [size]
466
+ * @returns {FieldDecorator<Date?>}
467
+ */
468
+ declare function deleting(size?: number): FieldDecorator<Date | null>;
469
+
470
+ /** @type {Hook<Hooks['where']>} */
471
+ declare const where: Hook<Hooks["where"]>;
472
+ /** @type {Hook<Hooks['beforeCreateMany']>} */
473
+ declare const beforeCreateMany: Hook<Hooks["beforeCreateMany"]>;
474
+ /** @type {Hook<Hooks['afterCreateMany']>} */
475
+ declare const afterCreateMany: Hook<Hooks["afterCreateMany"]>;
476
+ /** @type {Hook<Hooks['beforeCreate']>} */
477
+ declare const beforeCreate: Hook<Hooks["beforeCreate"]>;
478
+ /** @type {Hook<Hooks['afterCreate']>} */
479
+ declare const afterCreate: Hook<Hooks["afterCreate"]>;
480
+ /** @type {Hook<Hooks['beforeUpdate']>} */
481
+ declare const beforeUpdate: Hook<Hooks["beforeUpdate"]>;
482
+ /** @type {Hook<Hooks['afterUpdate']>} */
483
+ declare const afterUpdate: Hook<Hooks["afterUpdate"]>;
484
+ /** @type {Hook<Hooks['beforeDelete']>} */
485
+ declare const beforeDelete: Hook<Hooks["beforeDelete"]>;
486
+ /** @type {Hook<Hooks['afterDelete']>} */
487
+ declare const afterDelete: Hook<Hooks["afterDelete"]>;
488
+
489
+ type ClassDecorator = (val: Function, ctx: ClassDecoratorContext) => any;
490
+ type FieldDecorator<T> = (val: {
491
+ get(): T;
492
+ set(v: T): void;
493
+ }, ctx: ClassAccessorDecoratorContext) => any;
494
+ type MethodDecorator<T extends Function> = (val: T, ctx: ClassMethodDecoratorContext<any, T>) => any;
495
+
496
+ interface Environment<T extends object> {
497
+ transaction: T | null;
498
+ isDevelopment: boolean;
499
+ values: typeof values;
500
+ }
501
+
502
+ /** 特性支持情况 */
503
+ interface Support {
504
+ /** 是否支持 sql 查询 */
505
+ sql?: boolean;
506
+ /** 是否支持数组类型 */
507
+ array?: boolean;
508
+ /** 是否支持对象存储 */
509
+ object?: boolean;
510
+ /** 是否支持递归查询 */
511
+ recursive?: boolean;
512
+ [key: string]: undefined | boolean;
513
+ }
514
+
515
+ interface ColumnOptions<T = unknown> extends FieldDefineOption {
516
+ /**
517
+ * 是否可为空
518
+ * @default true
519
+ */
520
+ nullable?: boolean;
521
+ /** 是否为数组格式 */
522
+ array?: boolean;
523
+ /** 默认值 */
524
+ default?: T extends never ? never : T;
525
+ }
526
+ interface Column<T> extends ColumnOptions<T> {
527
+ column: string;
528
+ type: string;
529
+ }
530
+
531
+ /** @typedef {SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | SetValue.Coefficient | string | number | bigint | boolean | null | typeof now | typeof uuid | (string | number | bigint | boolean)[]} SetValue */
532
+ /** @typedef {{[values.increment]: number | bigint}} SetValue.Increment */
533
+ /** @typedef {{[values.decrement]: number | bigint}} SetValue.Decrement */
534
+ /** @typedef {{[values.multiply]: number | bigint}} SetValue.Multiply */
535
+ /** @typedef {{[values.divide]: number | bigint}} SetValue.Divide */
536
+ /** @typedef {{[values.coefficient]: (number | bigint)[]}} SetValue.Coefficient */
537
+ /**
538
+ * @param {number | bigint} value
539
+ * @returns {SetValue.Increment}
540
+ */
541
+ declare function increment(value: number | bigint): SetValue.Increment;
542
+ /**
543
+ * @param {number | bigint} value
544
+ * @returns {SetValue.Decrement}
545
+ */
546
+ declare function decrement(value: number | bigint): SetValue.Decrement;
547
+ /**
548
+ * @param {number | bigint} value
549
+ * @returns {SetValue.Multiply}
550
+ */
551
+ declare function multiply(value: number | bigint): SetValue.Multiply;
552
+ /**
553
+ * @param {number | bigint} value
554
+ * @returns {SetValue.Divide}
555
+ */
556
+ declare function divide(value: number | bigint): SetValue.Divide;
557
+ /**
558
+ * @param {...number | bigint} value
559
+ * @returns {SetValue.Coefficient}
560
+ */
561
+ declare function coefficient(...value: (number | bigint)[]): SetValue.Coefficient;
562
+ type SetValue = SetValue.Increment | SetValue.Decrement | SetValue.Multiply | SetValue.Divide | SetValue.Coefficient | string | number | bigint | boolean | null | typeof now | typeof uuid | (string | number | bigint | boolean)[];
563
+ declare namespace SetValue {
564
+ type Increment = {
565
+ [increment$1]: number | bigint;
566
+ };
567
+ type Decrement = {
568
+ [decrement$1]: number | bigint;
569
+ };
570
+ type Multiply = {
571
+ [multiply$1]: number | bigint;
572
+ };
573
+ type Divide = {
574
+ [divide$1]: number | bigint;
575
+ };
576
+ type Coefficient = {
577
+ [coefficient$1]: (number | bigint)[];
578
+ };
579
+ }
580
+
581
+ interface VirtualTable<E extends object = object> {
582
+ insert(environment: Environment<E>, conn: TableConnection<E>, columns: Record<string, DBColumn>, data: object[], key: string[], ignoreConflict?: boolean): PromiseLike<any[]>;
583
+ upsert(environment: Environment<E>, conn: TableConnection<E>, columns: Record<string, DBColumn>, data: object[], key: string[], conflict: true | string[], conflictSet?: Record<string, SetValue>): PromiseLike<any[]>;
584
+ update(environment: Environment<E>, conn: TableConnection<E>, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<number>;
585
+ updateReturn(environment: Environment<E>, conn: TableConnection<E>, returning: string[], columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<any[]>;
586
+ updateMany(environment: Environment<E>, conn: TableConnection<E>, fields: Fields<keyof FieldType>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[]): PromiseLike<number>;
587
+ updateManyReturn(environment: Environment<E>, conn: TableConnection<E>, fields: Fields<keyof FieldType>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[], returning: string[]): PromiseLike<any[]>;
588
+ count(environment: Environment<E>, conn: TableConnection<E>, columns: Record<string, DBColumn>, where: WhereValue[]): PromiseLike<number>;
589
+ select(environment: Environment<E>, conn: TableConnection<E>, select: SelectItem[]): PromiseLike<any[]>;
590
+ delete(environment: Environment<E>, conn: TableConnection<E>, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
591
+ deleteReturn(environment: Environment<E>, conn: TableConnection<E>, returning: string[], columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<any[]>;
592
+ }
593
+
594
+ type MaybePromise<T> = PromiseLike<T> | T;
595
+
596
+ interface Hook<T extends Hooks[keyof Hooks]> {
597
+ (approach?: boolean): MethodDecorator<T>;
598
+ (hook: T, approach?: boolean): ClassDecorator;
599
+ (Model: Function, hook: T, approach?: boolean): void;
600
+ }
601
+
602
+ /**
603
+ * @param {WhereValue[]?} [items]
604
+ * @returns {WhereValue[]?}
605
+ */
606
+ declare function toNot(items?: WhereValue[] | null): WhereValue[] | null;
607
+ type WhereItem = {
608
+ where?: null | undefined;
609
+ or?: null | undefined;
610
+ field: string | {
611
+ table?: string;
612
+ field: string;
613
+ } | (string | {
614
+ table?: string;
615
+ field: string;
616
+ })[];
617
+ value: any;
618
+ subquery?: SelectItem[] | null | undefined;
619
+ not?: boolean | undefined;
620
+ operator?: string | null | undefined;
621
+ };
622
+ type WhereRaw = {
623
+ field?: null | undefined;
624
+ or?: null | undefined;
625
+ where: readonly string[];
626
+ values: readonly any[];
627
+ not?: boolean | undefined;
628
+ };
629
+ type WhereOr = {
630
+ where?: null | undefined;
631
+ field?: null | undefined;
632
+ or: WhereValue[][];
633
+ not?: boolean | undefined;
634
+ };
635
+ type WhereValue = WhereItem | WhereRaw | WhereOr;
636
+ type WhereLike = {
637
+ toWhereValue(): WhereValue[][];
638
+ };
639
+ type Wheres = WhereLike | Record<string, any> | (WhereLike | Record<string, any>)[];
640
+ declare class Where {
641
+ /**
642
+ * @overload
643
+ * @param {Wheres} matches
644
+ * @returns {WhereValue[]?}
645
+ */
646
+ static parse(matches: Wheres): WhereValue[] | null;
647
+ /**
648
+ * @overload
649
+ * @param {string | string[]} field
650
+ * @param {any} value
651
+ * @returns {WhereValue[]?}
652
+ */
653
+ static parse(field: string | string[], value: any): WhereValue[] | null;
654
+ /**
655
+ * @overload
656
+ * @param {string | string[]} field
657
+ * @param {string} operator
658
+ * @param {any} value
659
+ * @param {boolean} [not]
660
+ * @returns {WhereValue[]?}
661
+ */
662
+ static parse(field: string | string[], operator: string, value: any, not?: boolean | undefined): WhereValue[] | null;
663
+ /**
664
+ * @overload
665
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
666
+ * @returns {WhereValue[]?}
667
+ */
668
+ static parse(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): WhereValue[] | null;
669
+ /**
670
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
671
+ * @returns {WhereValue[]?}
672
+ */
673
+ static parseParams(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): WhereValue[] | null;
674
+ /**
675
+ * @overload
676
+ * @param {Wheres} matches
677
+ * @returns {Where}
678
+ */
679
+ static and(matches: Wheres): Where;
680
+ /**
681
+ *
682
+ * @overload
683
+ * @param {TemplateStringsArray} where
684
+ * @param {...any} values
685
+ * @returns {Where}
686
+ */
687
+ static and(where: TemplateStringsArray, ...values: any[]): Where;
688
+ /**
689
+ * @overload
690
+ * @param {string} field
691
+ * @param {any} value
692
+ * @returns {Where}
693
+ */
694
+ static and(field: string, value: any): Where;
695
+ /**
696
+ * @overload
697
+ * @param {string[]} field
698
+ * @param {any[]} value
699
+ * @returns {Where}
700
+ */
701
+ static and(field: string[], value: any[]): Where;
702
+ /**
703
+ * @overload
704
+ * @param {string} field
705
+ * @param {string} operator
706
+ * @param {any} value
707
+ * @param {boolean} [not]
708
+ * @returns {Where}
709
+ */
710
+ static and(field: string, operator: string, value: any, not?: boolean | undefined): Where;
711
+ /**
712
+ * @overload
713
+ * @param {string[]} field
714
+ * @param {string} operator
715
+ * @param {any[]} value
716
+ * @param {boolean} [not]
717
+ * @returns {Where}
718
+ */
719
+ static and(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
720
+ /**
721
+ * @overload
722
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
723
+ * @returns {Where}
724
+ */
725
+ static and(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
726
+ /**
727
+ * @overload
728
+ * @param {Wheres} matches
729
+ * @returns {Where}
730
+ */
731
+ static or(matches: Wheres): Where;
732
+ /**
733
+ *
734
+ * @overload
735
+ * @param {TemplateStringsArray} where
736
+ * @param {...any} values
737
+ * @returns {Where}
738
+ */
739
+ static or(where: TemplateStringsArray, ...values: any[]): Where;
740
+ /**
741
+ * @overload
742
+ * @param {string} field
743
+ * @param {any} value
744
+ * @returns {Where}
745
+ */
746
+ static or(field: string, value: any): Where;
747
+ /**
748
+ * @overload
749
+ * @param {string[]} field
750
+ * @param {any[]} value
751
+ * @returns {Where}
752
+ */
753
+ static or(field: string[], value: any[]): Where;
754
+ /**
755
+ * @overload
756
+ * @param {string} field
757
+ * @param {string} operator
758
+ * @param {any} value
759
+ * @param {boolean} [not]
760
+ * @returns {Where}
761
+ */
762
+ static or(field: string, operator: string, value: any, not?: boolean | undefined): Where;
763
+ /**
764
+ * @overload
765
+ * @param {string[]} field
766
+ * @param {string} operator
767
+ * @param {any[]} value
768
+ * @param {boolean} [not]
769
+ * @returns {Where}
770
+ */
771
+ static or(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
772
+ /**
773
+ * @overload
774
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
775
+ * @returns {Where}
776
+ */
777
+ static or(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
778
+ /**
779
+ * @overload
780
+ * @param {Wheres} matches
781
+ * @returns {Where}
782
+ */
783
+ static not(matches: Wheres): Where;
784
+ /**
785
+ *
786
+ * @overload
787
+ * @param {TemplateStringsArray} where
788
+ * @param {...any} values
789
+ * @returns {Where}
790
+ */
791
+ static not(where: TemplateStringsArray, ...values: any[]): Where;
792
+ /**
793
+ * @overload
794
+ * @param {string} field
795
+ * @param {any} value
796
+ * @returns {Where}
797
+ */
798
+ static not(field: string, value: any): Where;
799
+ /**
800
+ * @overload
801
+ * @param {string[]} field
802
+ * @param {any[]} value
803
+ * @returns {Where}
804
+ */
805
+ static not(field: string[], value: any[]): Where;
806
+ /**
807
+ * @overload
808
+ * @param {string} field
809
+ * @param {string} operator
810
+ * @param {any} value
811
+ * @param {boolean} [not]
812
+ * @returns {Where}
813
+ */
814
+ static not(field: string, operator: string, value: any, not?: boolean | undefined): Where;
815
+ /**
816
+ * @overload
817
+ * @param {string[]} field
818
+ * @param {string} operator
819
+ * @param {any[]} value
820
+ * @param {boolean} [not]
821
+ * @returns {Where}
822
+ */
823
+ static not(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
824
+ /**
825
+ * @overload
826
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
827
+ * @returns {Where}
828
+ */
829
+ static not(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
830
+ /**
831
+ * @overload
832
+ * @param {Wheres} matches
833
+ * @returns {Where}
834
+ */
835
+ static orNot(matches: Wheres): Where;
836
+ /**
837
+ *
838
+ * @overload
839
+ * @param {TemplateStringsArray} where
840
+ * @param {...any} values
841
+ * @returns {Where}
842
+ */
843
+ static orNot(where: TemplateStringsArray, ...values: any[]): Where;
844
+ /**
845
+ * @overload
846
+ * @param {string} field
847
+ * @param {any} value
848
+ * @returns {Where}
849
+ */
850
+ static orNot(field: string, value: any): Where;
851
+ /**
852
+ * @overload
853
+ * @param {string[]} field
854
+ * @param {any[]} value
855
+ * @returns {Where}
856
+ */
857
+ static orNot(field: string[], value: any[]): Where;
858
+ /**
859
+ * @overload
860
+ * @param {string} field
861
+ * @param {string} operator
862
+ * @param {any} value
863
+ * @param {boolean} [not]
864
+ * @returns {Where}
865
+ */
866
+ static orNot(field: string, operator: string, value: any, not?: boolean | undefined): Where;
867
+ /**
868
+ * @overload
869
+ * @param {string[]} field
870
+ * @param {string} operator
871
+ * @param {any[]} value
872
+ * @param {boolean} [not]
873
+ * @returns {Where}
874
+ */
875
+ static orNot(field: string[], operator: string, value: any[], not?: boolean | undefined): Where;
876
+ /**
877
+ * @overload
878
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
879
+ * @returns {Where}
880
+ */
881
+ static orNot(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): Where;
882
+ toWhereValue(): WhereValue[][];
883
+ /**
884
+ * @overload
885
+ * @param {Wheres} matches
886
+ * @returns {this}
887
+ */
888
+ and(matches: Wheres): this;
889
+ /**
890
+ *
891
+ * @overload
892
+ * @param {TemplateStringsArray} where
893
+ * @param {...any} values
894
+ * @returns {this}
895
+ */
896
+ and(where: TemplateStringsArray, ...values: any[]): this;
897
+ /**
898
+ * @overload
899
+ * @param {string} field
900
+ * @param {any} value
901
+ * @returns {this}
902
+ */
903
+ and(field: string, value: any): this;
904
+ /**
905
+ * @overload
906
+ * @param {string[]} field
907
+ * @param {any[]} value
908
+ * @returns {this}
909
+ */
910
+ and(field: string[], value: any[]): this;
911
+ /**
912
+ * @overload
913
+ * @param {string} field
914
+ * @param {string} operator
915
+ * @param {any} value
916
+ * @param {boolean} [not]
917
+ * @returns {this}
918
+ */
919
+ and(field: string, operator: string, value: any, not?: boolean | undefined): this;
920
+ /**
921
+ * @overload
922
+ * @param {string[]} field
923
+ * @param {string} operator
924
+ * @param {any[]} value
925
+ * @param {boolean} [not]
926
+ * @returns {this}
927
+ */
928
+ and(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
929
+ /**
930
+ * @overload
931
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
932
+ * @returns {this}
933
+ */
934
+ and(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
935
+ /**
936
+ * @overload
937
+ * @param {Wheres} matches
938
+ * @returns {this}
939
+ */
940
+ or(matches: Wheres): this;
941
+ /**
942
+ *
943
+ * @overload
944
+ * @param {TemplateStringsArray} where
945
+ * @param {...any} values
946
+ * @returns {this}
947
+ */
948
+ or(where: TemplateStringsArray, ...values: any[]): this;
949
+ /**
950
+ * @overload
951
+ * @param {string} field
952
+ * @param {any} value
953
+ * @returns {this}
954
+ */
955
+ or(field: string, value: any): this;
956
+ /**
957
+ * @overload
958
+ * @param {string[]} field
959
+ * @param {any[]} value
960
+ * @returns {this}
961
+ */
962
+ or(field: string[], value: any[]): this;
963
+ /**
964
+ * @overload
965
+ * @param {string} field
966
+ * @param {string} operator
967
+ * @param {any} value
968
+ * @param {boolean} [not]
969
+ * @returns {this}
970
+ */
971
+ or(field: string, operator: string, value: any, not?: boolean | undefined): this;
972
+ /**
973
+ * @overload
974
+ * @param {string[]} field
975
+ * @param {string} operator
976
+ * @param {any[]} value
977
+ * @param {boolean} [not]
978
+ * @returns {this}
979
+ */
980
+ or(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
981
+ /**
982
+ * @overload
983
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
984
+ * @returns {this}
985
+ */
986
+ or(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
987
+ /**
988
+ * @overload
989
+ * @param {Wheres} matches
990
+ * @returns {this}
991
+ */
992
+ not(matches: Wheres): this;
993
+ /**
994
+ *
995
+ * @overload
996
+ * @param {TemplateStringsArray} where
997
+ * @param {...any} values
998
+ * @returns {this}
999
+ */
1000
+ not(where: TemplateStringsArray, ...values: any[]): this;
1001
+ /**
1002
+ * @overload
1003
+ * @param {string} field
1004
+ * @param {any} value
1005
+ * @returns {this}
1006
+ */
1007
+ not(field: string, value: any): this;
1008
+ /**
1009
+ * @overload
1010
+ * @param {string[]} field
1011
+ * @param {any[]} value
1012
+ * @returns {this}
1013
+ */
1014
+ not(field: string[], value: any[]): this;
1015
+ /**
1016
+ * @overload
1017
+ * @param {string} field
1018
+ * @param {string} operator
1019
+ * @param {any} value
1020
+ * @param {boolean} [not]
1021
+ * @returns {this}
1022
+ */
1023
+ not(field: string, operator: string, value: any, not?: boolean | undefined): this;
1024
+ /**
1025
+ * @overload
1026
+ * @param {string[]} field
1027
+ * @param {string} operator
1028
+ * @param {any[]} value
1029
+ * @param {boolean} [not]
1030
+ * @returns {this}
1031
+ */
1032
+ not(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
1033
+ /**
1034
+ * @overload
1035
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
1036
+ * @returns {this}
1037
+ */
1038
+ not(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
1039
+ /**
1040
+ * @overload
1041
+ * @param {Wheres} matches
1042
+ * @returns {this}
1043
+ */
1044
+ orNot(matches: Wheres): this;
1045
+ /**
1046
+ *
1047
+ * @overload
1048
+ * @param {TemplateStringsArray} where
1049
+ * @param {...any} values
1050
+ * @returns {this}
1051
+ */
1052
+ orNot(where: TemplateStringsArray, ...values: any[]): this;
1053
+ /**
1054
+ * @overload
1055
+ * @param {string} field
1056
+ * @param {any} value
1057
+ * @returns {this}
1058
+ */
1059
+ orNot(field: string, value: any): this;
1060
+ /**
1061
+ * @overload
1062
+ * @param {string[]} field
1063
+ * @param {any[]} value
1064
+ * @returns {this}
1065
+ */
1066
+ orNot(field: string[], value: any[]): this;
1067
+ /**
1068
+ * @overload
1069
+ * @param {string} field
1070
+ * @param {string} operator
1071
+ * @param {any} value
1072
+ * @param {boolean} [not]
1073
+ * @returns {this}
1074
+ */
1075
+ orNot(field: string, operator: string, value: any, not?: boolean | undefined): this;
1076
+ /**
1077
+ * @overload
1078
+ * @param {string[]} field
1079
+ * @param {string} operator
1080
+ * @param {any[]} value
1081
+ * @param {boolean} [not]
1082
+ * @returns {this}
1083
+ */
1084
+ orNot(field: string[], operator: string, value: any[], not?: boolean | undefined): this;
1085
+ /**
1086
+ * @overload
1087
+ * @param {[Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]} items
1088
+ * @returns {this}
1089
+ */
1090
+ orNot(items: [Wheres] | [TemplateStringsArray, ...any] | [string | string[], any] | [string | string[], string, any, boolean?]): this;
1091
+ #private;
1092
+ }
1093
+
1094
+ /** 字段基本类型 */
1095
+ interface FieldType {
1096
+ i16: number;
1097
+ i32: number;
1098
+ i64: bigint;
1099
+ f32: number;
1100
+ f64: number;
1101
+ decimal: number;
1102
+ numeric: number;
1103
+ money: number;
1104
+ /** 文本类型(含字符串类型) */
1105
+ string: string;
1106
+ text: string;
1107
+ char: string;
1108
+ uuid: string;
1109
+ ip: string;
1110
+ ipv4: string;
1111
+ ipv6: string;
1112
+ ipnet: string;
1113
+ ipnetv4: string;
1114
+ ipnetv6: string;
1115
+ mac48: string;
1116
+ mac64: string;
1117
+ /** 布尔型 */
1118
+ bool: boolean;
1119
+ /** 日期类型 */
1120
+ date: Date;
1121
+ /** 时间类型 */
1122
+ time: Date;
1123
+ /** 时间类型 */
1124
+ timetz: Date;
1125
+ /** 日期时间类型 */
1126
+ datetime: Date;
1127
+ /** 时间戳类型 */
1128
+ timestamp: Date;
1129
+ interval: number | bigint;
1130
+ /** 对象类型 */
1131
+ object: Record<string, unknown>;
1132
+ /** JSON 类型 */
1133
+ json: any;
1134
+ }
1135
+ interface Constraint {
1136
+ noField?: boolean;
1137
+ field?: string;
1138
+ value?: any;
1139
+ writeOnly?: boolean;
1140
+ }
1141
+ type MainFieldType = keyof FieldType | TableDefine | null;
1142
+ type ToFieldType<T> = T extends keyof FieldType ? FieldType[T] : unknown;
1143
+ type FieldSpecificValue = (string | number | bigint | boolean)[] | string | number | bigint | boolean | null | typeof now | typeof uuid;
1144
+ type FieldSpecific = FieldSpecificValue | {
1145
+ value?: FieldSpecificValue;
1146
+ computed?(): PromiseLike<FieldSpecificValue> | FieldSpecificValue;
1147
+ };
1148
+ interface FieldDefineOption {
1149
+ column?: string;
1150
+ /** 缩放比 */
1151
+ scale?: number;
1152
+ /** 占用空间大小 */
1153
+ size?: number;
1154
+ /** 是否为主键 */
1155
+ primary?: number;
1156
+ /** 唯一索引? */
1157
+ unique?: boolean;
1158
+ /** 索引信息 */
1159
+ index?: string | boolean;
1160
+ /** 创建时,自定义数据 */
1161
+ creating?: FieldSpecific;
1162
+ /** 更新时,自定义数据 */
1163
+ updating?: FieldSpecific;
1164
+ /** 伪删除时,自定义数据 */
1165
+ deleting?: FieldSpecific;
1166
+ /** 是否为可创建 */
1167
+ uncreatable?: boolean;
1168
+ /** 是否为固定值 */
1169
+ immutable?: boolean;
1170
+ /** 固定值 */
1171
+ fixedValue?: any;
1172
+ /** 文本查询 */
1173
+ text?: boolean;
1174
+ /** 排序顺序,绝对值表示在排序列表中的顺序,负数表示逆序排序, 0 表示不排序 */
1175
+ sort?: number;
1176
+ constraints?: Record<string, Constraint>;
1177
+ }
1178
+ /** 字段类型信息 */
1179
+ interface FieldDefine<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> extends FieldTypeDefine<T, A, N>, FieldDefineOption {
1180
+ /** 默认值 */
1181
+ default?: ToType<T, A, N>;
1182
+ }
1183
+ interface FieldTypeDefine<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> {
1184
+ /** 字段的基本类型 */
1185
+ type: T;
1186
+ /** 是否可为空 */
1187
+ nullable: N;
1188
+ /** 是否为数组 */
1189
+ array: A;
1190
+ }
1191
+ type ToType<T extends MainFieldType = MainFieldType, A extends boolean = boolean, N extends boolean = boolean> = N extends false ? A extends false ? ToFieldType<T> : A extends true ? ToFieldType<T>[] : ToFieldType<T> | ToFieldType<T>[] : A extends false ? ToFieldType<T> | null : A extends true ? ToFieldType<T>[] | null : ToFieldType<T> | ToFieldType<T>[] | null;
1192
+ type FieldDefineType<F extends FieldTypeDefine> = F extends FieldTypeDefine<infer T, infer A, infer N> ? ToType<T, A, N> : never;
1193
+ type Fields<T extends MainFieldType = MainFieldType> = Record<string, FieldDefine<T>>;
1194
+ type FieldValue<T extends Fields> = {
1195
+ [K in keyof T]: FieldDefineType<T[K]>;
1196
+ };
1197
+ interface IndexOptions {
1198
+ unique?: boolean;
1199
+ name?: string;
1200
+ includes?: string[];
1201
+ }
1202
+ interface IndexInfo extends IndexOptions {
1203
+ fields: string[];
1204
+ }
1205
+ interface Index extends IndexInfo {
1206
+ name: string;
1207
+ }
1208
+ interface Hooks<T extends Record<string, any> = Record<string, any>> {
1209
+ where(conn: Connection, model: TableDefine): PromiseLike<void | Where> | void;
1210
+ beforeCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void | Record<string, any>[]> | void | Record<string, any>[];
1211
+ afterCreateMany(conn: Connection, model: TableDefine, records: Record<string, any>[]): PromiseLike<void> | void;
1212
+ beforeCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
1213
+ afterCreate(conn: Connection, model: TableDefine, record: Record<string, any>): PromiseLike<void> | void;
1214
+ beforeUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, set: Record<string, any>): PromiseLike<void | Record<string, any>> | void | Record<string, any>;
1215
+ afterUpdate(conn: Connection, model: TableDefine, record: Record<string, any>, oldRecord: Record<string, any>): PromiseLike<void> | void;
1216
+ beforeDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
1217
+ afterDelete(conn: Connection, model: TableDefine, record: T, update: Record<string, any> | null): PromiseLike<void> | void;
1218
+ }
1219
+ interface TableDefine<T extends Fields = Fields, TT extends string | VirtualTable | undefined = string | VirtualTable | undefined> {
1220
+ readonly connect?: TableConnect<any> | null;
1221
+ /** 字段信息 */
1222
+ readonly fields: T;
1223
+ /** 表名 */
1224
+ readonly table: TT;
1225
+ /** 是否启用软删除 */
1226
+ readonly pseudo?: string;
1227
+ /** 索引信息 */
1228
+ readonly indexes?: readonly IndexInfo[];
1229
+ /** 钩子 */
1230
+ readonly hooks?: Partial<Hooks>;
1231
+ }
1232
+ type TableValue<T extends TableDefine> = T extends TableDefine<infer F> ? FieldValue<F> : never;
1331
1233
 
1332
- /** @type {Hook<Hooks['where']>} */
1333
- declare const where: Hook<Hooks["where"]>;
1334
- /** @type {Hook<Hooks['beforeCreateMany']>} */
1335
- declare const beforeCreateMany: Hook<Hooks["beforeCreateMany"]>;
1336
- /** @type {Hook<Hooks['afterCreateMany']>} */
1337
- declare const afterCreateMany: Hook<Hooks["afterCreateMany"]>;
1338
- /** @type {Hook<Hooks['beforeCreate']>} */
1339
- declare const beforeCreate: Hook<Hooks["beforeCreate"]>;
1340
- /** @type {Hook<Hooks['afterCreate']>} */
1341
- declare const afterCreate: Hook<Hooks["afterCreate"]>;
1342
- /** @type {Hook<Hooks['beforeUpdate']>} */
1343
- declare const beforeUpdate: Hook<Hooks["beforeUpdate"]>;
1344
- /** @type {Hook<Hooks['afterUpdate']>} */
1345
- declare const afterUpdate: Hook<Hooks["afterUpdate"]>;
1346
- /** @type {Hook<Hooks['beforeDelete']>} */
1347
- declare const beforeDelete: Hook<Hooks["beforeDelete"]>;
1348
- /** @type {Hook<Hooks['afterDelete']>} */
1349
- declare const afterDelete: Hook<Hooks["afterDelete"]>;
1234
+ type deleted = typeof deleted;
1235
+ declare const deleted: unique symbol;
1236
+ type undeleted = typeof undeleted;
1237
+ declare const undeleted: unique symbol;
1238
+ type withDeleted = typeof withDeleted;
1239
+ declare const withDeleted: unique symbol;
1240
+ type FindRange = deleted | undeleted | withDeleted;
1350
1241
 
1351
- type ClassDecorator = (val: Function, ctx: ClassDecoratorContext) => any;
1352
- type FieldDecorator<T> = (val: {
1353
- get(): T;
1354
- set(v: T): void;
1355
- }, ctx: ClassAccessorDecoratorContext) => any;
1356
- type MethodDecorator<T extends Function> = (val: T, ctx: ClassMethodDecoratorContext<any, T>) => any;
1242
+ type Select = {
1243
+ field: string;
1244
+ table?: string;
1245
+ fn?: string;
1246
+ };
1247
+ declare namespace Select {
1248
+ type Param = boolean | string | Select;
1249
+ }
1250
+ type JoinType = 'left' | 'right' | 'inner' | 'full';
1251
+ interface Join<T extends TableDefine = TableDefine> {
1252
+ type: JoinType;
1253
+ table: T;
1254
+ /** 关联项目 { [this table field]: base table field } */
1255
+ on: {
1256
+ [k: string]: string;
1257
+ };
1258
+ }
1259
+ interface Options {
1260
+ range?: FindRange;
1261
+ select?: Record<string, Select> | null;
1262
+ /** 排序配置,true 表示逆序 */
1263
+ sort?: [field: string, desc: boolean][];
1264
+ /** 匹配信息 */
1265
+ where: WhereValue[];
1266
+ /** 偏移量 */
1267
+ offset?: number;
1268
+ /** 最大返回数 */
1269
+ limit?: number;
1270
+ update?: Record<string, SetValue>;
1271
+ alias?: string;
1272
+ group?: string[];
1273
+ }
1274
+ interface Queryable<T extends Fields = Fields> extends TableDefine<T> {
1275
+ readonly options: Options;
1276
+ }
1357
1277
 
1358
- /** 特性支持情况 */
1359
- interface Support {
1360
- /** 是否支持 sql 查询 */
1361
- sql?: boolean;
1362
- /** 是否支持数组类型 */
1278
+ type TableType = '' | 'table' | 'view';
1279
+ interface FindArg {
1280
+ /** 排序配置,true 表示逆序 */
1281
+ sort?: [field: string, desc: boolean][];
1282
+ /** 匹配信息 */
1283
+ where: WhereValue[];
1284
+ select: [string, Select][];
1285
+ /** 偏移量 */
1286
+ offset?: number;
1287
+ /** 最大返回数 */
1288
+ limit?: number;
1289
+ }
1290
+ interface JOIN {
1291
+ type?: 'INNER' | 'LEFT' | 'RIGHT' | 'FULL' | 'CROSS';
1292
+ table: string;
1293
+ columns: Record<string, DBColumn>;
1294
+ alias?: string;
1295
+ condition?: WhereValue[];
1296
+ }
1297
+ interface SelectItem {
1298
+ distinct?: boolean;
1299
+ select: [string, Select][];
1300
+ table: string;
1301
+ columns: Record<string, DBColumn>;
1302
+ alias?: string;
1303
+ /** 匹配信息 */
1304
+ where: WhereValue[];
1305
+ group: string[];
1306
+ having: WhereValue[];
1307
+ /** 排序配置,true 表示逆序 */
1308
+ sort: [field: string, desc: boolean][];
1309
+ /** 偏移量 */
1310
+ offset?: number;
1311
+ /** 最大返回数 */
1312
+ limit?: number;
1313
+ }
1314
+ type GetName = (table?: string) => string;
1315
+ interface DBColumn {
1316
+ type: string;
1317
+ size?: number;
1318
+ scale?: number;
1319
+ default?: any;
1320
+ nullable?: boolean;
1363
1321
  array?: boolean;
1364
- /** 是否支持对象存储 */
1365
- object?: boolean;
1366
- /** 是否支持递归查询 */
1367
- recursive?: boolean;
1368
- [key: string]: undefined | boolean;
1322
+ primary?: number;
1369
1323
  }
1370
-
1371
- type MaybePromise<T> = PromiseLike<T> | T;
1372
-
1373
- interface Hook<T extends Hooks[keyof Hooks]> {
1374
- (approach?: boolean): MethodDecorator<T>;
1375
- (hook: T, approach?: boolean): ClassDecorator;
1376
- (Model: Function, hook: T, approach?: boolean): void;
1324
+ interface DBIndex {
1325
+ fields: string[];
1326
+ includes?: string[];
1327
+ unique?: boolean;
1328
+ name?: string;
1329
+ }
1330
+ interface DBTable {
1331
+ table: string;
1332
+ fields: Record<string, DBColumn>;
1333
+ indexes?: DBIndex[];
1334
+ primary?: string;
1335
+ }
1336
+ interface TableConnection<E extends object = object> {
1337
+ insert(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, data: object[], key: string[], ignoreConflict?: boolean): PromiseLike<any[]>;
1338
+ upsert(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, data: object[], key: string[], conflict: true | string[], conflictSet?: Record<string, SetValue>): PromiseLike<any[]>;
1339
+ update(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<number>;
1340
+ updateReturn(environment: Environment<E>, returning: string[], table: string, columns: Record<string, DBColumn>, update: Record<string, SetValue>, where?: WhereValue[] | null): PromiseLike<any[]>;
1341
+ updateMany(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[]): PromiseLike<number>;
1342
+ updateManyReturn(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, update: Record<string, any>, pKeys: string[], setKeys: string[], list: Record<string, any>[], where: WhereValue[], returning: string[]): PromiseLike<any[]>;
1343
+ count(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
1344
+ select(environment: Environment<E>, select: SelectItem[]): PromiseLike<any[]>;
1345
+ delete(environment: Environment<E>, table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<number>;
1346
+ deleteReturn(environment: Environment<E>, returning: string[], table: string, columns: Record<string, DBColumn>, where?: WhereValue[] | null): PromiseLike<any[]>;
1347
+ }
1348
+ interface IConnection<E extends object = object> extends TableConnection<E> {
1349
+ /** 数据库类型 */
1350
+ readonly dbType: string;
1351
+ /** 数据库版本 */
1352
+ dbVersion(environment: Environment<E>): PromiseLike<string>;
1353
+ exec(environment: Environment<E>, ...values: any): Promise<number>;
1354
+ /**
1355
+ * 执行一条语句
1356
+ * @param sql 要执行的 SQL 语句
1357
+ */
1358
+ query(environment: Environment<E>, ...values: any): PromiseLike<any[]>;
1359
+ type(environment: Environment<E>, table: string): PromiseLike<TableType | null>;
1360
+ createTable(environment: Environment<E>, table: string, columns: Column<any>[]): PromiseLike<number>;
1361
+ dropTable(environment: Environment<E>, table: string): PromiseLike<number>;
1362
+ renameTable(environment: Environment<E>, table: string, newName: string): PromiseLike<number>;
1363
+ addColumn(environment: Environment<E>, table: string, column: string, type: string, options: ColumnOptions<any>): PromiseLike<number>;
1364
+ changeColumn(environment: Environment<E>, table: string, column: string, type: string, options: ColumnOptions<any>): PromiseLike<number>;
1365
+ changeColumnNull(environment: Environment<E>, table: string, column: string, nullable: boolean): PromiseLike<number>;
1366
+ dropColumn(environment: Environment<E>, table: string, column: string): PromiseLike<number>;
1367
+ renameColumn(environment: Environment<E>, table: string, column: string, newName: string): PromiseLike<number>;
1368
+ addIndex(environment: Environment<E>, table: string, name: string, columns: string[], options: IndexOptions): PromiseLike<number>;
1369
+ dropIndex(environment: Environment<E>, table: string, name: string): PromiseLike<number>;
1370
+ loadTables(environment: Environment<E>, tables: string[]): PromiseLike<DBTable[]>;
1371
+ syncTables(environment: Environment<E>, tables: DBTable[]): PromiseLike<void>;
1372
+ transaction<R>(environment: Environment<E>, fn: (t: E) => R | PromiseLike<R>): PromiseLike<R>;
1373
+ }
1374
+ interface TableConnect<E extends object = object> {
1375
+ (conn: IConnection<E>): TableConnection<E>;
1376
+ }
1377
+ interface Skip {
1378
+ uncreatable?: boolean;
1379
+ immutable?: boolean;
1380
+ specific?: boolean;
1381
+ where?: boolean;
1382
+ }
1383
+ interface TransactionFn {
1384
+ <T>(fn: (t: Connection) => PromiseLike<T> | T): Promise<T>;
1377
1385
  }
1378
1386
 
1379
1387
  declare const Destroy: unique symbol;
@@ -1697,7 +1705,7 @@ declare class Connection<E extends {} = {}> {
1697
1705
  * @param {Record<string, SetValue>} [conflictSet]
1698
1706
  * @returns {Promise<T>}
1699
1707
  */
1700
- upsert<T extends unknown>(tableDefine: TableDefine, data: T, keys?: boolean | string[] | Record<string, SetValue> | null | undefined, conflictKeys?: boolean | string[] | Record<string, SetValue> | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T>;
1708
+ upsert<T extends unknown>(tableDefine: TableDefine, data: T, keys?: boolean | Record<string, SetValue> | string[] | null | undefined, conflictKeys?: boolean | Record<string, SetValue> | string[] | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T>;
1701
1709
  /**
1702
1710
  * @template {object} T
1703
1711
  * @overload
@@ -1708,7 +1716,7 @@ declare class Connection<E extends {} = {}> {
1708
1716
  * @param {Record<string, SetValue>} [conflictSet]
1709
1717
  * @returns {Promise<T[]>}
1710
1718
  */
1711
- upsert<T extends unknown>(tableDefine: TableDefine, data: T[], keys?: boolean | string[] | Record<string, SetValue> | null | undefined, conflictKeys?: boolean | string[] | Record<string, SetValue> | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T[]>;
1719
+ upsert<T extends unknown>(tableDefine: TableDefine, data: T[], keys?: boolean | Record<string, SetValue> | string[] | null | undefined, conflictKeys?: boolean | Record<string, SetValue> | string[] | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T[]>;
1712
1720
  /**
1713
1721
  * @template {object} T
1714
1722
  * @overload
@@ -1719,7 +1727,7 @@ declare class Connection<E extends {} = {}> {
1719
1727
  * @param {Record<string, SetValue>} [conflictSet]
1720
1728
  * @returns {Promise<T | T[]>}
1721
1729
  */
1722
- upsert<T extends unknown>(tableDefine: TableDefine, data: T | T[], keys?: boolean | string[] | Record<string, SetValue> | null | undefined, conflictKeys?: boolean | string[] | Record<string, SetValue> | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T | T[]>;
1730
+ upsert<T extends unknown>(tableDefine: TableDefine, data: T | T[], keys?: boolean | Record<string, SetValue> | string[] | null | undefined, conflictKeys?: boolean | Record<string, SetValue> | string[] | undefined, conflictSet?: Record<string, SetValue> | undefined): Promise<T | T[]>;
1723
1731
  /**
1724
1732
  * @param {TableDefine} tableDefine
1725
1733
  * @param {Record<string, any>} update
@@ -1727,7 +1735,7 @@ declare class Connection<E extends {} = {}> {
1727
1735
  * @param {boolean | Skip} [skip]
1728
1736
  * @returns {Promise<number>}
1729
1737
  */
1730
- update({ table, fields }: TableDefine, update: Record<string, any>, where?: Wheres | null, skip?: boolean | Skip): Promise<number>;
1738
+ update({ connect, table, fields }: TableDefine, update: Record<string, any>, where?: Wheres | null, skip?: boolean | Skip): Promise<number>;
1731
1739
  /**
1732
1740
  * @template {Fields<MainFieldType>} T
1733
1741
  * @param {TableDefine<T>} tableDefine
@@ -1737,7 +1745,7 @@ declare class Connection<E extends {} = {}> {
1737
1745
  * @param {boolean | Skip} [skip]
1738
1746
  * @returns {Promise<any[]>}
1739
1747
  */
1740
- updateReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: Wheres | null, skip?: boolean | Skip): Promise<any[]>;
1748
+ updateReturn<T extends Fields<MainFieldType>>({ connect, table, fields }: TableDefine<T>, update: Record<string, any>, returning?: string[] | null, where?: Wheres | null, skip?: boolean | Skip): Promise<any[]>;
1741
1749
  /**
1742
1750
  * @param {TableDefine} tableDefine
1743
1751
  * @param {string[]} pKeys
@@ -1746,7 +1754,7 @@ declare class Connection<E extends {} = {}> {
1746
1754
  * @param {boolean | Skip} [skip]
1747
1755
  * @returns {Promise<number>}
1748
1756
  */
1749
- updateMany({ table, fields }: TableDefine, pKeys: string[], setKeys: string[], list: Record<string, any>[], skip?: boolean | Skip): Promise<number>;
1757
+ updateMany({ connect, table, fields }: TableDefine, pKeys: string[], setKeys: string[], list: Record<string, any>[], skip?: boolean | Skip): Promise<number>;
1750
1758
  /**
1751
1759
  * @template {Fields<MainFieldType>} T
1752
1760
  * @param {TableDefine<T>} tableDefine
@@ -1757,13 +1765,13 @@ declare class Connection<E extends {} = {}> {
1757
1765
  * @param {boolean | Skip} [skip]
1758
1766
  * @returns {Promise<any[]>}
1759
1767
  */
1760
- updateManyReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, pKeys: string[], setKeys: string[], list: Record<string, any>[], returning?: string[] | null, skip?: boolean | Skip): Promise<any[]>;
1768
+ updateManyReturn<T extends Fields<MainFieldType>>({ connect, table, fields }: TableDefine<T>, pKeys: string[], setKeys: string[], list: Record<string, any>[], returning?: string[] | null, skip?: boolean | Skip): Promise<any[]>;
1761
1769
  /**
1762
1770
  * @param {TableDefine} tableDefine
1763
1771
  * @param {Wheres?} [where]
1764
1772
  * @returns {Promise<number>}
1765
1773
  */
1766
- delete({ table, fields }: TableDefine, where?: Wheres | null): Promise<number>;
1774
+ delete({ connect, table, fields }: TableDefine, where?: Wheres | null): Promise<number>;
1767
1775
  /**
1768
1776
  * @template {Fields<MainFieldType>} T
1769
1777
  * @param {TableDefine<T>} tableDefine
@@ -1771,7 +1779,7 @@ declare class Connection<E extends {} = {}> {
1771
1779
  * @param {Wheres?} [where]
1772
1780
  * @returns {Promise<any[]>}
1773
1781
  */
1774
- deleteReturn<T extends Fields<MainFieldType>>({ table, fields }: TableDefine<T>, returning?: string[] | null, where?: Wheres | null): Promise<any[]>;
1782
+ deleteReturn<T extends Fields<MainFieldType>>({ connect, table, fields }: TableDefine<T>, returning?: string[] | null, where?: Wheres | null): Promise<any[]>;
1775
1783
  /**
1776
1784
  *
1777
1785
  * @param {Queryable} queryable
@@ -1901,7 +1909,14 @@ declare function getPrimaryKeys(fieldsOrColumns: Fields): string[];
1901
1909
  */
1902
1910
  declare function isPseudo(model: Queryable): boolean;
1903
1911
 
1912
+ /** @import { SelectItem } from './types' */
1913
+ declare class Subquery {
1914
+ /** @returns {SelectItem[]} */
1915
+ toSubquery(): SelectItem[];
1916
+ }
1917
+
1904
1918
  type QueryOptions<T extends Fields, TB extends unknown = any> = {
1919
+ connect?: TableConnect<any> | null | undefined;
1905
1920
  table?: string | VirtualTable<object> | undefined;
1906
1921
  fields: T;
1907
1922
  pseudo?: string | undefined;
@@ -1911,6 +1926,7 @@ type QueryOptions<T extends Fields, TB extends unknown = any> = {
1911
1926
  * @template {Fields} T
1912
1927
  * @template {object} [TB=object]
1913
1928
  * @typedef {object} QueryOptions
1929
+ * @property {TableConnect<any>?} [connect]
1914
1930
  * @property {string | VirtualTable} [table]
1915
1931
  * @property {T} fields
1916
1932
  * @property {string} [pseudo]
@@ -1921,7 +1937,7 @@ type QueryOptions<T extends Fields, TB extends unknown = any> = {
1921
1937
  * @template {object} [TB=object]
1922
1938
  * @implements {Queryable<T>}
1923
1939
  */
1924
- declare class Query<T extends Fields, TB extends unknown = any> implements Queryable {
1940
+ declare class Query<T extends Fields, TB extends unknown = any> extends Subquery implements Queryable {
1925
1941
  /**
1926
1942
  * @template {Fields} F
1927
1943
  * @template {new (o: QueryOptions<F>) => Query<F>} T
@@ -1937,6 +1953,8 @@ declare class Query<T extends Fields, TB extends unknown = any> implements Query
1937
1953
  * @param {boolean} [noBuild]
1938
1954
  */
1939
1955
  constructor(options: QueryOptions<T, TB> | Query<T, TB>, noBuild?: boolean);
1956
+ /** @readonly @type {TableConnect | null | undefined} */
1957
+ readonly connect: TableConnect | null | undefined;
1940
1958
  /** @readonly @type {string | VirtualTable | undefined} */
1941
1959
  readonly table: string | VirtualTable | undefined;
1942
1960
  /** @readonly @type {T} */
@@ -2518,4 +2536,4 @@ declare function field<T extends MainFieldType>(type: T, options: {
2518
2536
  declare function setDevelopment(d?: boolean): void;
2519
2537
  declare let isDevelopment: boolean;
2520
2538
 
2521
- export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableDefine, type TableType, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };
2539
+ export { Build, type ClassDecorator, type Column, type ColumnOptions, Connection, type Constraint, Create, type DBColumn, type DBIndex, type DBTable, Destroy, type Environment, type FieldDecorator, type FieldDefine, type FieldDefineOption, type FieldDefineType, type FieldSpecific, type FieldSpecificValue, type FieldType, type FieldTypeDefine, type FieldValue, type Fields, type FindArg, type FindRange, type GetName, type Hook, type Hooks, type IConnection, type Index, type IndexInfo, type IndexOptions, type JOIN, type Join, type JoinType, type MainFieldType, type MaybePromise, type MethodDecorator, Model, type Options, PseudoDestroy, Query, type QueryOptions, type Queryable, Save, Scene, Select, type SelectItem, SetValue, type Skip, Submodel, type Support, type TableConnect, type TableConnection, type TableDefine, type TableType, type TableValue, type ToFieldType, type ToType, type TransactionFn, type VirtualTable, Where, type WhereItem, type WhereLike, type WhereOr, type WhereRaw, type WhereValue, type Wheres, afterCreate, afterCreateMany, afterDelete, afterUpdate, beforeCreate, beforeCreateMany, beforeDelete, beforeUpdate, coefficient, column, creating, decrement, defaultValue, field as define, deleted, deleting, divide, field$1 as field, getPrimaryFields, getPrimaryKeys, immutable, increment, index, isDevelopment, isPseudo, model, multiply, now, primary, prop, pseudo, setDevelopment, sort, model as submodel, toNot, toPrimaries, toPrimary, uncreatable, undeleted, updating, uuid, values, where, withDeleted };