kern-orm 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/database-CP4oapsQ.mjs +4693 -0
- package/dist/{index--0tyG7sU.d.mts → index-CoKpXcBp.d.mts} +1347 -703
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +87 -3961
- package/dist/testing.d.mts +28 -2
- package/dist/testing.mjs +530 -1
- package/dist/{text-BTFErHIi.mjs → text-Cti9QLK7.mjs} +7 -1
- package/dist/types-DfoL8Oi9.mjs +248 -0
- package/dist/types.d.mts +49 -49
- package/dist/types.mjs +2 -247
- package/package.json +3 -1
|
@@ -209,9 +209,9 @@ interface SqlStatement {
|
|
|
209
209
|
declare function createStatement(text: string, parameters?: readonly TextValue[]): SqlStatement;
|
|
210
210
|
/** Result of a statement. */
|
|
211
211
|
interface DriverResult {
|
|
212
|
-
/** Command tag that PostgreSQL completed the statement with, such as `SELECT` or `ROLLBACK` for a `COMMIT` of a failed transaction. */
|
|
213
|
-
readonly command: string;
|
|
214
|
-
/** Number of rows that the command tag reports, such as the rows that an `UPDATE` changes, also without `RETURNING`; `0`
|
|
212
|
+
/** Command of the tag that PostgreSQL completed the statement with, such as `SELECT` or `ROLLBACK` for a `COMMIT` of a failed transaction; a driver may give only the first word of a tag of several words, such as `CREATE` of `CREATE TABLE`; `null` for a statement that PostgreSQL completes without a tag, such as an empty text or only a comment. */
|
|
213
|
+
readonly command: string | null;
|
|
214
|
+
/** Number of rows that the command tag reports, such as the rows that an `UPDATE` changes, also without `RETURNING`; `0` when PostgreSQL reports none, such as for `BEGIN` or a statement without a command tag. */
|
|
215
215
|
readonly count: number;
|
|
216
216
|
/** Names of the result columns in their order, also when the statement returns no rows; none for a statement without result columns, such as `BEGIN` or an `UPDATE` without `RETURNING`. */
|
|
217
217
|
readonly columns: readonly string[];
|
|
@@ -278,6 +278,13 @@ interface RecordTemplateField {
|
|
|
278
278
|
readonly key: string;
|
|
279
279
|
readonly value: unknown;
|
|
280
280
|
}
|
|
281
|
+
/** How code converts the decoded result of a query, by `map` and `transform` of the query: `map` runs before `transform`, since `map` does not apply after `transform`. */
|
|
282
|
+
interface ResultConversion {
|
|
283
|
+
/** Converts each record or value, also `null` of a `NULL` value, and is not called for a missing row. */
|
|
284
|
+
readonly map: ((row: unknown) => unknown) | undefined;
|
|
285
|
+
/** Converts the whole result, once: an array of rows, one row, or `null` or `undefined` of a missing row. */
|
|
286
|
+
readonly transform: ((result: unknown) => unknown) | undefined;
|
|
287
|
+
}
|
|
281
288
|
/**
|
|
282
289
|
* How a query returns its rows: `many` as an array; `one` as one row, rejecting without it; `nullable` as one row or `null` without it; `optional` as one row or `undefined` without it.
|
|
283
290
|
*
|
|
@@ -303,6 +310,8 @@ interface ResultPlan extends ResultMode {
|
|
|
303
310
|
* A record starts from it, so its keys keep their order whichever of them a row holds.
|
|
304
311
|
*/
|
|
305
312
|
readonly recordTemplate?: readonly RecordTemplateField[] | undefined;
|
|
313
|
+
/** How code converts the result that the plan decodes; `undefined` for a query without `transform` and `map`. */
|
|
314
|
+
readonly conversion?: ResultConversion | undefined;
|
|
306
315
|
}
|
|
307
316
|
/** Error thrown when a query of one row finds no row, executed or nested, unless a required relation guarantees the row. */
|
|
308
317
|
declare class NotFoundError extends Error {
|
|
@@ -328,13 +337,23 @@ declare class RequiredRelationError extends Error {
|
|
|
328
337
|
/** Path of the field of the relation from the result field that holds it, such as `fields[2].form`, after the index of the row of an executed query of rows `many`, such as `[2].form`. */
|
|
329
338
|
path: string);
|
|
330
339
|
}
|
|
331
|
-
/** Error thrown when a row does not match the result fields
|
|
340
|
+
/** Error thrown when a row does not match the result fields, a value fails to decode, or a callback of `transform` or `map` fails; the failure is its cause. */
|
|
332
341
|
declare class ResultDecodeError extends Error {
|
|
333
|
-
/**
|
|
342
|
+
/**
|
|
343
|
+
* Path of the value from its result field, such as `fields[2].label`, after the index of the row of an executed query of rows `many`, such as `[2].label`.
|
|
344
|
+
*
|
|
345
|
+
* A callback of `map` that fails has the path of its row, such as `fields[2]` or `[2]`, and one of `transform` the path of the field of its nested rows, such as `fields`.
|
|
346
|
+
* It is `undefined` for a row of the driver of another width than the result fields, and for a callback that fails on the only row or the whole result of an executed query.
|
|
347
|
+
*/
|
|
334
348
|
readonly path: string | undefined;
|
|
335
349
|
readonly name = "ResultDecodeError";
|
|
336
350
|
constructor(message: string,
|
|
337
|
-
/**
|
|
351
|
+
/**
|
|
352
|
+
* Path of the value from its result field, such as `fields[2].label`, after the index of the row of an executed query of rows `many`, such as `[2].label`.
|
|
353
|
+
*
|
|
354
|
+
* A callback of `map` that fails has the path of its row, such as `fields[2]` or `[2]`, and one of `transform` the path of the field of its nested rows, such as `fields`.
|
|
355
|
+
* It is `undefined` for a row of the driver of another width than the result fields, and for a callback that fails on the only row or the whole result of an executed query.
|
|
356
|
+
*/
|
|
338
357
|
path: string | undefined, options?: ErrorOptions);
|
|
339
358
|
}
|
|
340
359
|
/** Tells whether `T` is `any`: only `any` turns `1 & T` into a type that `0` extends. */
|
|
@@ -390,6 +409,11 @@ interface TableOptions<Column extends string = string, PrimaryKeyColumn extends
|
|
|
390
409
|
/** Unique keys of several columns; mark a unique key of one column with `unique()`. */
|
|
391
410
|
readonly unique?: readonly (readonly [Column, Column, ...Column[]])[];
|
|
392
411
|
}
|
|
412
|
+
/** Options of `createTableFactory`. */
|
|
413
|
+
interface TableFactoryOptions {
|
|
414
|
+
/** Returns the physical name of a column from its key, such as `toSnakeCase` does, for each column without an explicit `columnName()`. */
|
|
415
|
+
readonly columnName: (key: string) => string;
|
|
416
|
+
}
|
|
393
417
|
/** Record shape returned when all columns of a table are selected. */
|
|
394
418
|
type Selectable<T extends AnyTable> = { [K in keyof T["columns"]]: TypeOutput<T["columns"][K]["type"]>; };
|
|
395
419
|
/** Input values of the types of the columns. */
|
|
@@ -462,10 +486,123 @@ declare function defineTable<Name extends string, Definitions extends ColumnDefi
|
|
|
462
486
|
* A key of one column is marked on its column, and `options` list the columns of the keys of several columns.
|
|
463
487
|
*/
|
|
464
488
|
declare function defineTable<Name extends string, Definitions extends ColumnDefinitions, const Options extends TableOptions<ColumnKey<ToColumns<Definitions>>, PrimaryKeyOption<ToColumns<Definitions>>> | undefined>(name: Name, definitions: Definitions, options: Options): Table<Name, ToColumns<Definitions>, DefinedKeys<ToColumns<Definitions>, Options>>;
|
|
465
|
-
/**
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
489
|
+
/**
|
|
490
|
+
* Creates a table-definition function with the given naming convention, for an application to define its tables with.
|
|
491
|
+
*
|
|
492
|
+
* Its tables have the same types as those of `defineTable`.
|
|
493
|
+
*/
|
|
494
|
+
declare function createTableFactory(factoryOptions: TableFactoryOptions): typeof defineTable;
|
|
495
|
+
/** Records of a query without selected fields. */
|
|
496
|
+
type EmptyRecord = Record<never, never>;
|
|
497
|
+
/** State of a query in types: the record or value of its rows, how its rows become its result, and the relations it selects records of. */
|
|
498
|
+
interface QueryState {
|
|
499
|
+
/** Record of a row, or the value of its only field for a query made by `get`, `pluck`, an aggregate method, such as `count`, or `exists`, or what the callback of `map` or `transform` returns. */
|
|
500
|
+
readonly result: unknown;
|
|
501
|
+
/** How the rows become the result. */
|
|
502
|
+
readonly rows: ResultRows;
|
|
503
|
+
/**
|
|
504
|
+
* What a row is: a record for `false`, the value of its only field for `true`, or what a callback of code returns: of `map` for `"mapped"`, whichever the row was before it, or of `transform` for `"transformed"`, of the whole result.
|
|
505
|
+
*
|
|
506
|
+
* The states of the methods that read the fields of a row or the value of its only field narrow it to `false` or `true`, so that they reject a query that code converts.
|
|
507
|
+
*/
|
|
508
|
+
readonly value: boolean | "mapped" | "transformed";
|
|
509
|
+
/** Relations whose records the query and the queries nested in it select, which a `Db` must have. */
|
|
510
|
+
readonly uses: UsedRelation;
|
|
511
|
+
}
|
|
512
|
+
/** Type of the value of a query: the values that it encodes, such as the fallback of `coalesce`, and its operators, which `expr` gives its expression. */
|
|
513
|
+
interface QueryValueType {
|
|
514
|
+
readonly input: unknown;
|
|
515
|
+
readonly operators: OperatorDefinitions;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* State of a select query in types: its table, the relations of its schema, the record or value of its rows, the type of its value or the types of its fields, how its rows become its result, and the relations it selects records of.
|
|
519
|
+
*
|
|
520
|
+
* Methods change only their own parts of the state.
|
|
521
|
+
*/
|
|
522
|
+
interface SelectQueryState extends QueryState {
|
|
523
|
+
readonly table: AnyTable;
|
|
524
|
+
readonly schema: SchemaContext;
|
|
525
|
+
/** Type of the value of a query of values, a `QueryValueType`; `never` for a query of records. */
|
|
526
|
+
readonly valueType: unknown;
|
|
527
|
+
/** Types of the values of the selected fields of a query of records, the values that each encodes and its operators, which the callback of `having` gives its references: a `SelectedFieldTypes`, or `EmptyRecord` without selected fields. */
|
|
528
|
+
readonly fieldTypes: unknown;
|
|
529
|
+
/**
|
|
530
|
+
* Whether a required relation guarantees the row of the query, such as of `field.form`.
|
|
531
|
+
*
|
|
532
|
+
* The relation does not guarantee a record that also passes a filter, so `where`, `find`, and `findBy` end the guarantee and make rows `one` `nullable`, or `optional` for a query of values; `optional`, `nullable`, and `required` end it as well.
|
|
533
|
+
* The callback of a helper starts with it, so that the effect of the helper records whether the callback filters.
|
|
534
|
+
*/
|
|
535
|
+
readonly guaranteed: boolean;
|
|
536
|
+
/** Whether the query is made by an aggregate method, such as `count`, or by `exists`, or converted by `map`, which makes `get`, `pluck`, the aggregate methods, and `exists` reject it. */
|
|
537
|
+
readonly aggregate: boolean;
|
|
538
|
+
}
|
|
539
|
+
/** State of a query over table `T` with schema context `C`, whatever its result. */
|
|
540
|
+
interface QueryStateOf<T extends AnyTable, C extends SchemaContext = SchemaContext> extends SelectQueryState {
|
|
541
|
+
readonly table: T;
|
|
542
|
+
readonly schema: C;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* State of a query that starts from table `T` with schema context `C`: rows as records without fields, all of them or of the records of relation `U` when given, in rows mode `Rows`, guaranteed by a required relation when `Guaranteed`.
|
|
546
|
+
*
|
|
547
|
+
* Without relations in `C`, it also types a query of the table in a schema that has relations of the table.
|
|
548
|
+
*/
|
|
549
|
+
interface InitialQueryState<T extends AnyTable, C extends SchemaContext = NoRelations, U extends UsedRelation = never, Rows extends ResultRows = "many", Guaranteed extends boolean = false> extends QueryStateOf<T, C> {
|
|
550
|
+
readonly result: EmptyRecord;
|
|
551
|
+
readonly valueType: never;
|
|
552
|
+
readonly fieldTypes: EmptyRecord;
|
|
553
|
+
readonly rows: Rows;
|
|
554
|
+
readonly value: false;
|
|
555
|
+
readonly uses: U;
|
|
556
|
+
readonly guaranteed: Guaranteed;
|
|
557
|
+
readonly aggregate: false;
|
|
558
|
+
}
|
|
559
|
+
/** Mark of `aggregate: false` of a query of groups, which aggregate methods replace. */
|
|
560
|
+
interface Grouped {
|
|
561
|
+
readonly grouped: true;
|
|
562
|
+
}
|
|
563
|
+
/** Key of the value of a query in an expression, for types only. */
|
|
564
|
+
declare const scalarValue: unique symbol;
|
|
565
|
+
/** Value of a query of state `S` in an expression: its value, or `NULL` without its row unless the query always has it. */
|
|
566
|
+
type ScalarValue<S extends SelectQueryState> = S["aggregate"] extends true ? S["result"] : S["guaranteed"] extends true ? S["result"] : S["result"] | null;
|
|
567
|
+
/**
|
|
568
|
+
* Query of one value of a table, such as of `get`, of an aggregate, or of `exists`, which an expression takes as a scalar subquery of its value, or as `EXISTS` of the rows of a query of `exists`.
|
|
569
|
+
*
|
|
570
|
+
* `T` is its value there: without its row, a query of `get` is `NULL`, unless a required relation guarantees the row, such as of `field.form`.
|
|
571
|
+
*/
|
|
572
|
+
interface ScalarQuery<T = unknown> {
|
|
573
|
+
/** Value of the query in an expression, from which an expression infers `T`. */
|
|
574
|
+
readonly [scalarValue]: T;
|
|
575
|
+
readonly types: ScalarQueryState;
|
|
576
|
+
readonly exists: unknown;
|
|
577
|
+
}
|
|
578
|
+
/** Value of query `Q` of one value in an expression. */
|
|
579
|
+
type ScalarQueryValue<Q extends ScalarQuery> = Q[typeof scalarValue];
|
|
580
|
+
/** Relations whose records `E`, an expression or a query of one value that a callback returns, selects: those of a query, as an expression does not carry the relations of its queries. */
|
|
581
|
+
type OperandUses<E> = E extends ScalarQuery ? E["types"]["uses"] : never;
|
|
582
|
+
/** State of a query of one value, which an expression takes. */
|
|
583
|
+
interface ScalarQueryState extends SelectQueryState {
|
|
584
|
+
readonly rows: "one" | "optional";
|
|
585
|
+
readonly value: true;
|
|
586
|
+
}
|
|
587
|
+
/** State of a query of one value with the type of its value, whose expression `expr` gives. */
|
|
588
|
+
interface ExpressionQueryState extends ScalarQueryState {
|
|
589
|
+
readonly valueType: QueryValueType;
|
|
590
|
+
}
|
|
591
|
+
/** Query of one value of a table with the type of its value, such as of `get` or of an aggregate, which gives the expression of a function the decoding and the operators of its value. */
|
|
592
|
+
interface TypedScalarQuery extends ScalarQuery {
|
|
593
|
+
readonly types: ExpressionQueryState;
|
|
594
|
+
}
|
|
595
|
+
/** Query of values of a table of any rows, such as of `pluck`, whose value is `T`, which `in` takes as a subquery of its values. */
|
|
596
|
+
interface ValuesQuery<T = unknown> {
|
|
597
|
+
readonly types: ValuesQueryState<T>;
|
|
598
|
+
readonly exists: unknown;
|
|
599
|
+
}
|
|
600
|
+
/** Value of query `Q` of values, of each of its rows. */
|
|
601
|
+
type ValuesQueryValue<Q extends ValuesQuery> = Q["types"]["result"];
|
|
602
|
+
/** State of a query of values whose value is `T`. */
|
|
603
|
+
interface ValuesQueryState<T> extends SelectQueryState {
|
|
604
|
+
readonly result: T;
|
|
605
|
+
readonly value: true;
|
|
469
606
|
}
|
|
470
607
|
/**
|
|
471
608
|
* Value compared with an expression of type `T` accepting values of type `I`: a value, an expression, or a query of one value, such as of `get` or of an aggregate; `null` is matched with `isNull` instead.
|
|
@@ -485,6 +622,39 @@ interface ComparisonSignature extends OperatorSignature {
|
|
|
485
622
|
* Operators of custom types build comparisons with it, such as `compare(self, "ILIKE", pattern)`.
|
|
486
623
|
*/
|
|
487
624
|
declare function compare<T, V>(self: AnyExpression<T>, operator: ComparisonNode["operator"], value: V): Expression<BooleanResult<T | OperandValue<V>>>;
|
|
625
|
+
/** Runtime mark of the operators of `equalityOperators`, which tell that a type has SQL `=`. */
|
|
626
|
+
declare const equalityBrand: unique symbol;
|
|
627
|
+
/** Generic operator of `equalityOperators`, which gives the type it is added to SQL `=`. */
|
|
628
|
+
interface EqualityOperator<S extends OperatorSignature> extends GenericOperator<S> {
|
|
629
|
+
readonly [equalityBrand]: true;
|
|
630
|
+
}
|
|
631
|
+
/** Key of the method of a query of a table that returns its node as the values of `in`, or rejects a query of records. */
|
|
632
|
+
declare const valuesQueryNode: unique symbol;
|
|
633
|
+
/** Signature of `in`: membership in a list of operands of the expression type, or in the values of a query, such as of `pluck`; an empty list or query yields `FALSE`. */
|
|
634
|
+
interface InSignature extends OperatorSignature {
|
|
635
|
+
readonly method: <const L extends readonly Operand<this["T"], this["I"]>[] | ValuesQuery<NonNullable<this["T"]> | null>>(list: L) => Expression<L extends readonly unknown[] ? L extends readonly [] ? boolean : BooleanResult<this["T"] | OperandValue<L[number]>> : BooleanResult<this["T"] | (L extends ValuesQuery ? ValuesQueryValue<L> : never)>>;
|
|
636
|
+
}
|
|
637
|
+
/** Operators of `equalityOperators`. */
|
|
638
|
+
type EqualityOperators = {
|
|
639
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
640
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
641
|
+
in: EqualityOperator<InSignature>;
|
|
642
|
+
};
|
|
643
|
+
/** Tells whether operators `O` include an operator of `equalityOperators`, so that their type has SQL `=`. */
|
|
644
|
+
type HasEquality<O> = true extends { [K in keyof O]: O[K] extends {
|
|
645
|
+
readonly [equalityBrand]: true;
|
|
646
|
+
} ? true : false; }[keyof O] ? true : false;
|
|
647
|
+
/**
|
|
648
|
+
* Operators of SQL equality: `eq`, `ne`, and `in`; `orderingOperators` include them.
|
|
649
|
+
*
|
|
650
|
+
* A type has SQL `=` only with these operators, so that an array of it has them too; another operator named `eq` does not give it `=`.
|
|
651
|
+
*/
|
|
652
|
+
declare const equalityOperators: EqualityOperators;
|
|
653
|
+
/** Physical placement of the tables of a query, fixed at compilation. */
|
|
654
|
+
interface CompileOptions {
|
|
655
|
+
/** PostgreSQL schema qualifying every table; tables stay unqualified when it is not set. */
|
|
656
|
+
schemaName?: string;
|
|
657
|
+
}
|
|
488
658
|
/** Selection of the rows of a nested query as the value of the field `key`. */
|
|
489
659
|
interface NestedSelectionNode {
|
|
490
660
|
readonly key: string;
|
|
@@ -510,13 +680,31 @@ interface QueryNode extends ResultMode {
|
|
|
510
680
|
/** Fields in the order of the result, with unique keys; a query without them returns records without fields. */
|
|
511
681
|
readonly projections: readonly SelectedNode[];
|
|
512
682
|
readonly filters: QueryFilters;
|
|
683
|
+
/** Keys of the groups of the rows; a query with them gives one row of each group. */
|
|
684
|
+
readonly groupBy: readonly ExpressionNode[];
|
|
685
|
+
/** Condition of the groups, apart from `filters`, which choose the rows that they group. */
|
|
686
|
+
readonly having: ExpressionNode | undefined;
|
|
513
687
|
readonly orderBy: readonly OrderNode[];
|
|
514
688
|
readonly limit: ExpressionNode | undefined;
|
|
515
689
|
readonly offset: ExpressionNode | undefined;
|
|
690
|
+
/** Whether `distinct` removes the duplicates of the rows, comparing the fields that the query selects when it compiles. */
|
|
691
|
+
readonly distinct: boolean;
|
|
692
|
+
/** Keys of `distinctOn`, whose equal values give one row, the first in the order of the query; none without `distinctOn`. */
|
|
693
|
+
readonly distinctOn: readonly ExpressionNode[];
|
|
694
|
+
/**
|
|
695
|
+
* Fields of a query of `distinct` or `distinctOn` that an aggregate method or `exists` replaced, or `undefined` otherwise.
|
|
696
|
+
*
|
|
697
|
+
* `distinct` compares them, and the keys, the order, and the argument of the aggregate read a selected field through its projection.
|
|
698
|
+
*/
|
|
699
|
+
readonly distinctFields: readonly SelectedNode[] | undefined;
|
|
700
|
+
/** Lock of the rows that the query reads. */
|
|
701
|
+
readonly lock: RowLockNode | undefined;
|
|
516
702
|
/** Whether a required relation guarantees the row of the query, so that a filter makes a query of rows `one` resolve without it; see `SelectQueryState`. */
|
|
517
703
|
readonly guaranteed: boolean;
|
|
518
704
|
/** Whether `all` allows a mutation of the query to change every row of the table without a filter. */
|
|
519
705
|
readonly all: boolean;
|
|
706
|
+
/** How `map` and `transform` convert the decoded result. */
|
|
707
|
+
readonly conversion: ResultConversion | undefined;
|
|
520
708
|
}
|
|
521
709
|
/**
|
|
522
710
|
* Mutation of a table that returns the fields of the rows it changes, which may nest queries, or the number of the rows without them.
|
|
@@ -530,68 +718,15 @@ interface RecordQueryNode {
|
|
|
530
718
|
/** Fields in the order of the record, with unique keys. */
|
|
531
719
|
readonly projections: readonly SelectedNode[];
|
|
532
720
|
}
|
|
533
|
-
/** Records of a query without selected fields. */
|
|
534
|
-
type EmptyRecord = Record<never, never>;
|
|
535
|
-
/** State of a query in types: the record or value of its rows, how its rows become its result, and the relations it selects records of. */
|
|
536
|
-
interface QueryState extends ResultMode {
|
|
537
|
-
/** Record of a row, or the value of its only field for a query made by `get`, `pluck`, an aggregate method, such as `count`, or `exists`. */
|
|
538
|
-
readonly result: unknown;
|
|
539
|
-
/** Relations whose records the query and the queries nested in it select, which a `Db` must have. */
|
|
540
|
-
readonly uses: UsedRelation;
|
|
541
|
-
}
|
|
542
|
-
/** Type of the value of a query: the values that it encodes, such as the fallback of `coalesce`, and its operators, which `expr` gives its expression. */
|
|
543
|
-
interface QueryValueType {
|
|
544
|
-
readonly input: unknown;
|
|
545
|
-
readonly operators: OperatorDefinitions;
|
|
546
|
-
}
|
|
547
|
-
/**
|
|
548
|
-
* State of a select query in types: its table, the relations of its schema, the record or value of its rows and the type of its value, how its rows become its result, and the relations it selects records of.
|
|
549
|
-
*
|
|
550
|
-
* Methods change only their own parts of the state.
|
|
551
|
-
*/
|
|
552
|
-
interface SelectQueryState extends QueryState {
|
|
553
|
-
readonly table: AnyTable;
|
|
554
|
-
readonly schema: SchemaContext;
|
|
555
|
-
/** Type of the value of a query of values, a `QueryValueType`; `never` for a query of records. */
|
|
556
|
-
readonly valueType: unknown;
|
|
557
|
-
/**
|
|
558
|
-
* Whether a required relation guarantees the row of the query, such as of `field.form`.
|
|
559
|
-
*
|
|
560
|
-
* The relation does not guarantee a record that also passes a filter, so `where`, `find`, and `findBy` end the guarantee and make rows `one` `nullable`, or `optional` for a query of values; `optional`, `nullable`, and `required` end it as well.
|
|
561
|
-
* The callback of a helper starts with it, so that the effect of the helper records whether the callback filters.
|
|
562
|
-
*/
|
|
563
|
-
readonly guaranteed: boolean;
|
|
564
|
-
/** Whether the query is made by an aggregate method, such as `count`, or by `exists`, which makes `get`, `pluck`, the aggregate methods, and `exists` reject it. */
|
|
565
|
-
readonly aggregate: boolean;
|
|
566
|
-
}
|
|
567
|
-
/** State of a query over table `T` with schema context `C`, whatever its result. */
|
|
568
|
-
interface QueryStateOf<T extends AnyTable, C extends SchemaContext = SchemaContext> extends SelectQueryState {
|
|
569
|
-
readonly table: T;
|
|
570
|
-
readonly schema: C;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* State of a query that starts from table `T` with schema context `C`: rows as records without fields, all of them or of the records of relation `U` when given, in rows mode `Rows`, guaranteed by a required relation when `Guaranteed`.
|
|
574
|
-
*
|
|
575
|
-
* Without relations in `C`, it also types a query of the table in a schema that has relations of the table.
|
|
576
|
-
*/
|
|
577
|
-
interface InitialQueryState<T extends AnyTable, C extends SchemaContext = NoRelations, U extends UsedRelation = never, Rows extends ResultRows = "many", Guaranteed extends boolean = false> extends QueryStateOf<T, C> {
|
|
578
|
-
readonly result: EmptyRecord;
|
|
579
|
-
readonly valueType: never;
|
|
580
|
-
readonly rows: Rows;
|
|
581
|
-
readonly value: false;
|
|
582
|
-
readonly uses: U;
|
|
583
|
-
readonly guaranteed: Guaranteed;
|
|
584
|
-
readonly aggregate: false;
|
|
585
|
-
}
|
|
586
721
|
/** Type of a count: a `bigint` of PostgreSQL decoded as a number, rejecting a count that a number cannot hold exactly. */
|
|
587
722
|
declare const countType: BaseType<number, number, MergeOperators<NoOperators, {
|
|
723
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
724
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
725
|
+
in: EqualityOperator<InSignature>;
|
|
588
726
|
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
589
727
|
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
590
728
|
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
591
729
|
gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
592
|
-
eq: EqualityOperator<ComparisonSignature>;
|
|
593
|
-
ne: EqualityOperator<ComparisonSignature>;
|
|
594
|
-
in: EqualityOperator<InSignature>;
|
|
595
730
|
}>, undefined>;
|
|
596
731
|
/** Type of the value of a query of a count. */
|
|
597
732
|
interface CountValueType extends QueryValueType {
|
|
@@ -600,8 +735,8 @@ interface CountValueType extends QueryValueType {
|
|
|
600
735
|
}
|
|
601
736
|
/** Rows mode of a query of a relation of kind `Kind`: `many`, or one row, `nullable` unless `Required` is `true`; a union gives the union of their modes. */
|
|
602
737
|
type RelationRows<Kind extends RelationKind, Required extends boolean> = Kind extends "many" ? "many" : Required extends true ? "one" : "nullable";
|
|
603
|
-
/** Rows mode after a filter of a query whose row a required relation guarantees: `one` becomes `nullable`, or `optional` for a query of values; other modes stay. */
|
|
604
|
-
type FilteredRows<Rows extends ResultRows, Value
|
|
738
|
+
/** Rows mode after a filter of a query whose row a required relation guarantees: `one` becomes `nullable`, or `optional` for a query of values that `map` does not convert; other modes stay. */
|
|
739
|
+
type FilteredRows<Rows extends ResultRows, Value> = Rows extends "one" ? Value extends true ? "optional" : "nullable" : Rows;
|
|
605
740
|
/** Rows mode after `take`: `many` becomes `one`; the modes of one row stay. */
|
|
606
741
|
type OneRow<Rows extends ResultRows> = Rows extends "many" ? "one" : Rows;
|
|
607
742
|
/** Rows mode after `pluck`: `nullable` becomes `optional`; the other modes stay. */
|
|
@@ -616,23 +751,6 @@ type OneValueRows<Rows extends ResultRows> = Rows extends "many" ? "one" : Rows
|
|
|
616
751
|
type QueryResult<S extends QueryState> = S extends QueryState ? ResultByRows<S["result"], S["rows"]> : never;
|
|
617
752
|
/** Result of rows of `Result` returned in rows mode `Rows`; a union of modes gives the union of their results. */
|
|
618
753
|
type ResultByRows<Result, Rows extends ResultRows> = Rows extends "many" ? Result[] : Rows extends "one" ? Result : Rows extends "nullable" ? Result | null : Result | undefined;
|
|
619
|
-
/** References to the columns of the query source, passed to query callbacks. */
|
|
620
|
-
type ColumnRefs<T extends AnyTable> = { readonly [K in keyof T["columns"]]: Expression<Selectable<T>[K], ColumnOperators<T, K>, ColumnValues<T>[K]>; };
|
|
621
|
-
/**
|
|
622
|
-
* Queries of the records of the relations of the table of schema context `C`, by relation name, which the callbacks of `select`, `where`, and `orderBy` receive beside the columns.
|
|
623
|
-
*
|
|
624
|
-
* A relation name is never a column key of its table.
|
|
625
|
-
* A context whose table key is not known, such as one of a query of any schema, has no relations.
|
|
626
|
-
*/
|
|
627
|
-
type RelationRefs<C extends SchemaContext> = string extends C["key"] ? EmptyRecord : C["key"] extends keyof C["relations"] ? RelationQueries<C["relations"], C["key"]> : EmptyRecord;
|
|
628
|
-
type RelationQueries<R extends SchemaContext["relations"], K extends keyof R & string> = { readonly [N in keyof R[K]]: RelatedQuery<R, K, N & string>; };
|
|
629
|
-
/** Unbound query of the records of relation `N` of the table under key `K` of a schema of relations `R`, in the schema context of its target and the rows mode of the relation, whose row a required relation guarantees. */
|
|
630
|
-
type RelatedQuery<R extends SchemaContext["relations"], K extends keyof R & string, N extends keyof R[K] & string> = SelectQuery<InitialQueryState<R[K][N]["target"], TableContext<R, R[K][N]["targetKey"]>, UsedRelation<K, N, R[K][N]>, RelationRows<R[K][N]["kind"], R[K][N]["required"]>, R[K][N]["required"]>>;
|
|
631
|
-
/** Runtime context of the queries of a table in the schema they start from. */
|
|
632
|
-
interface QueryContext {
|
|
633
|
-
/** Getters of the queries of the relations of the table, which the references of the callbacks of `select`, `where`, and `orderBy` have as their own properties and which read the key columns from them. */
|
|
634
|
-
readonly relations: PropertyDescriptorMap;
|
|
635
|
-
}
|
|
636
754
|
/** Type of the value of a query of `exists`. */
|
|
637
755
|
interface ExistsValueType extends QueryValueType {
|
|
638
756
|
readonly input: boolean;
|
|
@@ -686,20 +804,12 @@ type WhereCallback<T extends AnyTable, C extends SchemaContext = NoRelations> =
|
|
|
686
804
|
/** Argument of `where` of a query over table `T` with schema context `C`: a condition, or a callback that receives the columns and the relations of the query and returns one. */
|
|
687
805
|
type WhereFilter<T extends AnyTable, C extends SchemaContext = NoRelations> = WhereCondition<T> | WhereCallback<T, C>;
|
|
688
806
|
/**
|
|
689
|
-
*
|
|
807
|
+
* Argument of `where` of a query of state `S`: a `WhereFilter` of its table and schema, which TypeScript resolves with what it has inferred for `S` before it checks a callback in it.
|
|
690
808
|
*
|
|
691
|
-
*
|
|
809
|
+
* TypeScript types the body of a callback once, for every overload, and a plain `WhereFilter` leaves `S` unresolved there, so the body would widen a literal, such as of an enum column, to `string`.
|
|
810
|
+
* It reads the filter only in its branch, since a general alias that takes the filter as an argument resolves it on every call.
|
|
692
811
|
*/
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* Defines a reusable callback of `where` for queries over a table, checking its objects of column values against the columns.
|
|
696
|
-
*
|
|
697
|
-
* Defined with a query of a schema or a `Db`, such as `schema.form`, it receives the queries of the relations of the table in that schema, and applies to queries whose schema has them.
|
|
698
|
-
* Another callback of `where` can call it with the columns and the relations it receives.
|
|
699
|
-
*/
|
|
700
|
-
declare function defineFilter<T extends AnyTable, C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, filter: NoInfer<WhereCallback<T, C>>): WhereCallback<T, C>;
|
|
701
|
-
/** Defines a reusable filter of `where` for queries over a table, either a condition or a callback. */
|
|
702
|
-
declare function defineFilter<T extends AnyTable, C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, filter: NoInfer<WhereFilter<T, C>>): WhereFilter<T, C>;
|
|
812
|
+
type QueryWhereFilter<S extends SelectQueryState> = [S] extends [unknown] ? WhereFilter<S["table"], S["schema"]> : never;
|
|
703
813
|
/**
|
|
704
814
|
* Values of the primary key of table `T` that `find` takes, in the order of its columns, or `never` where the type of `T` tells no primary key.
|
|
705
815
|
*
|
|
@@ -716,100 +826,42 @@ type KeyCheck<T extends AnyTable, Columns> = [IsKey<T, Columns>] extends [true]
|
|
|
716
826
|
interface NotAKey<Keys> {
|
|
717
827
|
readonly "columns must be exactly one key of the table": Keys;
|
|
718
828
|
}
|
|
829
|
+
/** Key of a group returned by the callback of `groupBy` or `distinctOn`: an expression or a query of one value, such as of a value of a related record. */
|
|
830
|
+
type GroupTerm = AnyExpression | ScalarQuery;
|
|
831
|
+
/** Keys of a group returned by the callback of `groupBy` or `distinctOn`: one or an array of them. */
|
|
832
|
+
type GroupTerms = GroupTerm | readonly GroupTerm[];
|
|
719
833
|
/**
|
|
720
|
-
*
|
|
834
|
+
* What the callback of `having` of a query of state `S` receives as its second parameter: the references to the selected fields of a query of records, with the operators and the values of the types of their fields, the expression of the value of a query made by `get` or `pluck`, and `undefined` for an aggregate query, a query of `exists`, and a query that `map` converts.
|
|
721
835
|
*
|
|
722
|
-
*
|
|
836
|
+
* A field of nested rows or of a literal is among the references, but reading it throws; a field of the rows of a query of one value, such as of `get`, has the operators of its value in types.
|
|
723
837
|
*/
|
|
724
|
-
type
|
|
725
|
-
/**
|
|
726
|
-
type
|
|
727
|
-
/**
|
|
728
|
-
type
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
readonly [ordering]: true;
|
|
733
|
-
}
|
|
734
|
-
/** Sort key returned by the callback of `orderBy`: an expression or a query of one value, such as of an aggregate of related records, sorted in ascending order, or one with a direction. */
|
|
735
|
-
type OrderTerm = AnyExpression | ScalarQuery | Ordering;
|
|
838
|
+
type HavingFields<S extends SelectQueryState> = S["value"] extends false ? { readonly [K in keyof S["result"] & string]: FieldRef<SelectedFieldTypesOf<S["fieldTypes"]>, K, S["result"][K]>; } : S["value"] extends true ? S["aggregate"] extends false ? ToFieldRef<S["valueType"], S["result"]> : undefined : undefined;
|
|
839
|
+
/** Reference of the callback of `having` to field `K` of values `V`, with the type of its value among field types `F`, for each member of a union of them. */
|
|
840
|
+
type FieldRef<F, K extends string, V> = K extends keyof F ? ToFieldRef<Exclude<F[K], undefined>, V> : AnyExpression<V>;
|
|
841
|
+
/** Expression of values `V` with the operators and the values of type `D` of a value, or without them when `D` is none. */
|
|
842
|
+
type ToFieldRef<D, V> = D extends QueryValueType ? D extends {
|
|
843
|
+
readonly group: infer G;
|
|
844
|
+
} ? unknown extends G ? Expression<V, D["operators"], D["input"]> // References can escape this callback and be selected without grouping, so retain standalone nullability and the group mark.
|
|
845
|
+
: Expression<V | null, D["operators"], D["input"]> & GroupValue<G> : Expression<V, D["operators"], D["input"]> : AnyExpression<V>;
|
|
736
846
|
/**
|
|
737
|
-
*
|
|
847
|
+
* Argument of `having` of a query of state `S`: a condition of `where`, or a callback that receives the columns and the relations of the table, and the fields of `HavingFields`, and returns one.
|
|
738
848
|
*
|
|
739
|
-
*
|
|
849
|
+
* It reads `S` only in its branch, as `QueryWhereFilter` does.
|
|
740
850
|
*/
|
|
741
|
-
type
|
|
742
|
-
/** Sort keys returned by the callback of `orderBy`: one or an array of them. */
|
|
743
|
-
type OrderTerms = OrderTerm | readonly OrderTerm[];
|
|
744
|
-
/** Sorts by `value`, an expression or a query of one value, in ascending order, in the callback of `orderBy`. */
|
|
745
|
-
declare function asc(value: AnyExpression | ScalarQuery): Ordering;
|
|
746
|
-
/** Sorts by `value`, an expression or a query of one value, in descending order, in the callback of `orderBy`. */
|
|
747
|
-
declare function desc(value: AnyExpression | ScalarQuery): Ordering;
|
|
748
|
-
/** Sorts by `value`, an expression or a query of one value, in `direction`, such as `"desc nulls last"`, in the callback of `orderBy`. */
|
|
749
|
-
declare function orderBy(value: AnyExpression | ScalarQuery, direction: OrderDirection): Ordering;
|
|
851
|
+
type QueryHavingFilter<S extends SelectQueryState> = [S] extends [unknown] ? WhereCondition<S["table"]> | ((columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>, selected: HavingFields<S>) => WhereCondition<S["table"]>) : never;
|
|
750
852
|
/**
|
|
751
|
-
*
|
|
853
|
+
* What a query with a row lock does with a row that another transaction has locked, instead of waiting for it:
|
|
752
854
|
*
|
|
753
|
-
*
|
|
855
|
+
* - `skipLocked` leaves the row out of its result, such as to take the next free job of a queue.
|
|
856
|
+
* - `noWait` rejects with a PostgreSQL error of SQLSTATE `55P03`.
|
|
754
857
|
*/
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
readonly
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
[operandNode](): never;
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
|
-
* Creates a field of a selection with `value` in every record, without selecting anything in SQL: the query returns the value itself, not a copy of it, whatever it is.
|
|
766
|
-
*
|
|
767
|
-
* It lets a branch of a selection skip a column or a nested query, such as `withHint ? field.hint : literal(null)`.
|
|
768
|
-
* To use a value in SQL, such as in a condition, pass it through `param` instead.
|
|
769
|
-
*/
|
|
770
|
-
declare function literal<T extends {} | null | undefined>(value: T): Literal<T>;
|
|
771
|
-
/** Expressions, nested queries, and literals selected under the keys of the result fields; a record has no symbol keys. */
|
|
772
|
-
interface Selection {
|
|
773
|
-
readonly [key: string]: TypedExpression | NestedQuery | Literal<unknown>;
|
|
774
|
-
readonly [key: symbol]: never;
|
|
775
|
-
}
|
|
776
|
-
/** Query whose rows a selection nests as the value of a field: a query of any state and kind, such as a query of a relation. */
|
|
777
|
-
type NestedQuery = QueryOf<SelectQueryState>;
|
|
778
|
-
/** Record of the values of the expressions, of the nested rows, and of the literals of a selection. */
|
|
779
|
-
type SelectionResult<S extends Selection> = { [K in keyof S]: FieldValue<S[K]>; };
|
|
780
|
-
/** Value of a selected field: of an expression, of the nested rows of a query as the result of executing it, or of a literal, for each member of a union of them. */
|
|
781
|
-
type FieldValue<V> = V extends NestedQuery ? QueryResult<V["types"]> : V extends Literal<infer T> ? T : ExpressionValue<V>;
|
|
782
|
-
/** Relations whose records the nested queries of a selection select. */
|
|
783
|
-
type SelectionUses<S extends Selection> = S[keyof S] extends (infer V) ? V extends NestedQuery ? V["types"]["uses"] : never : never;
|
|
784
|
-
/** Value of a selected expression. */
|
|
785
|
-
type ExpressionValue<E> = E extends AnyExpression<infer T> ? T : never;
|
|
786
|
-
/** Values that the type of an expression encodes, such as in place of its value. */
|
|
787
|
-
type ExpressionInput<E> = E extends AnyExpression<unknown, infer I> ? I : never;
|
|
788
|
-
declare const helperFields: unique symbol;
|
|
789
|
-
/** Marker of the input fields of a helper, carrying the keys selected by its callback. */
|
|
790
|
-
interface HelperFields<Keys extends PropertyKey = PropertyKey> {
|
|
791
|
-
readonly [helperFields]: Keys;
|
|
792
|
-
}
|
|
793
|
-
/** Records with `Fields` added or replaced, for each branch of either record type; `any` stays `any`. */
|
|
794
|
-
type MergeSelection<Previous, Fields> = Previous extends unknown ? Fields extends unknown ? MergeFields<RecordFields<Previous>, RecordFields<Fields>> & (IsAny<Previous> extends true ? Previous : unknown) : never : never;
|
|
795
|
-
/** Fields with numeric keys represented as the string keys used in result records. */
|
|
796
|
-
type RecordFields<Fields> = Fields extends unknown ? [Extract<keyof Fields, number>] extends [never] ? Fields : { [K in keyof Fields as K extends number ? `${K}` : K]: Fields[K]; } : never;
|
|
797
|
-
/** Merge of normalized fields, retaining the previous value when an optional replacement is absent. */
|
|
798
|
-
type MergeFields<Previous, Fields> = Simplify<{ [K in keyof Previous as K extends keyof Fields ? ({} extends Pick<Fields, K> ? K : never) : K]: K extends typeof helperFields // A branch selecting extra keys must not be reduced to one selecting fewer fields or none.
|
|
799
|
-
? Previous[K] | keyof Fields : K extends keyof Fields // A decoder can return undefined, so it cannot be excluded as merely an absent property.
|
|
800
|
-
? Previous[K] | Fields[K] : Previous[K]; } & { [K in keyof Fields as K extends keyof Previous ? ({} extends Pick<Fields, K> ? never : K) : K]: Fields[K]; }>;
|
|
801
|
-
/** Value of column `K` of table `T` that an insert or an update sets: a value that its type encodes, `null` for a nullable column, or an expression of its values. */
|
|
802
|
-
type ColumnValue<T extends AnyTable, K extends keyof T["columns"]> = ColumnValues<T>[K] | AnyExpression<Selectable<T>[K]>;
|
|
803
|
-
/** Key of a column of table `T` that an insert may omit, which gets its default: one that the database gives a value, as `hasDefault` marks it, or a nullable one, whose default is `NULL` unless the database declares another. */
|
|
804
|
-
type OptionalColumnKey<T extends AnyTable> = { [K in keyof T["columns"]]: T["columns"][K]["attributes"]["hasDefault"] extends true ? K : null extends ColumnValues<T>[K] ? K : never; }[keyof T["columns"]];
|
|
805
|
-
/** Record of an insert into table `T`: the values of its columns, which may omit a column with a default or a nullable column, or give it as `undefined`. */
|
|
806
|
-
type InsertValues<T extends AnyTable> = { readonly [K in keyof T["columns"] as K extends OptionalColumnKey<T> ? never : K]: ColumnValue<T, K>; } & { readonly [K in OptionalColumnKey<T>]?: ColumnValue<T, K> | undefined; };
|
|
807
|
-
/** Values of the columns of table `T` that an update sets; a column omitted or given as `undefined` keeps its value. */
|
|
808
|
-
type UpdateValues<T extends AnyTable> = { readonly [K in keyof T["columns"]]?: ColumnValue<T, K> | undefined; };
|
|
809
|
-
/** Argument of `update`: the values that it sets, or a callback that receives the columns of the changed rows and returns them. */
|
|
810
|
-
type UpdateSet<T extends AnyTable> = UpdateValues<T> | ((columns: ColumnRefs<T>) => UpdateValues<T>);
|
|
811
|
-
/** State `S` with the parts of `Changes` replaced. */
|
|
812
|
-
type ChangeState<S extends SelectQueryState, Changes extends Partial<SelectQueryState>> = { readonly [K in keyof SelectQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
858
|
+
type RowLockOptions = {
|
|
859
|
+
readonly noWait?: boolean;
|
|
860
|
+
readonly skipLocked?: false;
|
|
861
|
+
} | {
|
|
862
|
+
readonly noWait?: false;
|
|
863
|
+
readonly skipLocked?: boolean;
|
|
864
|
+
};
|
|
813
865
|
/** Rows mode of a query of state `S` after a filter: `FilteredRows` of a query whose row is guaranteed, and the rows of other queries. */
|
|
814
866
|
type FilteredStateRows<S extends SelectQueryState> = [S["guaranteed"]] extends [false] ? S["rows"] : [S["value"]] extends [false] ? S["result"] extends HelperFields ? S["rows"] : FilteredRows<S["rows"], false> : FilteredRows<S["rows"], S["value"]>;
|
|
815
867
|
/** State `S` after a filter, which ends the guarantee of its row. */
|
|
@@ -822,49 +874,6 @@ type FoundState<S extends SelectQueryState> = ChangeState<S, {
|
|
|
822
874
|
rows: OneRow<FilteredStateRows<S>>;
|
|
823
875
|
guaranteed: false;
|
|
824
876
|
}>;
|
|
825
|
-
/** Query of state `S`, bound or unbound. */
|
|
826
|
-
type QueryOf<S extends SelectQueryState> = SelectQuery<S, SelectQueryKind>;
|
|
827
|
-
declare const scalarValue: unique symbol;
|
|
828
|
-
/** Value of a query of state `S` in an expression: its value, or `NULL` without its row unless the query always has it. */
|
|
829
|
-
type ScalarValue<S extends SelectQueryState> = S["aggregate"] extends true ? S["result"] : S["guaranteed"] extends true ? S["result"] : S["result"] | null;
|
|
830
|
-
/**
|
|
831
|
-
* Query of one value of a table, such as of `get`, of an aggregate, or of `exists`, which an expression takes as a scalar subquery of its value, or as `EXISTS` of the rows of a query of `exists`.
|
|
832
|
-
*
|
|
833
|
-
* `T` is its value there: without its row, a query of `get` is `NULL`, unless a required relation guarantees the row, such as of `field.form`.
|
|
834
|
-
*/
|
|
835
|
-
interface ScalarQuery<T = unknown> {
|
|
836
|
-
/** Value of the query in an expression, from which an expression infers `T`. */
|
|
837
|
-
readonly [scalarValue]: T;
|
|
838
|
-
readonly types: ScalarQueryState;
|
|
839
|
-
readonly exists: unknown;
|
|
840
|
-
}
|
|
841
|
-
/** Value of query `Q` of one value in an expression. */
|
|
842
|
-
type ScalarQueryValue<Q extends ScalarQuery> = Q[typeof scalarValue];
|
|
843
|
-
/** State of a query of one value, which an expression takes. */
|
|
844
|
-
interface ScalarQueryState extends SelectQueryState {
|
|
845
|
-
readonly rows: "one" | "optional";
|
|
846
|
-
readonly value: true;
|
|
847
|
-
}
|
|
848
|
-
/** State of a query of one value with the type of its value, whose expression `expr` gives. */
|
|
849
|
-
interface ExpressionQueryState extends ScalarQueryState {
|
|
850
|
-
readonly valueType: QueryValueType;
|
|
851
|
-
}
|
|
852
|
-
/** Query of one value of a table with the type of its value, such as of `get` or of an aggregate, which gives the expression of a function the decoding and the operators of its value. */
|
|
853
|
-
interface TypedScalarQuery extends ScalarQuery {
|
|
854
|
-
readonly types: ExpressionQueryState;
|
|
855
|
-
}
|
|
856
|
-
/** Query of values of a table of any rows, such as of `pluck`, whose value is `T`, which `in` takes as a subquery of its values. */
|
|
857
|
-
interface ValuesQuery<T = unknown> {
|
|
858
|
-
readonly types: ValuesQueryState<T>;
|
|
859
|
-
readonly exists: unknown;
|
|
860
|
-
}
|
|
861
|
-
/** Value of query `Q` of values, of each of its rows. */
|
|
862
|
-
type ValuesQueryValue<Q extends ValuesQuery> = Q["types"]["result"];
|
|
863
|
-
/** State of a query of values whose value is `T`. */
|
|
864
|
-
interface ValuesQueryState<T> extends SelectQueryState {
|
|
865
|
-
readonly result: T;
|
|
866
|
-
readonly value: true;
|
|
867
|
-
}
|
|
868
877
|
/** State of a query whose rows are records, which `select` and `clearSelect` change. */
|
|
869
878
|
type RecordQueryState$1 = SelectQueryState & {
|
|
870
879
|
readonly value: false;
|
|
@@ -877,26 +886,53 @@ type UnguaranteedQueryState = SelectQueryState & {
|
|
|
877
886
|
type ValueQueryState = SelectQueryState & {
|
|
878
887
|
readonly value: true;
|
|
879
888
|
};
|
|
889
|
+
/** State of a query converted by `map`, whose callback of `orderBy` reads no selected fields, which its result no longer tells. */
|
|
890
|
+
type MappedQueryState = SelectQueryState & {
|
|
891
|
+
readonly value: "mapped";
|
|
892
|
+
};
|
|
880
893
|
/** State of a query of values with the values that the type of its value encodes, which `coalesce` takes; it narrows nothing else, so that a call compares no operators. */
|
|
881
894
|
type CoalesceQueryState = ValueQueryState & {
|
|
882
895
|
readonly valueType: {
|
|
883
896
|
readonly input: unknown;
|
|
884
897
|
};
|
|
885
898
|
};
|
|
886
|
-
/** State of a query not made by an aggregate method or `exists`, which `get`, `pluck`, the aggregate methods, and `
|
|
899
|
+
/** State of a query not made by an aggregate method or `exists`, which `get`, `pluck`, the aggregate methods, `exists`, and `groupBy` apply to. */
|
|
887
900
|
type UnaggregatedQueryState = SelectQueryState & {
|
|
888
901
|
readonly aggregate: false;
|
|
889
902
|
};
|
|
903
|
+
/** State of a query of values not made by an aggregate method or `exists`, such as of `pluck`, whose callback of `groupBy` reads its value. */
|
|
904
|
+
type UnaggregatedValueQueryState = ValueQueryState & UnaggregatedQueryState;
|
|
890
905
|
/** Type of the value of a query of column `K` of table `T`. */
|
|
891
906
|
type ColumnValueType<T extends AnyTable, K extends keyof T["columns"]> = {
|
|
892
907
|
readonly input: ColumnValues<T>[K];
|
|
893
908
|
readonly operators: ColumnOperators<T, K>;
|
|
894
909
|
};
|
|
895
|
-
/** Type of the value of a query of
|
|
896
|
-
type
|
|
897
|
-
readonly input:
|
|
898
|
-
readonly operators:
|
|
910
|
+
/** Type of the value of a query of `E`, an expression or a query of one value that a callback returns. */
|
|
911
|
+
type OperandValueType<E extends TypedOperand> = {
|
|
912
|
+
readonly input: TypedOperandInput<E>;
|
|
913
|
+
readonly operators: TypedOperandOperators<E>;
|
|
899
914
|
};
|
|
915
|
+
/** State `S` after grouping, with the values of its directly selected aggregates in nonempty groups. */
|
|
916
|
+
interface GroupedState<S extends SelectQueryState> extends SelectQueryState {
|
|
917
|
+
readonly table: S["table"];
|
|
918
|
+
readonly schema: S["schema"];
|
|
919
|
+
readonly result: GroupedResult<S>;
|
|
920
|
+
readonly rows: S["rows"];
|
|
921
|
+
readonly value: S["value"];
|
|
922
|
+
readonly valueType: S["valueType"];
|
|
923
|
+
readonly fieldTypes: S["fieldTypes"];
|
|
924
|
+
readonly uses: S["uses"];
|
|
925
|
+
readonly guaranteed: S["guaranteed"];
|
|
926
|
+
readonly aggregate: false & Grouped;
|
|
927
|
+
}
|
|
928
|
+
/** Selected result after grouping, retaining the records or scalar value of the query. */
|
|
929
|
+
type GroupedResult<S extends SelectQueryState> = S["value"] extends false ? S["fieldTypes"] extends SelectedFieldTypes<unknown, unknown> ? GroupedRecord<S["result"], S["fieldTypes"]["types"]> : S["result"] : S["value"] extends true ? GroupedField<S["result"], S["valueType"]> : S["result"];
|
|
930
|
+
/** Type of a selected operand, carrying its value in a group for a later call of `groupBy`. */
|
|
931
|
+
interface SelectedOperandType<E extends TypedOperand> extends QueryValueType {
|
|
932
|
+
readonly input: TypedOperandInput<E>;
|
|
933
|
+
readonly operators: TypedOperandOperators<E>;
|
|
934
|
+
readonly group: E extends GroupValue<infer V> ? V : unknown;
|
|
935
|
+
}
|
|
900
936
|
/** State of a query of the value `V` of type `Type` of an aggregate or of `exists` over the rows of a query of state `S`, which always returns one row. */
|
|
901
937
|
type AggregateState<S extends SelectQueryState, V, Type extends QueryValueType> = ChangeState<S, {
|
|
902
938
|
result: V;
|
|
@@ -906,26 +942,60 @@ type AggregateState<S extends SelectQueryState, V, Type extends QueryValueType>
|
|
|
906
942
|
guaranteed: false;
|
|
907
943
|
aggregate: true;
|
|
908
944
|
}>;
|
|
945
|
+
/** State of a query of the value `V` of type `Type` of an aggregate over the rows of a query of state `S`, whose argument is `E`, an expression or a query of one value that a callback returns. */
|
|
946
|
+
type OperandAggregateState<S extends SelectQueryState, V, Type extends QueryValueType, E> = ChangeState<S, {
|
|
947
|
+
result: V;
|
|
948
|
+
valueType: Type;
|
|
949
|
+
rows: "one";
|
|
950
|
+
value: true;
|
|
951
|
+
guaranteed: false;
|
|
952
|
+
aggregate: true;
|
|
953
|
+
uses: S["uses"] | OperandUses<E>;
|
|
954
|
+
}>;
|
|
909
955
|
/** State of a query of the number of rows or of values among the rows of a query of state `S`. */
|
|
910
956
|
type CountState<S extends SelectQueryState> = AggregateState<S, number, CountValueType>;
|
|
911
957
|
/** State of a query of an aggregate of the values of column `K` of the rows of a query of state `S`, `NULL` without values other than `NULL`. */
|
|
912
958
|
type ColumnAggregateState<S extends SelectQueryState, K extends keyof S["table"]["columns"]> = AggregateState<S, Selectable<S["table"]>[K] | null, ColumnValueType<S["table"], K>>;
|
|
913
|
-
/** State of a query of an aggregate of the values of
|
|
914
|
-
type
|
|
959
|
+
/** State of a query of an aggregate of the values of `E`, an expression or a query of one value that a callback returns, over the rows of a query of state `S`, `NULL` without values other than `NULL`. */
|
|
960
|
+
type OperandValueAggregateState<S extends SelectQueryState, E extends TypedOperand> = OperandAggregateState<S, OperandValue<E> | null, OperandValueType<E>, E>;
|
|
915
961
|
/** Value `V` of a query of values whose `NULL` `coalesce` replaces with fallback `F`. */
|
|
916
962
|
type CoalescedValue<V, F> = Exclude<V, null> | Extract<OperandValue<F>, null>;
|
|
917
963
|
/** State of a query of many rows, which `limit` and `offset` apply to. */
|
|
918
964
|
type ManyRowsQueryState = SelectQueryState & {
|
|
919
965
|
readonly rows: "many";
|
|
920
966
|
};
|
|
967
|
+
/** State of a query that returns rows of its table, which a row lock applies to: not of an aggregate or of `exists`. */
|
|
968
|
+
type LockableQueryState = SelectQueryState & ({
|
|
969
|
+
readonly aggregate: false;
|
|
970
|
+
} | {
|
|
971
|
+
readonly value: "mapped";
|
|
972
|
+
});
|
|
921
973
|
/** State of a query of one row, which `optional` and `required` apply to. */
|
|
922
974
|
type OneRowQueryState = SelectQueryState & {
|
|
923
975
|
readonly rows: "one" | "nullable" | "optional";
|
|
924
976
|
};
|
|
925
|
-
/** State of a query of one row whose rows are records, which `nullable` applies to. */
|
|
977
|
+
/** State of a query of one row whose rows are records or what `map` gives, which `nullable` applies to. */
|
|
926
978
|
type OneRecordQueryState = OneRowQueryState & {
|
|
927
|
-
readonly value: false;
|
|
979
|
+
readonly value: false | "mapped";
|
|
928
980
|
};
|
|
981
|
+
/**
|
|
982
|
+
* Value of column `K` of table `T` that an insert or an update sets: a value that its type encodes, `null` for a nullable column, an expression of its values, or a query of one such value.
|
|
983
|
+
*
|
|
984
|
+
* A query may be `NULL` without its row, which PostgreSQL rejects for a column that is not nullable.
|
|
985
|
+
*/
|
|
986
|
+
type ColumnValue<T extends AnyTable, K extends keyof T["columns"]> = ColumnValues<T>[K] | AnyExpression<Selectable<T>[K]> | ScalarQuery<Selectable<T>[K] | null>;
|
|
987
|
+
/** Key of a column of table `T` that an insert may omit, which gets its default: one that the database gives a value, as `hasDefault` marks it, or a nullable one, whose default is `NULL` unless the database declares another. */
|
|
988
|
+
type OptionalColumnKey<T extends AnyTable> = { [K in keyof T["columns"]]: T["columns"][K]["attributes"]["hasDefault"] extends true ? K : null extends ColumnValues<T>[K] ? K : never; }[keyof T["columns"]];
|
|
989
|
+
/** Record of an insert into table `T`: the values of its columns, which may omit a column with a default or a nullable column, or give it as `undefined`. */
|
|
990
|
+
type InsertValues<T extends AnyTable> = { readonly [K in keyof T["columns"] as K extends OptionalColumnKey<T> ? never : K]: ColumnValue<T, K>; } & { readonly [K in OptionalColumnKey<T>]?: ColumnValue<T, K> | undefined; };
|
|
991
|
+
/** Values of the columns of table `T` that an update sets; a column omitted or given as `undefined` keeps its value. */
|
|
992
|
+
type UpdateValues<T extends AnyTable> = { readonly [K in keyof T["columns"]]?: ColumnValue<T, K> | undefined; };
|
|
993
|
+
/** Argument of `update` of table `T` with schema context `C`: the values that it sets, or a callback that receives the columns and the queries of the relations of the changed rows and returns them. */
|
|
994
|
+
type UpdateSet<T extends AnyTable, C extends SchemaContext = NoRelations> = UpdateValues<T> | ((columns: ColumnRefs<T> & RelationRefs<C>) => UpdateValues<T>);
|
|
995
|
+
/** State `S` with the parts of `Changes` replaced. */
|
|
996
|
+
type ChangeState<S extends SelectQueryState, Changes extends Partial<SelectQueryState>> = { readonly [K in keyof SelectQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
997
|
+
/** Query of state `S`, bound or unbound. */
|
|
998
|
+
type QueryOf<S extends SelectQueryState> = SelectQuery<S, SelectQueryKind>;
|
|
929
999
|
/**
|
|
930
1000
|
* Immutable select query of state `S`, unbound or bound to a `Db`.
|
|
931
1001
|
*
|
|
@@ -936,7 +1006,7 @@ type OneRecordQueryState = OneRowQueryState & {
|
|
|
936
1006
|
declare class SelectQuery<out S extends SelectQueryState, out Kind extends SelectQueryKind = "unbound"> {
|
|
937
1007
|
readonly table: S["table"];
|
|
938
1008
|
readonly node: QueryNode;
|
|
939
|
-
/** Relations of the table in the schema the query starts from, which
|
|
1009
|
+
/** Relations of the table in the schema the query starts from, which its callbacks receive. */
|
|
940
1010
|
readonly context: QueryContext;
|
|
941
1011
|
readonly types: S;
|
|
942
1012
|
/** Value of the query in an expression, for types only. */
|
|
@@ -946,23 +1016,25 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
946
1016
|
/** Executes a bound query on its `Db`; `undefined` for an unbound query, so that it is not thenable. */
|
|
947
1017
|
readonly then: QueryThen<S, Kind>;
|
|
948
1018
|
constructor(table: S["table"], node: QueryNode,
|
|
949
|
-
/** Relations of the table in the schema the query starts from, which
|
|
1019
|
+
/** Relations of the table in the schema the query starts from, which its callbacks receive. */
|
|
950
1020
|
context: QueryContext, db?: AnyDb);
|
|
951
1021
|
/** Adds a filter combined with `AND` with the previous ones; a callback receives the columns and the queries of the relations of the table. */
|
|
952
|
-
where<Q extends QueryOf<UnguaranteedQueryState>>(this: Q, filter:
|
|
1022
|
+
where<Q extends QueryOf<UnguaranteedQueryState>>(this: Q, filter: QueryWhereFilter<Q["types"]>): Q;
|
|
953
1023
|
/**
|
|
954
1024
|
* Adds a filter combined with `AND` with the previous ones; a callback receives the columns and the queries of the relations of the table.
|
|
955
1025
|
*
|
|
956
1026
|
* A query of the record of a required relation, such as `field.form`, resolves to `null` without a record that passes the filter, or to `undefined` after `get` or `pluck`.
|
|
957
1027
|
*/
|
|
958
|
-
where<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, filter:
|
|
1028
|
+
where<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, filter: QueryWhereFilter<S>): SelectQuery<FilteredState<S>, Kind>;
|
|
959
1029
|
/** Selects all columns of the table, replacing fields under their keys. */
|
|
960
1030
|
select<S extends RecordQueryState$1, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, all: "*"): SelectQuery<ChangeState<S, {
|
|
961
1031
|
result: MergeSelection<S["result"], Selectable<S["table"]>>;
|
|
1032
|
+
fieldTypes: SelectedFieldTypes<S["fieldTypes"], ColumnFieldTypes<S["table"], keyof S["table"]["columns"]>>;
|
|
962
1033
|
}>, Kind>;
|
|
963
1034
|
/** Selects the listed columns, replacing fields under their keys. */
|
|
964
1035
|
select<S extends RecordQueryState$1, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, ...keys: K[]): SelectQuery<ChangeState<S, {
|
|
965
1036
|
result: MergeSelection<S["result"], Pick<Selectable<S["table"]>, K>>;
|
|
1037
|
+
fieldTypes: SelectedFieldTypes<S["fieldTypes"], ColumnFieldTypes<S["table"], K>>;
|
|
966
1038
|
}>, Kind>;
|
|
967
1039
|
/**
|
|
968
1040
|
* Selects the expressions and nested queries returned by `callback`, replacing fields under their keys.
|
|
@@ -972,12 +1044,14 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
972
1044
|
* A nested query is unbound, such as a query of a relation, or bound to the `Db` that runs the query; its rows become the value of its field as they become the result of an executed query.
|
|
973
1045
|
*/
|
|
974
1046
|
select<S extends RecordQueryState$1, Kind extends SelectQueryKind, Sel extends Selection>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => Sel): SelectQuery<ChangeState<S, {
|
|
975
|
-
result: MergeSelection<S["result"], SelectionResult<Sel>>;
|
|
1047
|
+
result: MergeSelection<S["result"], S["aggregate"] extends Grouped ? GroupSelectionResult<Sel> : SelectionResult<Sel>>;
|
|
976
1048
|
uses: S["uses"] | SelectionUses<Sel>;
|
|
1049
|
+
fieldTypes: SelectedFieldTypes<S["fieldTypes"], SelectionFieldTypes<Sel>>;
|
|
977
1050
|
}>, Kind>;
|
|
978
1051
|
/** Removes all selected fields and keeps the other clauses. */
|
|
979
1052
|
clearSelect<S extends RecordQueryState$1, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): SelectQuery<ChangeState<S, {
|
|
980
1053
|
result: EmptyRecord;
|
|
1054
|
+
fieldTypes: EmptyRecord;
|
|
981
1055
|
}>, Kind>;
|
|
982
1056
|
/**
|
|
983
1057
|
* Limits the query to one row and returns the row instead of an array; without a row, it rejects with `NotFoundError` unless `optional` or `nullable` makes the query resolve to `undefined` or `null`.
|
|
@@ -1018,15 +1092,18 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1018
1092
|
/**
|
|
1019
1093
|
* Replaces the selected fields with the expression returned by `callback`, limits the query to one row, and returns the value of the expression instead of a record.
|
|
1020
1094
|
*
|
|
1095
|
+
* `callback` receives the columns and the queries of the relations of the table.
|
|
1096
|
+
* It may return a query of one value, such as of an aggregate of related records, which gives `NULL` without its row.
|
|
1021
1097
|
* Without a row, it rejects with `NotFoundError`, or with `RequiredRelationError` when a required relation still guarantees the row, such as of `field.form`.
|
|
1022
1098
|
* An optional or nullable query resolves to `undefined` without a row instead.
|
|
1023
1099
|
* A `NULL` value is `null`.
|
|
1024
1100
|
*/
|
|
1025
|
-
get<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends
|
|
1026
|
-
result:
|
|
1027
|
-
valueType:
|
|
1101
|
+
get<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<ChangeState<S, {
|
|
1102
|
+
result: S["aggregate"] extends Grouped ? GroupOperandValue<E> : OperandValue<E>;
|
|
1103
|
+
valueType: SelectedOperandType<E>;
|
|
1028
1104
|
rows: OneValueRows<S["rows"]>;
|
|
1029
1105
|
value: true;
|
|
1106
|
+
uses: S["uses"] | OperandUses<E>;
|
|
1030
1107
|
}>, Kind>;
|
|
1031
1108
|
/**
|
|
1032
1109
|
* Replaces the selected fields with a column and returns the value of the column of each row instead of a record.
|
|
@@ -1042,60 +1119,99 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1042
1119
|
/**
|
|
1043
1120
|
* Replaces the selected fields with the expression returned by `callback` and returns the value of the expression for each row instead of a record.
|
|
1044
1121
|
*
|
|
1122
|
+
* `callback` receives the columns and the queries of the relations of the table.
|
|
1123
|
+
* It may return a query of one value, such as of an aggregate of related records, which gives `NULL` without its row.
|
|
1045
1124
|
* It keeps whether the query returns an array or one value, such as after `take`, and how it handles a missing row, except that a nullable query resolves to `undefined` without a row.
|
|
1046
1125
|
*/
|
|
1047
|
-
pluck<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends
|
|
1048
|
-
result:
|
|
1049
|
-
valueType:
|
|
1126
|
+
pluck<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<ChangeState<S, {
|
|
1127
|
+
result: S["aggregate"] extends Grouped ? GroupOperandValue<E> : OperandValue<E>;
|
|
1128
|
+
valueType: SelectedOperandType<E>;
|
|
1050
1129
|
rows: ValueRows<S["rows"]>;
|
|
1051
1130
|
value: true;
|
|
1131
|
+
uses: S["uses"] | OperandUses<E>;
|
|
1052
1132
|
}>, Kind>;
|
|
1053
1133
|
/**
|
|
1054
1134
|
* Replaces the selected fields with the number of rows of the query and returns the number instead of the rows.
|
|
1055
1135
|
*
|
|
1056
|
-
* It keeps the filters and ignores the order; with `limit` or `offset`, it counts the rows of that page.
|
|
1136
|
+
* It keeps the filters and ignores the order; with `limit` or `offset`, it counts the rows of that page, after `groupBy`, the groups, after `distinct`, the distinct rows of the selected fields, and after `distinctOn`, the first rows.
|
|
1057
1137
|
*/
|
|
1058
1138
|
count<S extends UnaggregatedQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): SelectQuery<CountState<S>, Kind>;
|
|
1059
1139
|
/** Replaces the selected fields with the number of the values of the column other than `NULL` among the rows of the query and returns it instead of the rows. */
|
|
1060
1140
|
count<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<CountState<S>, Kind>;
|
|
1061
|
-
/**
|
|
1062
|
-
|
|
1141
|
+
/**
|
|
1142
|
+
* Replaces the selected fields with the number of the values other than `NULL` of the expression returned by `callback` among the rows of the query and returns it instead of the rows.
|
|
1143
|
+
*
|
|
1144
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1145
|
+
*/
|
|
1146
|
+
count<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandAggregateState<S, number, CountValueType, E>, Kind>;
|
|
1063
1147
|
/** Replaces the selected fields with the number of the distinct values of the column other than `NULL` among the rows of the query and returns it instead of the rows. */
|
|
1064
1148
|
countDistinct<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<CountState<S>, Kind>;
|
|
1065
|
-
/**
|
|
1066
|
-
|
|
1149
|
+
/**
|
|
1150
|
+
* Replaces the selected fields with the number of the distinct values other than `NULL` of the expression returned by `callback` among the rows of the query and returns it instead of the rows.
|
|
1151
|
+
*
|
|
1152
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1153
|
+
*/
|
|
1154
|
+
countDistinct<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandAggregateState<S, number, CountValueType, E>, Kind>;
|
|
1067
1155
|
/** Replaces the selected fields with the smallest value of the column among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`. */
|
|
1068
1156
|
min<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1069
|
-
/**
|
|
1070
|
-
|
|
1157
|
+
/**
|
|
1158
|
+
* Replaces the selected fields with the smallest value of the expression returned by `callback` among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`.
|
|
1159
|
+
*
|
|
1160
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1161
|
+
*/
|
|
1162
|
+
min<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1071
1163
|
/** Replaces the selected fields with the largest value of the column among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`. */
|
|
1072
1164
|
max<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1073
|
-
/**
|
|
1074
|
-
|
|
1165
|
+
/**
|
|
1166
|
+
* Replaces the selected fields with the largest value of the expression returned by `callback` among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`.
|
|
1167
|
+
*
|
|
1168
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1169
|
+
*/
|
|
1170
|
+
max<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1075
1171
|
/** Replaces the selected fields with the sum of the values of the column among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`. */
|
|
1076
1172
|
sum<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1077
|
-
/**
|
|
1078
|
-
|
|
1173
|
+
/**
|
|
1174
|
+
* Replaces the selected fields with the sum of the values of the expression returned by `callback` among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`.
|
|
1175
|
+
*
|
|
1176
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1177
|
+
*/
|
|
1178
|
+
sum<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1079
1179
|
/** Replaces the selected fields with the average of the values of the column among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`; for `bigint` values, average an expression of a type that can represent fractional values. */
|
|
1080
1180
|
avg<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1081
|
-
/**
|
|
1082
|
-
|
|
1181
|
+
/**
|
|
1182
|
+
* Replaces the selected fields with the average of the values of the expression returned by `callback` among the rows of the query and returns it instead of the rows, or `null` without values other than `NULL`; for `bigint` values, average an expression of a type that can represent fractional values.
|
|
1183
|
+
*
|
|
1184
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1185
|
+
*/
|
|
1186
|
+
avg<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1083
1187
|
/** Replaces the selected fields with whether every value of the Boolean column other than `NULL` among the rows of the query is true and returns it instead of the rows, or `null` without such values. */
|
|
1084
1188
|
every<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1085
|
-
/**
|
|
1086
|
-
|
|
1189
|
+
/**
|
|
1190
|
+
* Replaces the selected fields with whether every value other than `NULL` of the Boolean expression returned by `callback` among the rows of the query is true and returns it instead of the rows, or `null` without such values.
|
|
1191
|
+
*
|
|
1192
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1193
|
+
*/
|
|
1194
|
+
every<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1087
1195
|
/** Replaces the selected fields with whether any value of the Boolean column among the rows of the query is true and returns it instead of the rows, or `null` without values other than `NULL`. */
|
|
1088
1196
|
some<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1089
|
-
/**
|
|
1090
|
-
|
|
1197
|
+
/**
|
|
1198
|
+
* Replaces the selected fields with whether any value of the Boolean expression returned by `callback` among the rows of the query is true and returns it instead of the rows, or `null` without values other than `NULL`.
|
|
1199
|
+
*
|
|
1200
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1201
|
+
*/
|
|
1202
|
+
some<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1091
1203
|
/** Replaces the selected fields with the values of the text column other than `NULL` among the rows of the query, joined with `delimiter` in the order of the query, and returns them instead of the rows, or `null` without such values. */
|
|
1092
1204
|
stringAgg<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, key: K, delimiter: string): SelectQuery<ColumnAggregateState<S, K>, Kind>;
|
|
1093
|
-
/**
|
|
1094
|
-
|
|
1205
|
+
/**
|
|
1206
|
+
* Replaces the selected fields with the values other than `NULL` of the text expression returned by `callback` among the rows of the query, joined with `delimiter` in the order of the query, and returns them instead of the rows, or `null` without such values.
|
|
1207
|
+
*
|
|
1208
|
+
* `callback` receives the columns and the queries of the relations of the table and returns an expression or a query of one value.
|
|
1209
|
+
*/
|
|
1210
|
+
stringAgg<S extends UnaggregatedQueryState, Kind extends SelectQueryKind, E extends TypedOperand>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E, delimiter: string): SelectQuery<OperandValueAggregateState<S, E>, Kind>;
|
|
1095
1211
|
/**
|
|
1096
1212
|
* Replaces the selected fields with whether the query has a row and returns it instead of the rows, such as to filter by related records in `where`.
|
|
1097
1213
|
*
|
|
1098
|
-
* It keeps the filters, the limit, and the offset, and ignores the order.
|
|
1214
|
+
* It keeps the filters, the limit, and the offset, and ignores the order; after `groupBy`, it tells whether the query has a group, and after `distinct` and `distinctOn`, it checks the distinct rows and the first rows.
|
|
1099
1215
|
*/
|
|
1100
1216
|
exists<S extends UnaggregatedQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): SelectQuery<AggregateState<S, boolean, ExistsValueType>, Kind>;
|
|
1101
1217
|
/**
|
|
@@ -1106,6 +1222,9 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1106
1222
|
*/
|
|
1107
1223
|
coalesce<S extends CoalesceQueryState, Kind extends SelectQueryKind, F extends Operand<S["result"], S["valueType"]["input"]>>(this: SelectQuery<S, Kind>, fallback: F): SelectQuery<ChangeState<S, {
|
|
1108
1224
|
result: CoalescedValue<S["result"], F>;
|
|
1225
|
+
valueType: S["valueType"] extends {
|
|
1226
|
+
readonly group: unknown;
|
|
1227
|
+
} ? Omit<S["valueType"], "group"> : S["valueType"];
|
|
1109
1228
|
}>, Kind>;
|
|
1110
1229
|
/**
|
|
1111
1230
|
* Returns an expression of the value of a query of one value, such as of `get` or of an aggregate, with the operators of its type, such as `form.fields.count().expr().gt(3)`.
|
|
@@ -1113,6 +1232,36 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1113
1232
|
* Without a row, the expression is `NULL`, and the query does not reject.
|
|
1114
1233
|
*/
|
|
1115
1234
|
expr<Q extends QueryOf<ExpressionQueryState>>(this: Q): Expression<ScalarQueryValue<Q>, Q["types"]["valueType"]["operators"], Q["types"]["valueType"]["input"]>;
|
|
1235
|
+
/** Groups the rows by the listed columns, after the keys of earlier calls, so that the query gives one row of each group, whose fields are its keys and the aggregates of its rows, such as `sum(field.position)`. */
|
|
1236
|
+
groupBy<S extends UnaggregatedQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, ...keys: (keyof S["table"]["columns"] & string)[]): SelectQuery<GroupedState<S>, Kind>;
|
|
1237
|
+
/**
|
|
1238
|
+
* Groups the rows by the keys returned by `callback`, after the keys of earlier calls, so that the query gives one row of each group, whose fields are its keys and the aggregates of its rows, such as `sum(field.position)`.
|
|
1239
|
+
*
|
|
1240
|
+
* `callback` receives the columns with the queries of the relations of the table, and the fields selected so far, and returns an expression or a query of one value, or an array of them.
|
|
1241
|
+
* A selected field groups by the value of the field; a field of nested rows or of a literal cannot be grouped by.
|
|
1242
|
+
* Methods that choose rows and read values, such as `limit`, `take`, and `pluck`, apply to the groups, and an aggregate method aggregates them, such as `count` for the number of the groups.
|
|
1243
|
+
* The query cannot take a row lock, `update`, or `delete`.
|
|
1244
|
+
*/
|
|
1245
|
+
groupBy<S extends RecordQueryState$1, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>, selected: SelectedRefs<S["result"]>) => GroupTerms): SelectQuery<GroupedState<S>, Kind>;
|
|
1246
|
+
/**
|
|
1247
|
+
* Groups the rows of a query made by `get` or `pluck` by the keys returned by `callback`, after the keys of earlier calls.
|
|
1248
|
+
*
|
|
1249
|
+
* `callback` receives the columns with the queries of the relations of the table, and the expression of the value, and returns what `groupBy` of a query of records takes.
|
|
1250
|
+
*/
|
|
1251
|
+
groupBy<S extends UnaggregatedValueQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>, value: AnyExpression<S["result"]>) => GroupTerms): SelectQuery<GroupedState<S>, Kind>;
|
|
1252
|
+
/**
|
|
1253
|
+
* Filters the groups of a query of `groupBy` by what `where` takes, such as an aggregate of a group, combined with `AND` with the previous conditions of the groups; `where` keeps filtering the rows of the groups.
|
|
1254
|
+
*
|
|
1255
|
+
* A callback also receives the fields selected so far, such as `having((submission, selected) => selected.submissions.gt(1))`, or the value of a query made by `get` or `pluck`.
|
|
1256
|
+
*/
|
|
1257
|
+
having<Q extends QueryOf<UnguaranteedQueryState>>(this: Q, filter: QueryHavingFilter<Q["types"]>): Q;
|
|
1258
|
+
/**
|
|
1259
|
+
* Filters the groups of a query of `groupBy` by what `where` takes, such as an aggregate of a group, combined with `AND` with the previous conditions of the groups; `where` keeps filtering the rows of the groups.
|
|
1260
|
+
*
|
|
1261
|
+
* A callback also receives the fields selected so far, or the value of a query made by `get` or `pluck`.
|
|
1262
|
+
* A query of the record of a required relation, such as `field.form`, resolves to `null` without a group that passes the filter, or to `undefined` after `get` or `pluck`.
|
|
1263
|
+
*/
|
|
1264
|
+
having<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, filter: QueryHavingFilter<S>): SelectQuery<FilteredState<S>, Kind>;
|
|
1116
1265
|
/**
|
|
1117
1266
|
* Sorts the rows by the sort keys returned by `callback`, after the sort keys of earlier calls.
|
|
1118
1267
|
*
|
|
@@ -1128,12 +1277,73 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1128
1277
|
* It returns an expression or a query of one value, sorted in ascending order, `asc`, `desc`, or `orderBy` of one, or an array of them.
|
|
1129
1278
|
*/
|
|
1130
1279
|
orderBy<Q extends QueryOf<ValueQueryState>>(this: Q, callback: (columns: ColumnRefs<Q["types"]["table"]> & RelationRefs<Q["types"]["schema"]>, value: AnyExpression<Q["types"]["result"]>) => OrderTerms): Q;
|
|
1280
|
+
/**
|
|
1281
|
+
* Sorts the rows of a query that `map` converts by the sort keys returned by `callback`, after the sort keys of earlier calls.
|
|
1282
|
+
*
|
|
1283
|
+
* `callback` receives the columns with the queries of the relations of the table, and returns what `orderBy` of a query of records takes.
|
|
1284
|
+
*/
|
|
1285
|
+
orderBy<Q extends QueryOf<MappedQueryState>>(this: Q, callback: (columns: ColumnRefs<Q["types"]["table"]> & RelationRefs<Q["types"]["schema"]>) => OrderTerms): Q;
|
|
1131
1286
|
/** Sorts the rows by the listed columns in ascending order, or in the directions of objects of columns, after the sort keys of earlier calls. */
|
|
1132
1287
|
orderBy<Q extends AnySelectQuery>(this: Q, ...keys: OrderKey<Q["types"]["table"]>[]): Q;
|
|
1133
1288
|
/** Returns at most `count` rows of a query of many rows, replacing an earlier limit. */
|
|
1134
1289
|
limit<Q extends QueryOf<ManyRowsQueryState>>(this: Q, count: number): Q;
|
|
1135
1290
|
/** Skips the first `count` rows of a query of many rows, replacing an earlier offset. */
|
|
1136
1291
|
offset<Q extends QueryOf<ManyRowsQueryState>>(this: Q, count: number): Q;
|
|
1292
|
+
/**
|
|
1293
|
+
* Removes the duplicate rows of a query of many rows, such as of `pluck` for `in`, comparing the fields that the query selects, also those selected after it.
|
|
1294
|
+
*
|
|
1295
|
+
* Sort the query only by its selected fields.
|
|
1296
|
+
* The query cannot select nested rows, which `distinct` cannot compare.
|
|
1297
|
+
* The aggregate methods and `exists` after it read the distinct rows of the fields selected before them, and an aggregate method takes only a selected column or expression.
|
|
1298
|
+
*/
|
|
1299
|
+
distinct<Q extends QueryOf<ManyRowsQueryState>>(this: Q): Q;
|
|
1300
|
+
/**
|
|
1301
|
+
* Keeps the first row of each group of the rows of a query of many rows with equal values of the keys returned by `callback`, after the keys of earlier calls, such as the latest submission of each user.
|
|
1302
|
+
*
|
|
1303
|
+
* `callback` receives the columns with the queries of the relations of the table, and the fields selected so far, and returns an expression or a query of one value, or an array of them; a field of nested rows or of a literal cannot be a key.
|
|
1304
|
+
* Sort the query by the keys first, then by the order that chooses the first row of each group; the rows keep that order.
|
|
1305
|
+
* To sort by a key of an expression, select it and take it from the second parameter of the callbacks of `distinctOn` and `orderBy`.
|
|
1306
|
+
* The aggregate methods and `exists` after it read the first rows, such as `count` for the number of the groups.
|
|
1307
|
+
*/
|
|
1308
|
+
distinctOn<Q extends QueryOf<RecordQueryState$1 & ManyRowsQueryState>>(this: Q, callback: (columns: ColumnRefs<Q["types"]["table"]> & RelationRefs<Q["types"]["schema"]>, selected: SelectedRefs<Q["types"]["result"]>) => GroupTerms): Q;
|
|
1309
|
+
/**
|
|
1310
|
+
* Keeps the first value of each group of the values of a query made by `pluck` with equal values of the keys returned by `callback`, after the keys of earlier calls.
|
|
1311
|
+
*
|
|
1312
|
+
* `callback` receives the columns with the queries of the relations of the table, and the expression of the value, and returns what `distinctOn` of a query of records takes.
|
|
1313
|
+
*/
|
|
1314
|
+
distinctOn<Q extends QueryOf<ValueQueryState & ManyRowsQueryState>>(this: Q, callback: (columns: ColumnRefs<Q["types"]["table"]> & RelationRefs<Q["types"]["schema"]>, value: AnyExpression<Q["types"]["result"]>) => GroupTerms): Q;
|
|
1315
|
+
/**
|
|
1316
|
+
* Keeps the first row of each group of the rows of a query that `map` converts with equal values of the keys returned by `callback`, after the keys of earlier calls.
|
|
1317
|
+
*
|
|
1318
|
+
* `callback` receives the columns with the queries of the relations of the table, and returns what `distinctOn` of a query of records takes.
|
|
1319
|
+
*/
|
|
1320
|
+
distinctOn<Q extends QueryOf<MappedQueryState & ManyRowsQueryState>>(this: Q, callback: (columns: ColumnRefs<Q["types"]["table"]> & RelationRefs<Q["types"]["schema"]>) => GroupTerms): Q;
|
|
1321
|
+
/**
|
|
1322
|
+
* Keeps the first row of each group of the rows of a query of many rows with equal values of the listed columns, after the keys of earlier calls, such as the latest submission of each user.
|
|
1323
|
+
*
|
|
1324
|
+
* Sort the query by the keys first, then by the order that chooses the first row of each group; the rows keep that order.
|
|
1325
|
+
*/
|
|
1326
|
+
distinctOn<Q extends QueryOf<ManyRowsQueryState>>(this: Q, ...keys: (keyof Q["types"]["table"]["columns"] & string)[]): Q;
|
|
1327
|
+
/**
|
|
1328
|
+
* Locks the rows of the query until its transaction ends, so that code can read a row and then update it based on the values it read: another transaction waits to change or lock them.
|
|
1329
|
+
*
|
|
1330
|
+
* It replaces an earlier lock of the query, and reaches no other query: lock the records of a nested query by calling it there.
|
|
1331
|
+
* Without `options`, the query waits for the rows that another transaction has locked.
|
|
1332
|
+
*
|
|
1333
|
+
* It requires a transaction, unless the query is part of a mutation, such as of its filter.
|
|
1334
|
+
* The query cannot use `distinct`, `distinctOn`, or `groupBy`, and the aggregate methods and `exists` reject it after the lock.
|
|
1335
|
+
*/
|
|
1336
|
+
forUpdate<Q extends QueryOf<LockableQueryState>>(this: Q, options?: RowLockOptions): Q;
|
|
1337
|
+
/**
|
|
1338
|
+
* Locks the rows of the query as `forUpdate` does, but lets other transactions insert records that refer to them.
|
|
1339
|
+
*
|
|
1340
|
+
* Use it when the transaction only updates columns outside the keys of the rows.
|
|
1341
|
+
*/
|
|
1342
|
+
forNoKeyUpdate<Q extends QueryOf<LockableQueryState>>(this: Q, options?: RowLockOptions): Q;
|
|
1343
|
+
/** Keeps the rows of the query from changing until its transaction ends, as `forUpdate` does, while other transactions read and share-lock them too. */
|
|
1344
|
+
forShare<Q extends QueryOf<LockableQueryState>>(this: Q, options?: RowLockOptions): Q;
|
|
1345
|
+
/** Keeps the rows of the query from being deleted or changing their keys until its transaction ends, as `forUpdate` does, while other transactions change their other columns. */
|
|
1346
|
+
forKeyShare<Q extends QueryOf<LockableQueryState>>(this: Q, options?: RowLockOptions): Q;
|
|
1137
1347
|
/** Makes a query of one row, such as one of `take` or `get`, resolve to `undefined` without a row. */
|
|
1138
1348
|
optional<S extends OneRowQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): SelectQuery<ChangeState<S, {
|
|
1139
1349
|
rows: "optional";
|
|
@@ -1142,7 +1352,7 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1142
1352
|
/**
|
|
1143
1353
|
* Makes a query of one record, such as one of `take`, resolve to `null` without a row.
|
|
1144
1354
|
*
|
|
1145
|
-
* A value of `get` or `pluck` is never `null` for a missing row, which would read as a `NULL` value, so a query of values does not accept it, and after `get` or `pluck` the query resolves to `undefined` without a row.
|
|
1355
|
+
* A value of `get` or `pluck` is never `null` for a missing row, which would read as a `NULL` value, so a query of values does not accept it, unless `map` converts them, and after `get` or `pluck` the query resolves to `undefined` without a row.
|
|
1146
1356
|
*/
|
|
1147
1357
|
nullable<S extends OneRecordQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): SelectQuery<ChangeState<S, {
|
|
1148
1358
|
rows: "nullable";
|
|
@@ -1157,7 +1367,7 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1157
1367
|
* Inserts a record into the table of a query without clauses, such as a table of a `Db`, and resolves to the number of the inserted rows, `1`.
|
|
1158
1368
|
*
|
|
1159
1369
|
* A column omitted or given as `undefined` gets its default, so only columns marked with `hasDefault` and nullable columns may be omitted.
|
|
1160
|
-
* A value is what the type of its column takes, `null` for `NULL`,
|
|
1370
|
+
* A value is what the type of its column takes, `null` for `NULL`, an expression, such as a `sql` fragment, or a query of one value, which gives `NULL` without its row.
|
|
1161
1371
|
* Select the fields that it returns after it, such as `insert(values).get("id")`.
|
|
1162
1372
|
*/
|
|
1163
1373
|
insert<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, values: InsertValues<S["table"]>): MutationQuery<InitialMutationState<S["table"], S["schema"], "one">, Kind>;
|
|
@@ -1168,15 +1378,26 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1168
1378
|
* Select the fields that it returns for the inserted rows after it, such as `insertMany(records).select("id", "email")`, and match them to the records by a key rather than by their order, which PostgreSQL does not guarantee.
|
|
1169
1379
|
*/
|
|
1170
1380
|
insertMany<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, records: readonly InsertValues<S["table"]>[]): MutationQuery<InitialMutationState<S["table"], S["schema"], "many">, Kind>;
|
|
1381
|
+
/**
|
|
1382
|
+
* Updates the rows that the filters of the query choose with the values that `callback` returns, and resolves to the number of the updated rows.
|
|
1383
|
+
*
|
|
1384
|
+
* `callback` receives the columns and the queries of the relations of the changed rows, such as to set a column from another one or from related records, and returns the values of the columns to update; a column omitted or given as `undefined` keeps its value.
|
|
1385
|
+
* A value is what `insert` takes as a value, such as a query of an aggregate of related records.
|
|
1386
|
+
* A query of `find` or `findBy` rejects with `NotFoundError` without its row, unless `optional` or `nullable` allows it.
|
|
1387
|
+
* A query without a filter must call `all` to update every row.
|
|
1388
|
+
* Select the fields that it returns after it, with their values after the update, such as `update(callback).select("id")`.
|
|
1389
|
+
*/
|
|
1390
|
+
update<S extends SelectQueryState, Kind extends SelectQueryKind, F extends (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => UpdateValues<S["table"]>>(this: SelectQuery<S, Kind>, callback: F): MutationQuery<InitialMutationState<S["table"], S["schema"], S["rows"], OperandUses<ReturnType<F>[keyof ReturnType<F>]>>, Kind>;
|
|
1171
1391
|
/**
|
|
1172
1392
|
* Updates the rows that the filters of the query choose with the values of `set`, and resolves to the number of the updated rows.
|
|
1173
1393
|
*
|
|
1174
|
-
* `set` gives the values of the columns to update, or is a callback that
|
|
1394
|
+
* `set` gives the values of the columns to update, or is a callback that returns them; a column omitted or given as `undefined` keeps its value.
|
|
1395
|
+
* A value is what `insert` takes as a value.
|
|
1175
1396
|
* A query of `find` or `findBy` rejects with `NotFoundError` without its row, unless `optional` or `nullable` allows it.
|
|
1176
1397
|
* A query without a filter must call `all` to update every row.
|
|
1177
1398
|
* Select the fields that it returns after it, with their values after the update, such as `update(set).select("id")`.
|
|
1178
1399
|
*/
|
|
1179
|
-
update<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, set: UpdateSet<S["table"]>): MutationQuery<InitialMutationState<S["table"], S["schema"], S["rows"]>, Kind>;
|
|
1400
|
+
update<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, set: UpdateSet<S["table"], S["schema"]>): MutationQuery<InitialMutationState<S["table"], S["schema"], S["rows"]>, Kind>;
|
|
1180
1401
|
/**
|
|
1181
1402
|
* Deletes the rows that the filters of the query choose, and resolves to the number of the deleted rows.
|
|
1182
1403
|
*
|
|
@@ -1187,6 +1408,26 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1187
1408
|
delete<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): MutationQuery<InitialMutationState<S["table"], S["schema"], S["rows"]>, Kind>;
|
|
1188
1409
|
/** Allows `update` and `delete` to change every row of the table when the query has no filter; it changes nothing else. */
|
|
1189
1410
|
all<Q extends QueryOf<ManyRowsQueryState>>(this: Q): Q;
|
|
1411
|
+
/**
|
|
1412
|
+
* Converts each record or value of the result in code, after decoding, executed or nested, such as to add a field that SQL cannot compute.
|
|
1413
|
+
*
|
|
1414
|
+
* `callback` receives one record or value, also `null` of a `NULL` value, and is not called for a missing row, so an optional query still resolves to `undefined` without a row; it returns its result synchronously.
|
|
1415
|
+
* The SQL stays as it is, so the methods that read the fields, such as `select` and `count`, and expressions reject the query after it, whereas `where`, `orderBy`, `limit`, `take`, and further `map` apply.
|
|
1416
|
+
* A failure of `callback` rejects with `ResultDecodeError`, whose cause is the error.
|
|
1417
|
+
*/
|
|
1418
|
+
map<S extends SelectQueryState, Kind extends SelectQueryKind, R>(this: SelectQuery<S, Kind>, callback: (row: S["result"]) => R & SynchronousResult<R>): SelectQuery<ChangeState<S, {
|
|
1419
|
+
result: R;
|
|
1420
|
+
value: "mapped";
|
|
1421
|
+
aggregate: true;
|
|
1422
|
+
}>, Kind>;
|
|
1423
|
+
/**
|
|
1424
|
+
* Converts the whole result in code, after decoding, executed or nested, such as to group rows or to compute a value from them.
|
|
1425
|
+
*
|
|
1426
|
+
* `callback` receives what the query resolves to, once: the array of rows, one row, or `null` or `undefined` of a missing row of a nullable or optional query; it returns its result synchronously.
|
|
1427
|
+
* The SQL stays as it is, so call it last: the returned query only executes, nests in a selection, and takes further `transform`.
|
|
1428
|
+
* A failure of `callback` rejects with `ResultDecodeError`, whose cause is the error.
|
|
1429
|
+
*/
|
|
1430
|
+
transform<S extends SelectQueryState, Kind extends SelectQueryKind, R>(this: SelectQuery<S, Kind>, callback: (result: QueryResult<S>) => R & SynchronousResult<R>): TransformedQuery<TransformedQueryState<S["table"], R, S["uses"]>, Kind>;
|
|
1190
1431
|
/**
|
|
1191
1432
|
* Returns the query that `callback` builds from this query, such as one with a conditional clause, or that a helper of `defineHelper` builds with `args`.
|
|
1192
1433
|
*
|
|
@@ -1219,20 +1460,30 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1219
1460
|
* It adds the changes of `take` to `changes`, which must leave the limit as it is.
|
|
1220
1461
|
*/
|
|
1221
1462
|
private deriveTaken;
|
|
1222
|
-
/**
|
|
1463
|
+
/**
|
|
1464
|
+
* Creates the query of the value of `projection`, of an aggregate or of `exists` over the rows of this query, which always returns one row.
|
|
1465
|
+
*
|
|
1466
|
+
* A query of `distinct` or `distinctOn` keeps the fields that `projection` replaces: `distinct` compares them, and the keys, the order, and the argument of an aggregate read a selected field through its projection.
|
|
1467
|
+
*/
|
|
1223
1468
|
private deriveAggregate;
|
|
1224
|
-
/** Creates the query of the aggregate of `method` over `arg`, a column key or a callback that receives the columns and returns an expression; `delimiter` is the delimiter of `stringAgg`. */
|
|
1469
|
+
/** Creates the query of the aggregate of `method` over `arg`, a column key or a callback that receives the columns and the relations and returns an expression or a query of one value; `delimiter` is the delimiter of `stringAgg`. */
|
|
1225
1470
|
private aggregateBy;
|
|
1226
1471
|
/** Returns the changes of the node that add `filter` with `AND`, ending the guarantee of the row as `FilteredState` does in types. */
|
|
1227
1472
|
private addFilter;
|
|
1473
|
+
/** Returns the rows mode of this query after a filter, as `FilteredStateRows` gives it in types. */
|
|
1474
|
+
private getFilteredRows;
|
|
1228
1475
|
/** Creates the query of `find` and `findBy`: `where` of `values`, an object of column values, returning one row like `take`. */
|
|
1229
1476
|
private takeWhere;
|
|
1230
1477
|
/** Creates the query of one row with rows mode `rows` that `method` returns, rejecting a query of many rows. */
|
|
1231
1478
|
private changeOneRow;
|
|
1479
|
+
/** Rejects `method`, which reads or changes the fields of the query, on a query that `map` converts. */
|
|
1480
|
+
private rejectMapped;
|
|
1232
1481
|
/** Rejects a change of the selection by `method` of a query made by `get`, `pluck`, or an aggregate method. */
|
|
1233
1482
|
private rejectValue;
|
|
1234
1483
|
/** Rejects `method`, which replaces the value or the selection, on a query made by an aggregate method or `exists`. */
|
|
1235
1484
|
private rejectAggregate;
|
|
1485
|
+
/** Rejects `method`, an aggregate method or `exists`, on a query made by one of them or with a row lock. */
|
|
1486
|
+
private rejectAggregation;
|
|
1236
1487
|
}
|
|
1237
1488
|
/** Select query bound to a `Db`, executed by awaiting it; every method returns a query bound to the same `Db`. */
|
|
1238
1489
|
type BoundSelectQuery<S extends SelectQueryState> = SelectQuery<S, "bound">;
|
|
@@ -1240,63 +1491,733 @@ type BoundSelectQuery<S extends SelectQueryState> = SelectQuery<S, "bound">;
|
|
|
1240
1491
|
type AnySelectQuery = SelectQuery<any, SelectQueryKind>;
|
|
1241
1492
|
/** Type of a record of a query result, or of its value for a query made by `get`, `pluck`, or an aggregate method. */
|
|
1242
1493
|
type ResultOf<Q extends AnyQuery> = Q["types"]["result"];
|
|
1243
|
-
/** Table of a definition such as `
|
|
1494
|
+
/** Table of a definition such as `defineExpression`: the table itself or a query over it with schema context `C`, such as `schema.form`. */
|
|
1244
1495
|
type TableOrQuery<T extends AnyTable, C extends SchemaContext = SchemaContext> = T | QueryOf<QueryStateOf<T, C>>;
|
|
1245
1496
|
/** Starts a query over a new occurrence of the table. */
|
|
1246
1497
|
declare function from<T extends AnyTable>(table: T): SelectQuery<InitialQueryState<T>>;
|
|
1247
|
-
/**
|
|
1248
|
-
|
|
1249
|
-
/** Generic operator of `equalityOperators`, which gives the type it is added to SQL `=`. */
|
|
1250
|
-
interface EqualityOperator<S extends OperatorSignature> extends GenericOperator<S> {
|
|
1251
|
-
readonly [equalityBrand]: true;
|
|
1252
|
-
}
|
|
1253
|
-
/** Key of the method of a query of a table that returns its node as the values of `in`, or rejects a query of records. */
|
|
1254
|
-
declare const valuesQueryNode: unique symbol;
|
|
1255
|
-
/** Signature of `in`: membership in a list of operands of the expression type, or in the values of a query, such as of `pluck`; an empty list or query yields `FALSE`. */
|
|
1256
|
-
interface InSignature extends OperatorSignature {
|
|
1257
|
-
readonly method: <const L extends readonly Operand<this["T"], this["I"]>[] | ValuesQuery<NonNullable<this["T"]> | null>>(list: L) => Expression<L extends readonly unknown[] ? L extends readonly [] ? boolean : BooleanResult<this["T"] | OperandValue<L[number]>> : BooleanResult<this["T"] | (L extends ValuesQuery ? ValuesQueryValue<L> : never)>>;
|
|
1258
|
-
}
|
|
1259
|
-
/** Operators of `equalityOperators`. */
|
|
1260
|
-
type EqualityOperators = {
|
|
1261
|
-
eq: EqualityOperator<ComparisonSignature>;
|
|
1262
|
-
ne: EqualityOperator<ComparisonSignature>;
|
|
1263
|
-
in: EqualityOperator<InSignature>;
|
|
1264
|
-
};
|
|
1265
|
-
/** Tells whether operators `O` include an operator of `equalityOperators`, so that their type has SQL `=`. */
|
|
1266
|
-
type HasEquality<O> = true extends { [K in keyof O]: O[K] extends {
|
|
1267
|
-
readonly [equalityBrand]: true;
|
|
1268
|
-
} ? true : false; }[keyof O] ? true : false;
|
|
1498
|
+
/** References to the columns of the query source, passed to query callbacks. */
|
|
1499
|
+
type ColumnRefs<T extends AnyTable> = { readonly [K in keyof T["columns"]]: Expression<Selectable<T>[K], ColumnOperators<T, K>, ColumnValues<T>[K]>; };
|
|
1269
1500
|
/**
|
|
1270
|
-
*
|
|
1501
|
+
* Queries of the records of the relations of the table of schema context `C`, by relation name, which the callbacks of queries receive beside the columns.
|
|
1271
1502
|
*
|
|
1272
|
-
* A
|
|
1503
|
+
* A relation name is never a column key of its table.
|
|
1504
|
+
* A context whose table key is not known, such as one of a query of any schema, has no relations.
|
|
1273
1505
|
*/
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1506
|
+
type RelationRefs<C extends SchemaContext> = string extends C["key"] ? EmptyRecord : C["key"] extends keyof C["relations"] ? RelationQueries<C["relations"], C["key"]> : EmptyRecord;
|
|
1507
|
+
type RelationQueries<R extends SchemaContext["relations"], K extends keyof R & string> = { readonly [N in keyof R[K]]: RelatedQuery<R, K, N & string>; };
|
|
1508
|
+
/** Unbound query of the records of relation `N` of the table under key `K` of a schema of relations `R`, in the schema context of its target and the rows mode of the relation, whose row a required relation guarantees. */
|
|
1509
|
+
type RelatedQuery<R extends SchemaContext["relations"], K extends keyof R & string, N extends keyof R[K] & string> = SelectQuery<InitialQueryState<R[K][N]["target"], TableContext<R, R[K][N]["targetKey"]>, UsedRelation<K, N, R[K][N]>, RelationRows<R[K][N]["kind"], R[K][N]["required"]>, R[K][N]["required"]>>;
|
|
1510
|
+
/** Runtime context of the queries of a table in the schema they start from. */
|
|
1511
|
+
interface QueryContext {
|
|
1512
|
+
/** Getters of the queries of the relations of the table, which the references of the callbacks of queries have as their own properties and which read the key columns from them. */
|
|
1513
|
+
readonly relations: PropertyDescriptorMap;
|
|
1281
1514
|
}
|
|
1282
|
-
/** State of the query of `$select` of `Sel`: the record of the values of its expressions and of its literals and the results of its queries. */
|
|
1283
|
-
type SelectionState<Sel extends Selection> = RecordQueryState<MergeSelection<EmptyRecord, SelectionResult<Sel>>, SelectionUses<Sel>>;
|
|
1284
1515
|
/**
|
|
1285
|
-
*
|
|
1516
|
+
* Direction of a sort key, optionally with the placement of `NULL`.
|
|
1286
1517
|
*
|
|
1287
|
-
*
|
|
1288
|
-
* A bound query executes on its `Db` when awaited.
|
|
1518
|
+
* Without a placement, `NULL` comes last in ascending order and first in descending order.
|
|
1289
1519
|
*/
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1520
|
+
type OrderDirection = "asc" | "desc" | "asc nulls first" | "asc nulls last" | "desc nulls first" | "desc nulls last";
|
|
1521
|
+
/** Columns of table `T` with the directions to sort by them, in the order of their keys. */
|
|
1522
|
+
type OrderColumns<T extends AnyTable> = { readonly [K in keyof T["columns"]]?: OrderDirection; };
|
|
1523
|
+
/** Argument of `orderBy` of a query over table `T`: a column key, sorted in ascending order, or columns with directions. */
|
|
1524
|
+
type OrderKey<T extends AnyTable> = (keyof T["columns"] & string) | OrderColumns<T>;
|
|
1525
|
+
declare const ordering: unique symbol;
|
|
1526
|
+
/** Expression or query of one value with the direction of its sort key, made by `asc`, `desc`, or `orderBy`. */
|
|
1527
|
+
interface Ordering {
|
|
1528
|
+
readonly [ordering]: true;
|
|
1529
|
+
}
|
|
1530
|
+
/** Sort key returned by the callback of `orderBy`: an expression or a query of one value, such as of an aggregate of related records, sorted in ascending order, or one with a direction. */
|
|
1531
|
+
type OrderTerm = AnyExpression | ScalarQuery | Ordering;
|
|
1532
|
+
/**
|
|
1533
|
+
* References of the callbacks of `orderBy`, `groupBy`, and `distinctOn` to the fields of records `R` selected by a query: their expressions by key.
|
|
1534
|
+
*
|
|
1535
|
+
* A field of nested rows or of a literal is among them in types, but reading it throws; a field of an aggregate or of `exists` gives its value.
|
|
1536
|
+
*/
|
|
1537
|
+
type SelectedRefs<R> = { readonly [K in keyof R & string]: AnyExpression<R[K]>; };
|
|
1538
|
+
/** Sort keys returned by the callback of `orderBy`: one or an array of them. */
|
|
1539
|
+
type OrderTerms = OrderTerm | readonly OrderTerm[];
|
|
1540
|
+
/** Sorts by `value`, an expression or a query of one value, in ascending order, in the callback of `orderBy`. */
|
|
1541
|
+
declare function asc(value: AnyExpression | ScalarQuery): Ordering;
|
|
1542
|
+
/** Sorts by `value`, an expression or a query of one value, in descending order, in the callback of `orderBy`. */
|
|
1543
|
+
declare function desc(value: AnyExpression | ScalarQuery): Ordering;
|
|
1544
|
+
/** Sorts by `value`, an expression or a query of one value, in `direction`, such as `"desc nulls last"`, in the callback of `orderBy`. */
|
|
1545
|
+
declare function orderBy(value: AnyExpression | ScalarQuery, direction: OrderDirection): Ordering;
|
|
1546
|
+
/** Expression of a count: a number, with the operators of `orderingOperators`. */
|
|
1547
|
+
type CountExpression = Expression<number, CountValueType["operators"], CountValueType["input"]>;
|
|
1548
|
+
/** Expression of an aggregate of the values of `E`, an expression or a query of one value, `NULL` without values other than `NULL`, with the operators of its type. */
|
|
1549
|
+
type AggregateExpression<E extends TypedOperand> = Expression<OperandValue<E> | null, TypedOperandOperators<E>, TypedOperandInput<E>>;
|
|
1550
|
+
declare const groupValue: unique symbol;
|
|
1551
|
+
/** Value `T` of a directly selected expression in a nonempty group. */
|
|
1552
|
+
interface GroupValue<T> {
|
|
1553
|
+
readonly [groupValue]: T;
|
|
1554
|
+
}
|
|
1555
|
+
/** Aggregate without a filter, whose value in a group keeps only the nullability of its argument. */
|
|
1556
|
+
type GroupAggregateExpression<E extends TypedOperand> = AggregateExpression<E> & GroupValue<OperandValue<E>>;
|
|
1557
|
+
/** Options of an aggregate function. */
|
|
1558
|
+
interface AggregateOptions {
|
|
1559
|
+
/** Condition of the rows that the function aggregates, as `FILTER (WHERE ...)`: a Boolean expression or a query of one Boolean value. */
|
|
1560
|
+
readonly filter?: AnyExpression<boolean | null> | ScalarQuery<boolean | null>;
|
|
1561
|
+
}
|
|
1562
|
+
/** Options of `stringAgg`. */
|
|
1563
|
+
interface StringAggOptions extends AggregateOptions {
|
|
1564
|
+
/** Order in which the values are joined: a sort key or an array of them, as the callback of `orderBy` of a query returns; without it, PostgreSQL joins them in any order. */
|
|
1565
|
+
readonly orderBy?: OrderTerms;
|
|
1566
|
+
}
|
|
1567
|
+
/** Options of an aggregate function without `filter`, which takes every row. */
|
|
1568
|
+
interface UnfilteredAggregateOptions {
|
|
1569
|
+
/** No rows are removed from the aggregate input. */
|
|
1570
|
+
readonly filter?: undefined;
|
|
1571
|
+
}
|
|
1572
|
+
/** Options of `stringAgg` without `filter`, which takes every row. */
|
|
1573
|
+
interface UnfilteredStringAggOptions extends UnfilteredAggregateOptions {
|
|
1574
|
+
/** Order in which the values are joined: a sort key or an array of them, as the callback of `orderBy` of a query returns; without it, PostgreSQL joins them in any order. */
|
|
1575
|
+
readonly orderBy?: OrderTerms;
|
|
1576
|
+
}
|
|
1577
|
+
/** Returns the number of the rows of the query that selects it, or of each of its groups, as `count(*)`. */
|
|
1578
|
+
declare function count(): CountExpression;
|
|
1579
|
+
/** Returns the number of the rows of the query that selects it, or of each of its groups, as `count(*)`; `options.filter` chooses the rows that it counts. */
|
|
1580
|
+
declare function count(options: AggregateOptions): CountExpression;
|
|
1581
|
+
/** Returns the number of the values of `value`, an expression with a type or a query of one value, other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1582
|
+
declare function count(value: TypedOperand, options?: AggregateOptions): CountExpression;
|
|
1583
|
+
/** Returns the number of the distinct values of `value`, an expression with a type or a query of one value, other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1584
|
+
declare function countDistinct(value: TypedOperand, options?: AggregateOptions): CountExpression;
|
|
1585
|
+
/** Returns the smallest of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`. */
|
|
1586
|
+
declare function min<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1587
|
+
/** Returns the smallest of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1588
|
+
declare function min<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1589
|
+
/** Returns the largest of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`. */
|
|
1590
|
+
declare function max<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1591
|
+
/** Returns the largest of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1592
|
+
declare function max<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1593
|
+
/** Returns the sum of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`. */
|
|
1594
|
+
declare function sum<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1595
|
+
/** Returns the sum of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1596
|
+
declare function sum<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1597
|
+
/**
|
|
1598
|
+
* Returns the average of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`.
|
|
1599
|
+
*
|
|
1600
|
+
* For `bigint` values, average an expression of a type that can represent fractional values.
|
|
1601
|
+
*/
|
|
1602
|
+
declare function avg<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1603
|
+
/**
|
|
1604
|
+
* Returns the average of the values of `value`, an expression with a type or a query of one value, or `null` without values other than `NULL`; `options.filter` chooses the rows that it takes.
|
|
1605
|
+
*
|
|
1606
|
+
* For `bigint` values, average an expression of a type that can represent fractional values.
|
|
1607
|
+
*/
|
|
1608
|
+
declare function avg<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1609
|
+
/** Returns whether every value of `value`, a Boolean expression with a type or a query of one Boolean value, other than `NULL` is true, or `null` without such values. */
|
|
1610
|
+
declare function every<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1611
|
+
/** Returns whether every value of `value`, a Boolean expression with a type or a query of one Boolean value, other than `NULL` is true, or `null` without such values; `options.filter` chooses the rows that it takes. */
|
|
1612
|
+
declare function every<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1613
|
+
/** Returns whether any value of `value`, a Boolean expression with a type or a query of one Boolean value, is true, or `null` without values other than `NULL`. */
|
|
1614
|
+
declare function some<E extends TypedOperand>(value: E, options?: UnfilteredAggregateOptions): GroupAggregateExpression<E>;
|
|
1615
|
+
/** Returns whether any value of `value`, a Boolean expression with a type or a query of one Boolean value, is true, or `null` without values other than `NULL`; `options.filter` chooses the rows that it takes. */
|
|
1616
|
+
declare function some<E extends TypedOperand>(value: E, options?: AggregateOptions): AggregateExpression<E>;
|
|
1617
|
+
/** Returns the values other than `NULL` of `value`, a text expression with a type or a query of one text value, joined with `delimiter` in the order of `options.orderBy`, or `null` without such values. */
|
|
1618
|
+
declare function stringAgg<E extends TypedOperand>(value: E, delimiter: string, options?: UnfilteredStringAggOptions): GroupAggregateExpression<E>;
|
|
1619
|
+
/** Returns the values other than `NULL` of `value`, a text expression with a type or a query of one text value, joined with `delimiter` in the order of `options.orderBy`, or `null` without such values; `options.filter` chooses the rows that it takes. */
|
|
1620
|
+
declare function stringAgg<E extends TypedOperand>(value: E, delimiter: string, options?: StringAggOptions): AggregateExpression<E>;
|
|
1621
|
+
/**
|
|
1622
|
+
* Field of a selection whose value is `T`, made by `literal`.
|
|
1623
|
+
*
|
|
1624
|
+
* It is not an expression: it has no SQL, so only a selection takes it.
|
|
1625
|
+
*/
|
|
1626
|
+
declare class Literal<out T> {
|
|
1627
|
+
/** Value of the field in every record, which the query returns as it is. */
|
|
1628
|
+
readonly literal: T;
|
|
1629
|
+
constructor(
|
|
1630
|
+
/** Value of the field in every record, which the query returns as it is. */
|
|
1631
|
+
literal: T);
|
|
1632
|
+
/** Rejects the literal as an operand of an expression: a statement has no SQL of it. */
|
|
1633
|
+
[operandNode](): never;
|
|
1634
|
+
}
|
|
1635
|
+
/**
|
|
1636
|
+
* Creates a field of a selection with `value` in every record, without selecting anything in SQL: the query returns the value itself, not a copy of it, whatever it is.
|
|
1637
|
+
*
|
|
1638
|
+
* It lets a branch of a selection skip a column or a nested query, such as `withHint ? field.hint : literal(null)`.
|
|
1639
|
+
* To use a value in SQL, such as in a condition, pass it through `param` instead.
|
|
1640
|
+
*/
|
|
1641
|
+
declare function literal<T extends {} | null | undefined>(value: T): Literal<T>;
|
|
1642
|
+
/** Expressions, nested queries, and literals selected under the keys of the result fields; a record has no symbol keys. */
|
|
1643
|
+
interface Selection {
|
|
1644
|
+
readonly [key: string]: TypedExpression | NestedQuery | Literal<unknown>;
|
|
1645
|
+
readonly [key: symbol]: never;
|
|
1646
|
+
}
|
|
1647
|
+
/** Query whose rows a selection nests as the value of a field: a query of any state and kind, such as a query of a relation, also converted by `map` or `transform`. */
|
|
1648
|
+
type NestedQuery = QueryOf<SelectQueryState> | TransformedQuery<TransformedQueryState, SelectQueryKind>;
|
|
1649
|
+
/** Record of the values of the expressions, of the nested rows, and of the literals of a selection. */
|
|
1650
|
+
type SelectionResult<S extends Selection> = { [K in keyof S]: FieldValue<S[K]>; };
|
|
1651
|
+
/** Record of a selection in a query of groups. */
|
|
1652
|
+
type GroupSelectionResult<S extends Selection> = { [K in keyof S]: GroupFieldValue<S[K]>; };
|
|
1653
|
+
/** Value of a field selected in a group, leaving nested queries and literals unchanged. */
|
|
1654
|
+
type GroupFieldValue<V> = V extends GroupValue<infer T> ? T : FieldValue<V>;
|
|
1655
|
+
/** Value of an operand in a query of groups. */
|
|
1656
|
+
type GroupOperandValue<E> = E extends GroupValue<infer T> ? T : OperandValue<E>;
|
|
1657
|
+
/** Value of a selected field: of an expression, of the nested rows of a query as the result of executing it, or of a literal, for each member of a union of them. */
|
|
1658
|
+
type FieldValue<V> = V extends NestedQuery ? QueryResult<V["types"]> : V extends Literal<infer T> ? T : ExpressionValue<V>;
|
|
1659
|
+
/** Relations whose records the nested queries of a selection select. */
|
|
1660
|
+
type SelectionUses<S extends Selection> = S[keyof S] extends (infer V) ? V extends NestedQuery ? V["types"]["uses"] : never : never;
|
|
1661
|
+
/** Value of a selected expression. */
|
|
1662
|
+
type ExpressionValue<E> = E extends AnyExpression<infer T> ? T : never;
|
|
1663
|
+
/** Values that the type of an expression encodes, such as in place of its value. */
|
|
1664
|
+
type ExpressionInput<E> = E extends AnyExpression<unknown, infer I> ? I : never;
|
|
1665
|
+
declare const helperFields: unique symbol;
|
|
1666
|
+
/** Marker of the input fields of a helper, carrying the keys selected by its callback. */
|
|
1667
|
+
interface HelperFields<Keys extends PropertyKey = PropertyKey> {
|
|
1668
|
+
readonly [helperFields]: Keys;
|
|
1669
|
+
}
|
|
1670
|
+
/** Result `R` of a callback of a helper without the marker, which a record of the callback keeps, also through a spread, so that the declarations of a module exporting a helper never name it; `any` stays `any`. */
|
|
1671
|
+
type WithoutHelperFields<R> = IsAny<R> extends true ? R : R extends HelperFields ? { [K in keyof R as K extends keyof HelperFields ? never : K]: R[K]; } : R;
|
|
1672
|
+
/** Records with `Fields` added or replaced, for each branch of either record type; `any` stays `any`. */
|
|
1673
|
+
type MergeSelection<Previous, Fields> = Previous extends unknown ? Fields extends unknown ? MergeFields<RecordFields<Previous>, RecordFields<Fields>> & (IsAny<Previous> extends true ? Previous : unknown) : never : never;
|
|
1674
|
+
/** Fields with numeric keys represented as the string keys used in result records. */
|
|
1675
|
+
type RecordFields<Fields> = Fields extends unknown ? [Extract<keyof Fields, number>] extends [never] ? Fields : { [K in keyof Fields as K extends number ? `${K}` : K]: Fields[K]; } : never;
|
|
1676
|
+
/** Merge of normalized fields, retaining the previous value when an optional replacement is absent. */
|
|
1677
|
+
type MergeFields<Previous, Fields> = Simplify<{ [K in keyof Previous as K extends keyof Fields ? ({} extends Pick<Fields, K> ? K : never) : K]: K extends typeof helperFields // A branch selecting extra keys must not be reduced to one selecting fewer fields or none.
|
|
1678
|
+
? Previous[K] | keyof Fields : K extends keyof Fields // A decoder can return undefined, so it cannot be excluded as merely an absent property.
|
|
1679
|
+
? Previous[K] | Fields[K] : Previous[K]; } & { [K in keyof Fields as K extends keyof Previous ? ({} extends Pick<Fields, K> ? never : K) : K]: Fields[K]; }>;
|
|
1680
|
+
/** Types of the values of columns `K` of table `T` as selected fields: the values that each encodes and its operators. */
|
|
1681
|
+
type ColumnFieldTypes<T extends AnyTable, K extends keyof T["columns"]> = { [P in K]: ColumnValueType<T, P>; };
|
|
1682
|
+
/** Types of the values of the expressions and the queries of one value of selection `Sel`, under the keys of the result records, as `MergeSelection` normalizes them. */
|
|
1683
|
+
type SelectionFieldTypes<Sel> = { [K in keyof Sel as K extends number ? `${K}` : K]: FieldType<Exclude<Sel[K], undefined>>; };
|
|
1684
|
+
/** Type of the value of a selected field `F`, for each member of a union of them, so that each keeps its own operators and values. */
|
|
1685
|
+
type FieldType<F> = F extends TypedOperand ? F extends GroupValue<infer T> ? OperandValueType<F> & {
|
|
1686
|
+
readonly group: T;
|
|
1687
|
+
} : OperandValueType<F> : unknown;
|
|
1688
|
+
/** Record `R` of a query whose rows become groups, with field types `F`. */
|
|
1689
|
+
type GroupedRecord<R, F> = IsAny<R> extends true ? R : R extends unknown ? { [K in keyof R]: K extends keyof F ? GroupedField<R[K], Exclude<F[K], undefined>> : R[K]; } : never;
|
|
1690
|
+
/** Value of a selected field after grouping; a union of field types must all guarantee a non-null value to remove `null`. */
|
|
1691
|
+
type GroupedField<V, D> = [D] extends [{
|
|
1692
|
+
readonly group: infer G;
|
|
1693
|
+
}] ? null extends G ? V : Exclude<V, null> : V;
|
|
1694
|
+
/** Types of the values of fields `P` with those of fields `N` added or replaced, by the rules of `MergeSelection`, for each branch of either. */
|
|
1695
|
+
type MergeFieldTypes<P, N> = P extends unknown ? N extends unknown ? [keyof N] extends [never] ? P : [keyof P] extends [never] ? N : { [K in keyof P | keyof N]: K extends keyof N ? {} extends Pick<N, K> ? K extends keyof P ? P[K] | Exclude<N[K], undefined> : Exclude<N[K], undefined> : N[K] : K extends keyof P ? P[K] : never; } : never : never;
|
|
1696
|
+
/** Types of the values of the selected fields of a query: those of `P`, the `fieldTypes` part of the state before a selection, with `N` added or replaced. */
|
|
1697
|
+
interface SelectedFieldTypes<out P, N> {
|
|
1698
|
+
readonly types: MergeFieldTypes<SelectedFieldTypesOf<P>, N>;
|
|
1699
|
+
}
|
|
1700
|
+
/** Types of the values of the selected fields of the `fieldTypes` part `F` of a state, by key; none for `EmptyRecord` of a query without selected fields. */
|
|
1701
|
+
type SelectedFieldTypesOf<F> = F extends SelectedFieldTypes<unknown, unknown> ? F["types"] : EmptyRecord;
|
|
1702
|
+
/** First operand of `coalesce`, `greatest`, and `least`: an expression with a type, or a query of one value, such as of an aggregate, whose type decodes the result and gives it its operators. */
|
|
1703
|
+
type TypedOperand = TypedExpression | TypedScalarQuery;
|
|
1704
|
+
/** Values that the type of the first operand `F` encodes. */
|
|
1705
|
+
type TypedOperandInput<F> = F extends TypedScalarQuery ? F["types"]["valueType"]["input"] : ExpressionInput<F>;
|
|
1706
|
+
/** Operators of the type of the first operand `F`. */
|
|
1707
|
+
type TypedOperandOperators<F> = F extends TypedScalarQuery ? F["types"]["valueType"]["operators"] : F extends TypedExpression ? ExpressionOperators<F> : never;
|
|
1708
|
+
/** Operand of `coalesce`, `greatest`, and `least` after the first operand `F`: a value that its type encodes, or an expression or a query of one value of its values. */
|
|
1709
|
+
type FollowingOperand<F> = Operand<OperandValue<F>, TypedOperandInput<F>>;
|
|
1710
|
+
/** `null` when every operand can be `NULL`: the first operand `F` and the following operands `R`. */
|
|
1711
|
+
type NullOfEveryOperand<F, R extends readonly unknown[]> = null extends OperandValue<F> ? number extends R["length"] ? null : { [K in keyof R]: null extends OperandValue<R[K]> ? never : true; }[number] extends never ? null : never : never;
|
|
1712
|
+
/** Result of a function of the first operand `F` and the following operands `R` that is `NULL` only when every operand is, whose operands all have the values of `F`. */
|
|
1713
|
+
type NullSkippingResult<F, R extends readonly unknown[]> = Expression<Exclude<OperandValue<F>, null> | NullOfEveryOperand<F, R>, TypedOperandOperators<F>, TypedOperandInput<F>>;
|
|
1714
|
+
/**
|
|
1715
|
+
* Returns the first of its operands other than `NULL`, such as `coalesce(field.hint, "")`, as an expression usable wherever a column is.
|
|
1716
|
+
*
|
|
1717
|
+
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
1718
|
+
* A query without its row is `NULL` here, unlike with the method `coalesce` of a query, which keeps a missing row missing.
|
|
1719
|
+
*/
|
|
1720
|
+
declare function coalesce<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
1721
|
+
/**
|
|
1722
|
+
* Returns the largest of its operands, skipping `NULL`, which is the result only when every operand is `NULL`.
|
|
1723
|
+
*
|
|
1724
|
+
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
1725
|
+
*/
|
|
1726
|
+
declare function greatest<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
1727
|
+
/**
|
|
1728
|
+
* Returns the smallest of its operands, skipping `NULL`, which is the result only when every operand is `NULL`.
|
|
1729
|
+
*
|
|
1730
|
+
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
1731
|
+
*/
|
|
1732
|
+
declare function least<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
1733
|
+
/** Condition of a branch of `caseWhen`: a predicate, or a query of one Boolean value, such as of `exists`. */
|
|
1734
|
+
type CaseCondition = Predicate | ScalarQuery<boolean | null>;
|
|
1735
|
+
/** Value of a branch of `caseWhen` of a type of values `T` that encodes values `I`: an operand of the type, or a plain `null`. */
|
|
1736
|
+
type CaseValue<T, I> = Operand<T, I> | null;
|
|
1737
|
+
/** Branch of `caseWhen`: a condition and the value of the result when it holds. */
|
|
1738
|
+
type CaseBranch<T, I> = readonly [condition: CaseCondition, value: CaseValue<T, I>];
|
|
1739
|
+
/** Value of `caseWhen` of a type of values `T` whose branches and fallback have the values `V`, of which one gives the result. */
|
|
1740
|
+
type CaseResult<T, V> = Exclude<T, null> | Extract<OperandValue<V>, null>;
|
|
1741
|
+
/**
|
|
1742
|
+
* Returns the value of the first branch whose condition holds, as a SQL `CASE` expression, or `NULL` when none holds.
|
|
1743
|
+
*
|
|
1744
|
+
* `type` decodes the result and gives it its operators, and a plain value of a branch is cast to it, so that PostgreSQL does not take plain values for text.
|
|
1745
|
+
*/
|
|
1746
|
+
declare function caseWhen<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>, branches: NoInfer<readonly CaseBranch<Output, Input>[]>): Expression<Exclude<Output, null> | null, O, Input>;
|
|
1747
|
+
/**
|
|
1748
|
+
* Returns the value of the first branch whose condition holds, as a SQL `CASE` expression, or `otherwise` when none holds.
|
|
1749
|
+
*
|
|
1750
|
+
* `type` decodes the result and gives it its operators, and a plain value of a branch or of `otherwise` is cast to it, so that PostgreSQL does not take plain values for text.
|
|
1751
|
+
*/
|
|
1752
|
+
declare function caseWhen<Output, Input, O extends OperatorDefinitions, const B extends readonly CaseBranch<Output, Input>[], const E extends CaseValue<Output, Input>>(type: Type<Output, Input, O, any>, branches: B, otherwise: E): Expression<CaseResult<Output, B[number][1] | E>, O, Input>;
|
|
1753
|
+
/**
|
|
1754
|
+
* State of the query that the callback of a helper of table `T` with schema context `C` receives: the fields of the query the helper is applied to, and all its rows.
|
|
1755
|
+
*
|
|
1756
|
+
* It is guaranteed, so that the state that the callback returns tells whether the callback ends the guarantee of the query the helper is applied to.
|
|
1757
|
+
*/
|
|
1758
|
+
interface HelperState<T extends AnyTable, C extends SchemaContext> extends QueryStateOf<T, C> {
|
|
1759
|
+
readonly result: HelperFields<never>;
|
|
1760
|
+
readonly valueType: never;
|
|
1761
|
+
readonly fieldTypes: EmptyRecord;
|
|
1762
|
+
readonly rows: "many";
|
|
1763
|
+
readonly value: false;
|
|
1764
|
+
readonly uses: never;
|
|
1765
|
+
readonly guaranteed: true;
|
|
1766
|
+
readonly aggregate: false;
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Effect of a helper: the fields it selects and the types of their values, or whether `map` converts its rows and what it gives, its rows mode on a query of many rows, `one` after `take` or `required` and `nullable` or `optional` after the method of the same name, the relations it selects records of, and whether it keeps the guarantee of the row of a query of a required relation.
|
|
1770
|
+
*
|
|
1771
|
+
* `reads` are the relations of the schema of the helper among them, which its callback may read from the references of the query it is applied to, and `key` is the key of its table there.
|
|
1772
|
+
*/
|
|
1773
|
+
interface HelperEffect {
|
|
1774
|
+
/** Whether the helper guarantees that the query is grouped. */
|
|
1775
|
+
readonly grouped: boolean;
|
|
1776
|
+
readonly fields: object;
|
|
1777
|
+
/** Types of the values of the fields it selects, the `fieldTypes` part of the state that its callback returns, which `having` and the narrowing of grouped selections read. */
|
|
1778
|
+
readonly fieldTypes: unknown;
|
|
1779
|
+
/** Whether `map` converts the rows, whose fields the result no longer tells, so that `fields` are empty. */
|
|
1780
|
+
readonly mapped: boolean;
|
|
1781
|
+
/** What `map` gives for each row of a query the helper is applied to; `never` when `mapped` is `false`. */
|
|
1782
|
+
readonly output: unknown;
|
|
1783
|
+
readonly rows: ResultRows;
|
|
1784
|
+
readonly uses: UsedRelation;
|
|
1785
|
+
readonly reads: UsedRelation;
|
|
1786
|
+
readonly key: string;
|
|
1787
|
+
readonly guaranteed: boolean;
|
|
1788
|
+
}
|
|
1789
|
+
/** Effect of a helper whose callback returns a query of state `S`, for each state of a union. */
|
|
1790
|
+
type ReadEffect<S extends SelectQueryState> = S extends SelectQueryState ? {
|
|
1791
|
+
readonly grouped: S["aggregate"] extends Grouped ? true : false;
|
|
1792
|
+
readonly fields: S["value"] extends "mapped" ? EmptyRecord : { [K in keyof S["result"] as K extends keyof HelperFields ? never : K]: S["result"][K]; };
|
|
1793
|
+
readonly fieldTypes: S["value"] extends "mapped" ? EmptyRecord : S["fieldTypes"];
|
|
1794
|
+
readonly mapped: S["value"] extends "mapped" ? true : false;
|
|
1795
|
+
readonly output: S["value"] extends "mapped" ? WithoutHelperFields<S["result"]> : never;
|
|
1796
|
+
readonly rows: S["rows"];
|
|
1797
|
+
readonly uses: S["uses"];
|
|
1798
|
+
readonly reads: Extract<UsableRelation<S["schema"]["relations"]>, S["uses"]>;
|
|
1799
|
+
readonly key: S["schema"]["key"];
|
|
1800
|
+
readonly guaranteed: S["guaranteed"];
|
|
1801
|
+
} : never;
|
|
1802
|
+
/** Error of a helper that a query does not accept, or whose callback returns a query it does not accept. */
|
|
1803
|
+
interface HelperError<Message extends string> {
|
|
1804
|
+
readonly error: Message;
|
|
1805
|
+
}
|
|
1806
|
+
/** `unknown` when every result state retains the input marker and returns records, or converts such records with `map`, or the error. */
|
|
1807
|
+
type CheckResult<S extends SelectQueryState> = [S] extends [{
|
|
1808
|
+
readonly result: HelperFields;
|
|
1809
|
+
readonly value: false;
|
|
1810
|
+
} | {
|
|
1811
|
+
readonly value: "mapped";
|
|
1812
|
+
readonly valueType: never;
|
|
1813
|
+
}] ? unknown : HelperError<"A helper can filter, select fields, map them, and take one row, but cannot clear the selection or use get, pluck, or aggregates">;
|
|
1814
|
+
/** State of a query over table `T` that accepts a helper of effect `E`. */
|
|
1815
|
+
type HelperInput<T extends AnyTable, E extends HelperEffect> = QueryStateOf<T, ReadsContext<E>> & ValueCheck<E> & (true extends E["grouped"] ? {
|
|
1816
|
+
readonly aggregate: false;
|
|
1817
|
+
} : unknown);
|
|
1818
|
+
/** Constraint of `value` of a query that accepts a helper of effect `E`. */
|
|
1819
|
+
type ValueCheck<E extends HelperEffect> = [E extends HelperEffect ? keyof E["fields"] : never] extends [never] ? [Extract<E["rows"], "nullable">] extends [never] ? [E["mapped"]] extends [false] ? unknown : RecordRows<"A helper cannot apply map to a query made by get, pluck, an aggregate, or map"> : RecordRows<"A helper cannot apply nullable to a query made by get, pluck, an aggregate, or map"> : RecordRows<"A helper cannot select fields on a query made by get, pluck, an aggregate, or map">;
|
|
1820
|
+
/** Constraint of a query whose rows are records, which a query of values fails with error `Message`. */
|
|
1821
|
+
interface RecordRows<Message extends string> {
|
|
1822
|
+
readonly value: false | HelperError<Message>;
|
|
1823
|
+
}
|
|
1824
|
+
/** Schema context of a query that has the relations of its schema that a helper of effect `E` may read. */
|
|
1825
|
+
type ReadsContext<E extends HelperEffect> = [E["reads"]] extends [never] ? SchemaContext : SchemaContext<{ readonly [K in E["reads"]["key"]]: { readonly [N in Extract<E["reads"], {
|
|
1826
|
+
readonly key: K;
|
|
1827
|
+
}>["name"]]: Extract<E["reads"], {
|
|
1828
|
+
readonly key: K;
|
|
1829
|
+
readonly name: N;
|
|
1830
|
+
}>["relation"]; }; }, E["key"]>;
|
|
1831
|
+
/** State of a query of state `S` after a helper of effect `E`. */
|
|
1832
|
+
type ApplyEffect<S extends SelectQueryState, E extends HelperEffect> = ApplySelectionEffect<E["grouped"] extends true ? GroupedState<S> : S, E>;
|
|
1833
|
+
/** Applies the selected fields and row effects after the grouping guaranteed by the helper. */
|
|
1834
|
+
type ApplySelectionEffect<S extends SelectQueryState, E extends HelperEffect> = ChangeState<S, {
|
|
1835
|
+
result: E["mapped"] extends true ? E["output"] : [keyof E["fields"]] extends [never] ? S["result"] : MergeSelection<S["result"], S["aggregate"] extends Grouped ? GroupedRecord<E["fields"], SelectedFieldTypesOf<E["fieldTypes"]>> : E["fields"]>;
|
|
1836
|
+
fieldTypes: SelectedFieldTypes<S["fieldTypes"], SelectedFieldTypesOf<E["fieldTypes"]>>;
|
|
1837
|
+
value: E["mapped"] extends true ? "mapped" : S["value"];
|
|
1838
|
+
aggregate: E["mapped"] extends true ? true : S["aggregate"];
|
|
1839
|
+
rows: E["rows"] extends "many" ? EffectRows<S, E> : E["rows"] extends "one" ? OneRow<EffectRows<S, E>> : E["rows"];
|
|
1840
|
+
uses: S["uses"] | E["uses"];
|
|
1841
|
+
guaranteed: E["guaranteed"] extends true ? S["guaranteed"] : false;
|
|
1842
|
+
}>;
|
|
1843
|
+
/** Rows mode of a query of state `S` after a helper of effect `E` that keeps the rows, before `take`: relaxed as after a filter when the helper ends the guarantee. */
|
|
1844
|
+
type EffectRows<S extends SelectQueryState, E extends HelperEffect> = E["guaranteed"] extends true ? S["rows"] : FilteredStateRows<S>;
|
|
1845
|
+
/**
|
|
1846
|
+
* Helper of `defineHelper` for queries over table `T`: it applies effect `E` to the query it receives with arguments `A` and returns a query of the same kind.
|
|
1847
|
+
*
|
|
1848
|
+
* Selected fields replace earlier fields under the same keys; a query made by `get`, `pluck`, an aggregate method, or `map` does not accept a helper that selects fields or maps them.
|
|
1849
|
+
* `use` of a mutation also applies a helper that only selects fields and maps them.
|
|
1850
|
+
*/
|
|
1851
|
+
type Helper<T extends AnyTable, E extends HelperEffect, A extends unknown[]> = <S extends HelperInput<T, E>, Kind extends SelectQueryKind>(query: SelectQuery<S, Kind>, ...args: A) => E extends HelperEffect ? SelectQuery<ApplyEffect<S, E>, Kind> : never;
|
|
1852
|
+
/**
|
|
1853
|
+
* Defines a reusable helper of a table that filters, selects fields, converts them with `map`, and applies `take`, `optional`, `nullable`, and `required`, used with `use` or called with a query.
|
|
1854
|
+
*
|
|
1855
|
+
* `helper` receives the query it is applied to and any further arguments, and returns the query with its clauses.
|
|
1856
|
+
* Defined with a query of a schema, such as `schema.form`, it can select the relations of the table in that schema, and applies to queries whose schema has them.
|
|
1857
|
+
* The helper returns a query of the same kind as the one it is applied to, bound to the same `Db` if any.
|
|
1858
|
+
* A helper that only selects fields and maps them also applies to an insert, an update, and a delete with their `use`, where it selects what they return; other methods fail there.
|
|
1859
|
+
*/
|
|
1860
|
+
declare function defineHelper<T extends AnyTable, R extends SelectQuery<any, SelectQueryKind>, A extends unknown[], C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, helper: (query: SelectQuery<HelperState<T, C>, SelectQueryKind>, ...args: A) => R & CheckResult<R["types"]>): Helper<T, ReadEffect<R["types"]>, A>;
|
|
1861
|
+
/**
|
|
1862
|
+
* State in types of a query that inserts, updates, or deletes rows: its table and the relations of its schema, the rows mode of the rows it changes, and its result, which is their number until a selection returns their fields or `transform` converts it.
|
|
1863
|
+
*
|
|
1864
|
+
* Methods change only their own parts of the state.
|
|
1865
|
+
*/
|
|
1866
|
+
interface MutationQueryState extends QueryState {
|
|
1867
|
+
readonly table: AnyTable;
|
|
1868
|
+
readonly schema: SchemaContext;
|
|
1869
|
+
/** Rows mode of the rows that the query changes, which `select`, `get`, and `pluck` return. */
|
|
1870
|
+
readonly changed: ResultRows;
|
|
1871
|
+
/** Whether the query resolves to something other than the number of the rows it changes: their fields or what `transform` makes of that number. */
|
|
1872
|
+
readonly returning: boolean;
|
|
1873
|
+
}
|
|
1874
|
+
/** State of a query that inserts, updates, or deletes rows of table `T` with schema context `C` in rows mode `Rows` and returns their number, whose values select records of relations `U`. */
|
|
1875
|
+
interface InitialMutationState<T extends AnyTable, C extends SchemaContext, Rows extends ResultRows, U extends UsedRelation = never> extends MutationQueryState {
|
|
1876
|
+
readonly table: T;
|
|
1877
|
+
readonly schema: C;
|
|
1878
|
+
readonly changed: Rows;
|
|
1879
|
+
readonly returning: false;
|
|
1880
|
+
readonly result: number;
|
|
1881
|
+
readonly rows: "one";
|
|
1882
|
+
readonly value: true;
|
|
1883
|
+
readonly uses: U;
|
|
1884
|
+
}
|
|
1885
|
+
/** State `S` of a mutation with the parts of `Changes` replaced. */
|
|
1886
|
+
type ChangeMutationState<S extends MutationQueryState, Changes extends Partial<MutationQueryState>> = { readonly [K in keyof MutationQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
1887
|
+
/** State of a mutation that returns the number of the rows it changes or their records, which `select` applies to. */
|
|
1888
|
+
type RecordMutationState = MutationQueryState & ({
|
|
1889
|
+
readonly returning: false;
|
|
1890
|
+
} | {
|
|
1891
|
+
readonly value: false;
|
|
1892
|
+
});
|
|
1893
|
+
/** State of a mutation that `map` and `transform` do not convert, which `pluck` applies to. */
|
|
1894
|
+
type UnconvertedMutationState = MutationQueryState & {
|
|
1895
|
+
readonly value: boolean;
|
|
1896
|
+
};
|
|
1897
|
+
/** State of a mutation of one row that `map` and `transform` do not convert, which `get` applies to. */
|
|
1898
|
+
type OneRowMutationState = UnconvertedMutationState & {
|
|
1899
|
+
readonly changed: "one" | "nullable" | "optional";
|
|
1900
|
+
};
|
|
1901
|
+
/** State of a mutation that returns the fields of the rows it changes, which `map` applies to unless `transform` converts it; the number of the rows has no rows to convert. */
|
|
1902
|
+
type ReturningMutationState = MutationQueryState & {
|
|
1903
|
+
readonly returning: true;
|
|
1904
|
+
readonly value: boolean | "mapped";
|
|
1905
|
+
};
|
|
1906
|
+
/** Constraint of a mutation that does not accept a helper that filters or takes one row, whose methods it lacks. */
|
|
1907
|
+
interface NotMutationEffect {
|
|
1908
|
+
readonly returning: HelperError<"A helper of a mutation can only select fields and map them">;
|
|
1909
|
+
}
|
|
1910
|
+
/** Constraint of a mutation that accepts a helper of table `T` of effect `E`, whose callback reads the relations of its schema context from the mutation. */
|
|
1911
|
+
type HelperMutationInput<T extends AnyTable, E extends HelperEffect> = MutationQueryState & {
|
|
1912
|
+
readonly table: T;
|
|
1913
|
+
readonly schema: ReadsContext<E>;
|
|
1914
|
+
} & ([E["guaranteed"]] extends [true] ? [E["rows"]] extends ["many"] ? [E extends HelperEffect ? keyof E["fields"] : never, E["mapped"]] extends [never, false] ? unknown : RecordMutationState : NotMutationEffect : NotMutationEffect);
|
|
1915
|
+
/** State of a mutation of state `S` after a helper of effect `E`: the rows that `map` gives, or the records with the fields that it selects. */
|
|
1916
|
+
type HelperMutationState<S extends MutationQueryState, E extends HelperEffect> = E["mapped"] extends true ? ChangeMutationState<S, {
|
|
1917
|
+
result: E["output"];
|
|
1918
|
+
rows: S["changed"];
|
|
1919
|
+
value: "mapped";
|
|
1920
|
+
returning: true;
|
|
1921
|
+
uses: S["uses"] | E["uses"];
|
|
1922
|
+
}> : [keyof E["fields"]] extends [never] ? ChangeMutationState<S, {
|
|
1923
|
+
uses: S["uses"] | E["uses"];
|
|
1924
|
+
}> : RecordsState<S, E["fields"], E["uses"]>;
|
|
1925
|
+
/** Record that the returned fields of a mutation of state `S` add to: the fields selected earlier, or none. */
|
|
1926
|
+
type ReturnedRecord$1<S extends MutationQueryState> = S["returning"] extends true ? S["result"] : EmptyRecord;
|
|
1927
|
+
/** State of a mutation of state `S` that returns records with `Fields`, which its nested queries select records of relations `U` for. */
|
|
1928
|
+
type RecordsState<S extends MutationQueryState, Fields, U extends UsedRelation = never> = ChangeMutationState<S, {
|
|
1929
|
+
result: MergeSelection<ReturnedRecord$1<S>, Fields>;
|
|
1930
|
+
rows: S["changed"];
|
|
1931
|
+
value: false;
|
|
1932
|
+
returning: true;
|
|
1933
|
+
uses: S["uses"] | U;
|
|
1934
|
+
}>;
|
|
1935
|
+
/** State of a mutation of state `S` that returns value `V` of each row it changes in rows mode `Rows`. */
|
|
1936
|
+
type ValueState$1<S extends MutationQueryState, V, Rows extends ResultRows> = ChangeMutationState<S, {
|
|
1937
|
+
result: V;
|
|
1938
|
+
rows: Rows;
|
|
1939
|
+
value: true;
|
|
1940
|
+
returning: true;
|
|
1941
|
+
}>;
|
|
1942
|
+
/** State of a mutation of state `S` that returns the value of `E`, an expression or a query of one value that a callback returns, of each row it changes in rows mode `Rows`. */
|
|
1943
|
+
type OperandState<S extends MutationQueryState, E extends TypedOperand, Rows extends ResultRows> = ChangeMutationState<S, {
|
|
1944
|
+
result: OperandValue<E>;
|
|
1945
|
+
rows: Rows;
|
|
1946
|
+
value: true;
|
|
1947
|
+
returning: true;
|
|
1948
|
+
uses: S["uses"] | OperandUses<E>;
|
|
1949
|
+
}>;
|
|
1950
|
+
/**
|
|
1951
|
+
* Immutable query of state `S` that inserts, updates, or deletes rows, unbound or bound to a `Db`.
|
|
1952
|
+
*
|
|
1953
|
+
* Without a selection, it resolves to the number of the rows it changes; `select`, `get`, and `pluck` return the fields of those rows instead, and `map` and `transform` convert them in code.
|
|
1954
|
+
* An unbound query is not thenable, so it can be returned from async functions; it is compiled with `toSQL` or executed by a `Db`.
|
|
1955
|
+
* A bound query executes on its `Db` when awaited.
|
|
1956
|
+
*/
|
|
1957
|
+
declare class MutationQuery<out S extends MutationQueryState, out Kind extends SelectQueryKind = "unbound"> {
|
|
1958
|
+
readonly table: S["table"];
|
|
1959
|
+
readonly node: MutationNode;
|
|
1960
|
+
/** Relations of the table in the schema the query starts from, which its callbacks receive. */
|
|
1961
|
+
readonly context: QueryContext;
|
|
1962
|
+
readonly types: S;
|
|
1963
|
+
/** `Db` that executes the query when it is awaited; `undefined` for an unbound query. */
|
|
1964
|
+
readonly db: QueryDb<Kind>;
|
|
1965
|
+
/** Executes a bound query on its `Db`; `undefined` for an unbound query, so that it is not thenable. */
|
|
1966
|
+
readonly then: QueryThen<S, Kind>;
|
|
1967
|
+
constructor(table: S["table"], node: MutationNode,
|
|
1968
|
+
/** Relations of the table in the schema the query starts from, which its callbacks receive. */
|
|
1969
|
+
context: QueryContext, db?: AnyDb);
|
|
1970
|
+
/** Returns all columns of each changed row, replacing fields under their keys, instead of the number of the rows. */
|
|
1971
|
+
select<S extends RecordMutationState, Kind extends SelectQueryKind>(this: MutationQuery<S, Kind>, all: "*"): MutationQuery<RecordsState<S, Selectable<S["table"]>>, Kind>;
|
|
1972
|
+
/** Returns the listed columns of each changed row, replacing fields under their keys, instead of the number of the rows. */
|
|
1973
|
+
select<S extends RecordMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, ...keys: K[]): MutationQuery<RecordsState<S, Pick<Selectable<S["table"]>, K>>, Kind>;
|
|
1974
|
+
/**
|
|
1975
|
+
* Returns the expressions and nested queries that `callback` returns for each changed row, replacing fields under their keys, instead of the number of the rows.
|
|
1976
|
+
*
|
|
1977
|
+
* `callback` receives the columns of the changed row, with their values after an insert or an update and those of the deleted row, and the queries of the relations of the table, as the callback of `select` of a select query does.
|
|
1978
|
+
* A nested query reads the database as it was before the change, apart from the changed row.
|
|
1979
|
+
*/
|
|
1980
|
+
select<S extends RecordMutationState, Kind extends SelectQueryKind, Sel extends Selection>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => Sel): MutationQuery<RecordsState<S, SelectionResult<Sel>, SelectionUses<Sel>>, Kind>;
|
|
1981
|
+
/**
|
|
1982
|
+
* Returns the value of a column of the changed row instead of the number of the rows, in a query that changes one row, such as of `insert` or `find`.
|
|
1983
|
+
*
|
|
1984
|
+
* A query of a nullable row resolves to `undefined` without the row.
|
|
1985
|
+
*/
|
|
1986
|
+
get<S extends OneRowMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, key: K): MutationQuery<ValueState$1<S, Selectable<S["table"]>[K], OneValueRows<S["changed"]>>, Kind>;
|
|
1987
|
+
/**
|
|
1988
|
+
* Returns the value of the expression that `callback` returns for the changed row instead of the number of the rows, in a query that changes one row, such as of `insert` or `find`.
|
|
1989
|
+
*
|
|
1990
|
+
* `callback` receives what the callback of `select` receives, and may return a query of one value, which gives `NULL` without its row.
|
|
1991
|
+
* A query of a nullable row resolves to `undefined` without the row.
|
|
1992
|
+
*/
|
|
1993
|
+
get<S extends OneRowMutationState, Kind extends SelectQueryKind, E extends TypedOperand>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): MutationQuery<OperandState<S, E, OneValueRows<S["changed"]>>, Kind>;
|
|
1994
|
+
/** Returns the value of a column of each changed row instead of the number of the rows, keeping whether the query returns an array or one value. */
|
|
1995
|
+
pluck<S extends UnconvertedMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, key: K): MutationQuery<ValueState$1<S, Selectable<S["table"]>[K], ValueRows<S["changed"]>>, Kind>;
|
|
1996
|
+
/**
|
|
1997
|
+
* Returns the value of the expression that `callback` returns for each changed row instead of the number of the rows, keeping whether the query returns an array or one value.
|
|
1998
|
+
*
|
|
1999
|
+
* `callback` receives what the callback of `select` receives, and may return a query of one value, which gives `NULL` without its row.
|
|
2000
|
+
*/
|
|
2001
|
+
pluck<S extends UnconvertedMutationState, Kind extends SelectQueryKind, E extends TypedOperand>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => E): MutationQuery<OperandState<S, E, ValueRows<S["changed"]>>, Kind>;
|
|
2002
|
+
/**
|
|
2003
|
+
* Converts each record or value that the mutation returns in code, after decoding, such as to add a field that SQL cannot compute; select the fields before it, since the number of the rows has no rows to convert, which `transform` converts instead.
|
|
2004
|
+
*
|
|
2005
|
+
* `callback` receives one record or value, also `null` of a `NULL` value, and is not called for a missing row; it returns its result synchronously.
|
|
2006
|
+
* A failure of `callback` rejects with `ResultDecodeError`, after the mutation has changed its rows.
|
|
2007
|
+
*/
|
|
2008
|
+
map<S extends ReturningMutationState, Kind extends SelectQueryKind, R>(this: MutationQuery<S, Kind>, callback: (row: S["result"]) => R & SynchronousResult<R>): MutationQuery<ChangeMutationState<S, {
|
|
2009
|
+
result: R;
|
|
2010
|
+
value: "mapped";
|
|
2011
|
+
}>, Kind>;
|
|
2012
|
+
/**
|
|
2013
|
+
* Converts the whole result of the mutation in code, after decoding, such as the number of the rows it changes into whether it changed any.
|
|
2014
|
+
*
|
|
2015
|
+
* `callback` receives what the mutation resolves to, once, and returns its result synchronously; call `select`, `get`, `pluck`, and `map` before it.
|
|
2016
|
+
* A failure of `callback` rejects with `ResultDecodeError`, after the mutation has changed its rows.
|
|
2017
|
+
*/
|
|
2018
|
+
transform<S extends MutationQueryState, Kind extends SelectQueryKind, R>(this: MutationQuery<S, Kind>, callback: (result: QueryResult<S>) => R & SynchronousResult<R>): MutationQuery<ChangeMutationState<S, {
|
|
2019
|
+
result: R;
|
|
2020
|
+
rows: "one";
|
|
2021
|
+
value: "transformed";
|
|
2022
|
+
returning: true;
|
|
2023
|
+
}>, Kind>;
|
|
2024
|
+
/**
|
|
2025
|
+
* Returns the mutation that `callback` builds from this mutation, such as one that selects the fields it returns.
|
|
2026
|
+
*
|
|
2027
|
+
* `callback` receives this mutation and `args` and returns this mutation or a mutation derived from it, which keeps the binding of this mutation.
|
|
2028
|
+
*/
|
|
2029
|
+
use<Q, Kind extends SelectQueryKind, R extends MutationQuery<any, Kind>, A extends unknown[] = []>(this: Q & MutationQuery<any, Kind>, callback: (query: Q, ...args: A) => R, ...args: A): R;
|
|
2030
|
+
/**
|
|
2031
|
+
* Applies a helper of `defineHelper` that only selects fields and maps them to what the mutation returns, as `use` of a select query does.
|
|
2032
|
+
*
|
|
2033
|
+
* Another method of a query in the helper fails: types reject those that filter or take one row, but not `groupBy`, `orderBy`, `limit`, `offset`, `distinct`, `distinctOn`, and the row locks, which they do not tell from a selection.
|
|
2034
|
+
* A selection of no fields fails as well on a mutation that returns the number of its rows.
|
|
2035
|
+
*/
|
|
2036
|
+
use<S extends HelperMutationInput<T, E>, Kind extends SelectQueryKind, T extends AnyTable, E extends HelperEffect, A extends unknown[]>(this: MutationQuery<S, Kind>, helper: Helper<T, E, A>, ...args: A): E extends HelperEffect ? MutationQuery<HelperMutationState<S, E>, Kind> : never;
|
|
2037
|
+
/**
|
|
2038
|
+
* Compiles the query into SQL text and parameters; `schemaName` qualifies its tables.
|
|
2039
|
+
*
|
|
2040
|
+
* A query bound to a `Db` compiles as the `Db` executes it, with the PostgreSQL schema of the `Db`, and rejects options.
|
|
2041
|
+
*/
|
|
2042
|
+
toSQL(...args: ToSQLArgs<Kind>): SqlStatement;
|
|
2043
|
+
/** Rejects the query as an operand of an expression, which takes a query of one value of a table. */
|
|
2044
|
+
[operandNode](): never;
|
|
2045
|
+
/** Creates a mutation of the same kind and `Db` with the node changes applied. */
|
|
2046
|
+
private derive;
|
|
2047
|
+
/** Creates the mutation that returns the value of `arg` of `method`, a column key or a callback that receives the columns and the relations and returns an expression or a query of one value, of each changed row. */
|
|
2048
|
+
private deriveValue;
|
|
2049
|
+
/** Rejects `method`, which changes the fields that the mutation returns, on a mutation that `map` or `transform` converts. */
|
|
2050
|
+
private rejectConverted;
|
|
2051
|
+
}
|
|
2052
|
+
/** Query of any state and kind that inserts, updates, or deletes rows. */
|
|
2053
|
+
type AnyMutationQuery = MutationQuery<any, SelectQueryKind>;
|
|
2054
|
+
/** Types of the fields of the rows of a query of SQL text by the names of the result columns they decode. */
|
|
2055
|
+
interface FieldTypes {
|
|
2056
|
+
readonly [key: string]: AnyType;
|
|
2057
|
+
readonly [key: symbol]: never;
|
|
2058
|
+
}
|
|
2059
|
+
/** Record of the values that the types of fields `F` decode. */
|
|
2060
|
+
type DecodedFields<F extends FieldTypes> = { -readonly [K in keyof F]: TypeOutput<F[K]>; };
|
|
2061
|
+
/**
|
|
2062
|
+
* State in types of a query of SQL text: the rows mode of the rows that its statement returns, and its result, which is the number of the rows of its command tag until a selection returns their fields.
|
|
2063
|
+
*
|
|
2064
|
+
* Methods change only their own parts of the state.
|
|
2065
|
+
*/
|
|
2066
|
+
interface SqlQueryState extends QueryState {
|
|
2067
|
+
/** Rows mode of the rows of the statement, which `select`, `get`, and `pluck` return. */
|
|
2068
|
+
readonly returned: ResultRows;
|
|
2069
|
+
/** Whether the query returns the fields of the rows of the statement rather than their number. */
|
|
2070
|
+
readonly returning: boolean;
|
|
2071
|
+
}
|
|
2072
|
+
/** State of a query of SQL text that returns the number of the rows of its command tag. */
|
|
2073
|
+
interface InitialSqlQueryState extends SqlQueryState {
|
|
2074
|
+
readonly result: number;
|
|
2075
|
+
readonly rows: "one";
|
|
2076
|
+
readonly value: true;
|
|
2077
|
+
readonly uses: never;
|
|
2078
|
+
readonly returned: "many";
|
|
2079
|
+
readonly returning: false;
|
|
2080
|
+
}
|
|
2081
|
+
/** State `S` of a query of SQL text with the parts of `Changes` replaced. */
|
|
2082
|
+
type ChangeSqlQueryState<S extends SqlQueryState, Changes extends Partial<SqlQueryState>> = { readonly [K in keyof SqlQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
2083
|
+
/** State of a query of SQL text that returns the number of the rows or their records, which `select` applies to. */
|
|
2084
|
+
type RecordSqlQueryState = SqlQueryState & ({
|
|
2085
|
+
readonly returning: false;
|
|
2086
|
+
} | {
|
|
2087
|
+
readonly value: false;
|
|
2088
|
+
});
|
|
2089
|
+
/** State of a query of SQL text of one row, which `optional` applies to. */
|
|
2090
|
+
type OneRowSqlQueryState = SqlQueryState & {
|
|
2091
|
+
readonly returned: "one" | "nullable" | "optional";
|
|
2092
|
+
};
|
|
2093
|
+
/** State of a query of SQL text of one row that returns its number or its record, which `nullable` applies to. */
|
|
2094
|
+
type OneRecordSqlQueryState = OneRowSqlQueryState & RecordSqlQueryState;
|
|
2095
|
+
/** Record that the fields of a query of SQL text of state `S` add to: the fields selected earlier, or none. */
|
|
2096
|
+
type ReturnedRecord<S extends SqlQueryState> = S["returning"] extends true ? S["result"] : EmptyRecord;
|
|
2097
|
+
/** State of a query of SQL text of state `S` whose rows are in rows mode `Rows`. */
|
|
2098
|
+
type RowsState<S extends SqlQueryState, Rows extends ResultRows> = ChangeSqlQueryState<S, {
|
|
2099
|
+
returned: Rows;
|
|
2100
|
+
rows: S["returning"] extends true ? Rows : "one";
|
|
2101
|
+
}>;
|
|
2102
|
+
/** State of a query of SQL text of state `S` that returns value `V` of each row in rows mode `Rows`. */
|
|
2103
|
+
type ValueState<S extends SqlQueryState, V, Rows extends ResultRows> = ChangeSqlQueryState<S, {
|
|
2104
|
+
result: V;
|
|
2105
|
+
rows: Rows;
|
|
2106
|
+
returned: Rows;
|
|
2107
|
+
value: true;
|
|
2108
|
+
returning: true;
|
|
2109
|
+
}>;
|
|
2110
|
+
/**
|
|
2111
|
+
* Immutable query of a statement of SQL text, bound to the `Db` that executes it when awaited.
|
|
2112
|
+
*
|
|
2113
|
+
* Without a selection, it resolves to the number of the rows of the command tag of its statement, such as the rows that an `UPDATE` changes; `select`, `get`, and `pluck` return the fields of the rows of the statement instead, decoded by types.
|
|
2114
|
+
*/
|
|
2115
|
+
declare class SqlQuery<out S extends SqlQueryState> {
|
|
2116
|
+
readonly node: TextStatementNode;
|
|
2117
|
+
/** `Db` that executes the query when it is awaited. */
|
|
2118
|
+
readonly db: AnyDb;
|
|
2119
|
+
readonly types: S;
|
|
2120
|
+
/** Executes the query on its `Db`. */
|
|
2121
|
+
readonly then: QueryThen<S, "bound">;
|
|
2122
|
+
constructor(node: TextStatementNode,
|
|
2123
|
+
/** `Db` that executes the query when it is awaited. */
|
|
2124
|
+
db: AnyDb);
|
|
2125
|
+
/**
|
|
2126
|
+
* Returns the records of the rows of the statement instead of their number, with the values of the result columns named by the keys of `fields`, decoded by their types; it adds the fields to those selected earlier, replacing fields under their keys.
|
|
2127
|
+
*
|
|
2128
|
+
* A type of a column that can be `NULL` must be nullable.
|
|
2129
|
+
* The statement rejects without a column of a field, or with the column twice; other columns are ignored.
|
|
2130
|
+
*/
|
|
2131
|
+
select<S extends RecordSqlQueryState, F extends FieldTypes>(this: SqlQuery<S>, fields: F): SqlQuery<ChangeSqlQueryState<S, {
|
|
2132
|
+
result: MergeSelection<ReturnedRecord<S>, DecodedFields<F>>;
|
|
2133
|
+
rows: S["returned"];
|
|
2134
|
+
value: false;
|
|
2135
|
+
returning: true;
|
|
2136
|
+
}>>;
|
|
2137
|
+
/**
|
|
2138
|
+
* Returns the value of the only column of the row of the statement instead of the number of the rows, decoded by `type`.
|
|
2139
|
+
*
|
|
2140
|
+
* Without a row, it rejects with `NotFoundError` unless `optional` makes the query resolve to `undefined`; it rejects several rows.
|
|
2141
|
+
* A `NULL` value is `null`, so a type of a column that can be `NULL` must be nullable.
|
|
2142
|
+
*/
|
|
2143
|
+
get<S extends SqlQueryState, T extends AnyType>(this: SqlQuery<S>, type: T): SqlQuery<ValueState<S, TypeOutput<T>, OneValueRows<S["returned"]>>>;
|
|
2144
|
+
/**
|
|
2145
|
+
* Returns the values of the only column of the rows of the statement instead of their number, decoded by `type`, keeping whether the query returns an array or one value.
|
|
2146
|
+
*
|
|
2147
|
+
* A `NULL` value is `null`, so a type of a column that can be `NULL` must be nullable.
|
|
2148
|
+
*/
|
|
2149
|
+
pluck<S extends SqlQueryState, T extends AnyType>(this: SqlQuery<S>, type: T): SqlQuery<ValueState<S, TypeOutput<T>, ValueRows<S["returned"]>>>;
|
|
2150
|
+
/**
|
|
2151
|
+
* Returns the row of the statement instead of an array, and rejects with `NotFoundError` without it and with several rows; it adds no limit to the statement.
|
|
2152
|
+
*
|
|
2153
|
+
* Before a selection, the query resolves to the number of the rows, `1`, and rejects as well.
|
|
2154
|
+
*/
|
|
2155
|
+
take<S extends SqlQueryState>(this: SqlQuery<S>): SqlQuery<RowsState<S, OneRow<S["returned"]>>>;
|
|
2156
|
+
/**
|
|
2157
|
+
* Makes a query of one row, such as one of `take` or `get`, resolve to `undefined` without the row.
|
|
2158
|
+
*
|
|
2159
|
+
* Before a selection, the query resolves to `0` without the row.
|
|
2160
|
+
*/
|
|
2161
|
+
optional<S extends OneRowSqlQueryState>(this: SqlQuery<S>): SqlQuery<RowsState<S, "optional">>;
|
|
2162
|
+
/**
|
|
2163
|
+
* Makes a query of one record, such as one of `take`, resolve to `null` without the row.
|
|
2164
|
+
*
|
|
2165
|
+
* A value of `get` or `pluck` is never `null` for a missing row, which would read as a `NULL` value, so a query of values does not accept it.
|
|
2166
|
+
* Before a selection, the query resolves to `0` without the row.
|
|
2167
|
+
*/
|
|
2168
|
+
nullable<S extends OneRecordSqlQueryState>(this: SqlQuery<S>): SqlQuery<RowsState<S, "nullable">>;
|
|
2169
|
+
/** Compiles the query into SQL text and parameters, as its `Db` executes it. */
|
|
2170
|
+
toSQL(): SqlStatement;
|
|
2171
|
+
/** Rejects the query as an operand of an expression, which takes a query of one value of a table. */
|
|
2172
|
+
[operandNode](): never;
|
|
2173
|
+
/** Creates a query of the same `Db` with the node changes applied. */
|
|
2174
|
+
private derive;
|
|
2175
|
+
/** Creates the query that returns the value of the only column of each row in rows mode `rows`, decoded by `type` of `method`. */
|
|
2176
|
+
private deriveValue;
|
|
2177
|
+
/** Creates the query of one row in rows mode `rows`, rejecting a query of many rows. */
|
|
2178
|
+
private changeOneRow;
|
|
2179
|
+
}
|
|
2180
|
+
/** Query of SQL text of any state. */
|
|
2181
|
+
type AnySqlQuery = SqlQuery<any>;
|
|
2182
|
+
/** Kind of a select query: `unbound`, or `bound` to a `Db`. */
|
|
2183
|
+
type SelectQueryKind = "unbound" | "bound";
|
|
2184
|
+
/**
|
|
2185
|
+
* `then` of a query of kind `Kind`: a bound query executes when awaited, and an unbound query is not thenable.
|
|
2186
|
+
*
|
|
2187
|
+
* Awaiting a query of either kind or returning it from an async function is a type error, since it may execute.
|
|
2188
|
+
*/
|
|
2189
|
+
type QueryThen<S extends QueryState, Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? <R1 = QueryResult<S>, R2 = never>(onFulfilled?: ((result: QueryResult<S>) => R1 | PromiseLike<R1>) | null, onRejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | null) => Promise<R1 | R2> : [Kind] extends ["unbound"] ? undefined : ((onFulfilled: never) => unknown) | undefined;
|
|
2190
|
+
/** `Db` of a query of kind `Kind`, which only a bound query has. */
|
|
2191
|
+
type QueryDb<Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? AnyDb : [Kind] extends ["unbound"] ? undefined : AnyDb | undefined;
|
|
2192
|
+
/** Arguments of `toSQL` of a query of kind `Kind`: a bound query compiles with the options of its `Db`. */
|
|
2193
|
+
type ToSQLArgs<Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? [] : [options?: CompileOptions];
|
|
2194
|
+
/** Query of any class, state, and kind: a select query of a table, one converted by `transform`, a query of `$select`, one that inserts, updates, or deletes rows, or a query of SQL text. */
|
|
2195
|
+
type AnyQuery = AnySelectQuery | AnyTransformedQuery | AnyRecordQuery | AnyMutationQuery | AnySqlQuery;
|
|
2196
|
+
/** State of a query of `$select` in types: one record `R`, whose nested queries select records of relations `U`. */
|
|
2197
|
+
interface RecordQueryState<R = unknown, U extends UsedRelation = UsedRelation> extends QueryState {
|
|
2198
|
+
readonly result: R;
|
|
2199
|
+
readonly rows: "one";
|
|
2200
|
+
readonly value: false;
|
|
2201
|
+
readonly uses: U;
|
|
2202
|
+
}
|
|
2203
|
+
/** State of the query of `$select` of `Sel`: the record of the values of its expressions and of its literals and the results of its queries. */
|
|
2204
|
+
type SelectionState<Sel extends Selection> = RecordQueryState<MergeSelection<EmptyRecord, SelectionResult<Sel>>, SelectionUses<Sel>>;
|
|
2205
|
+
/**
|
|
2206
|
+
* Immutable query of `$select` of state `S`, unbound or bound to a `Db`: one record whose fields are the values of expressions and of literals and the results of independent queries, selected in one statement.
|
|
2207
|
+
*
|
|
2208
|
+
* An unbound query is not thenable, so it can be returned from async functions; it is compiled with `toSQL` or executed by a `Db`.
|
|
2209
|
+
* A bound query executes on its `Db` when awaited.
|
|
2210
|
+
*/
|
|
2211
|
+
declare class RecordQuery<out S extends RecordQueryState, out Kind extends SelectQueryKind = "unbound"> {
|
|
2212
|
+
readonly node: RecordQueryNode;
|
|
2213
|
+
readonly types: S;
|
|
2214
|
+
/** `Db` that executes the query when it is awaited; `undefined` for an unbound query. */
|
|
2215
|
+
readonly db: QueryDb<Kind>;
|
|
2216
|
+
/** Executes a bound query on its `Db`; `undefined` for an unbound query, so that it is not thenable. */
|
|
2217
|
+
readonly then: QueryThen<S, Kind>;
|
|
2218
|
+
constructor(node: RecordQueryNode, db?: AnyDb);
|
|
2219
|
+
/**
|
|
2220
|
+
* Compiles the query into SQL text and parameters; `schemaName` qualifies its tables.
|
|
1300
2221
|
*
|
|
1301
2222
|
* A query bound to a `Db` compiles as the `Db` executes it, with the PostgreSQL schema of the `Db`, and rejects options.
|
|
1302
2223
|
*/
|
|
@@ -1507,272 +2428,59 @@ type TableContext<R extends SchemaRelations, K> = K extends keyof R ? [keyof R[K
|
|
|
1507
2428
|
* A composite key lists its columns in arrays of the same length.
|
|
1508
2429
|
*/
|
|
1509
2430
|
declare function many<Source extends AnyTable, Target extends AnyTable, const From extends RelationKey<Source>>(target: Target, keys: RelationKeys<Target, From>): RelationDeclaration<"many", Source, Target, From, false>;
|
|
1510
|
-
/**
|
|
1511
|
-
* Declares a relation of a table to the record of `target` whose columns `to` equal its columns `from`, for `$relations` of a schema.
|
|
1512
|
-
*
|
|
1513
|
-
* A composite key lists its columns in arrays of the same length.
|
|
1514
|
-
* The columns `to` must include a key of `target`, primary or unique, so that they match at most one record.
|
|
1515
|
-
* Without the record, a query of the relation gives `null`, and a query of a relation declared with `required: true` rejects unless it filters the record.
|
|
1516
|
-
* Set `required: true` only where the database guarantees the record, such as by a foreign key on columns `from` that are not nullable.
|
|
1517
|
-
*/
|
|
1518
|
-
declare function one<Source extends AnyTable, Target extends AnyTable, const From extends RelationKey<Source>, const To extends MatchingKey<Target, From>, const Keys extends object = EmptyRecord>(target: Target, keys: OneRelationKeys<From, To> & RequiredMember<Keys> & TargetKeyCheck<Target, To>): RelationDeclaration<"one", Source, Target, From, NoInfer<DeclaredRequiredOf<Keys>>>;
|
|
1519
|
-
/**
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
*/
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
readonly
|
|
1529
|
-
/** Whether the query returns the fields of the rows it changes rather than their number. */
|
|
1530
|
-
readonly returning: boolean;
|
|
1531
|
-
}
|
|
1532
|
-
/** State of a query that inserts, updates, or deletes rows of table `T` with schema context `C` in rows mode `Rows` and returns their number. */
|
|
1533
|
-
interface InitialMutationState<T extends AnyTable, C extends SchemaContext, Rows extends ResultRows> extends MutationQueryState {
|
|
1534
|
-
readonly table: T;
|
|
1535
|
-
readonly schema: C;
|
|
1536
|
-
readonly changed: Rows;
|
|
1537
|
-
readonly returning: false;
|
|
1538
|
-
readonly result: number;
|
|
1539
|
-
readonly rows: "one";
|
|
1540
|
-
readonly value: true;
|
|
1541
|
-
readonly uses: never;
|
|
1542
|
-
}
|
|
1543
|
-
/** State `S` of a mutation with the parts of `Changes` replaced. */
|
|
1544
|
-
type ChangeMutationState<S extends MutationQueryState, Changes extends Partial<MutationQueryState>> = { readonly [K in keyof MutationQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
1545
|
-
/** State of a mutation that returns the number of the rows it changes or their records, which `select` applies to. */
|
|
1546
|
-
type RecordMutationState = MutationQueryState & ({
|
|
1547
|
-
readonly returning: false;
|
|
1548
|
-
} | {
|
|
1549
|
-
readonly value: false;
|
|
1550
|
-
});
|
|
1551
|
-
/** State of a mutation of one row, which `get` applies to. */
|
|
1552
|
-
type OneRowMutationState = MutationQueryState & {
|
|
1553
|
-
readonly changed: "one" | "nullable" | "optional";
|
|
1554
|
-
};
|
|
1555
|
-
/** Record that the returned fields of a mutation of state `S` add to: the fields selected earlier, or none. */
|
|
1556
|
-
type ReturnedRecord$1<S extends MutationQueryState> = S["returning"] extends true ? S["result"] : EmptyRecord;
|
|
1557
|
-
/** State of a mutation of state `S` that returns records with `Fields`, which its nested queries select records of relations `U` for. */
|
|
1558
|
-
type RecordsState<S extends MutationQueryState, Fields, U extends UsedRelation = never> = ChangeMutationState<S, {
|
|
1559
|
-
result: MergeSelection<ReturnedRecord$1<S>, Fields>;
|
|
1560
|
-
rows: S["changed"];
|
|
1561
|
-
value: false;
|
|
1562
|
-
returning: true;
|
|
1563
|
-
uses: S["uses"] | U;
|
|
1564
|
-
}>;
|
|
1565
|
-
/** State of a mutation of state `S` that returns value `V` of each row it changes in rows mode `Rows`. */
|
|
1566
|
-
type ValueState$1<S extends MutationQueryState, V, Rows extends ResultRows> = ChangeMutationState<S, {
|
|
1567
|
-
result: V;
|
|
1568
|
-
rows: Rows;
|
|
1569
|
-
value: true;
|
|
1570
|
-
returning: true;
|
|
1571
|
-
}>;
|
|
1572
|
-
/**
|
|
1573
|
-
* Immutable query of state `S` that inserts, updates, or deletes rows, unbound or bound to a `Db`.
|
|
1574
|
-
*
|
|
1575
|
-
* Without a selection, it resolves to the number of the rows it changes; `select`, `get`, and `pluck` return the fields of those rows instead.
|
|
1576
|
-
* An unbound query is not thenable, so it can be returned from async functions; it is compiled with `toSQL` or executed by a `Db`.
|
|
1577
|
-
* A bound query executes on its `Db` when awaited.
|
|
1578
|
-
*/
|
|
1579
|
-
declare class MutationQuery<out S extends MutationQueryState, out Kind extends SelectQueryKind = "unbound"> {
|
|
1580
|
-
readonly table: S["table"];
|
|
1581
|
-
readonly node: MutationNode;
|
|
1582
|
-
/** Relations of the table in the schema the query starts from, which `select` gives its callback. */
|
|
1583
|
-
readonly context: QueryContext;
|
|
1584
|
-
readonly types: S;
|
|
1585
|
-
/** `Db` that executes the query when it is awaited; `undefined` for an unbound query. */
|
|
1586
|
-
readonly db: QueryDb<Kind>;
|
|
1587
|
-
/** Executes a bound query on its `Db`; `undefined` for an unbound query, so that it is not thenable. */
|
|
1588
|
-
readonly then: QueryThen<S, Kind>;
|
|
1589
|
-
constructor(table: S["table"], node: MutationNode,
|
|
1590
|
-
/** Relations of the table in the schema the query starts from, which `select` gives its callback. */
|
|
1591
|
-
context: QueryContext, db?: AnyDb);
|
|
1592
|
-
/** Returns all columns of each changed row, replacing fields under their keys, instead of the number of the rows. */
|
|
1593
|
-
select<S extends RecordMutationState, Kind extends SelectQueryKind>(this: MutationQuery<S, Kind>, all: "*"): MutationQuery<RecordsState<S, Selectable<S["table"]>>, Kind>;
|
|
1594
|
-
/** Returns the listed columns of each changed row, replacing fields under their keys, instead of the number of the rows. */
|
|
1595
|
-
select<S extends RecordMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, ...keys: K[]): MutationQuery<RecordsState<S, Pick<Selectable<S["table"]>, K>>, Kind>;
|
|
1596
|
-
/**
|
|
1597
|
-
* Returns the expressions and nested queries that `callback` returns for each changed row, replacing fields under their keys, instead of the number of the rows.
|
|
1598
|
-
*
|
|
1599
|
-
* `callback` receives the columns of the changed row, with their values after an insert or an update and those of the deleted row, and the queries of the relations of the table, as the callback of `select` of a select query does.
|
|
1600
|
-
* A nested query reads the database as it was before the change, apart from the changed row.
|
|
1601
|
-
*/
|
|
1602
|
-
select<S extends RecordMutationState, Kind extends SelectQueryKind, Sel extends Selection>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>) => Sel): MutationQuery<RecordsState<S, SelectionResult<Sel>, SelectionUses<Sel>>, Kind>;
|
|
1603
|
-
/**
|
|
1604
|
-
* Returns the value of a column of the changed row instead of the number of the rows, in a query that changes one row, such as of `insert` or `find`.
|
|
1605
|
-
*
|
|
1606
|
-
* A query of a nullable row resolves to `undefined` without the row.
|
|
1607
|
-
*/
|
|
1608
|
-
get<S extends OneRowMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, key: K): MutationQuery<ValueState$1<S, Selectable<S["table"]>[K], OneValueRows<S["changed"]>>, Kind>;
|
|
1609
|
-
/**
|
|
1610
|
-
* Returns the value of the expression that `callback` returns for the changed row instead of the number of the rows, in a query that changes one row, such as of `insert` or `find`.
|
|
1611
|
-
*
|
|
1612
|
-
* A query of a nullable row resolves to `undefined` without the row.
|
|
1613
|
-
*/
|
|
1614
|
-
get<S extends OneRowMutationState, Kind extends SelectQueryKind, E extends TypedExpression>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]>) => E): MutationQuery<ValueState$1<S, ExpressionValue<E>, OneValueRows<S["changed"]>>, Kind>;
|
|
1615
|
-
/** Returns the value of a column of each changed row instead of the number of the rows, keeping whether the query returns an array or one value. */
|
|
1616
|
-
pluck<S extends MutationQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: MutationQuery<S, Kind>, key: K): MutationQuery<ValueState$1<S, Selectable<S["table"]>[K], ValueRows<S["changed"]>>, Kind>;
|
|
1617
|
-
/** Returns the value of the expression that `callback` returns for each changed row instead of the number of the rows, keeping whether the query returns an array or one value. */
|
|
1618
|
-
pluck<S extends MutationQueryState, Kind extends SelectQueryKind, E extends TypedExpression>(this: MutationQuery<S, Kind>, callback: (columns: ColumnRefs<S["table"]>) => E): MutationQuery<ValueState$1<S, ExpressionValue<E>, ValueRows<S["changed"]>>, Kind>;
|
|
1619
|
-
/**
|
|
1620
|
-
* Compiles the query into SQL text and parameters; `schemaName` qualifies its tables.
|
|
1621
|
-
*
|
|
1622
|
-
* A query bound to a `Db` compiles as the `Db` executes it, with the PostgreSQL schema of the `Db`, and rejects options.
|
|
1623
|
-
*/
|
|
1624
|
-
toSQL(...args: ToSQLArgs<Kind>): SqlStatement;
|
|
1625
|
-
/** Rejects the query as an operand of an expression, which takes a query of one value of a table. */
|
|
1626
|
-
[operandNode](): never;
|
|
1627
|
-
/** Creates a mutation of the same kind and `Db` with the node changes applied. */
|
|
1628
|
-
private derive;
|
|
1629
|
-
/** Creates the mutation that returns the value of `arg` of `method`, a column key or a callback that receives the columns and returns an expression, of each changed row. */
|
|
1630
|
-
private deriveValue;
|
|
1631
|
-
}
|
|
1632
|
-
/** Query of any state and kind that inserts, updates, or deletes rows. */
|
|
1633
|
-
type AnyMutationQuery = MutationQuery<any, SelectQueryKind>;
|
|
1634
|
-
/** Types of the fields of the rows of a query of SQL text by the names of the result columns they decode. */
|
|
1635
|
-
interface FieldTypes {
|
|
1636
|
-
readonly [key: string]: AnyType;
|
|
1637
|
-
readonly [key: symbol]: never;
|
|
1638
|
-
}
|
|
1639
|
-
/** Record of the values that the types of fields `F` decode. */
|
|
1640
|
-
type DecodedFields<F extends FieldTypes> = { -readonly [K in keyof F]: TypeOutput<F[K]>; };
|
|
1641
|
-
/**
|
|
1642
|
-
* State in types of a query of SQL text: the rows mode of the rows that its statement returns, and its result, which is the number of the rows of its command tag until a selection returns their fields.
|
|
1643
|
-
*
|
|
1644
|
-
* Methods change only their own parts of the state.
|
|
1645
|
-
*/
|
|
1646
|
-
interface SqlQueryState extends QueryState {
|
|
1647
|
-
/** Rows mode of the rows of the statement, which `select`, `get`, and `pluck` return. */
|
|
1648
|
-
readonly returned: ResultRows;
|
|
1649
|
-
/** Whether the query returns the fields of the rows of the statement rather than their number. */
|
|
1650
|
-
readonly returning: boolean;
|
|
1651
|
-
}
|
|
1652
|
-
/** State of a query of SQL text that returns the number of the rows of its command tag. */
|
|
1653
|
-
interface InitialSqlQueryState extends SqlQueryState {
|
|
1654
|
-
readonly result: number;
|
|
2431
|
+
/**
|
|
2432
|
+
* Declares a relation of a table to the record of `target` whose columns `to` equal its columns `from`, for `$relations` of a schema.
|
|
2433
|
+
*
|
|
2434
|
+
* A composite key lists its columns in arrays of the same length.
|
|
2435
|
+
* The columns `to` must include a key of `target`, primary or unique, so that they match at most one record.
|
|
2436
|
+
* Without the record, a query of the relation gives `null`, and a query of a relation declared with `required: true` rejects unless it filters the record.
|
|
2437
|
+
* Set `required: true` only where the database guarantees the record, such as by a foreign key on columns `from` that are not nullable.
|
|
2438
|
+
*/
|
|
2439
|
+
declare function one<Source extends AnyTable, Target extends AnyTable, const From extends RelationKey<Source>, const To extends MatchingKey<Target, From>, const Keys extends object = EmptyRecord>(target: Target, keys: OneRelationKeys<From, To> & RequiredMember<Keys> & TargetKeyCheck<Target, To>): RelationDeclaration<"one", Source, Target, From, NoInfer<DeclaredRequiredOf<Keys>>>;
|
|
2440
|
+
/** Type that the result of a callback of `transform` and `map` is checked against when it may be a Promise. */
|
|
2441
|
+
interface NotSynchronous {
|
|
2442
|
+
readonly "the callback must return its result synchronously": never;
|
|
2443
|
+
}
|
|
2444
|
+
/** Check of `R`, the result of a callback of `transform` and `map`: it fails against `NotSynchronous` when `R` may be a Promise, such as a union with one, but not for `any` and `unknown`. */
|
|
2445
|
+
type SynchronousResult<R> = [Extract<R, PromiseLike<unknown>>] extends [never] ? unknown : IsAny<R> extends true ? unknown : NotSynchronous;
|
|
2446
|
+
/** State of a query of `transform` of a table in types: over table `T`, whose whole result is `R`, selecting records of relations `U`. */
|
|
2447
|
+
interface TransformedQueryState<T extends AnyTable = AnyTable, R = unknown, U extends UsedRelation = UsedRelation> extends QueryState {
|
|
2448
|
+
readonly table: T;
|
|
2449
|
+
readonly result: R;
|
|
1655
2450
|
readonly rows: "one";
|
|
1656
|
-
readonly value:
|
|
1657
|
-
readonly uses:
|
|
1658
|
-
readonly returned: "many";
|
|
1659
|
-
readonly returning: false;
|
|
2451
|
+
readonly value: "transformed";
|
|
2452
|
+
readonly uses: U;
|
|
1660
2453
|
}
|
|
1661
|
-
/** State `S` of a query of SQL text with the parts of `Changes` replaced. */
|
|
1662
|
-
type ChangeSqlQueryState<S extends SqlQueryState, Changes extends Partial<SqlQueryState>> = { readonly [K in keyof SqlQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
1663
|
-
/** State of a query of SQL text that returns the number of the rows or their records, which `select` applies to. */
|
|
1664
|
-
type RecordSqlQueryState = SqlQueryState & ({
|
|
1665
|
-
readonly returning: false;
|
|
1666
|
-
} | {
|
|
1667
|
-
readonly value: false;
|
|
1668
|
-
});
|
|
1669
|
-
/** State of a query of SQL text of one row, which `optional` applies to. */
|
|
1670
|
-
type OneRowSqlQueryState = SqlQueryState & {
|
|
1671
|
-
readonly returned: "one" | "nullable" | "optional";
|
|
1672
|
-
};
|
|
1673
|
-
/** State of a query of SQL text of one row that returns its number or its record, which `nullable` applies to. */
|
|
1674
|
-
type OneRecordSqlQueryState = OneRowSqlQueryState & RecordSqlQueryState;
|
|
1675
|
-
/** Record that the fields of a query of SQL text of state `S` add to: the fields selected earlier, or none. */
|
|
1676
|
-
type ReturnedRecord<S extends SqlQueryState> = S["returning"] extends true ? S["result"] : EmptyRecord;
|
|
1677
|
-
/** State of a query of SQL text of state `S` whose rows are in rows mode `Rows`. */
|
|
1678
|
-
type RowsState<S extends SqlQueryState, Rows extends ResultRows> = ChangeSqlQueryState<S, {
|
|
1679
|
-
returned: Rows;
|
|
1680
|
-
rows: S["returning"] extends true ? Rows : "one";
|
|
1681
|
-
}>;
|
|
1682
|
-
/** State of a query of SQL text of state `S` that returns value `V` of each row in rows mode `Rows`. */
|
|
1683
|
-
type ValueState<S extends SqlQueryState, V, Rows extends ResultRows> = ChangeSqlQueryState<S, {
|
|
1684
|
-
result: V;
|
|
1685
|
-
rows: Rows;
|
|
1686
|
-
returned: Rows;
|
|
1687
|
-
value: true;
|
|
1688
|
-
returning: true;
|
|
1689
|
-
}>;
|
|
1690
2454
|
/**
|
|
1691
|
-
* Immutable query of a
|
|
2455
|
+
* Immutable query of a table of state `S` whose whole result `transform` converts after decoding, unbound or bound to a `Db`.
|
|
1692
2456
|
*
|
|
1693
|
-
*
|
|
2457
|
+
* It executes and nests in a selection as the query it comes from, with the same SQL, so it has only `transform`, which converts the result further: another method would change what the callback receives, and an expression does not take it, since SQL does not see the conversion.
|
|
2458
|
+
* An unbound query is not thenable, so it can be returned from async functions; it is compiled with `toSQL` or executed by a `Db`.
|
|
2459
|
+
* A bound query executes on its `Db` when awaited.
|
|
1694
2460
|
*/
|
|
1695
|
-
declare class
|
|
1696
|
-
readonly node:
|
|
1697
|
-
/** `Db` that executes the query when it is awaited. */
|
|
1698
|
-
readonly db: AnyDb;
|
|
2461
|
+
declare class TransformedQuery<out S extends TransformedQueryState, out Kind extends SelectQueryKind = "unbound"> {
|
|
2462
|
+
readonly node: QueryNode;
|
|
1699
2463
|
readonly types: S;
|
|
1700
|
-
/**
|
|
1701
|
-
readonly
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
* The statement rejects without a column of a field, or with the column twice; other columns are ignored.
|
|
1710
|
-
*/
|
|
1711
|
-
select<S extends RecordSqlQueryState, F extends FieldTypes>(this: SqlQuery<S>, fields: F): SqlQuery<ChangeSqlQueryState<S, {
|
|
1712
|
-
result: MergeSelection<ReturnedRecord<S>, DecodedFields<F>>;
|
|
1713
|
-
rows: S["returned"];
|
|
1714
|
-
value: false;
|
|
1715
|
-
returning: true;
|
|
1716
|
-
}>>;
|
|
1717
|
-
/**
|
|
1718
|
-
* Returns the value of the only column of the row of the statement instead of the number of the rows, decoded by `type`.
|
|
1719
|
-
*
|
|
1720
|
-
* Without a row, it rejects with `NotFoundError` unless `optional` makes the query resolve to `undefined`; it rejects several rows.
|
|
1721
|
-
* A `NULL` value is `null`, so a type of a column that can be `NULL` must be nullable.
|
|
1722
|
-
*/
|
|
1723
|
-
get<S extends SqlQueryState, T extends AnyType>(this: SqlQuery<S>, type: T): SqlQuery<ValueState<S, TypeOutput<T>, OneValueRows<S["returned"]>>>;
|
|
1724
|
-
/**
|
|
1725
|
-
* Returns the values of the only column of the rows of the statement instead of their number, decoded by `type`, keeping whether the query returns an array or one value.
|
|
1726
|
-
*
|
|
1727
|
-
* A `NULL` value is `null`, so a type of a column that can be `NULL` must be nullable.
|
|
1728
|
-
*/
|
|
1729
|
-
pluck<S extends SqlQueryState, T extends AnyType>(this: SqlQuery<S>, type: T): SqlQuery<ValueState<S, TypeOutput<T>, ValueRows<S["returned"]>>>;
|
|
1730
|
-
/**
|
|
1731
|
-
* Returns the row of the statement instead of an array, and rejects with `NotFoundError` without it and with several rows; it adds no limit to the statement.
|
|
1732
|
-
*
|
|
1733
|
-
* Before a selection, the query resolves to the number of the rows, `1`, and rejects as well.
|
|
1734
|
-
*/
|
|
1735
|
-
take<S extends SqlQueryState>(this: SqlQuery<S>): SqlQuery<RowsState<S, OneRow<S["returned"]>>>;
|
|
1736
|
-
/**
|
|
1737
|
-
* Makes a query of one row, such as one of `take` or `get`, resolve to `undefined` without the row.
|
|
1738
|
-
*
|
|
1739
|
-
* Before a selection, the query resolves to `0` without the row.
|
|
1740
|
-
*/
|
|
1741
|
-
optional<S extends OneRowSqlQueryState>(this: SqlQuery<S>): SqlQuery<RowsState<S, "optional">>;
|
|
2464
|
+
/** Marks a query of `transform`, for types only, so that TypeScript rejects a query of a table as one by this member, without comparing their states. */
|
|
2465
|
+
readonly transformed: true;
|
|
2466
|
+
/** `Db` that executes the query when it is awaited; `undefined` for an unbound query. */
|
|
2467
|
+
readonly db: QueryDb<Kind>;
|
|
2468
|
+
/** Executes a bound query on its `Db`; `undefined` for an unbound query, so that it is not thenable. */
|
|
2469
|
+
readonly then: QueryThen<S, Kind>;
|
|
2470
|
+
constructor(node: QueryNode, db?: AnyDb);
|
|
2471
|
+
/** Converts the whole result further, after the earlier conversions; see `transform` of a query of a table. */
|
|
2472
|
+
transform<S extends TransformedQueryState, Kind extends SelectQueryKind, R>(this: TransformedQuery<S, Kind>, callback: (result: QueryResult<S>) => R & SynchronousResult<R>): TransformedQuery<TransformedQueryState<S["table"], R, S["uses"]>, Kind>;
|
|
1742
2473
|
/**
|
|
1743
|
-
*
|
|
2474
|
+
* Compiles the query into SQL text and parameters; `schemaName` qualifies its tables.
|
|
1744
2475
|
*
|
|
1745
|
-
* A
|
|
1746
|
-
* Before a selection, the query resolves to `0` without the row.
|
|
2476
|
+
* A query bound to a `Db` compiles as the `Db` executes it, with the PostgreSQL schema of the `Db`, and rejects options.
|
|
1747
2477
|
*/
|
|
1748
|
-
|
|
1749
|
-
/**
|
|
1750
|
-
toSQL(): SqlStatement;
|
|
1751
|
-
/** Rejects the query as an operand of an expression, which takes a query of one value of a table. */
|
|
2478
|
+
toSQL(...args: ToSQLArgs<Kind>): SqlStatement;
|
|
2479
|
+
/** Rejects the query as an operand of an expression, whose value SQL reads without its conversion. */
|
|
1752
2480
|
[operandNode](): never;
|
|
1753
|
-
/** Creates a query of the same `Db` with the node changes applied. */
|
|
1754
|
-
private derive;
|
|
1755
|
-
/** Creates the query that returns the value of the only column of each row in rows mode `rows`, decoded by `type` of `method`. */
|
|
1756
|
-
private deriveValue;
|
|
1757
|
-
/** Creates the query of one row in rows mode `rows`, rejecting a query of many rows. */
|
|
1758
|
-
private changeOneRow;
|
|
1759
2481
|
}
|
|
1760
|
-
/** Query of
|
|
1761
|
-
type
|
|
1762
|
-
/** Kind of a select query: `unbound`, or `bound` to a `Db`. */
|
|
1763
|
-
type SelectQueryKind = "unbound" | "bound";
|
|
1764
|
-
/**
|
|
1765
|
-
* `then` of a query of kind `Kind`: a bound query executes when awaited, and an unbound query is not thenable.
|
|
1766
|
-
*
|
|
1767
|
-
* Awaiting a query of either kind or returning it from an async function is a type error, since it may execute.
|
|
1768
|
-
*/
|
|
1769
|
-
type QueryThen<S extends QueryState, Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? <R1 = QueryResult<S>, R2 = never>(onFulfilled?: ((result: QueryResult<S>) => R1 | PromiseLike<R1>) | null, onRejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | null) => Promise<R1 | R2> : [Kind] extends ["unbound"] ? undefined : ((onFulfilled: never) => unknown) | undefined;
|
|
1770
|
-
/** `Db` of a query of kind `Kind`, which only a bound query has. */
|
|
1771
|
-
type QueryDb<Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? AnyDb : [Kind] extends ["unbound"] ? undefined : AnyDb | undefined;
|
|
1772
|
-
/** Arguments of `toSQL` of a query of kind `Kind`: a bound query compiles with the options of its `Db`. */
|
|
1773
|
-
type ToSQLArgs<Kind extends SelectQueryKind> = [Kind] extends ["bound"] ? [] : [options?: CompileOptions];
|
|
1774
|
-
/** Query of any class, state, and kind: a select query of a table, a query of `$select`, one that inserts, updates, or deletes rows, or a query of SQL text. */
|
|
1775
|
-
type AnyQuery = AnySelectQuery | AnyRecordQuery | AnyMutationQuery | AnySqlQuery;
|
|
2482
|
+
/** Query of `transform` of a table of any state and kind. */
|
|
2483
|
+
type AnyTransformedQuery = TransformedQuery<any, SelectQueryKind>;
|
|
1776
2484
|
/** Test fixture of `useTestFixture`, open while the tests of its `describe` block or test file run. */
|
|
1777
2485
|
interface TestFixture<T = unknown> {
|
|
1778
2486
|
/** Result of the seed of the fixture; reading it while the fixture is not open throws. */
|
|
@@ -1987,8 +2695,8 @@ interface BindOptions {
|
|
|
1987
2695
|
type Db<T extends Tables, R extends SchemaRelations = EmptyRecord> = DbEntries<T, R> & DbMethods<T, R>;
|
|
1988
2696
|
/** Bound query entry points; each access starts a query over a new table occurrence. */
|
|
1989
2697
|
type DbEntries<T extends Tables, R extends SchemaRelations> = { readonly [K in keyof T]: BoundSelectQuery<InitialQueryState<T[K], TableContext<R, K>>>; };
|
|
1990
|
-
/** Bound or unbound query over a table of `T`, of `$select`, or that changes the rows of a table of `T`, that selects records only of relations of `R`, or a query of SQL text. */
|
|
1991
|
-
type DbQuery<T extends Tables, R extends SchemaRelations> = QueryOf<DbQueryState<T[keyof T], UsableRelation<R>>> | RecordQuery<RecordQueryState<unknown, UsableRelation<R>>, SelectQueryKind> | MutationQuery<DbMutationState<T[keyof T], UsableRelation<R>>, SelectQueryKind> | SqlQuery<SqlQueryState>;
|
|
2698
|
+
/** Bound or unbound query over a table of `T`, also converted by `transform`, of `$select`, or that changes the rows of a table of `T`, that selects records only of relations of `R`, or a query of SQL text. */
|
|
2699
|
+
type DbQuery<T extends Tables, R extends SchemaRelations> = QueryOf<DbQueryState<T[keyof T], UsableRelation<R>>> | TransformedQuery<TransformedQueryState<T[keyof T], unknown, UsableRelation<R>>, SelectQueryKind> | RecordQuery<RecordQueryState<unknown, UsableRelation<R>>, SelectQueryKind> | MutationQuery<DbMutationState<T[keyof T], UsableRelation<R>>, SelectQueryKind> | SqlQuery<SqlQueryState>;
|
|
1992
2700
|
/** State of a query that a `Db` can execute: over table `T`, selecting records only of relations `U`. */
|
|
1993
2701
|
interface DbQueryState<T extends AnyTable, U extends UsedRelation> extends QueryStateOf<T> {
|
|
1994
2702
|
readonly uses: U;
|
|
@@ -2215,6 +2923,8 @@ interface AggregateNode {
|
|
|
2215
2923
|
readonly distinct: boolean;
|
|
2216
2924
|
readonly arguments: readonly ExpressionNode[];
|
|
2217
2925
|
readonly orderBy: readonly OrderNode[];
|
|
2926
|
+
/** Condition of the rows that the function takes, as `FILTER (WHERE ...)`, or `undefined` for every row. */
|
|
2927
|
+
readonly filter: ExpressionNode | undefined;
|
|
2218
2928
|
}
|
|
2219
2929
|
type ExpressionNode = ColumnRefNode | ParamNode | CastNode | BooleanNode | AndNode | OrNode | NotNode | ValuesNode | NestedQueryNode | ComparisonNode | InNode | InQueryNode | IsNullNode | RawSqlNode | TableNameNode | SubqueryNode | ExistsNode | TextSubqueryNode | AggregateNode;
|
|
2220
2930
|
/**
|
|
@@ -2251,6 +2961,13 @@ interface OrderNode {
|
|
|
2251
2961
|
readonly direction: "asc" | "desc";
|
|
2252
2962
|
readonly nulls: "first" | "last" | undefined;
|
|
2253
2963
|
}
|
|
2964
|
+
/** Lock of the rows that a select statement reads, in the words of PostgreSQL. */
|
|
2965
|
+
interface RowLockNode {
|
|
2966
|
+
/** Strength of the lock, which follows `FOR`. */
|
|
2967
|
+
readonly strength: "UPDATE" | "NO KEY UPDATE" | "SHARE" | "KEY SHARE";
|
|
2968
|
+
/** What the statement does with a row that another transaction has locked, instead of waiting for it. */
|
|
2969
|
+
readonly onLocked: "NOWAIT" | "SKIP LOCKED" | undefined;
|
|
2970
|
+
}
|
|
2254
2971
|
/** Select query; its result mode applies only when it is executed or its rows are packed. */
|
|
2255
2972
|
interface SelectNode<From extends SourceNode | undefined = SourceNode | undefined> extends ResultMode {
|
|
2256
2973
|
readonly kind: "select";
|
|
@@ -2260,13 +2977,25 @@ interface SelectNode<From extends SourceNode | undefined = SourceNode | undefine
|
|
|
2260
2977
|
/** Projections in the order of the result fields, with unique keys; a query without them returns records without fields. */
|
|
2261
2978
|
readonly projections: readonly ProjectionNode[];
|
|
2262
2979
|
readonly filters: QueryFilters;
|
|
2980
|
+
/** Keys of `GROUP BY`, at least one; a key that a projection has is compiled as the position of the projection, whichever part of lowering adds it. */
|
|
2981
|
+
readonly groupBy?: readonly ExpressionNode[] | undefined;
|
|
2982
|
+
/** Condition of `HAVING`; it requires `groupBy`, which the semantic query checks. */
|
|
2983
|
+
readonly having?: ExpressionNode | undefined;
|
|
2263
2984
|
readonly orderBy: readonly OrderNode[];
|
|
2264
2985
|
readonly limit: ExpressionNode | undefined;
|
|
2265
2986
|
readonly offset: ExpressionNode | undefined;
|
|
2987
|
+
/** Whether the query removes the duplicates of its rows, as `SELECT DISTINCT`; it requires a projection, as PostgreSQL does. */
|
|
2988
|
+
readonly distinct?: boolean | undefined;
|
|
2989
|
+
/** Keys of `DISTINCT ON`, at least one, whose equal values give one row, the first in the order; a key that a projection has is compiled as the position of the projection, as a key of `groupBy` is. */
|
|
2990
|
+
readonly distinctOn?: readonly ExpressionNode[] | undefined;
|
|
2991
|
+
/** Lock of the rows of the source of the statement, which does not reach the subqueries of its other clauses; it requires a source of rows, as PostgreSQL does. */
|
|
2992
|
+
readonly lock?: RowLockNode | undefined;
|
|
2266
2993
|
/** Required relation that guarantees the row of the query, such as `field.form`; see `ResultPlan`. */
|
|
2267
2994
|
readonly guaranteedBy: string | undefined;
|
|
2268
2995
|
/** Template of the records of a query with literals; see `ResultPlan`. */
|
|
2269
2996
|
readonly recordTemplate?: readonly RecordTemplateField[] | undefined;
|
|
2997
|
+
/** How code converts the decoded result of the query; see `ResultPlan`. */
|
|
2998
|
+
readonly conversion?: ResultConversion | undefined;
|
|
2270
2999
|
}
|
|
2271
3000
|
/** Assignment of `SET` of an update: the value of the column under `key`. */
|
|
2272
3001
|
interface AssignmentNode {
|
|
@@ -2283,6 +3012,8 @@ interface MutationParts<Field> extends ResultMode {
|
|
|
2283
3012
|
readonly returning: readonly Field[] | undefined;
|
|
2284
3013
|
/** Template of the records of a mutation with literals; see `ResultPlan`. */
|
|
2285
3014
|
readonly recordTemplate?: readonly RecordTemplateField[] | undefined;
|
|
3015
|
+
/** How code converts the decoded result of the mutation; see `ResultPlan`. */
|
|
3016
|
+
readonly conversion?: ResultConversion | undefined;
|
|
2286
3017
|
}
|
|
2287
3018
|
/** Insert statement of rows of `VALUES`. */
|
|
2288
3019
|
interface InsertNode<Field = ProjectionNode> extends MutationParts<Field> {
|
|
@@ -2355,13 +3086,13 @@ interface BaseOperators {
|
|
|
2355
3086
|
}
|
|
2356
3087
|
/** Operators of ordered types: those of `equalityOperators`, and `lt`, `lte`, `gt`, and `gte` comparing with operands of the expression type. */
|
|
2357
3088
|
declare const orderingOperators: {
|
|
3089
|
+
eq: EqualityOperator<ComparisonSignature>;
|
|
3090
|
+
ne: EqualityOperator<ComparisonSignature>;
|
|
3091
|
+
in: EqualityOperator<InSignature>;
|
|
2358
3092
|
lt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
2359
3093
|
lte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
2360
3094
|
gt: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
2361
3095
|
gte: GenericOperator<ComparisonSignature, AnyExpression<unknown, unknown>>;
|
|
2362
|
-
eq: EqualityOperator<ComparisonSignature>;
|
|
2363
|
-
ne: EqualityOperator<ComparisonSignature>;
|
|
2364
|
-
in: EqualityOperator<InSignature>;
|
|
2365
3096
|
};
|
|
2366
3097
|
/** Operators of the built-in types of numbers: those of `orderingOperators`. */
|
|
2367
3098
|
type NumberTypeOperators = MergeOperators<NoOperators, typeof orderingOperators>;
|
|
@@ -2398,140 +3129,47 @@ declare const textOperators: TextOperators;
|
|
|
2398
3129
|
declare const TRUE: Expression<boolean>;
|
|
2399
3130
|
/** Condition that never holds, such as for a branch of `or` built in code: `or(check, isAdmin ? other : FALSE)`. */
|
|
2400
3131
|
declare const FALSE: Expression<boolean>;
|
|
2401
|
-
/** First operand of `coalesce`, `greatest`, and `least`: an expression with a type, or a query of one value, such as of an aggregate, whose type decodes the result and gives it its operators. */
|
|
2402
|
-
type TypedOperand = TypedExpression | TypedScalarQuery;
|
|
2403
|
-
/** Values that the type of the first operand `F` encodes. */
|
|
2404
|
-
type TypedOperandInput<F> = F extends TypedScalarQuery ? F["types"]["valueType"]["input"] : ExpressionInput<F>;
|
|
2405
|
-
/** Operators of the type of the first operand `F`. */
|
|
2406
|
-
type TypedOperandOperators<F> = F extends TypedScalarQuery ? F["types"]["valueType"]["operators"] : F extends TypedExpression ? ExpressionOperators<F> : never;
|
|
2407
|
-
/** Operand of `coalesce`, `greatest`, and `least` after the first operand `F`: a value that its type encodes, or an expression or a query of one value of its values. */
|
|
2408
|
-
type FollowingOperand<F> = Operand<OperandValue<F>, TypedOperandInput<F>>;
|
|
2409
|
-
/** `null` when every operand can be `NULL`: the first operand `F` and the following operands `R`. */
|
|
2410
|
-
type NullOfEveryOperand<F, R extends readonly unknown[]> = null extends OperandValue<F> ? number extends R["length"] ? null : { [K in keyof R]: null extends OperandValue<R[K]> ? never : true; }[number] extends never ? null : never : never;
|
|
2411
|
-
/** Result of a function of the first operand `F` and the following operands `R` that is `NULL` only when every operand is, whose operands all have the values of `F`. */
|
|
2412
|
-
type NullSkippingResult<F, R extends readonly unknown[]> = Expression<Exclude<OperandValue<F>, null> | NullOfEveryOperand<F, R>, TypedOperandOperators<F>, TypedOperandInput<F>>;
|
|
2413
|
-
/**
|
|
2414
|
-
* Returns the first of its operands other than `NULL`, such as `coalesce(field.hint, "")`, as an expression usable wherever a column is.
|
|
2415
|
-
*
|
|
2416
|
-
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
2417
|
-
* A query without its row is `NULL` here, unlike with the method `coalesce` of a query, which keeps a missing row missing.
|
|
2418
|
-
*/
|
|
2419
|
-
declare function coalesce<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
2420
|
-
/**
|
|
2421
|
-
* Returns the largest of its operands, skipping `NULL`, which is the result only when every operand is `NULL`.
|
|
2422
|
-
*
|
|
2423
|
-
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
2424
|
-
*/
|
|
2425
|
-
declare function greatest<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
2426
|
-
/**
|
|
2427
|
-
* Returns the smallest of its operands, skipping `NULL`, which is the result only when every operand is `NULL`.
|
|
2428
|
-
*
|
|
2429
|
-
* `first` is an expression with a type or a query of one value; its type decodes the result and gives it its operators, and encodes plain values among `rest`.
|
|
2430
|
-
*/
|
|
2431
|
-
declare function least<F extends TypedOperand, const R extends readonly FollowingOperand<F>[]>(first: F, ...rest: R): NullSkippingResult<F, R>;
|
|
2432
|
-
/** Condition of a branch of `caseWhen`: a predicate, or a query of one Boolean value, such as of `exists`. */
|
|
2433
|
-
type CaseCondition = Predicate | ScalarQuery<boolean | null>;
|
|
2434
|
-
/** Value of a branch of `caseWhen` of a type of values `T` that encodes values `I`: an operand of the type, or a plain `null`. */
|
|
2435
|
-
type CaseValue<T, I> = Operand<T, I> | null;
|
|
2436
|
-
/** Branch of `caseWhen`: a condition and the value of the result when it holds. */
|
|
2437
|
-
type CaseBranch<T, I> = readonly [condition: CaseCondition, value: CaseValue<T, I>];
|
|
2438
|
-
/** Value of `caseWhen` of a type of values `T` whose branches and fallback have the values `V`, of which one gives the result. */
|
|
2439
|
-
type CaseResult<T, V> = Exclude<T, null> | Extract<OperandValue<V>, null>;
|
|
2440
|
-
/**
|
|
2441
|
-
* Returns the value of the first branch whose condition holds, as a SQL `CASE` expression, or `NULL` when none holds.
|
|
2442
|
-
*
|
|
2443
|
-
* `type` decodes the result and gives it its operators, and a plain value of a branch is cast to it, so that PostgreSQL does not take plain values for text.
|
|
2444
|
-
*/
|
|
2445
|
-
declare function caseWhen<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>, branches: NoInfer<readonly CaseBranch<Output, Input>[]>): Expression<Exclude<Output, null> | null, O, Input>;
|
|
2446
|
-
/**
|
|
2447
|
-
* Returns the value of the first branch whose condition holds, as a SQL `CASE` expression, or `otherwise` when none holds.
|
|
2448
|
-
*
|
|
2449
|
-
* `type` decodes the result and gives it its operators, and a plain value of a branch or of `otherwise` is cast to it, so that PostgreSQL does not take plain values for text.
|
|
2450
|
-
*/
|
|
2451
|
-
declare function caseWhen<Output, Input, O extends OperatorDefinitions, const B extends readonly CaseBranch<Output, Input>[], const E extends CaseValue<Output, Input>>(type: Type<Output, Input, O, any>, branches: B, otherwise: E): Expression<CaseResult<Output, B[number][1] | E>, O, Input>;
|
|
2452
|
-
/**
|
|
2453
|
-
* State of the query that the callback of a helper of table `T` with schema context `C` receives: the fields of the query the helper is applied to, and all its rows.
|
|
2454
|
-
*
|
|
2455
|
-
* It is guaranteed, so that the state that the callback returns tells whether the callback ends the guarantee of the query the helper is applied to.
|
|
2456
|
-
*/
|
|
2457
|
-
interface HelperState<T extends AnyTable, C extends SchemaContext> extends QueryStateOf<T, C> {
|
|
2458
|
-
readonly result: HelperFields<never>;
|
|
2459
|
-
readonly valueType: never;
|
|
2460
|
-
readonly rows: "many";
|
|
2461
|
-
readonly value: false;
|
|
2462
|
-
readonly uses: never;
|
|
2463
|
-
readonly guaranteed: true;
|
|
2464
|
-
readonly aggregate: false;
|
|
2465
|
-
}
|
|
2466
3132
|
/**
|
|
2467
|
-
*
|
|
3133
|
+
* Defines a reusable expression of a table, which the callbacks of queries over the table call with their columns and any further arguments, such as to filter by it and select it in one query.
|
|
2468
3134
|
*
|
|
2469
|
-
* `
|
|
3135
|
+
* `expression` returns an expression or a query of one value, and a call has its type, so that a selected field has its values and an expression has its operators.
|
|
3136
|
+
* Defined with a query of a schema or a `Db`, such as `schema.form`, it receives the queries of the relations of the table in that schema, and queries whose schema has them can call it.
|
|
3137
|
+
* Defined with a table, it receives only the columns, and every query of the table can call it, also one without relations, such as of `from`.
|
|
2470
3138
|
*/
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
readonly
|
|
2477
|
-
readonly guaranteed: boolean;
|
|
2478
|
-
}
|
|
2479
|
-
/** Effect of a helper whose callback returns a query of state `S`, for each state of a union. */
|
|
2480
|
-
type ReadEffect<S extends SelectQueryState> = S extends SelectQueryState ? {
|
|
2481
|
-
readonly fields: { [K in keyof S["result"] as K extends keyof HelperFields ? never : K]: S["result"][K]; };
|
|
2482
|
-
readonly rows: S["rows"];
|
|
2483
|
-
readonly uses: S["uses"];
|
|
2484
|
-
readonly reads: Extract<UsableRelation<S["schema"]["relations"]>, S["uses"]>;
|
|
2485
|
-
readonly key: S["schema"]["key"];
|
|
2486
|
-
readonly guaranteed: S["guaranteed"];
|
|
2487
|
-
} : never;
|
|
2488
|
-
/** Error of a helper that a query does not accept, or whose callback returns a query it does not accept. */
|
|
2489
|
-
interface HelperError<Message extends string> {
|
|
2490
|
-
readonly error: Message;
|
|
2491
|
-
}
|
|
2492
|
-
/** `unknown` when every result state retains the input marker and returns records, or the error. */
|
|
2493
|
-
type CheckResult<S extends SelectQueryState> = [S] extends [{
|
|
2494
|
-
readonly result: HelperFields;
|
|
2495
|
-
readonly value: false;
|
|
2496
|
-
}] ? unknown : HelperError<"A helper can filter, select fields, and take one row, but cannot clear the selection or use get, pluck, or aggregates">;
|
|
2497
|
-
/** State of a query over table `T` that accepts a helper of effect `E`. */
|
|
2498
|
-
type HelperInput<T extends AnyTable, E extends HelperEffect> = QueryStateOf<T, ReadsContext<E>> & ValueCheck<E>;
|
|
2499
|
-
/** Constraint of `value` of a query that accepts a helper of effect `E`. */
|
|
2500
|
-
type ValueCheck<E extends HelperEffect> = [E extends HelperEffect ? keyof E["fields"] : never] extends [never] ? [Extract<E["rows"], "nullable">] extends [never] ? unknown : RecordRows<"A helper cannot apply nullable to a query made by get, pluck, or an aggregate"> : RecordRows<"A helper cannot select fields on a query made by get, pluck, or an aggregate">;
|
|
2501
|
-
/** Constraint of a query whose rows are records, which a query of values fails with error `Message`. */
|
|
2502
|
-
interface RecordRows<Message extends string> {
|
|
2503
|
-
readonly value: false | HelperError<Message>;
|
|
3139
|
+
declare function defineExpression<T extends AnyTable, R extends AnyExpression | ScalarQuery, A extends unknown[], C extends SchemaContext = NoRelations>(table: TableOrQuery<T, C>, expression: (columns: ColumnRefs<T> & RelationRefs<C>, ...args: A) => R): (columns: ColumnRefs<T> & RelationRefs<C>, ...args: A) => R;
|
|
3140
|
+
/** Keys of the columns among references `R`, without the queries of relations. */
|
|
3141
|
+
type ColumnRefKey<R> = { [K in keyof R]: R[K] extends AnyExpression ? K : never; }[keyof R];
|
|
3142
|
+
/** Type that a key of the object of `matches` is checked against when it is not a column of its references, naming the columns. */
|
|
3143
|
+
interface NotAColumn<Keys> {
|
|
3144
|
+
readonly "keys must be columns of the references": Keys;
|
|
2504
3145
|
}
|
|
2505
|
-
/**
|
|
2506
|
-
type
|
|
2507
|
-
readonly
|
|
2508
|
-
}>
|
|
2509
|
-
readonly key: K;
|
|
2510
|
-
readonly name: N;
|
|
2511
|
-
}>["relation"]; }; }, E["key"]>;
|
|
2512
|
-
/** State of a query of state `S` after a helper of effect `E`. */
|
|
2513
|
-
type ApplyEffect<S extends SelectQueryState, E extends HelperEffect> = ChangeState<S, {
|
|
2514
|
-
result: [keyof E["fields"]] extends [never] ? S["result"] : MergeSelection<S["result"], E["fields"]>;
|
|
2515
|
-
rows: E["rows"] extends "many" ? EffectRows<S, E> : E["rows"] extends "one" ? OneRow<EffectRows<S, E>> : E["rows"];
|
|
2516
|
-
uses: S["uses"] | E["uses"];
|
|
2517
|
-
guaranteed: E["guaranteed"] extends true ? S["guaranteed"] : false;
|
|
2518
|
-
}>;
|
|
2519
|
-
/** Rows mode of a query of state `S` after a helper of effect `E` that keeps the rows, before `take`: relaxed as after a filter when the helper ends the guarantee. */
|
|
2520
|
-
type EffectRows<S extends SelectQueryState, E extends HelperEffect> = E["guaranteed"] extends true ? S["rows"] : FilteredStateRows<S>;
|
|
3146
|
+
/** Value of column `E` in the object of `matches`: an operand of its `eq`, or `null` when its input type includes it. */
|
|
3147
|
+
type MatchValue<E> = (E extends TypedExpression ? ExpressionOperators<E> extends {
|
|
3148
|
+
readonly eq: EqualityOperator<ComparisonSignature>;
|
|
3149
|
+
} ? Operand<ExpressionValue<E>, ExpressionInput<E>> : EqOperand<E> : EqOperand<E>) | (null extends ExpressionInput<E> ? null : never);
|
|
2521
3150
|
/**
|
|
2522
|
-
*
|
|
3151
|
+
* Check of object `V` of `matches` against references `R`, which gives each of its keys the value of its column.
|
|
2523
3152
|
*
|
|
2524
|
-
*
|
|
3153
|
+
* It maps the keys of the object rather than those of the references, so that a call reads the operands of only the columns it matches.
|
|
2525
3154
|
*/
|
|
2526
|
-
type
|
|
3155
|
+
type MatchCheck<R, V> = { readonly [K in keyof V]: K extends ColumnRefKey<R> ? MatchValue<R[K]> : NotAColumn<ColumnRefKey<R>>; };
|
|
3156
|
+
/** `null` that matching expression `E` with value `V` adds to the result; `never` when `V` is `null`, since `IS NULL` is never `NULL`. */
|
|
3157
|
+
type MatchNull<E, V> = V extends null ? never : E extends TypedExpression ? ExpressionOperators<E> extends {
|
|
3158
|
+
readonly eq: EqualityOperator<ComparisonSignature>;
|
|
3159
|
+
} ? null extends ExpressionValue<E> | OperandValue<V> ? null : never : CustomEqNull<E> : CustomEqNull<E>;
|
|
3160
|
+
/** `null` of the result of a custom `eq` of expression `E`. */
|
|
3161
|
+
type CustomEqNull<E> = E extends {
|
|
3162
|
+
eq: (...args: never[]) => AnyExpression<infer O>;
|
|
3163
|
+
} ? Extract<O, null> : never;
|
|
3164
|
+
/** Value of `matches` of object `V` over references `R`: `boolean`, or also `null` when a comparison of a column can be `NULL`. */
|
|
3165
|
+
type MatchResult<R, V> = boolean | (V extends unknown ? { [K in keyof V]-?: K extends keyof R ? MatchNull<R[K], V[K]> : never; }[keyof V] : never);
|
|
2527
3166
|
/**
|
|
2528
|
-
*
|
|
3167
|
+
* Creates a Boolean expression of an object of column values over the columns that a callback of a query receives, such as `matches(form, { published: true })`.
|
|
2529
3168
|
*
|
|
2530
|
-
* `
|
|
2531
|
-
*
|
|
2532
|
-
* The helper returns a query of the same kind as the one it is applied to, bound to the same `Db` if any.
|
|
3169
|
+
* Each value matches its column as in an object passed to `where`: `null` by `IS NULL`, and another value by the `eq` operator of the column; all of them must match, and an empty object is `TRUE`.
|
|
3170
|
+
* Unlike such an object in `and`, `or`, and `not`, the expression refers to the columns it receives whichever query it is used in, and applies wherever an expression does, such as in `select`.
|
|
2533
3171
|
*/
|
|
2534
|
-
declare function
|
|
3172
|
+
declare function matches<R extends object, const V extends { readonly [K in ColumnRefKey<R>]?: unknown; }>(columns: R, values: V & MatchCheck<R, V>): Expression<MatchResult<R, V>>;
|
|
2535
3173
|
/** Tagged template building a SQL fragment of values `T` that accepts compared values `I`. */
|
|
2536
3174
|
type SqlTemplate<T, O extends OperatorDefinitions, I = T> = (strings: TemplateStringsArray, ...values: unknown[]) => Expression<T, O, I>;
|
|
2537
3175
|
/**
|
|
@@ -2568,4 +3206,10 @@ declare function identifier(...names: [string, ...string[]]): UntypedExpression;
|
|
|
2568
3206
|
*/
|
|
2569
3207
|
declare function param<Output, Input, O extends OperatorDefinitions>(type: Type<Output, Input, O, any>, value: Input): Expression<Output, O, Input>;
|
|
2570
3208
|
declare function param<E extends AnyExpression, V extends (E extends AnyExpression<infer T, infer I> ? Operand<T, I> : never)>(type: E, value: V): V extends AnyExpression ? V : V extends ScalarQuery ? AnyExpression<ScalarQueryValue<V>> : E;
|
|
2571
|
-
|
|
3209
|
+
/**
|
|
3210
|
+
* Converts a column key to snake case, such as `form_id` for `formId`, for the `columnName` option of `createTableFactory`.
|
|
3211
|
+
*
|
|
3212
|
+
* A run of capital letters is one word, such as `user_id` for `userID`, and digits stay in their word, such as `line2`.
|
|
3213
|
+
*/
|
|
3214
|
+
declare function toSnakeCase(key: string): string;
|
|
3215
|
+
export { NoRelations as $, UpdateSet as $n, AnyTableKeys as $r, least as $t, Db as A, TypeOptions as Ai, some as An, CompileOptions as Ar, DecodedFields as At, PendingTestFixture as B, MergeOperators as Bi, asc as Bn, compare as Br, HelperEffect as Bt, BaseOperators as C, createStatement as Ci, UnfilteredStringAggOptions as Cn, FilteredRows as Cr, AnyRecordQuery as Ct, UntypedExpression as D, Type as Di, every as Dn, ResultByRows as Dr, AnyQuery as Dt, TypedExpression as E, TextValue as Ei, countDistinct as En, QueryResult as Er, SelectionState as Et, DatabaseOptions as F, Column as Fi, OrderKey as Fn, equalityOperators as Fr, AnyMutationQuery as Ft, createTestHelpers as G, OperatorSignature as Gi, RelationRefs as Gn, EmptyRecord as Gr, FollowingOperand as Gt, TestHelpers as H, OperatorDefinitions as Hi, orderBy as Hn, ScalarQuery as Hr, CaseBranch as Ht, TransactionOptions as I, ColumnAttributes as Ii, OrderTerm as In, BooleanResult as Ir, InitialMutationState as It, SynchronousResult as J, ResultOf as Jn, QueryState as Jr, TypedOperandInput as Jt, AnyTransformedQuery as K, defineGenericOperator as Ki, AnySelectQuery as Kn, Grouped as Kr, NullSkippingResult as Kt, createDatabase as L, KeyKind as Li, OrderTerms as Ln, ComparisonSignature as Lr, MutationQuery as Lt, DbOf as M, TypeTransform as Mi, sum as Mn, EqualityOperators as Mr, InitialSqlQueryState as Mt, DbQueryState as N, defineType as Ni, OrderColumns as Nn, HasEquality as Nr, SqlQuery as Nt, AnyDb as O, TypeInput as Oi, max as On, ValueRows as Or, SelectQueryKind as Ot, Database as P, AnyColumn as Pi, OrderDirection as Pn, InSignature as Pr, SqlQueryState as Pt, MergeRelations as Q, InsertValues as Qn, AnyTable as Qr, greatest as Qt, TransactionCoordinator as R, ToColumn as Ri, Ordering as Rn, Operand as Rr, MutationQueryState as Rt, AnyExpression as S, TransactionFailure as Si, UnfilteredAggregateOptions as Sn, ExistsValueType as Sr, defineSchema as St, ExpressionOperators as T, BaseType as Ti, count as Tn, OneValueRows as Tr, RecordQueryState as Tt, TestHooks as U, GenericOperator as Ui, ColumnRefs as Un, TypedScalarQuery as Ur, CaseCondition as Ut, TestFixture as V, NoOperators as Vi, desc as Vn, OperandUses as Vr, defineHelper as Vt, TestTransactionOptions as W, GenericOperatorImplementation as Wi, QueryContext as Wn, ValuesQuery as Wr, CaseValue as Wt, TransformedQueryState as X, from as Xn, QueryValueType as Xr, caseWhen as Xt, TransformedQuery as Y, SelectQuery as Yn, QueryStateOf as Yr, TypedOperandOperators as Yt, DeclaredRelations as Z, ColumnValue as Zn, SelectQueryState as Zr, coalesce as Zt, TextOperators as _, ResultRows as _i, AggregateOptions as _n, ConditionOperand as _r, Schema as _t, sql as a, Selectable as ai, GroupedField as an, GroupTerm as ar, RelationKeys as at, NumberTypeOperators as b, DriverResult as bi, GroupValue as bn, not as br, SchemaTables as bt, MatchResult as c, TableKeys as ci, NestedQuery as cn, NotAKey as cr, SchemaContext as ct, defineExpression as d, createTableFactory as di, SelectionFieldTypes as dn, WhereCallback as dr, TableRelations as dt, ColumnDefinitions as ei, ColumnFieldTypes as en, UpdateValues as er, OneRelationKeys as et, FALSE as f, defineTable as fi, SelectionResult as fn, WhereCondition as fr, UsableRelation as ft, TextLengthSignature as g, ResultMode as gi, AggregateExpression as gn, Condition as gr, AnySchema as gt, TextConversionSignature as h, ResultDecodeError as hi, literal as hn, CombinedCondition as hr, one as ht, param as i, KeyColumns as ii, GroupSelectionResult as in, RowLockOptions as ir, RelationKey as it, DbMutationState as j, TypeOutput as ji, stringAgg as jn, EqualityOperator as jr, FieldTypes as jt, BindOptions as k, TypeOperators as ki, min as kn, CountValueType as kr, AnySqlQuery as kt, NotAColumn as l, TableOptions as li, SelectedFieldTypes as ln, PrimaryKeyValues as lr, SchemaRelations as lt, PatternSignature as m, RequiredRelationError as mi, Literal as mn, WhereValues as mr, many as mt, SqlTemplate as n, ColumnShape as ni, ExpressionValue as nn, GroupedState as nr, RelationDeclaration as nt, ColumnRefKey as o, Table as oi, GroupedRecord as on, GroupTerms as or, RelationKind as ot, TRUE as p, NotFoundError as pi, SelectionUses as pn, WhereFilter as pr, UsedRelation as pt, NotSynchronous as q, BoundSelectQuery as qn, InitialQueryState as qr, TypedOperand as qt, identifier as r, ColumnValues as ri, GroupOperandValue as rn, SelectedOperandType as rr, RelationDeclarations as rt, MatchCheck as s, TableFactoryOptions as si, MergeSelection as sn, KeyCheck as sr, RequiredMember as st, toSnakeCase as t, ColumnOperators as ti, ExpressionInput as tn, FilteredStateRows as tr, Relation as tt, matches as u, ToColumns as ui, Selection as un, EqValue as ur, TableContext as ut, TextTypeOperators as v, Connection as vi, CountExpression as vn, Predicate as vr, SchemaPart as vt, Expression as w, AnyType as wi, avg as wn, OneRow as wr, RecordQuery as wt, orderingOperators as x, SqlStatement as xi, StringAggOptions as xn, or as xr, Tables as xt, textOperators as y, Driver as yi, GroupAggregateExpression as yn, and as yr, SchemaPartRelations as yt, OpenedTestFixture as z, BoundOperators as zi, SelectedRefs as zn, OperandValue as zr, Helper as zt };
|