-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprimary-key.ts
62 lines (58 loc) · 1.82 KB
/
primary-key.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {TableWithPrimaryKey} from "../table";
import {TypeMapUtil} from "../type-map";
import {UnionToIntersection} from "../type-util";
/**
* Assumes `TableT` is not a union.
*
* If it is a union, use `PrimaryKey_Output/Input<U>` instead.
*
* -----
*
* Also assumes `TableT["columns"]` and `TableT["primaryKey"]` are not unions.
* They really shouldn't be unions.
* + Why does your table not have a definite set of columns?
* Is it Schrödinger's columns?
* + Why does your table not have a definite primary key?
* Is it Schrödinger's primary key?
*/
export type PrimaryKey_NonUnion<TableT extends Pick<TableWithPrimaryKey, "columns"|"primaryKey">> = (
TypeMapUtil.FromColumnMap<
Pick<TableT["columns"], TableT["primaryKey"][number]>
>
);
/**
* Works properly, even when `TableT` is a union.
*
* Will return a union of primary keys.
* Meant for output/read/covariant positions.
*/
export type PrimaryKey_Output<TableT extends Pick<TableWithPrimaryKey, "columns"|"primaryKey">> = (
TableT extends Pick<TableWithPrimaryKey, "columns"|"primaryKey"> ?
PrimaryKey_NonUnion<TableT> :
never
);
/**
* Works properly, even when `TableT` is a union.
*
* Will return an intersection of primary keys.
* Meant for input/write/contravariant positions.
*/
export type PrimaryKey_Input<TableT extends Pick<TableWithPrimaryKey, "columns"|"primaryKey">> = (
UnionToIntersection<
PrimaryKey_Output<TableT>
>
);
/**
* Represents a primary key of the table, when retrieved from the database.
*
* An alias of `PrimaryKey_NonUnion<>` for convenience reasons.
*
* -----
*
* Assumes `TableT` is not a union.
*
* If it is a union, use `PrimaryKey_Output/Input<U>` instead.
*/
export type PrimaryKey<TableT extends Pick<TableWithPrimaryKey, "columns"|"primaryKey">> =
PrimaryKey_NonUnion<TableT>
;