kern-orm 0.6.0 → 0.7.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 → database-B7Kf5Q9X.mjs} +261 -31
- package/dist/{index-CoKpXcBp.d.mts → index-CLKi9gZL.d.mts} +239 -36
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/testing.d.mts +1 -1
- package/dist/testing.mjs +21 -3
- package/dist/{text-Cti9QLK7.mjs → text-CIUxPWPl.mjs} +1 -0
- package/dist/{types-DfoL8Oi9.mjs → types-D3d35qR2.mjs} +4 -13
- package/dist/types.d.mts +3 -8
- package/dist/types.mjs +2 -2
- package/package.json +1 -1
|
@@ -49,7 +49,7 @@ type BoundOperators<O extends OperatorDefinitions, T, I = T> = { [K in keyof O]:
|
|
|
49
49
|
/** Kind of a key of a table: its primary key, or a unique key. */
|
|
50
50
|
type KeyKind = "primaryKey" | "unique";
|
|
51
51
|
/**
|
|
52
|
-
* Type that `primaryKey()` of a type or a column
|
|
52
|
+
* Type that `primaryKey()` of a type or a column that accepts `null` is checked against, naming the reason.
|
|
53
53
|
*
|
|
54
54
|
* Without `strictNullChecks`, `null` belongs to every type, so every column looks nullable; the type then names the option instead.
|
|
55
55
|
*/
|
|
@@ -86,7 +86,7 @@ declare class Column<T extends AnyType, Key extends KeyKind | undefined = KeyKin
|
|
|
86
86
|
/** Uses a physical column name different from the column key. */
|
|
87
87
|
columnName(name: string): Column<T, Key, Default>;
|
|
88
88
|
/** Marks the column as the primary key; the type must not be nullable. */
|
|
89
|
-
primaryKey(this: null extends
|
|
89
|
+
primaryKey(this: null extends TypeInput<T> ? NullablePrimaryKey : Column<T, undefined, Default>): Column<T, "primaryKey", Default>;
|
|
90
90
|
/** Marks the column as a unique key. */
|
|
91
91
|
unique(this: Column<T, undefined, Default>): Column<T, "unique", Default>;
|
|
92
92
|
/** Marks the column as one that the database gives a value when an insert omits it, such as by `DEFAULT` or an identity, so that inserts may omit it. */
|
|
@@ -148,19 +148,26 @@ interface BaseType<Output, Input, Operators extends OperatorDefinitions, Paramet
|
|
|
148
148
|
withParameters<P extends unknown[]>(format: (...parameters: P) => string): Type<Output, Input, Operators, P>;
|
|
149
149
|
/**
|
|
150
150
|
* Creates a type with another representation of the values of this type, keeping its SQL type, operators, and nullability.
|
|
151
|
+
* An `encode` that takes `null` also makes the result nullable.
|
|
151
152
|
*
|
|
152
153
|
* `decode` receives the values that this type decodes from values other than `NULL`, including `undefined` that an earlier decoder returns, and returns a value other than `null`; `encode` returns values that this type encodes; `NULL` stays `null` without them.
|
|
153
154
|
*/
|
|
154
|
-
transform<NewOutput extends {} | undefined, NewInput>(options: TypeTransform<Exclude<Output, null>, NonNullable<Input>, NewOutput, NewInput>): Type<NewOutput |
|
|
155
|
+
transform<NewOutput extends {} | undefined, NewInput>(options: TypeTransform<Exclude<Output, null>, NonNullable<Input>, NewOutput, NewInput>): Type<NewOutput | AcceptedNull<NewInput | Input>, NewInput | AcceptedNull<Input>, Operators, Parameters>;
|
|
155
156
|
/** Creates a type that decodes the values of this type into another representation, other than `null`, and accepts the values that this type accepts. */
|
|
156
157
|
transform<NewOutput extends {} | undefined>(options: {
|
|
157
158
|
decode: (value: Exclude<Output, null>) => NewOutput;
|
|
158
|
-
}): Type<NewOutput |
|
|
159
|
+
}): Type<NewOutput | AcceptedNull<Input>, Input, Operators, Parameters>;
|
|
159
160
|
nullable(): Type<Output | null, Input | null, Operators, Parameters>;
|
|
161
|
+
/**
|
|
162
|
+
* Narrows the values of this type to `T` in TypeScript, such as to a union of strings that a `CHECK` constraint allows, or to the shape of JSON values.
|
|
163
|
+
*
|
|
164
|
+
* The values are not validated at runtime. `T` must be among the values that this type both returns and accepts, and `nullable` keeps `null` apart from it.
|
|
165
|
+
*/
|
|
166
|
+
of<T extends AcceptedValues<Exclude<Output, null>, Exclude<Input, null>>>(): Type<T | AcceptedNull<Input>, T | AcceptedNull<Input>, Operators, Parameters>;
|
|
160
167
|
/** Creates a column of this type with a physical name different from the column key. */
|
|
161
168
|
columnName(name: string): Column<Type<Output, Input, Operators, Parameters>, undefined, false>;
|
|
162
169
|
/** Creates a primary key column of this type; the type must not be nullable. */
|
|
163
|
-
primaryKey(this: null extends
|
|
170
|
+
primaryKey(this: null extends Input ? NullablePrimaryKey : Type<Output, Input, Operators, Parameters>): Column<Type<Output, Input, Operators, Parameters>, "primaryKey", false>;
|
|
164
171
|
/** Creates a column of this type that is a unique key. */
|
|
165
172
|
unique(): Column<Type<Output, Input, Operators, Parameters>, "unique", false>;
|
|
166
173
|
/** Creates a column of this type that the database gives a value when an insert omits it, such as by `DEFAULT` or an identity, so that inserts may omit it. */
|
|
@@ -171,6 +178,18 @@ interface TypeWithParameters<Output, Input, Operators extends OperatorDefinition
|
|
|
171
178
|
/** Creates this type with PostgreSQL type parameters, such as `varchar.with(255)` for `varchar(255)`; other changes of the type are kept. */
|
|
172
179
|
with(...parameters: Parameters): Type<Output, Input, Operators, Parameters>;
|
|
173
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Values that `of` can narrow a type to: those both among the values `O` that it returns and among the values `I` that it accepts, or `UnacceptedValues` when there are none.
|
|
183
|
+
*
|
|
184
|
+
* A narrowed value is also accepted, so its encoder must take it.
|
|
185
|
+
*/
|
|
186
|
+
type AcceptedValues<O, I> = [O] extends [I] ? O : [I] extends [O] ? I : [O & I] extends [never] ? UnacceptedValues : O & I;
|
|
187
|
+
/** `null` when the accepted values `I` include it, also as `unknown`; `never` otherwise. */
|
|
188
|
+
type AcceptedNull<I> = null extends I ? null : never;
|
|
189
|
+
/** Type that `AcceptedValues` names for a type that accepts none of the values it returns. */
|
|
190
|
+
interface UnacceptedValues {
|
|
191
|
+
readonly "of narrows only a type that accepts the values it returns": never;
|
|
192
|
+
}
|
|
174
193
|
/** Conversion between the values of a type and their new representation. */
|
|
175
194
|
interface TypeTransform<Output, Input, NewOutput, NewInput> {
|
|
176
195
|
decode: (value: Output) => NewOutput;
|
|
@@ -230,7 +249,11 @@ interface Driver {
|
|
|
230
249
|
executeScript(text: string): Promise<void>;
|
|
231
250
|
/** Reserves a dedicated connection, such as for a transaction. */
|
|
232
251
|
acquire(): Promise<Connection>;
|
|
233
|
-
/**
|
|
252
|
+
/**
|
|
253
|
+
* Closes the connections once the statements on the pool and the transactions on reserved connections end.
|
|
254
|
+
*
|
|
255
|
+
* From the call, it rejects a new statement or script on the pool and a new connection.
|
|
256
|
+
*/
|
|
234
257
|
close(): Promise<void>;
|
|
235
258
|
}
|
|
236
259
|
/** Connection reserved by a driver, such as for a transaction. */
|
|
@@ -385,21 +408,23 @@ interface TableKeys<PrimaryKey extends KeyColumns | undefined, UniqueKey extends
|
|
|
385
408
|
readonly [keysType]: (primaryKey: PrimaryKey, unique: UniqueKey) => readonly [PrimaryKey, UniqueKey];
|
|
386
409
|
}
|
|
387
410
|
/**
|
|
388
|
-
* Immutable table descriptor: physical name, columns, and
|
|
411
|
+
* Immutable table descriptor: physical name, columns, keys, and whether it holds at most one row.
|
|
389
412
|
*
|
|
390
413
|
* A table knows nothing about relations, schemas, or database connections, so the same table can belong to several schemas.
|
|
391
414
|
* Its object identity is the logical identity of the table.
|
|
392
415
|
*/
|
|
393
|
-
interface Table<Name extends string = string, Columns extends ColumnShape = ColumnShape, Keys extends AnyTableKeys = AnyTableKeys> {
|
|
416
|
+
interface Table<Name extends string = string, Columns extends ColumnShape = ColumnShape, Keys extends AnyTableKeys = AnyTableKeys, Singleton extends boolean = boolean> {
|
|
394
417
|
readonly kind: "table";
|
|
395
418
|
readonly name: Name;
|
|
396
419
|
readonly columns: Columns;
|
|
397
420
|
readonly keys: Keys;
|
|
421
|
+
/** Whether the table holds at most one row, as the `singleton` option of `defineTable` declares; `boolean` where the type of the table does not tell it. */
|
|
422
|
+
readonly singleton: Singleton;
|
|
398
423
|
}
|
|
399
424
|
/** Table of any name, columns, and keys. */
|
|
400
425
|
type AnyTable = Table<string, ColumnShape>;
|
|
401
426
|
/**
|
|
402
|
-
* Options of `defineTable` over column keys `Column`: the keys of several columns, whose columns are listed in their order.
|
|
427
|
+
* Options of `defineTable` over column keys `Column`: the keys of several columns, whose columns are listed in their order, and whether the table holds at most one row.
|
|
403
428
|
*
|
|
404
429
|
* The columns of the primary key are of `PrimaryKeyColumn`.
|
|
405
430
|
*/
|
|
@@ -408,6 +433,12 @@ interface TableOptions<Column extends string = string, PrimaryKeyColumn extends
|
|
|
408
433
|
readonly primaryKey?: readonly [PrimaryKeyColumn, PrimaryKeyColumn, ...PrimaryKeyColumn[]];
|
|
409
434
|
/** Unique keys of several columns; mark a unique key of one column with `unique()`. */
|
|
410
435
|
readonly unique?: readonly (readonly [Column, Column, ...Column[]])[];
|
|
436
|
+
/**
|
|
437
|
+
* Whether the table holds at most one row, such as the settings of an application, which a unique index of `(true)` must guarantee.
|
|
438
|
+
*
|
|
439
|
+
* A query of the table starts as a query of that row, which resolves to `undefined` without it, and `upsert` inserts or updates it.
|
|
440
|
+
*/
|
|
441
|
+
readonly singleton?: boolean;
|
|
411
442
|
}
|
|
412
443
|
/** Options of `createTableFactory`. */
|
|
413
444
|
interface TableFactoryOptions {
|
|
@@ -439,8 +470,8 @@ type MatchingKey$1<Key, Columns> = Key extends KeyColumns ? [Key[number]] extend
|
|
|
439
470
|
type IncludedKey<Key, Columns> = Key extends KeyColumns ? [Key[number]] extends [Columns] ? true : never : never;
|
|
440
471
|
/** Column key of columns `C` as a string, as object keys are at runtime. */
|
|
441
472
|
type ColumnKey<C extends ColumnShape> = `${keyof C & (string | number)}`;
|
|
442
|
-
/** Column key of columns `C` whose type
|
|
443
|
-
type NonNullableColumnKey<C extends ColumnShape> = { [K in keyof C]: null extends
|
|
473
|
+
/** Column key of columns `C` whose type does not accept `null`, as a string like `ColumnKey`. */
|
|
474
|
+
type NonNullableColumnKey<C extends ColumnShape> = { [K in keyof C]: null extends TypeInput<C[K]["type"]> ? never : `${K & (string | number)}`; }[keyof C];
|
|
444
475
|
/** Column key of columns `C`, as a string like `ColumnKey`, whose column forms a key of kind `Kind` alone, as `KnownKeyKind` tells. */
|
|
445
476
|
type KeyedColumn<C extends ColumnShape, Kind> = { [K in keyof C]: KnownKeyKind<C[K]["attributes"]["keyKind"]> extends Kind ? `${K & (string | number)}` : never; }[keyof C];
|
|
446
477
|
/** Kind `Kind` of the key that a column forms alone, or `"unknown"` where the type of the column does not tell it. */
|
|
@@ -462,6 +493,8 @@ type KnownKeysOf<PrimaryKey extends KeyColumns | undefined, UniqueKey extends Ke
|
|
|
462
493
|
type DefinedPrimaryKey<C extends ColumnShape, PrimaryKey> = PrimaryKey extends KeyColumns ? PrimaryKey : [KeyedColumn<C, "primaryKey">] extends [never] ? undefined : SingleColumnKey<KeyedColumn<C, "primaryKey">>;
|
|
463
494
|
/** Unique key of a table of columns `C`: `UniqueKey` of its options, or of a column marked with `unique()`. */
|
|
464
495
|
type DefinedUniqueKey<C extends ColumnShape, UniqueKey> = Extract<UniqueKey, KeyColumns> | SingleColumnKey<KeyedColumn<C, "unique">>;
|
|
496
|
+
/** Whether a table of options `O` holds at most one row: `true` or `false` of their `singleton`, `false` without it, or `boolean` where its type does not tell it. */
|
|
497
|
+
type DefinedSingleton<O> = O extends unknown ? [O["singleton" & keyof O]] extends [false | undefined] ? false : [O["singleton" & keyof O]] extends [true] ? true : boolean : never;
|
|
465
498
|
/** Key of each single column of `K`. */
|
|
466
499
|
type SingleColumnKey<K> = K extends string ? readonly [K] : never;
|
|
467
500
|
/** Primary key in options `O`: `undefined` without one, or `"unknown"` where its type does not tell it. */
|
|
@@ -479,13 +512,13 @@ type IsExactKey<K> = true extends IsUnion<K> ? false : K extends readonly unknow
|
|
|
479
512
|
*
|
|
480
513
|
* A key of one column is marked on its column.
|
|
481
514
|
*/
|
|
482
|
-
declare function defineTable<Name extends string, Definitions extends ColumnDefinitions>(name: Name, definitions: Definitions): Table<Name, ToColumns<Definitions>, DefinedKeys<ToColumns<Definitions>, undefined
|
|
515
|
+
declare function defineTable<Name extends string, Definitions extends ColumnDefinitions>(name: Name, definitions: Definitions): Table<Name, ToColumns<Definitions>, DefinedKeys<ToColumns<Definitions>, undefined>, false>;
|
|
483
516
|
/**
|
|
484
517
|
* Defines a table; a type without column attributes becomes a column as it is.
|
|
485
518
|
*
|
|
486
|
-
* A key of one column is marked on its column, and `options` list the columns of the keys of several columns.
|
|
519
|
+
* A key of one column is marked on its column, and `options` list the columns of the keys of several columns and tell whether the table holds at most one row.
|
|
487
520
|
*/
|
|
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>>;
|
|
521
|
+
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>, DefinedSingleton<Options>>;
|
|
489
522
|
/**
|
|
490
523
|
* Creates a table-definition function with the given naming convention, for an application to define its tables with.
|
|
491
524
|
*
|
|
@@ -556,6 +589,23 @@ interface InitialQueryState<T extends AnyTable, C extends SchemaContext = NoRela
|
|
|
556
589
|
readonly guaranteed: Guaranteed;
|
|
557
590
|
readonly aggregate: false;
|
|
558
591
|
}
|
|
592
|
+
/**
|
|
593
|
+
* State of a query that starts from table `T` with schema context `C`, as the entry points of a schema and a `Db` and `from` start it: of all the rows of the table, or of the one row of a singleton table.
|
|
594
|
+
*
|
|
595
|
+
* Where the type of `T` does not tell whether it is a singleton, such as `Table<Name, Columns>`, it is the union of both.
|
|
596
|
+
*/
|
|
597
|
+
type TableQueryState<T extends AnyTable, C extends SchemaContext = NoRelations> = SingletonOrInitialState<T, C, T["singleton"]>;
|
|
598
|
+
/** State of a query that starts from table `T` with schema context `C`, as `TableQueryState` gives it, by `Singleton`, the `singleton` of the type of `T`. */
|
|
599
|
+
type SingletonOrInitialState<T extends AnyTable, C extends SchemaContext, Singleton> = Singleton extends true ? SingletonQueryState<T, C> : InitialQueryState<T, C>;
|
|
600
|
+
/** State of a query that starts from singleton table `T` with schema context `C`: its one row as a record without fields, which resolves to `undefined` without it. */
|
|
601
|
+
interface SingletonQueryState<T extends AnyTable, C extends SchemaContext = NoRelations> extends InitialQueryState<T, C, never, "optional">, FoundKey<never> {}
|
|
602
|
+
/** Key of the member of `FoundKey` that makes it invariant in the columns of the key. */
|
|
603
|
+
declare const foundKeyVariance: unique symbol;
|
|
604
|
+
/** Part of the state of a query of `find` or `findBy` with the columns `K` of its key, or of a query of a singleton table without them, which `upsert` applies to. */
|
|
605
|
+
interface FoundKey<K extends string> {
|
|
606
|
+
readonly foundKey: K;
|
|
607
|
+
readonly [foundKeyVariance]: { readonly [C in K]: true; };
|
|
608
|
+
}
|
|
559
609
|
/** Mark of `aggregate: false` of a query of groups, which aggregate methods replace. */
|
|
560
610
|
interface Grouped {
|
|
561
611
|
readonly grouped: true;
|
|
@@ -705,6 +755,12 @@ interface QueryNode extends ResultMode {
|
|
|
705
755
|
readonly all: boolean;
|
|
706
756
|
/** How `map` and `transform` convert the decoded result. */
|
|
707
757
|
readonly conversion: ResultConversion | undefined;
|
|
758
|
+
/**
|
|
759
|
+
* Values of the columns of the key of `find` or `findBy` of a query of a table without clauses, which `upsert` inserts, an empty object for a query that starts from a singleton table, whose key has no columns, or `undefined` for any other query.
|
|
760
|
+
*
|
|
761
|
+
* Every method that derives a node from the query drops them, whereas `use` returns the query of its callback, such as the query itself.
|
|
762
|
+
*/
|
|
763
|
+
readonly foundKey: Readonly<Record<string, unknown>> | undefined;
|
|
708
764
|
}
|
|
709
765
|
/**
|
|
710
766
|
* 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.
|
|
@@ -816,13 +872,15 @@ type QueryWhereFilter<S extends SelectQueryState> = [S] extends [unknown] ? Wher
|
|
|
816
872
|
* A union of tables gives the union of their values.
|
|
817
873
|
*/
|
|
818
874
|
type PrimaryKeyValues<T extends AnyTable> = T extends AnyTable ? T["keys"] extends TableKeys<infer PrimaryKey extends KeyColumns, infer _UniqueKey> ? { readonly [I in keyof PrimaryKey]: NonNullable<EqValue<T, PrimaryKey[I] & keyof T["columns"]>>; } : never : never;
|
|
875
|
+
/** Columns of the primary key of table `T`, or `never` where the type of `T` tells no primary key. */
|
|
876
|
+
type PrimaryKeyColumn<T extends AnyTable> = T extends AnyTable ? T["keys"] extends TableKeys<infer PrimaryKey extends KeyColumns, infer _UniqueKey> ? PrimaryKey[number] : never : never;
|
|
819
877
|
/**
|
|
820
|
-
* Check of columns `Columns
|
|
878
|
+
* Check of columns `Columns`, of the object of `findBy` or of `onConflict`, that rejects them unless they are exactly a key of table `T`, primary or unique, as the type of `T` tells.
|
|
821
879
|
*
|
|
822
880
|
* A table of `any`, such as of `AnySelectQuery`, takes any columns, as `where` takes any values there.
|
|
823
881
|
*/
|
|
824
882
|
type KeyCheck<T extends AnyTable, Columns> = [IsKey<T, Columns>] extends [true] ? unknown : IsAny<T> extends true ? unknown : NotAKey<KeyOf<T["keys"]>>;
|
|
825
|
-
/** Type that the
|
|
883
|
+
/** Type that the columns of `findBy` and `onConflict` are checked against when they are not exactly a key of the table, naming the keys of the table. */
|
|
826
884
|
interface NotAKey<Keys> {
|
|
827
885
|
readonly "columns must be exactly one key of the table": Keys;
|
|
828
886
|
}
|
|
@@ -869,11 +927,23 @@ type FilteredState<S extends SelectQueryState> = ChangeState<S, {
|
|
|
869
927
|
rows: FilteredStateRows<S>;
|
|
870
928
|
guaranteed: false;
|
|
871
929
|
}>;
|
|
872
|
-
/** State `S` after a filter followed by `take`, as `find` and `findBy` change it
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
930
|
+
/** State `S` after a filter followed by `take`, as `find` and `findBy` change it, with the columns `K` of their key for `upsert`. */
|
|
931
|
+
interface FoundState<S extends SelectQueryState, K extends string> extends SelectQueryState, FoundKey<K> {
|
|
932
|
+
readonly table: S["table"];
|
|
933
|
+
readonly schema: S["schema"];
|
|
934
|
+
readonly result: S["result"];
|
|
935
|
+
readonly rows: OneRow<FilteredStateRows<S>>;
|
|
936
|
+
readonly value: S["value"];
|
|
937
|
+
readonly valueType: S["valueType"];
|
|
938
|
+
readonly fieldTypes: S["fieldTypes"];
|
|
939
|
+
readonly uses: S["uses"];
|
|
940
|
+
readonly guaranteed: false;
|
|
941
|
+
readonly aggregate: S["aggregate"];
|
|
942
|
+
}
|
|
943
|
+
/** State of a query of `find` or `findBy`, or of a singleton table, which `upsert` applies to. */
|
|
944
|
+
type FoundQueryState = SelectQueryState & {
|
|
945
|
+
readonly foundKey: string;
|
|
946
|
+
};
|
|
877
947
|
/** State of a query whose rows are records, which `select` and `clearSelect` change. */
|
|
878
948
|
type RecordQueryState$1 = SelectQueryState & {
|
|
879
949
|
readonly value: false;
|
|
@@ -988,10 +1058,14 @@ type ColumnValue<T extends AnyTable, K extends keyof T["columns"]> = ColumnValue
|
|
|
988
1058
|
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
1059
|
/** 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
1060
|
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; };
|
|
1061
|
+
/** Values of `upsert` into table `T`: a record of an insert without the columns `K` of the key, whose values `find` or `findBy` gives. */
|
|
1062
|
+
type UpsertValues<T extends AnyTable, K extends string> = { readonly [C in keyof T["columns"] as C extends OptionalColumnKey<T> | K ? never : C]: ColumnValue<T, C>; } & { readonly [C in Exclude<OptionalColumnKey<T>, K>]?: ColumnValue<T, C> | undefined; };
|
|
991
1063
|
/** Values of the columns of table `T` that an update sets; a column omitted or given as `undefined` keeps its value. */
|
|
992
1064
|
type UpdateValues<T extends AnyTable> = { readonly [K in keyof T["columns"]]?: ColumnValue<T, K> | undefined; };
|
|
993
1065
|
/** 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
1066
|
type UpdateSet<T extends AnyTable, C extends SchemaContext = NoRelations> = UpdateValues<T> | ((columns: ColumnRefs<T> & RelationRefs<C>) => UpdateValues<T>);
|
|
1067
|
+
/** Argument of `update` of a conflict of an insert into table `T` with schema context `C`: the values that it sets on the existing record, or a callback that receives the columns and the queries of the relations of the existing record and the columns of the row proposed for insertion, and returns them. */
|
|
1068
|
+
type ConflictUpdateSet<T extends AnyTable, C extends SchemaContext = NoRelations> = UpdateValues<T> | ((columns: ColumnRefs<T> & RelationRefs<C>, excluded: ColumnRefs<T>) => UpdateValues<T>);
|
|
995
1069
|
/** State `S` with the parts of `Changes` replaced. */
|
|
996
1070
|
type ChangeState<S extends SelectQueryState, Changes extends Partial<SelectQueryState>> = { readonly [K in keyof SelectQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
997
1071
|
/** Query of state `S`, bound or unbound. */
|
|
@@ -1067,15 +1141,17 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1067
1141
|
*
|
|
1068
1142
|
* A value is what `where` takes for its column, such as an expression, but never `null` or `undefined`.
|
|
1069
1143
|
* Without a row, it rejects with `NotFoundError` unless `optional` or `nullable` makes the query resolve to `undefined` or `null`.
|
|
1144
|
+
* To insert the record of the key or update the existing one, call `upsert` right after it.
|
|
1070
1145
|
*/
|
|
1071
|
-
find<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, ...values: PrimaryKeyValues<S["table"]>): SelectQuery<FoundState<S
|
|
1146
|
+
find<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, ...values: PrimaryKeyValues<S["table"]>): SelectQuery<FoundState<S, PrimaryKeyColumn<S["table"]>>, Kind>;
|
|
1072
1147
|
/**
|
|
1073
1148
|
* Filters the query by the values of the columns of one key of the table, primary or unique, and returns the row instead of an array, as `where` followed by `take`, but without a limit.
|
|
1074
1149
|
*
|
|
1075
1150
|
* A value is what `where` takes for its column, such as an expression, but never `null` or `undefined`.
|
|
1076
1151
|
* Without a row, it rejects with `NotFoundError` unless `optional` or `nullable` makes the query resolve to `undefined` or `null`.
|
|
1152
|
+
* To insert the record of the key or update the existing one, call `upsert` right after it.
|
|
1077
1153
|
*/
|
|
1078
|
-
findBy<S extends SelectQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, values: { readonly [C in K]: NonNullable<EqValue<S["table"], C>>; } & KeyCheck<S["table"], K>): SelectQuery<FoundState<S>, Kind>;
|
|
1154
|
+
findBy<S extends SelectQueryState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string>(this: SelectQuery<S, Kind>, values: { readonly [C in K]: NonNullable<EqValue<S["table"], C>>; } & KeyCheck<S["table"], K>): SelectQuery<FoundState<S, K>, Kind>;
|
|
1079
1155
|
/**
|
|
1080
1156
|
* Replaces the selected fields with a column, limits the query to one row, and returns the value of the column instead of a record.
|
|
1081
1157
|
*
|
|
@@ -1369,22 +1445,34 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1369
1445
|
* A column omitted or given as `undefined` gets its default, so only columns marked with `hasDefault` and nullable columns may be omitted.
|
|
1370
1446
|
* 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.
|
|
1371
1447
|
* Select the fields that it returns after it, such as `insert(values).get("id")`.
|
|
1448
|
+
* To handle a conflict of the record with an existing one on a key, call `onConflict` right after `insert`.
|
|
1372
1449
|
*/
|
|
1373
|
-
insert<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, values: InsertValues<S["table"]>): MutationQuery<
|
|
1450
|
+
insert<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, values: InsertValues<S["table"]>): MutationQuery<InitialInsertState<S["table"], S["schema"], "one">, Kind>;
|
|
1374
1451
|
/**
|
|
1375
1452
|
* Inserts records into the table of a query without clauses, such as a table of a `Db`, in one statement, and resolves to the number of the inserted rows.
|
|
1376
1453
|
*
|
|
1377
1454
|
* A record takes the values that `insert` takes, and a column that a record omits gets its default.
|
|
1378
1455
|
* 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.
|
|
1456
|
+
* To handle a conflict of a record with an existing one on a key, call `onConflict` right after `insertMany`.
|
|
1457
|
+
* A singleton table, which holds one row, takes `insert` or `upsert` instead.
|
|
1379
1458
|
*/
|
|
1380
|
-
insertMany<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, records: readonly InsertValues<S["table"]>[]): MutationQuery<
|
|
1459
|
+
insertMany<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, records: readonly InsertValues<S["table"]>[] & InsertManyCheck<S["table"]>): MutationQuery<InitialInsertState<S["table"], S["schema"], "many">, Kind>;
|
|
1460
|
+
/**
|
|
1461
|
+
* Inserts the record of the key of `find` or `findBy` with `values`, or updates `values` of the existing record of the key, in one statement, and resolves to the number of the inserted or updated rows, `1`.
|
|
1462
|
+
*
|
|
1463
|
+
* `values` takes what `insert` takes, apart from the columns of the key, and at least one value other than `undefined`; a column omitted or given as `undefined` gets its default when the record is inserted and keeps its value when the record is updated.
|
|
1464
|
+
* Call it right after `find` or `findBy` of a query of a table without other clauses, such as filters, or on a query of a singleton table without clauses, such as `db.settings.upsert(values)`, to insert or update its row.
|
|
1465
|
+
* Select the fields that it returns after it, such as `upsert(values).get("id")`.
|
|
1466
|
+
* To update the existing record with other values than the inserted ones, such as to increment a counter, call `onConflict` after `insert` instead.
|
|
1467
|
+
*/
|
|
1468
|
+
upsert<S extends FoundQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>, values: UpsertValues<S["table"], S["foundKey"]>): MutationQuery<InitialMutationState<S["table"], S["schema"], "one">, Kind>;
|
|
1381
1469
|
/**
|
|
1382
1470
|
* 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
1471
|
*
|
|
1384
1472
|
* `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
1473
|
* A value is what `insert` takes as a value, such as a query of an aggregate of related records.
|
|
1386
1474
|
* 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.
|
|
1475
|
+
* A query without a filter must call `all` to update every row, unless it is a query of a singleton table, which updates its row.
|
|
1388
1476
|
* Select the fields that it returns after it, with their values after the update, such as `update(callback).select("id")`.
|
|
1389
1477
|
*/
|
|
1390
1478
|
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>;
|
|
@@ -1394,7 +1482,7 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1394
1482
|
* `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
1483
|
* A value is what `insert` takes as a value.
|
|
1396
1484
|
* A query of `find` or `findBy` rejects with `NotFoundError` without its row, unless `optional` or `nullable` allows it.
|
|
1397
|
-
* A query without a filter must call `all` to update every row.
|
|
1485
|
+
* A query without a filter must call `all` to update every row, unless it is a query of a singleton table, which updates its row.
|
|
1398
1486
|
* Select the fields that it returns after it, with their values after the update, such as `update(set).select("id")`.
|
|
1399
1487
|
*/
|
|
1400
1488
|
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>;
|
|
@@ -1402,7 +1490,7 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1402
1490
|
* Deletes the rows that the filters of the query choose, and resolves to the number of the deleted rows.
|
|
1403
1491
|
*
|
|
1404
1492
|
* A query of `find` or `findBy` rejects with `NotFoundError` without its row, unless `optional` or `nullable` allows it.
|
|
1405
|
-
* A query without a filter must call `all` to delete every row.
|
|
1493
|
+
* A query without a filter must call `all` to delete every row, unless it is a query of a singleton table, which deletes its row.
|
|
1406
1494
|
* Select the fields that it returns after it, such as `delete().pluck("id")`.
|
|
1407
1495
|
*/
|
|
1408
1496
|
delete<S extends SelectQueryState, Kind extends SelectQueryKind>(this: SelectQuery<S, Kind>): MutationQuery<InitialMutationState<S["table"], S["schema"], S["rows"]>, Kind>;
|
|
@@ -1452,7 +1540,7 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1452
1540
|
* It rejects a query of records, which `ValuesQuery` rejects in types.
|
|
1453
1541
|
*/
|
|
1454
1542
|
[valuesQueryNode](): NestedQueryNode;
|
|
1455
|
-
/** Creates a query of the same kind and `Db` with the node changes applied. */
|
|
1543
|
+
/** Creates a query of the same kind and `Db` with the node changes applied, dropping the key of `find` and `findBy` unless the changes give it. */
|
|
1456
1544
|
private derive;
|
|
1457
1545
|
/**
|
|
1458
1546
|
* Creates a query of the same kind and `Db` with the node changes applied and then those of `take`, in one derived query.
|
|
@@ -1472,7 +1560,11 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1472
1560
|
private addFilter;
|
|
1473
1561
|
/** Returns the rows mode of this query after a filter, as `FilteredStateRows` gives it in types. */
|
|
1474
1562
|
private getFilteredRows;
|
|
1475
|
-
/**
|
|
1563
|
+
/**
|
|
1564
|
+
* Creates the query of `find` and `findBy`: `where` of `values`, an object of the values of the columns of a key, returning one row like `take`.
|
|
1565
|
+
*
|
|
1566
|
+
* On a query of a table without clauses, it keeps `values` as the key that `upsert` inserts.
|
|
1567
|
+
*/
|
|
1476
1568
|
private takeWhere;
|
|
1477
1569
|
/** Creates the query of one row with rows mode `rows` that `method` returns, rejecting a query of many rows. */
|
|
1478
1570
|
private changeOneRow;
|
|
@@ -1485,6 +1577,12 @@ declare class SelectQuery<out S extends SelectQueryState, out Kind extends Selec
|
|
|
1485
1577
|
/** Rejects `method`, an aggregate method or `exists`, on a query made by one of them or with a row lock. */
|
|
1486
1578
|
private rejectAggregation;
|
|
1487
1579
|
}
|
|
1580
|
+
/** Check of the records of `insertMany` into table `T` that rejects them for a singleton table, which holds one row. */
|
|
1581
|
+
type InsertManyCheck<T extends AnyTable> = T["singleton"] extends true ? SingletonInsertMany : unknown;
|
|
1582
|
+
/** Type that the records of `insertMany` into a singleton table are checked against, naming the reason. */
|
|
1583
|
+
interface SingletonInsertMany {
|
|
1584
|
+
readonly "insertMany cannot insert into a singleton table, which holds one row; use insert or upsert": never;
|
|
1585
|
+
}
|
|
1488
1586
|
/** Select query bound to a `Db`, executed by awaiting it; every method returns a query bound to the same `Db`. */
|
|
1489
1587
|
type BoundSelectQuery<S extends SelectQueryState> = SelectQuery<S, "bound">;
|
|
1490
1588
|
/** Select query of any state and kind. */
|
|
@@ -1494,7 +1592,11 @@ type ResultOf<Q extends AnyQuery> = Q["types"]["result"];
|
|
|
1494
1592
|
/** Table of a definition such as `defineExpression`: the table itself or a query over it with schema context `C`, such as `schema.form`. */
|
|
1495
1593
|
type TableOrQuery<T extends AnyTable, C extends SchemaContext = SchemaContext> = T | QueryOf<QueryStateOf<T, C>>;
|
|
1496
1594
|
/** Starts a query over a new occurrence of the table. */
|
|
1497
|
-
declare function from<T extends
|
|
1595
|
+
declare function from<T extends Table<string, ColumnShape, AnyTableKeys, false>>(table: T): SelectQuery<InitialQueryState<T>>;
|
|
1596
|
+
/** Starts a query of the one row of a singleton table over a new occurrence of the table. */
|
|
1597
|
+
declare function from<T extends Table<string, ColumnShape, AnyTableKeys, true>>(table: T): SelectQuery<SingletonQueryState<T>>;
|
|
1598
|
+
/** Starts a query over a new occurrence of a table whose type does not tell whether it is a singleton table, of all its rows or of its one row. */
|
|
1599
|
+
declare function from<T extends AnyTable>(table: T): SelectQuery<InitialQueryState<T> | SingletonQueryState<T>>;
|
|
1498
1600
|
/** References to the columns of the query source, passed to query callbacks. */
|
|
1499
1601
|
type ColumnRefs<T extends AnyTable> = { readonly [K in keyof T["columns"]]: Expression<Selectable<T>[K], ColumnOperators<T, K>, ColumnValues<T>[K]>; };
|
|
1500
1602
|
/**
|
|
@@ -1882,6 +1984,16 @@ interface InitialMutationState<T extends AnyTable, C extends SchemaContext, Rows
|
|
|
1882
1984
|
readonly value: true;
|
|
1883
1985
|
readonly uses: U;
|
|
1884
1986
|
}
|
|
1987
|
+
/** State of an insert of `insert` or `insertMany` of table `T` with schema context `C` in rows mode `Rows`, before any other method. */
|
|
1988
|
+
interface InitialInsertState<T extends AnyTable, C extends SchemaContext, Rows extends ResultRows> extends InitialMutationState<T, C, Rows> {
|
|
1989
|
+
readonly insert: true;
|
|
1990
|
+
}
|
|
1991
|
+
/** State of an insert that no method has changed since `insert` or `insertMany`, which `onConflict` applies to. */
|
|
1992
|
+
interface InsertMutationState extends MutationQueryState {
|
|
1993
|
+
readonly insert: true;
|
|
1994
|
+
}
|
|
1995
|
+
/** Rows mode of the rows that an insert of rows mode `Rows` changes when it skips the rows that conflict: a missing row of an insert of one record. */
|
|
1996
|
+
type SkippedRows<Rows extends ResultRows> = Rows extends "one" ? "optional" : Rows;
|
|
1885
1997
|
/** State `S` of a mutation with the parts of `Changes` replaced. */
|
|
1886
1998
|
type ChangeMutationState<S extends MutationQueryState, Changes extends Partial<MutationQueryState>> = { readonly [K in keyof MutationQueryState]: K extends keyof Changes ? Changes[K] : S[K]; } & {};
|
|
1887
1999
|
/** State of a mutation that returns the number of the rows it changes or their records, which `select` applies to. */
|
|
@@ -2034,6 +2146,12 @@ declare class MutationQuery<out S extends MutationQueryState, out Kind extends S
|
|
|
2034
2146
|
* A selection of no fields fails as well on a mutation that returns the number of its rows.
|
|
2035
2147
|
*/
|
|
2036
2148
|
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;
|
|
2149
|
+
/**
|
|
2150
|
+
* Chooses the key, primary or unique, of `columns` on which a row of the insert may conflict with an existing record; without columns, `doNothing` skips a row that conflicts on any key, and `merge` and `update` update the row of a singleton table.
|
|
2151
|
+
*
|
|
2152
|
+
* Call it right after `insert` or `insertMany`, choose the action on its result, such as `doNothing()`, and select the fields that the mutation returns after the action.
|
|
2153
|
+
*/
|
|
2154
|
+
onConflict<S extends InsertMutationState, Kind extends SelectQueryKind, K extends keyof S["table"]["columns"] & string = never>(this: MutationQuery<S, Kind>, ...columns: (K & KeyCheck<S["table"], K>)[]): ConflictTarget<S, Kind, K>;
|
|
2037
2155
|
/**
|
|
2038
2156
|
* Compiles the query into SQL text and parameters; `schemaName` qualifies its tables.
|
|
2039
2157
|
*
|
|
@@ -2051,6 +2169,74 @@ declare class MutationQuery<out S extends MutationQueryState, out Kind extends S
|
|
|
2051
2169
|
}
|
|
2052
2170
|
/** Query of any state and kind that inserts, updates, or deletes rows. */
|
|
2053
2171
|
type AnyMutationQuery = MutationQuery<any, SelectQueryKind>;
|
|
2172
|
+
/**
|
|
2173
|
+
* Conflict target of an insert of state `S` on the key of columns `K`, or without columns for `never`, with methods that choose the action and return the mutation: without columns, `doNothing` takes any key, and `merge` and `update` the row of a singleton table.
|
|
2174
|
+
*
|
|
2175
|
+
* It is no query: awaiting it is a type error and rejects, so that an insert without an action does not resolve without running.
|
|
2176
|
+
*/
|
|
2177
|
+
declare class ConflictTarget<out S extends InsertMutationState, out Kind extends SelectQueryKind = "unbound", out K extends string = string> {
|
|
2178
|
+
/** Insert whose rows conflict. */
|
|
2179
|
+
readonly insert: MutationQuery<S, Kind>;
|
|
2180
|
+
/** Columns of the key that the rows conflict on, in its order, or none, as `onConflict` without columns gives them. */
|
|
2181
|
+
readonly target: readonly K[];
|
|
2182
|
+
constructor(
|
|
2183
|
+
/** Insert whose rows conflict. */
|
|
2184
|
+
insert: MutationQuery<S, Kind>,
|
|
2185
|
+
/** Columns of the key that the rows conflict on, in its order, or none, as `onConflict` without columns gives them. */
|
|
2186
|
+
target: readonly K[]);
|
|
2187
|
+
/**
|
|
2188
|
+
* Skips each row that conflicts, as `ON CONFLICT DO NOTHING`, and returns the mutation.
|
|
2189
|
+
*
|
|
2190
|
+
* After `insert`, the mutation resolves to `0` when the row conflicts, and the fields selected after it to `undefined`.
|
|
2191
|
+
* After `insertMany`, it resolves to the number of the inserted rows, and the fields selected after it are those of the inserted rows.
|
|
2192
|
+
*/
|
|
2193
|
+
doNothing<S extends InsertMutationState, Kind extends SelectQueryKind>(this: ConflictTarget<S, Kind>): MutationQuery<ChangeMutationState<S, {
|
|
2194
|
+
changed: SkippedRows<S["changed"]>;
|
|
2195
|
+
}>, Kind>;
|
|
2196
|
+
/**
|
|
2197
|
+
* Updates the existing record of each row that conflicts with the values of `columns` of the row, as `DO UPDATE SET column = EXCLUDED.column`, or with every column that the insert gives, apart from the key, without columns, and returns the mutation of the inserted and the updated rows.
|
|
2198
|
+
*
|
|
2199
|
+
* Every record must give each column that it sets.
|
|
2200
|
+
* After `onConflict()` without columns, it updates the row of a singleton table.
|
|
2201
|
+
*/
|
|
2202
|
+
merge<S extends InsertMutationState, Kind extends SelectQueryKind, K extends string, C extends Exclude<keyof S["table"]["columns"] & string, K>>(this: ConflictTarget<S, Kind, K> & ConflictKeyCheck<S, K>, ...columns: C[]): MutationQuery<ChangeMutationState<S, {
|
|
2203
|
+
changed: S["changed"];
|
|
2204
|
+
}>, Kind>;
|
|
2205
|
+
/**
|
|
2206
|
+
* Updates the existing record of each row that conflicts with the values that `callback` returns, as `DO UPDATE SET`, and returns the mutation of the inserted and the updated rows.
|
|
2207
|
+
*
|
|
2208
|
+
* `callback` receives the columns and the queries of the relations of the existing record, and the columns of the row proposed for insertion, `excluded`, such as to add its value to the existing one.
|
|
2209
|
+
* After `onConflict()` without columns, it updates the row of a singleton table.
|
|
2210
|
+
*/
|
|
2211
|
+
update<S extends InsertMutationState, Kind extends SelectQueryKind, K extends string, F extends (columns: ColumnRefs<S["table"]> & RelationRefs<S["schema"]>, excluded: ColumnRefs<S["table"]>) => UpdateValues<S["table"]>>(this: ConflictTarget<S, Kind, K> & ConflictKeyCheck<S, K>, callback: F): MutationQuery<ChangeMutationState<S, {
|
|
2212
|
+
uses: S["uses"] | OperandUses<ReturnType<F>[keyof ReturnType<F>]>;
|
|
2213
|
+
}>, Kind>;
|
|
2214
|
+
/**
|
|
2215
|
+
* Updates the existing record of each row that conflicts with the values of `set`, as `DO UPDATE SET`, and returns the mutation of the inserted and the updated rows.
|
|
2216
|
+
*
|
|
2217
|
+
* `set` gives the values of the columns to update, or is a callback that returns them from the existing record and the row proposed for insertion, `excluded`.
|
|
2218
|
+
* After `onConflict()` without columns, it updates the row of a singleton table.
|
|
2219
|
+
*/
|
|
2220
|
+
update<S extends InsertMutationState, Kind extends SelectQueryKind, K extends string>(this: ConflictTarget<S, Kind, K> & ConflictKeyCheck<S, K>, set: ConflictUpdateSet<S["table"], S["schema"]>): MutationQuery<ChangeMutationState<S, {
|
|
2221
|
+
changed: S["changed"];
|
|
2222
|
+
}>, Kind>;
|
|
2223
|
+
/** Rejects awaiting the conflict target, which needs an action, such as `doNothing()`. */
|
|
2224
|
+
then(_action: ConflictActionRequired): never;
|
|
2225
|
+
/** Returns the node of the insert, which `onConflict` takes only of an insert. */
|
|
2226
|
+
private insertNode;
|
|
2227
|
+
/** Creates the insert of the same kind and `Db` with `conflict`, changing rows `rows`. */
|
|
2228
|
+
private derive;
|
|
2229
|
+
}
|
|
2230
|
+
/** Type of the parameter of `then` of a conflict target, which makes awaiting the target before an action a type error. */
|
|
2231
|
+
interface ConflictActionRequired {
|
|
2232
|
+
readonly "onConflict needs an action, such as doNothing()": never;
|
|
2233
|
+
}
|
|
2234
|
+
/** Check of the columns `K` of a conflict target of an insert of state `S` that rejects `merge` and `update` of a conflict on any key, which PostgreSQL does not update, unless the table is a singleton table, whose row they update. */
|
|
2235
|
+
type ConflictKeyCheck<S extends InsertMutationState, K extends string> = [K] extends [never] ? S["table"]["singleton"] extends true ? unknown : ConflictKeyRequired : unknown;
|
|
2236
|
+
/** Type that a conflict target on any key is checked against by `merge` and `update`, naming the reason. */
|
|
2237
|
+
interface ConflictKeyRequired {
|
|
2238
|
+
readonly "merge and update need the columns of a key in onConflict": never;
|
|
2239
|
+
}
|
|
2054
2240
|
/** Types of the fields of the rows of a query of SQL text by the names of the result columns they decode. */
|
|
2055
2241
|
interface FieldTypes {
|
|
2056
2242
|
readonly [key: string]: AnyType;
|
|
@@ -2239,8 +2425,8 @@ type Tables = Record<string, AnyTable>;
|
|
|
2239
2425
|
* A `Db` can execute such a query if its schema contains the tables and the selected relations of the query and every bound nested query belongs to that `Db`.
|
|
2240
2426
|
*/
|
|
2241
2427
|
type Schema<T extends Tables, R extends SchemaRelations = EmptyRecord> = SchemaEntries<T, R> & SchemaMembers<T, R>;
|
|
2242
|
-
/** Query entry points; each access starts a query over a new table occurrence. */
|
|
2243
|
-
type SchemaEntries<T extends Tables, R extends SchemaRelations> = { readonly [K in keyof T]: SelectQuery<
|
|
2428
|
+
/** Query entry points; each access starts a query over a new table occurrence, of the one row of a singleton table. */
|
|
2429
|
+
type SchemaEntries<T extends Tables, R extends SchemaRelations> = { readonly [K in keyof T]: SelectQuery<TableQueryState<T[K], TableContext<R, K>>>; };
|
|
2244
2430
|
/** Members of a schema that composition reads: its tables and the relations of its tables. */
|
|
2245
2431
|
interface SchemaContents<T extends Tables, R extends SchemaRelations> {
|
|
2246
2432
|
readonly [schemaBrand]: true;
|
|
@@ -2693,8 +2879,8 @@ interface BindOptions {
|
|
|
2693
2879
|
* Its table entries start queries bound to it, which execute when awaited.
|
|
2694
2880
|
*/
|
|
2695
2881
|
type Db<T extends Tables, R extends SchemaRelations = EmptyRecord> = DbEntries<T, R> & DbMethods<T, R>;
|
|
2696
|
-
/** Bound query entry points; each access starts a query over a new table occurrence. */
|
|
2697
|
-
type DbEntries<T extends Tables, R extends SchemaRelations> = { readonly [K in keyof T]: BoundSelectQuery<
|
|
2882
|
+
/** Bound query entry points; each access starts a query over a new table occurrence, of the one row of a singleton table. */
|
|
2883
|
+
type DbEntries<T extends Tables, R extends SchemaRelations> = { readonly [K in keyof T]: BoundSelectQuery<TableQueryState<T[K], TableContext<R, K>>>; };
|
|
2698
2884
|
/** 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
2885
|
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>;
|
|
2700
2886
|
/** State of a query that a `Db` can execute: over table `T`, selecting records only of relations `U`. */
|
|
@@ -3022,6 +3208,23 @@ interface InsertNode<Field = ProjectionNode> extends MutationParts<Field> {
|
|
|
3022
3208
|
readonly columns: readonly string[];
|
|
3023
3209
|
/** Rows of `VALUES`, at least one, each with a value of every column in their order, or `undefined` for `DEFAULT`. */
|
|
3024
3210
|
readonly values: readonly (readonly (ExpressionNode | undefined)[])[];
|
|
3211
|
+
/** `ON CONFLICT` of the insert, or `undefined` for an insert that rejects a row that conflicts on a key. */
|
|
3212
|
+
readonly conflict?: ConflictNode | undefined;
|
|
3213
|
+
}
|
|
3214
|
+
/** `ON CONFLICT` of an insert: a row that conflicts on `target` updates the existing row by `update`, or is skipped, as `DO NOTHING`, without it. */
|
|
3215
|
+
interface ConflictNode {
|
|
3216
|
+
/**
|
|
3217
|
+
* What a row conflicts on: the keys of the columns of a key of the table, in its order, none for any key, which only `DO NOTHING` takes, or `"singleton"` for the unique index of `(true)` of a singleton table, which holds its one row.
|
|
3218
|
+
*/
|
|
3219
|
+
readonly target: readonly string[] | "singleton";
|
|
3220
|
+
readonly update?: ConflictUpdateNode | undefined;
|
|
3221
|
+
}
|
|
3222
|
+
/** `DO UPDATE SET` of `ON CONFLICT`, whose values see the existing row as the target of the insert and the row proposed for insertion as `excluded`. */
|
|
3223
|
+
interface ConflictUpdateNode {
|
|
3224
|
+
/** Assignments of `SET`, at least one, with unique keys. */
|
|
3225
|
+
readonly set: readonly AssignmentNode[];
|
|
3226
|
+
/** Occurrence of the table that stands for the row proposed for insertion, `EXCLUDED` of PostgreSQL, which the values of `set` refer to and which is in no `FROM`. */
|
|
3227
|
+
readonly excluded: TableSourceNode;
|
|
3025
3228
|
}
|
|
3026
3229
|
/** Update statement of the rows that its filters choose. */
|
|
3027
3230
|
interface UpdateNode<Field = ProjectionNode> extends MutationParts<Field> {
|
|
@@ -3212,4 +3415,4 @@ declare function param<E extends AnyExpression, V extends (E extends AnyExpressi
|
|
|
3212
3415
|
* A run of capital letters is one word, such as `user_id` for `userID`, and digits stay in their word, such as `line2`.
|
|
3213
3416
|
*/
|
|
3214
3417
|
declare function toSnakeCase(key: string): string;
|
|
3215
|
-
export { NoRelations as $,
|
|
3418
|
+
export { NoRelations as $, BoundOperators as $i, ResultOf as $n, ScalarQuery as $r, TypedOperandInput as $t, Db as A, ResultMode as Ai, count as An, and as Ar, DecodedFields as At, PendingTestFixture as B, TextValue as Bi, OrderKey as Bn, CountValueType as Br, InitialMutationState as Bt, BaseOperators as C, TableOptions as Ci, CountExpression as Cn, WhereCondition as Cr, AnyRecordQuery as Ct, UntypedExpression as D, NotFoundError as Di, UnfilteredAggregateOptions as Dn, Condition as Dr, AnyQuery as Dt, TypedExpression as E, defineTable as Ei, StringAggOptions as En, CombinedCondition as Er, SelectionState as Et, DatabaseOptions as F, SqlStatement as Fi, some as Fn, OneRow as Fr, AnyMutationQuery as Ft, createTestHelpers as G, TypeOutput as Gi, asc as Gn, InSignature as Gr, HelperEffect as Gt, TestHelpers as H, TypeInput as Hi, OrderTerms as Hn, EqualityOperator as Hr, MutationQuery as Ht, TransactionOptions as I, TransactionFailure as Ii, stringAgg as In, OneValueRows as Ir, ConflictActionRequired as It, SynchronousResult as J, AnyColumn as Ji, ColumnRefs as Jn, ComparisonSignature as Jr, CaseCondition as Jt, AnyTransformedQuery as K, TypeTransform as Ki, desc as Kn, equalityOperators as Kr, defineHelper as Kt, createDatabase as L, createStatement as Li, sum as Ln, QueryResult as Lr, ConflictKeyRequired as Lt, DbOf as M, Connection as Mi, every as Mn, or as Mr, InitialSqlQueryState as Mt, DbQueryState as N, Driver as Ni, max as Nn, ExistsValueType as Nr, SqlQuery as Nt, AnyDb as O, RequiredRelationError as Oi, UnfilteredStringAggOptions as On, ConditionOperand as Or, SelectQueryKind as Ot, Database as P, DriverResult as Pi, min as Pn, FilteredRows as Pr, SqlQueryState as Pt, MergeRelations as Q, ToColumn as Qi, BoundSelectQuery as Qn, OperandUses as Qr, TypedOperand as Qt, TransactionCoordinator as R, AnyType as Ri, OrderColumns as Rn, ResultByRows as Rr, ConflictTarget as Rt, AnyExpression as S, TableKeys as Si, AggregateOptions as Sn, WhereCallback as Sr, defineSchema as St, ExpressionOperators as T, createTableFactory as Ti, GroupValue as Tn, WhereValues as Tr, RecordQueryState as Tt, TestHooks as U, TypeOperators as Ui, Ordering as Un, EqualityOperators as Ur, MutationQueryState as Ut, TestFixture as V, Type as Vi, OrderTerm as Vn, CompileOptions as Vr, InsertMutationState as Vt, TestTransactionOptions as W, TypeOptions as Wi, SelectedRefs as Wn, HasEquality as Wr, Helper as Wt, TransformedQueryState as X, ColumnAttributes as Xi, RelationRefs as Xn, OperandValue as Xr, FollowingOperand as Xt, TransformedQuery as Y, Column as Yi, QueryContext as Yn, Operand as Yr, CaseValue as Yt, DeclaredRelations as Z, KeyKind as Zi, AnySelectQuery as Zn, compare as Zr, NullSkippingResult as Zt, TextOperators as _, ColumnValues as _i, SelectionResult as _n, KeyCheck as _r, Schema as _t, sql as a, OperatorSignature as aa, InitialQueryState as ai, ColumnFieldTypes as an, InsertValues as ar, RelationKeys as at, NumberTypeOperators as b, Table as bi, literal as bn, PrimaryKeyValues as br, SchemaTables as bt, MatchResult as c, QueryValueType as ci, GroupOperandValue as cn, UpsertValues as cr, SchemaContext as ct, defineExpression as d, TableQueryState as di, GroupedRecord as dn, FoundState as dr, TableRelations as dt, MergeOperators as ea, TypedScalarQuery as ei, TypedOperandOperators as en, SelectQuery as er, OneRelationKeys as et, FALSE as f, AnyTable as fi, MergeSelection as fn, GroupedState as fr, UsableRelation as ft, TextLengthSignature as g, ColumnShape as gi, SelectionFieldTypes as gn, GroupTerms as gr, AnySchema as gt, TextConversionSignature as h, ColumnOperators as hi, Selection as hn, GroupTerm as hr, one as ht, param as i, GenericOperatorImplementation as ia, Grouped as ii, least as in, ConflictUpdateSet as ir, RelationKey as it, DbMutationState as j, ResultRows as ji, countDistinct as jn, not as jr, FieldTypes as jt, BindOptions as k, ResultDecodeError as ki, avg as kn, Predicate as kr, AnySqlQuery as kt, NotAColumn as l, SelectQueryState as li, GroupSelectionResult as ln, FilteredStateRows as lr, SchemaRelations as lt, PatternSignature as m, ColumnDefinitions as mi, SelectedFieldTypes as mn, RowLockOptions as mr, many as mt, SqlTemplate as n, OperatorDefinitions as na, EmptyRecord as ni, coalesce as nn, from as nr, RelationDeclaration as nt, ColumnRefKey as o, defineGenericOperator as oa, QueryState as oi, ExpressionInput as on, UpdateSet as or, RelationKind as ot, TRUE as p, AnyTableKeys as pi, NestedQuery as pn, SelectedOperandType as pr, UsedRelation as pt, NotSynchronous as q, defineType as qi, orderBy as qn, BooleanResult as qr, CaseBranch as qt, identifier as r, GenericOperator as ra, FoundKey as ri, greatest as rn, ColumnValue as rr, RelationDeclarations as rt, MatchCheck as s, QueryStateOf as si, ExpressionValue as sn, UpdateValues as sr, RequiredMember as st, toSnakeCase as t, NoOperators as ta, ValuesQuery as ti, caseWhen as tn, SingletonInsertMany as tr, Relation as tt, matches as u, SingletonQueryState as ui, GroupedField as un, FoundQueryState as ur, TableContext as ut, TextTypeOperators as v, KeyColumns as vi, SelectionUses as vn, NotAKey as vr, SchemaPart as vt, Expression as w, ToColumns as wi, GroupAggregateExpression as wn, WhereFilter as wr, RecordQuery as wt, orderingOperators as x, TableFactoryOptions as xi, AggregateExpression as xn, EqValue as xr, Tables as xt, textOperators as y, Selectable as yi, Literal as yn, PrimaryKeyColumn as yr, SchemaPartRelations as yt, OpenedTestFixture as z, BaseType as zi, OrderDirection as zn, ValueRows as zr, InitialInsertState as zt };
|