@rwillians/qx 0.1.20 → 0.1.21

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.
@@ -0,0 +1,1233 @@
1
+ import * as std from './standard-schema';
2
+ /**
3
+ * @private Symbol used to store the table name on aliased tables.
4
+ * @since 0.1.0
5
+ * @version 1
6
+ */
7
+ declare const TABLE_NAME: unique symbol;
8
+ /**
9
+ * @private Symbol used to store the table alias on aliased tables.
10
+ * @since 0.1.0
11
+ * @version 1
12
+ */
13
+ declare const TABLE_ALIAS: unique symbol;
14
+ /**
15
+ * @private Little trick to force TypeScript to resolve types instead
16
+ * of composing them.
17
+ * @since 0.1.0
18
+ * @version 1
19
+ */
20
+ type Expand<T> = T extends object ? {
21
+ [K in keyof T]: T[K];
22
+ } : never;
23
+ /**
24
+ * @public The list of primitive types supported by qx.
25
+ * @since 0.1.0
26
+ * @version 1
27
+ */
28
+ type Primitive = 'BINARY' | 'BOOLEAN' | 'DATETIME' | 'FLOAT' | 'INTEGER' | 'TEXT' | 'VARCHAR';
29
+ /**
30
+ * @private All properties of a column, except its name and the table
31
+ * to which they belong.
32
+ * @since 0.1.0
33
+ * @version 1
34
+ */
35
+ type ColumnProps<T extends std.Schema = std.Schema> = {
36
+ /**
37
+ * @public whether the column is autoincrementing.
38
+ * @since 0.1.0
39
+ * @version 1
40
+ */
41
+ autoincrement?: true;
42
+ /**
43
+ * @public The default value for the column. If a column has
44
+ * nullish value in an insert, then this value will be used
45
+ * instead.
46
+ * @since 0.1.0
47
+ * @version 1
48
+ */
49
+ default?: std.output<T> | (() => std.output<T>);
50
+ /**
51
+ * @public Whether the column is nullable.
52
+ * @since 0.1.0
53
+ * @version 1
54
+ */
55
+ nullable?: true;
56
+ /**
57
+ * @public Whether the column is a primary key.
58
+ * @since 0.1.0
59
+ * @version 1
60
+ */
61
+ primaryKey?: true;
62
+ /**
63
+ * @public The standard schema that defines the column's input and
64
+ * output types.
65
+ * @since 0.1.0
66
+ * @version 1
67
+ */
68
+ schema: T;
69
+ /**
70
+ * @public The size of a string column (VARCHAR). If provided for
71
+ * non-varchar columns, it will be ignored.
72
+ * @since 0.1.0
73
+ * @version 1
74
+ */
75
+ size?: number;
76
+ /**
77
+ * @public The type of the column, one of the supported primitive
78
+ * types by `qx`. See {@link Primitive} for the complete
79
+ * list of primitives.
80
+ * @since 0.1.0
81
+ * @version 1
82
+ */
83
+ type: Primitive;
84
+ /**
85
+ * @public Whether the column has a unique constraint.
86
+ * @since 0.1.0
87
+ * @version 1
88
+ */
89
+ unique?: true;
90
+ };
91
+ /**
92
+ * @public A column definition.
93
+ * @since 0.1.0
94
+ * @version 2
95
+ */
96
+ type Column<S extends string = string, T extends ColumnProps = ColumnProps> = Expand<T & {
97
+ /**
98
+ * @public The name of the column.
99
+ * @since 0.1.0
100
+ * @version 1
101
+ */
102
+ name: S;
103
+ /**
104
+ * @public The name of the table to which the column belongs or its
105
+ * alias when the table has been aliased.
106
+ * @since 0.1.0
107
+ * @version 1
108
+ */
109
+ table: string;
110
+ /**
111
+ * @public The column's input type, cached.
112
+ * @since 0.1.0
113
+ * @version 2
114
+ */
115
+ inferInput: std.input<T['schema']>;
116
+ /**
117
+ * @public The column's output type, cached.
118
+ * @since 0.1.0
119
+ * @version 2
120
+ */
121
+ inferOutput: std.output<T['schema']>;
122
+ }>;
123
+ /**
124
+ * @public A table definition.
125
+ * @since 0.1.0
126
+ * @version 1
127
+ */
128
+ type Table<S extends string = string, T extends Record<string, ColumnProps> = Record<string, ColumnProps>> = {
129
+ /**
130
+ * @public The name of the table.
131
+ * @since 0.1.0
132
+ * @version 1
133
+ */
134
+ name: S;
135
+ /**
136
+ * @public The columns of the table.
137
+ * @since 0.1.0
138
+ * @version 1
139
+ */
140
+ columns: {
141
+ [K in keyof T & string]: Column<K, T[K]>;
142
+ };
143
+ };
144
+ /**
145
+ * @private Represents a table when it's been aliased in a query.
146
+ * @since 0.1.0
147
+ * @version 1
148
+ */
149
+ type Aliased<S extends string = string, T extends Table = Table> = T['columns'] & {
150
+ /**
151
+ * @public The original name of the table.
152
+ * @since 0.1.0
153
+ * @version 1
154
+ */
155
+ [TABLE_NAME]: T['name'];
156
+ /**
157
+ * @public The alias of the table.
158
+ * @since 0.1.0
159
+ * @version 1
160
+ */
161
+ [TABLE_ALIAS]: S;
162
+ };
163
+ /**
164
+ * @private Adds the ability to alias a table.
165
+ * @since 0.1.0
166
+ * @version 1
167
+ */
168
+ type Aliasable<T extends Table> = T & {
169
+ /**
170
+ * @public Creates an aliased table from a regular table.
171
+ * @since 0.1.0
172
+ * @version 1
173
+ */
174
+ as: <S extends string>(alias: S) => Aliased<S, T>;
175
+ };
176
+ /**
177
+ * @private The pattern of columns that should be omitted when
178
+ * inserting a new row.
179
+ * @since 0.1.0
180
+ * @version 1
181
+ */
182
+ type OmitOnInsert = {
183
+ autoincrement: true;
184
+ };
185
+ /**
186
+ * @private The pattern of columns that should be optional when
187
+ * inserting a new row.
188
+ * @since 0.1.0
189
+ * @version 1
190
+ */
191
+ type OptionalOnInsert = {
192
+ default: any;
193
+ } | {
194
+ nullable: true;
195
+ };
196
+ /**
197
+ * @private The pattern of columns that should be omitted when
198
+ * updating a row.
199
+ * @since 0.1.0
200
+ * @version 1
201
+ */
202
+ type OmitOnUpdate = {
203
+ autoincrement: true;
204
+ } | {
205
+ primaryKey: true;
206
+ };
207
+ /**
208
+ * @private Infers the output type of a table's row.
209
+ * @since 0.1.0
210
+ * @version 2
211
+ */
212
+ type Infer<T extends Table> = {
213
+ [K in keyof T['columns']]: T['columns'][K]['inferOutput'];
214
+ };
215
+ /**
216
+ * @private Infers the input type required to insert a new row.
217
+ * @since 0.1.0
218
+ * @version 2
219
+ */
220
+ type InferForInsert<T extends Table> = Expand<{
221
+ [K in keyof T['columns'] as T['columns'][K] extends OmitOnInsert | OptionalOnInsert ? never : K]: T['columns'][K]['inferInput'];
222
+ } & {
223
+ [K in keyof T['columns'] as T['columns'][K] extends OptionalOnInsert ? K : never]?: T['columns'][K]['inferInput'];
224
+ }>;
225
+ /**
226
+ * @private Infers the input type required to update an existing row.
227
+ * @since 0.1.0
228
+ * @version 2
229
+ */
230
+ type InferForUpdate<T extends Table> = {
231
+ [K in keyof T['columns'] as T['columns'][K] extends OmitOnUpdate ? never : K]?: T['columns'][K]['inferInput'];
232
+ };
233
+ /**
234
+ * @private Adds the ability to infer types of a table.
235
+ * @since 0.1.0
236
+ * @version 1
237
+ */
238
+ type Inferrable<T extends Table> = T & {
239
+ /**
240
+ * @private The output type of a row, cached.
241
+ * @since 0.1.0
242
+ * @version 1
243
+ */
244
+ infer: Infer<T>;
245
+ /**
246
+ * @private The input type for a new row, cached.
247
+ * @since 0.1.0
248
+ * @version 1
249
+ */
250
+ inferForInsert: InferForInsert<T>;
251
+ /**
252
+ * @private The input type for updating an existing row, cached.
253
+ * @since 0.1.0
254
+ * @version 1
255
+ */
256
+ inferForUpdate: InferForUpdate<T>;
257
+ };
258
+ /**
259
+ * @private A builder for column properties.
260
+ * @since 0.1.0
261
+ * @version 2
262
+ */
263
+ declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
264
+ readonly props: T;
265
+ constructor(props: T);
266
+ /**
267
+ * @public Marks the column as autoincrementing.
268
+ * @since 0.1.0
269
+ * @version 1
270
+ */
271
+ autoincrement(): ColumnPropsBuilder<Expand<T & {
272
+ autoincrement: true;
273
+ }>>;
274
+ /**
275
+ * @public Sets a default value for the column. This is not the
276
+ * table's default value for the column, but rather a
277
+ * fallback value that qx will use when inserting a new row
278
+ * where this column is nullish.
279
+ * @since 0.1.0
280
+ * @version 2
281
+ */
282
+ default<S extends std.output<T['schema']> | (() => std.output<T['schema']>)>(value: S): ColumnPropsBuilder<Expand<T & {
283
+ default: S;
284
+ }>>;
285
+ /**
286
+ * @public Marks the column as nullable.
287
+ * @since 0.1.0
288
+ * @version 2
289
+ * @throws {Error} if the column is a primary key.
290
+ */
291
+ nullable(): ColumnPropsBuilder<Expand<Omit<T, "schema"> & {
292
+ nullable: true;
293
+ schema: std.QxNullable<T["schema"]>;
294
+ }>>;
295
+ /**
296
+ * @public Marks the column as a primary key.
297
+ * @since 0.1.0
298
+ * @version 1
299
+ * @throws {Error} if the column is nullable.
300
+ */
301
+ primaryKey(): ColumnPropsBuilder<Expand<T & {
302
+ primaryKey: true;
303
+ }>>;
304
+ /**
305
+ * @public Marks the column as unique.
306
+ * @since 0.1.0
307
+ * @version 2
308
+ */
309
+ unique(): ColumnPropsBuilder<Expand<T & {
310
+ unique: true;
311
+ }>>;
312
+ }
313
+ /**
314
+ * @public A way to define a column with a custom standard schema.
315
+ * @since 0.1.0
316
+ * @version 1
317
+ */
318
+ declare const defineColumn: <T extends ColumnProps>(baseProps: T) => ColumnPropsBuilder<T>;
319
+ /**
320
+ * @public Built-in column types.
321
+ * @since 0.1.0
322
+ * @version 1
323
+ */
324
+ declare const types: {
325
+ /**
326
+ * @public Defines a column type that accepts binary data.
327
+ * @since 0.1.0
328
+ * @version 1
329
+ */
330
+ binary: () => ColumnPropsBuilder<{
331
+ type: "BINARY";
332
+ schema: std.QxInstanceOf<Uint8Array<ArrayBuffer>>;
333
+ }>;
334
+ /**
335
+ * @public Defines a column type that accepts boolean values.
336
+ * @since 0.1.0
337
+ * @version 1
338
+ */
339
+ boolean: () => ColumnPropsBuilder<{
340
+ type: "BOOLEAN";
341
+ schema: std.QxBoolean;
342
+ }>;
343
+ /**
344
+ * @public Defines a column type that accepts a Date object, an
345
+ * ISO 8601 date string, or a Unix timestamp in milliseconds.
346
+ * @since 0.1.0
347
+ * @version 1
348
+ */
349
+ datetime: () => ColumnPropsBuilder<{
350
+ type: "DATETIME";
351
+ schema: std.QxCoercibleDate;
352
+ }>;
353
+ /**
354
+ * @public Defines a column type that accepts floating-point numbers.
355
+ * @since 0.1.0
356
+ * @version 1
357
+ */
358
+ float: () => ColumnPropsBuilder<{
359
+ type: "FLOAT";
360
+ schema: std.QxNumber;
361
+ }>;
362
+ /**
363
+ * @public Defines a column type that accepts integer numbers.
364
+ * @since 0.1.0
365
+ * @version 1
366
+ */
367
+ integer: () => ColumnPropsBuilder<{
368
+ type: "INTEGER";
369
+ schema: std.QxInteger;
370
+ }>;
371
+ /**
372
+ * @public Defines a column type that accepts small strings (VARCHAR,
373
+ * up to 255 characters).
374
+ * @since 0.1.0
375
+ * @version 1
376
+ * @throws {Error} if the specified size is greater than 255.
377
+ */
378
+ string: ({ size }?: {
379
+ size?: number;
380
+ }) => ColumnPropsBuilder<{
381
+ type: "VARCHAR";
382
+ schema: std.QxString;
383
+ size: number;
384
+ }>;
385
+ /**
386
+ * @public Defines a column type that accepts large strings (TEXT).
387
+ * @since 0.1.0
388
+ * @version 1
389
+ */
390
+ text: () => ColumnPropsBuilder<{
391
+ type: "TEXT";
392
+ schema: std.QxString;
393
+ }>;
394
+ };
395
+ /**
396
+ * @public Defines a new table.
397
+ * @since 0.1.0
398
+ * @version 1
399
+ */
400
+ declare const defineTable: <S extends string, T extends Record<string, ColumnPropsBuilder>>(tname: S, shapeFn: (t: typeof types) => T) => Aliasable<Inferrable<Table<S, { [K in keyof T & string]: T[K]["props"]; }>>>;
401
+ /**
402
+ * @public Represents an equality expression.
403
+ * @since 0.1.0
404
+ * @version 1
405
+ */
406
+ type ExprEq = {
407
+ lhs: Expr;
408
+ op: '=';
409
+ rhs: Expr;
410
+ };
411
+ /**
412
+ * @public Represents a not-equal expression.
413
+ * @since 0.1.0
414
+ * @version 1
415
+ */
416
+ type ExprNe = {
417
+ lhs: Expr;
418
+ op: '!=';
419
+ rhs: Expr;
420
+ };
421
+ /**
422
+ * @public Represents a less-than expression.
423
+ * @since 0.1.0
424
+ * @version 1
425
+ */
426
+ type ExprLt = {
427
+ lhs: Expr;
428
+ op: '<';
429
+ rhs: Expr;
430
+ };
431
+ /**
432
+ * @public Represents a less-than-or-equal expression.
433
+ * @since 0.1.0
434
+ * @version 1
435
+ */
436
+ type ExprLte = {
437
+ lhs: Expr;
438
+ op: '<=';
439
+ rhs: Expr;
440
+ };
441
+ /**
442
+ * @public Represents a greater-than expression.
443
+ * @since 0.1.0
444
+ * @version 1
445
+ */
446
+ type ExprGt = {
447
+ lhs: Expr;
448
+ op: '>';
449
+ rhs: Expr;
450
+ };
451
+ /**
452
+ * @public Represents a greater-than-or-equal expression.
453
+ * @since 0.1.0
454
+ * @version 1
455
+ */
456
+ type ExprGte = {
457
+ lhs: Expr;
458
+ op: '>=';
459
+ rhs: Expr;
460
+ };
461
+ /**
462
+ * @public Represents a LIKE expression.
463
+ * @since 0.1.0
464
+ * @version 1
465
+ */
466
+ type ExprLike = {
467
+ lhs: Expr;
468
+ op: 'LIKE';
469
+ rhs: string;
470
+ };
471
+ /**
472
+ * @public Represents a NOT LIKE expression.
473
+ * @since 0.1.0
474
+ * @version 1
475
+ */
476
+ type ExprNotLike = {
477
+ lhs: Expr;
478
+ op: 'NOT LIKE';
479
+ rhs: string;
480
+ };
481
+ /**
482
+ * @public Represents an IN expression.
483
+ * @since 0.1.0
484
+ * @version 1
485
+ */
486
+ type ExprIn = {
487
+ lhs: Expr;
488
+ op: 'IN';
489
+ rhs: Exclude<Expr, ExprLiteral> | ExprLiteral[];
490
+ };
491
+ /**
492
+ * @public Represents a NOT IN expression.
493
+ * @since 0.1.0
494
+ * @version 1
495
+ */
496
+ type ExprNotIn = {
497
+ lhs: Expr;
498
+ op: 'NOT IN';
499
+ rhs: Exclude<Expr, ExprLiteral> | ExprLiteral[];
500
+ };
501
+ /**
502
+ * @public Represents an AND expression.
503
+ * @since 0.1.0
504
+ * @version 1
505
+ */
506
+ type ExprAnd = {
507
+ and: Expr[];
508
+ };
509
+ /**
510
+ * @public Represents an OR expression.
511
+ * @since 0.1.0
512
+ * @version 1
513
+ */
514
+ type ExprOr = {
515
+ or: Expr[];
516
+ };
517
+ /**
518
+ * @public Represents a NOT expression.
519
+ * @since 0.1.0
520
+ * @version 1
521
+ */
522
+ type ExprNot = {
523
+ not: Expr;
524
+ };
525
+ /**
526
+ * @public Represents an IS expression.
527
+ * @since 0.1.0
528
+ * @version 1
529
+ */
530
+ type ExprIs = {
531
+ lhs: Expr;
532
+ op: 'IS';
533
+ rhs: true | false | null;
534
+ };
535
+ /**
536
+ * @public Represents an IS NOT expression.
537
+ * @since 0.1.0
538
+ * @version 1
539
+ */
540
+ type ExprIsNot = {
541
+ lhs: Expr;
542
+ op: 'IS NOT';
543
+ rhs: true | false | null;
544
+ };
545
+ /**
546
+ * @private Represents a literal value in a query expression.
547
+ * @since 0.1.0
548
+ * @version 1
549
+ */
550
+ type ExprLiteral = boolean | Date | number | string | null;
551
+ /**
552
+ * @private Represents any binary expressions.
553
+ * @since 0.1.0
554
+ * @version 1
555
+ */
556
+ type ExprBinaryOp = ExprEq | ExprNe | ExprLt | ExprLte | ExprGt | ExprGte | ExprLike | ExprNotLike | ExprIn | ExprNotIn | ExprIs | ExprIsNot;
557
+ /**
558
+ * @private Represents any supported expressions.
559
+ * @since 0.1.0
560
+ * @version 1
561
+ */
562
+ type Expr = Column | ExprEq | ExprNe | ExprLt | ExprLte | ExprGt | ExprGte | ExprLike | ExprNotLike | ExprIn | ExprNotIn | ExprIs | ExprIsNot | ExprAnd | ExprOr | ExprNot | ExprLiteral;
563
+ /**
564
+ * @public Expression builders.
565
+ * @since 0.1.0
566
+ * @version 1
567
+ */
568
+ declare const expr: {
569
+ /**
570
+ * @public Builds an equality expression.
571
+ * @since 0.1.0
572
+ * @version 1
573
+ */
574
+ eq: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprEq;
575
+ /**
576
+ * @public Builds a not-equal expression.
577
+ * @since 0.1.0
578
+ * @version 1
579
+ */
580
+ ne: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprNe;
581
+ /**
582
+ * @public Builds a less-than expression.
583
+ * @since 0.1.0
584
+ * @version 1
585
+ */
586
+ lt: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprLt;
587
+ /**
588
+ * @public Builds a less-than-or-equal expression.
589
+ * @since 0.1.0
590
+ * @version 1
591
+ */
592
+ lte: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprLte;
593
+ /**
594
+ * @public Builds a greater-than expression.
595
+ * @since 0.1.0
596
+ * @version 1
597
+ */
598
+ gt: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprGt;
599
+ /**
600
+ * @public Builds a greater-than-or-equal expression.
601
+ * @since 0.1.0
602
+ * @version 1
603
+ */
604
+ gte: <L extends Expr, R extends Expr>(lhs: L, rhs: R) => ExprGte;
605
+ /**
606
+ * @public Builds a LIKE expression.
607
+ * @since 0.1.0
608
+ * @version 1
609
+ */
610
+ like: <L extends Expr>(lhs: L, rhs: string) => ExprLike;
611
+ /**
612
+ * @public Builds a NOT LIKE expression.
613
+ * @since 0.1.0
614
+ * @version 1
615
+ */
616
+ notLike: <L extends Expr>(lhs: L, rhs: string) => ExprNotLike;
617
+ /**
618
+ * @public Builds an IN expression.
619
+ * @since 0.1.0
620
+ * @version 1
621
+ */
622
+ in: <L extends Expr, R extends Exclude<Expr, ExprLiteral> | ExprLiteral[]>(lhs: L, rhs: R) => ExprIn;
623
+ /**
624
+ * @public Builds a NOT IN expression.
625
+ * @since 0.1.0
626
+ * @version 1
627
+ */
628
+ notIn: <L extends Expr, R extends Exclude<Expr, ExprLiteral> | ExprLiteral[]>(lhs: L, rhs: R) => ExprNotIn;
629
+ /**
630
+ * @public Builds an IS expression.
631
+ * @since 0.1.0
632
+ * @version 1
633
+ */
634
+ is: (lhs: Expr, rhs: true | false | null) => ExprIs;
635
+ /**
636
+ * @public Builds an IS NOT expression.
637
+ * @since 0.1.0
638
+ * @version 1
639
+ */
640
+ isNot: (lhs: Expr, rhs: true | false | null) => ExprIsNot;
641
+ /**
642
+ * @public Builds an AND expression.
643
+ * @since 0.1.0
644
+ * @version 1
645
+ */
646
+ and: <E extends Expr>(exprs: E[]) => ExprAnd;
647
+ /**
648
+ * @public Builds an OR expression.
649
+ * @since 0.1.0
650
+ * @version 1
651
+ */
652
+ or: <E extends Expr>(exprs: E[]) => ExprOr;
653
+ /**
654
+ * @public Builds a NOT expression.
655
+ * @since 0.1.0
656
+ * @version 1
657
+ */
658
+ not: <E extends Expr>(expr: E) => ExprNot;
659
+ /**
660
+ * @public Builds an ascending order expression.
661
+ * @since 0.1.13
662
+ * @version 1
663
+ */
664
+ asc: (expr: Expr) => readonly [Expr, "ASC"];
665
+ /**
666
+ * @public Builds a descending order expression.
667
+ * @since 0.1.13
668
+ * @version 1
669
+ */
670
+ desc: (expr: Expr) => readonly [Expr, "DESC"];
671
+ };
672
+ /**
673
+ * @public Expression type guards.
674
+ * @since 0.1.0
675
+ * @version 1
676
+ */
677
+ declare const is: {
678
+ /**
679
+ * @public Checks whether the given value is a binary op expression.
680
+ * @since 0.1.0
681
+ * @version 1
682
+ */
683
+ binaryOp: (expr: Expr) => expr is ExprBinaryOp;
684
+ /**
685
+ * @public Checks whether the given value is an equality expression.
686
+ * @since 0.1.0
687
+ * @version 1
688
+ */
689
+ eq: (expr: Expr) => expr is ExprEq;
690
+ /**
691
+ * @public Checks whether the given value is a not-equal expression.
692
+ * @since 0.1.0
693
+ * @version 1
694
+ */
695
+ ne: (expr: Expr) => expr is ExprNe;
696
+ /**
697
+ * @public Checks whether the given value is a less-than expression.
698
+ * @since 0.1.0
699
+ * @version 1
700
+ */
701
+ lt: (expr: Expr) => expr is ExprLt;
702
+ /**
703
+ * @public Checks whether the given value is a less-than-or-equal expression.
704
+ * @since 0.1.0
705
+ * @version 1
706
+ */
707
+ lte: (expr: Expr) => expr is ExprLte;
708
+ /**
709
+ * @public Checks whether the given value is a greater-than expression.
710
+ * @since 0.1.0
711
+ * @version 1
712
+ */
713
+ gt: (expr: Expr) => expr is ExprGt;
714
+ /**
715
+ * @public Checks whether the given value is a greater-than-or-equal expression.
716
+ * @since 0.1.0
717
+ * @version 1
718
+ */
719
+ gte: (expr: Expr) => expr is ExprGte;
720
+ /**
721
+ * @public Checks whether the given value is a LIKE expression.
722
+ * @since 0.1.0
723
+ * @version 1
724
+ */
725
+ like: (expr: Expr) => expr is ExprLike;
726
+ /**
727
+ * @public Checks whether the given value is a NOT LIKE expression.
728
+ * @since 0.1.0
729
+ * @version 1
730
+ */
731
+ notLike: (expr: Expr) => expr is ExprNotLike;
732
+ /**
733
+ * @public Checks whether the given value is an IN expression.
734
+ * @since 0.1.0
735
+ * @version 1
736
+ */
737
+ in: (expr: Expr) => expr is ExprIn;
738
+ /**
739
+ * @public Checks whether the given value is a NOT IN expression.
740
+ * @since 0.1.0
741
+ * @version 1
742
+ */
743
+ notIn: (expr: Expr) => expr is ExprNotIn;
744
+ /**
745
+ * @public Checks whether the given value is an IS expression.
746
+ * @since 0.1.0
747
+ * @version 1
748
+ */
749
+ is: (expr: Expr) => expr is ExprIs;
750
+ /**
751
+ * @public Checks whether the given value is an AND expression.
752
+ * @since 0.1.0
753
+ * @version 1
754
+ */
755
+ and: (expr: Expr) => expr is ExprAnd;
756
+ /**
757
+ * @public Checks whether the given value is an OR expression.
758
+ * @since 0.1.0
759
+ * @version 1
760
+ */
761
+ or: (expr: Expr) => expr is ExprOr;
762
+ /**
763
+ * @public Checks whether the given value is a NOT expression.
764
+ * @since 0.1.0
765
+ * @version 1
766
+ */
767
+ not: (expr: Expr) => expr is ExprNot;
768
+ /**
769
+ * @public Checks whether the given value is a boolean literal.
770
+ * @since 0.1.0
771
+ * @version 1
772
+ */
773
+ boolean: (value: Expr) => value is boolean;
774
+ /**
775
+ * @public Checks whether the given value is a date literal.
776
+ * @since 0.1.0
777
+ * @version 1
778
+ */
779
+ date: (value: Expr) => value is Date;
780
+ /**
781
+ * @public Checks whether the given value is a null literal.
782
+ * @since 0.1.0
783
+ * @version 1
784
+ */
785
+ null: (expr: Expr) => expr is null;
786
+ /**
787
+ * @public Checks whether the given value is a number literal.
788
+ * @since 0.1.0
789
+ * @version 1
790
+ */
791
+ number: (expr: Expr) => expr is number;
792
+ /**
793
+ * @public Checks whether the given value is a string literal.
794
+ * @since 0.1.0
795
+ * @version 1
796
+ */
797
+ string: (expr: Expr) => expr is string;
798
+ /**
799
+ * @public Checks whether the given value is a literal expression.
800
+ * @since 0.1.0
801
+ * @version 1
802
+ */
803
+ literal: (expr: Expr) => expr is ExprLiteral;
804
+ /**
805
+ * @public Checks whether the given value is a column expression.
806
+ * @since 0.1.0
807
+ * @version 1
808
+ */
809
+ column: (expr: Expr) => expr is Column;
810
+ };
811
+ /**
812
+ * @public Query logger interface.
813
+ * @since 0.1.0
814
+ * @version 2
815
+ */
816
+ interface ILogger {
817
+ /**
818
+ * @public Logs a query that has executed successfully.
819
+ * @since 0.1.17
820
+ * @version 1
821
+ */
822
+ debug(sql: string, params: any[]): void;
823
+ /**
824
+ * @public Logs a query that has failed with an error.
825
+ * @since 0.1.12
826
+ * @version 2
827
+ */
828
+ error(sql: string, params: any[], error?: Error): void;
829
+ }
830
+ /**
831
+ * @public Represents the direction of ordering in an ORDER BY
832
+ * clause.
833
+ * @since 0.1.0
834
+ * @version 1
835
+ */
836
+ type OrderDirection = 'ASC' | 'DESC';
837
+ /**
838
+ * @public Represents a create table statement.
839
+ * @since 0.1.0
840
+ * @version 1
841
+ */
842
+ type CreateTableStatement = {
843
+ /**
844
+ * @public The name of the table to create.
845
+ * @since 0.1.0
846
+ * @version 1
847
+ */
848
+ table: string;
849
+ /**
850
+ * @public The columns of the table.
851
+ * @since 0.1.0
852
+ * @version 1
853
+ */
854
+ columns: Column[];
855
+ /**
856
+ * @public Whether to include an IF NOT EXISTS clause in the
857
+ * create table statement.
858
+ * @since 0.1.0
859
+ * @version 1
860
+ */
861
+ ifNotExists?: true;
862
+ /**
863
+ * @public Whether to create the table as unlogged (won't produce
864
+ * WAL entries).
865
+ * @since 0.1.0
866
+ * @version 1
867
+ */
868
+ unlogged?: true;
869
+ };
870
+ /**
871
+ * @public Represents an insert statement.
872
+ * @since 0.1.0
873
+ * @version 1
874
+ */
875
+ type InsertStatement = {
876
+ /**
877
+ * @public The table to insert to.
878
+ * @since 0.1.0
879
+ * @version 1
880
+ */
881
+ table: string;
882
+ /**
883
+ * @public The records to insert.
884
+ * @since 0.1.0
885
+ * @version 1
886
+ */
887
+ records: Record<string, any>[];
888
+ /**
889
+ * @public The shape of the records being inserted.
890
+ * @since 0.1.0
891
+ * @version 1
892
+ */
893
+ insertShape: Record<string, Column>;
894
+ /**
895
+ * @public The shape of the returned rows.
896
+ * @since 0.1.0
897
+ * @version 1
898
+ */
899
+ returnShape: Record<string, Column>;
900
+ };
901
+ /**
902
+ * @public Represents a select statement.
903
+ * @since 0.1.0
904
+ * @version 1
905
+ */
906
+ type SelectStatement = {
907
+ /**
908
+ * @public A map of table aliases to their actual name.
909
+ * @since 0.1.0
910
+ * @version 1
911
+ */
912
+ registry: Record<string, string>;
913
+ /**
914
+ * @public The query's selection.
915
+ * @since 0.1.0
916
+ * @version 1
917
+ */
918
+ select: Record<string, Column>;
919
+ /**
920
+ * @public The alias of the table from which to select.
921
+ * @since 0.1.0
922
+ * @version 1
923
+ */
924
+ from: string;
925
+ /**
926
+ * @public The query's join clauses.
927
+ * @since 0.1.9
928
+ * @version 1
929
+ */
930
+ joins?: Join[];
931
+ /**
932
+ * @public The query's where clause.
933
+ * @since 0.1.0
934
+ * @version 1
935
+ */
936
+ where?: Expr;
937
+ /**
938
+ * @public The query's order by clause.
939
+ * @since 0.1.0
940
+ * @version 1
941
+ */
942
+ orderBy?: (readonly [Expr, OrderDirection])[];
943
+ /**
944
+ * @public The maximum number of rows to return.
945
+ * @since 0.1.0
946
+ * @version 1
947
+ */
948
+ limit?: number;
949
+ /**
950
+ * @public The number of rows to skip before starting to return
951
+ * rows.
952
+ * @since 0.1.0
953
+ * @version 1
954
+ */
955
+ offset?: number;
956
+ };
957
+ /**
958
+ * @private A codec for encoding data to the database's expected type,
959
+ * and decoding data from the database to the application's
960
+ * expected type.
961
+ * @since 0.1.0
962
+ * @version 1
963
+ */
964
+ type Codec<Decoded = any, Encoded = any> = {
965
+ /**
966
+ * @private Encodes a value fomr the application's type into the
967
+ * database's expected type.
968
+ * @since 0.1.0
969
+ * @version 1
970
+ */
971
+ encode: (value: Decoded) => Encoded;
972
+ /**
973
+ * @private Decodes a value from the database's type into the
974
+ * application's expected type.
975
+ * @since 0.1.0
976
+ * @version 1
977
+ */
978
+ decode: (value: Encoded) => Decoded;
979
+ };
980
+ /**
981
+ * @public A registry of {@link Codec} for all supported primitive
982
+ * types.
983
+ * @since 0.1.0
984
+ * @version 1
985
+ */
986
+ type CodecsRegistry = {
987
+ [K in Primitive]: Codec<any, any>;
988
+ };
989
+ /**
990
+ * @public A mapping of qx's primitive types to their native database
991
+ * types.
992
+ */
993
+ type PrimitiveToNativeTypeFactory = {
994
+ [K in Primitive]: (col: Column) => string;
995
+ };
996
+ /**
997
+ * @public Represents the result of a statement rendered into DDL.
998
+ * @since 0.1.0
999
+ * @version 1
1000
+ */
1001
+ type DDL = {
1002
+ sql: string;
1003
+ params: any[];
1004
+ };
1005
+ /**
1006
+ * @public The interface that all database adapters must implement.
1007
+ * @since 0.1.0
1008
+ * @version 1
1009
+ */
1010
+ interface IDatabase {
1011
+ /**
1012
+ * @public Attaches a query logger to the database adapter.
1013
+ * @since 0.1.0
1014
+ * @version 1
1015
+ */
1016
+ attachLogger(logger: ILogger): this;
1017
+ /**
1018
+ * @public Executes a create table statement.
1019
+ * @since 0.1.0
1020
+ * @version 1
1021
+ */
1022
+ createTable(op: CreateTableStatement): Promise<void>;
1023
+ /**
1024
+ * @public Executes an insert statement, returning the newly
1025
+ * inserted rows.
1026
+ * @since 0.1.0
1027
+ * @version 1
1028
+ */
1029
+ insert(op: InsertStatement): Promise<any>;
1030
+ /**
1031
+ * @public Executes a select statement returning all matching rows.
1032
+ * @since 0.1.0
1033
+ * @version 1
1034
+ */
1035
+ query(op: SelectStatement): Promise<any[]>;
1036
+ /**
1037
+ * @public Executes a function within a transaction.
1038
+ * @since 0.1.10
1039
+ * @version 1
1040
+ */
1041
+ transaction<T>(fn: () => Promise<T>): Promise<T>;
1042
+ }
1043
+ /**
1044
+ * @public Executes the given function within a transaction.
1045
+ * @since 0.1.10
1046
+ * @version 1
1047
+ */
1048
+ declare const transaction: <T>(db: IDatabase, fn: () => Promise<T>) => Promise<T>;
1049
+ /**
1050
+ * @public Insert statement builder.
1051
+ * @since 0.1.0
1052
+ * @version 1
1053
+ */
1054
+ declare class InsertBuilder<T extends Table> {
1055
+ private readonly table;
1056
+ private rows;
1057
+ constructor(table: T, rows?: InferForInsert<T>[]);
1058
+ /**
1059
+ * @public Adds one or more rows to be inserted.
1060
+ * @since 0.1.0
1061
+ * @version 2
1062
+ */
1063
+ values(rows: InferForInsert<T> | InferForInsert<T>[]): this;
1064
+ /**
1065
+ * @public Executes the insert statement onto the given database.
1066
+ * @since 0.1.0
1067
+ * @version 1
1068
+ */
1069
+ insert(db: IDatabase): Promise<Expand<Infer<T>>[]>;
1070
+ }
1071
+ /**
1072
+ * @public Starts an insert statement for the given table.
1073
+ * @since 0.1.0
1074
+ * @version 1
1075
+ */
1076
+ declare const into: <T extends Table>(table: T) => InsertBuilder<T>;
1077
+ /**
1078
+ * @public Represents a join clause in a select statement.
1079
+ * @since 0.1.9
1080
+ * @version 1
1081
+ */
1082
+ type Join = {
1083
+ type: 'INNER JOIN' | 'LEFT OUTER JOIN' | 'RIGHT OUTER JOIN';
1084
+ table: string;
1085
+ alias: string;
1086
+ on: Expr;
1087
+ };
1088
+ /**
1089
+ * @private It's the object type for a query being built, that will
1090
+ * later been transformed into a {@link SelectStatement}.
1091
+ * @since 0.1.0
1092
+ * @version 1
1093
+ */
1094
+ type Query<T extends Record<string, Aliased<string, Table>> = Record<string, Aliased<string, Table>>, S extends Record<string, Column> = Record<string, Column>> = {
1095
+ /**
1096
+ * @private A registry of all aliased tables in the query.
1097
+ * @since 0.1.0
1098
+ * @version 1
1099
+ */
1100
+ registry: T;
1101
+ /**
1102
+ * @private The query's selection.
1103
+ * @since 0.1.0
1104
+ * @version 1
1105
+ */
1106
+ select: S;
1107
+ /**
1108
+ * @private The alias of the table from which to select.
1109
+ * @since 0.1.0
1110
+ * @version 1
1111
+ */
1112
+ from: string;
1113
+ /**
1114
+ * @private The query's join clauses.
1115
+ * @since 0.1.9
1116
+ * @version 1
1117
+ */
1118
+ joins?: Join[];
1119
+ /**
1120
+ * @private The query's where clause.
1121
+ * @since 0.1.0
1122
+ * @version 1
1123
+ */
1124
+ where?: Expr;
1125
+ /**
1126
+ * @private The query's order by clause.
1127
+ * @since 0.1.0
1128
+ * @version 1
1129
+ */
1130
+ orderBy?: (readonly [Expr, OrderDirection])[];
1131
+ /**
1132
+ * @private The query's limit.
1133
+ * @since 0.1.0
1134
+ * @version 1
1135
+ */
1136
+ limit?: number;
1137
+ /**
1138
+ * @private The query's offset.
1139
+ * @since 0.1.0
1140
+ * @version 1
1141
+ */
1142
+ offset?: number;
1143
+ };
1144
+ /**
1145
+ * @private Infers the selection output type of given query.
1146
+ * @since 0.1.0
1147
+ * @version 2
1148
+ */
1149
+ type InferSelection<T extends Query> = {
1150
+ [K in keyof T['select'] & string]: T['select'][K]['inferOutput'];
1151
+ };
1152
+ /**
1153
+ * @public Select statement builder with a fluent API.
1154
+ *
1155
+ * We only need to keep track of two types in here:
1156
+ * - the registry of aliased tables, so they can be
1157
+ * referenced in expressions and selection; and
1158
+ * - the selected columns, so we can infer the returning
1159
+ * type of the query.
1160
+ * @since 0.1.0
1161
+ * @version 1
1162
+ */
1163
+ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S extends Record<string, Column>> {
1164
+ readonly query: Query<T, S>;
1165
+ constructor(query: Query<T, S>);
1166
+ /**
1167
+ * @public Executes the select statement against the given database,
1168
+ * returning all matching rows.
1169
+ * @since 0.1.0
1170
+ * @version 1
1171
+ */
1172
+ all(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>>[]>;
1173
+ /**
1174
+ * @public Checks whether any rows exist matching the query.
1175
+ * @since 0.1.6
1176
+ * @version 1
1177
+ */
1178
+ exists(db: IDatabase): Promise<boolean>;
1179
+ /**
1180
+ * @public Adds an INNER JOIN clause to the query.
1181
+ * @since 0.1.9
1182
+ * @version 1
1183
+ */
1184
+ innerJoin<U extends Aliased<string, Table>>(table: U, on: (registry: Expand<T & {
1185
+ [K in U[typeof TABLE_ALIAS]]: U;
1186
+ }>) => Expr): QueryBuilder<Expand<T & { [K in U[typeof TABLE_ALIAS]]: U; }>, S>;
1187
+ /**
1188
+ * @public Sets a limit on the number of rows to be returned.
1189
+ * @since 0.1.0
1190
+ * @version 1
1191
+ */
1192
+ limit(n: number): QueryBuilder<T, S>;
1193
+ /**
1194
+ * @public Sets an offset for the rows to be returned.
1195
+ * @since 0.1.0
1196
+ * @version 1
1197
+ */
1198
+ offset(n: number): QueryBuilder<T, S>;
1199
+ /**
1200
+ * @public Executes the select statement against the given database,
1201
+ * returning the first matching row.
1202
+ * @since 0.1.0
1203
+ * @version 1
1204
+ */
1205
+ one(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>> | null>;
1206
+ /**
1207
+ * @public Defines the order by clause of the query.
1208
+ * @since 0.1.0
1209
+ * @version 1
1210
+ */
1211
+ orderBy(fn: (registry: T) => (readonly [Expr, OrderDirection])[]): QueryBuilder<T, S>;
1212
+ /**
1213
+ * @public Defines the selection of the query.
1214
+ * @since 0.1.0
1215
+ * @version 1
1216
+ */
1217
+ select<U extends Record<string, Column>>(fn: (r: T) => U): QueryBuilder<T, U>;
1218
+ /**
1219
+ * @public Defines the where clause of the query. If there's already
1220
+ * a WHERE clause in the query, the new clause will be
1221
+ * ANDed to the existing one.
1222
+ * @since 0.1.0
1223
+ * @version 1
1224
+ */
1225
+ where<E extends Expr>(fn: (tables: T) => E): QueryBuilder<T, S>;
1226
+ }
1227
+ /**
1228
+ * @public Starts a select statement from the given table.
1229
+ * @since 0.1.0
1230
+ * @version 1
1231
+ */
1232
+ declare const from: <S extends string, T extends Aliased<S, Table>>(table: T) => QueryBuilder<{ [K in T[typeof TABLE_ALIAS]]: T; }, T>;
1233
+ export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type Join, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, defineColumn as column, defineTable as table, expr, from, into, is, transaction, types as t, };