/
interfaces.ts
176 lines (159 loc) · 5.17 KB
/
interfaces.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { PgCodec, PgResource } from "@dataplan/pg";
import type { SQL } from "pg-sql2";
declare global {
namespace DataplanPg {
interface PgCodecExtensions {
isNumberLike?: boolean;
isIntervalLike?: boolean;
}
interface PgConditionStepExtensions {
pgFilterAttribute?: {
codec: PgCodec<any, any, any, any, any, any, any>;
expression: SQL;
};
}
}
namespace GraphileBuild {
interface Inflection {
filterTableAggregateType(
foreignTable: PgResource<any, any, any, any>,
spec: AggregateSpec
): string;
}
interface Build {
pgAggregateSpecs: AggregateSpec[];
pgAggregateGroupBySpecs: AggregateGroupBySpec[];
pgHavingFilterTypeNameForCodec(
codec: PgCodec<any, any, any, any>
): string | null;
}
interface ScopeObject {
isPgAggregateContainerType?: boolean;
isPgAggregateType?: boolean;
pgAggregateSpec?: AggregateSpec;
}
interface ScopeInputObject {
pgTypeResource?: PgResource<any, any, any, any>;
isPgHavingFilterInputType?: boolean;
isPgAggregateHavingInputType?: boolean;
pgHavingFilterSpec?: string;
isPgConnectionAggregateFilter?: boolean;
isPgConnectionAggregateAggregateFilter?: boolean;
pgConnectionAggregateFilterAggregateSpec?: AggregateSpec;
}
interface ScopeObjectFieldsField {
isPgAggregateField?: boolean;
isPgConnectionAggregateField?: boolean;
// TODO: remove this, it's redundant vs pgTypeResource?
pgFieldResource?: PgResource<any, any, any, any, any>;
}
interface ScopeInputObjectFieldsField {
isPgConnectionFilterAggregatesField?: boolean;
}
interface ScopeEnum {
pgTypeResource?: PgResource<any, any, any, any, any>;
isPgAggregateGroupEnum?: boolean;
}
}
}
export type AggregateTargetEntity =
| {
type: "attribute";
/** table codec - NOT attribute codec! */
codec: PgCodec<any, any, any, any, any, any, any>;
/** attribute name available on this codec */
attributeName: string;
resource?: never;
}
| {
type: "computedAttribute";
resource: PgResource<any, any, any, any, any>;
codec?: never;
attributeName?: never;
};
export interface AggregateGroupBySpec {
/** Must not change since it's used in type names/etc */
id: string; // e.g. 'truncated-to-hour'
/** Return true if we can process this type */
isSuitableType: (
codec: PgCodec<any, any, any, any, any, any, any>
) => boolean;
/** Return false if we cannot process this attribute (default: true) */
shouldApplyToEntity?: (entity: AggregateTargetEntity) => boolean;
/** Wraps the SQL to return a derivative (e.g.:
* ```
* sqlFrag => sql.fragment`date_trunc('hour', ${sqlFrag})`)
* ```
*/
sqlWrap: (sqlFrag: SQL) => SQL;
}
export interface AggregateSpec {
/** Must not change since it's used in type names/etc */
id: string;
/** Used in descriptions, starts with lowercase */
humanLabel: string;
/** Used in descriptions, starts with uppercase */
HumanLabel: string;
/** Return true if we can process this type */
isSuitableType: (codec: PgCodec<any, any, any, any>) => boolean;
/** Return false if we cannot process this attribute (default: true) */
shouldApplyToEntity?: (entity: AggregateTargetEntity) => boolean;
/** Wraps the SQL in an aggregate call */
sqlAggregateWrap: (sqlFrag: SQL, codec: PgCodec<any, any, any, any>) => SQL;
/**
* Used to translate the PostgreSQL return type for the aggregate; for example:
*
* - Sum over int should give bigint
* - Average of int should be float
* - Median of int should be int
*/
pgTypeCodecModifier?: (
codec: PgCodec<any, any, any, any>
) => PgCodec<any, any, any, any>;
/** Set true if the result is guaranteed to be non-null */
isNonNull?: boolean;
}
export const BIGINT_OID = "20";
export const INT2_OID = "21";
export const INT4_OID = "23";
export const FLOAT4_OID = "700";
export const FLOAT8_OID = "701";
export const NUMERIC_OID = "1700";
export const MONEY_OID = "790";
export const INTERVAL_OID = "1186";
export const DATE_OID = "1082";
export const TIMESTAMP_OID = "1114";
export const TIMESTAMPTZ_OID = "1184";
export const TIME_OID = "1083";
export const TIMETZ_OID = "1266";
export const JSON_OID = "114";
export const JSONB_OID = "3802";
export const UUID_OID = "2950";
export const BIT_OID = "1560";
export const VARBIT_OID = "1562";
export const CHAR_OID = "18";
export const TEXT_OID = "25";
export const VARCHAR_OID = "1043";
export const POINT_OID = "600";
export const INET_OID = "869";
export const CIDR_OID = "650";
export const MAC_ADDR_OID = "829";
export const MAC_ADDR8_OID = "774";
export const REG_PROC_OID = "24";
export const REG_PROCEDURE_OID = "2202";
export const REG_OPER_OID = "2203";
export const REG_OPERATOR_OID = "2204";
export const REG_CLASS_OID = "2205";
export const REG_OID = "2206";
export const REG_ROLE_OID = "4096";
export const REG_NAMESPACE_OID = "4089";
export const REG_CONFIG_OID = "3734";
export const REG_DICTIONARY_OID = "3769";
export const CORE_HAVING_FILTER_SPECS = [
"int",
"bigint",
"float",
"bigfloat",
"datetime",
"string",
] as const;