-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryHelpers.ts
233 lines (211 loc) · 6.47 KB
/
queryHelpers.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { Prisma } from "@prisma/client";
import { Sql } from "@prisma/client/runtime";
import { BatchedKeys, batchKeys } from "./dataloaderHelper";
import format from 'pg-format';
export type OrderByType = {
field: string;
direction: "ASC" | "DESC";
};
type ValueOf<T> = T[keyof T];
export enum QueryArgsType {"Query" , "Mutation"}
export type QueryArgs = {
id?: string
where?: Where;
batchedKeys?: BatchedKeys;
first?: number;
after?: string;
partitionBy?: string[];
order?: OrderByType[];
paginateFiled?: string;
many?:boolean
type?: QueryArgsType
};
const HARD_LIMIT = 20;
export const prismaPartition = (args: QueryArgs) => {
return Prisma.sql`
OVER(
${
args.partitionBy == undefined
? Prisma.empty
: Prisma.sql`partition BY ${Prisma.join([
...selectFields(args.partitionBy),
])}`
}
${
args.order == undefined
? Prisma.empty
: Prisma.sql`ORDER BY ${Prisma.join(
args.order.map((item) => sqlWrap(format('%I %s', item.field, item.direction))
)
)}`
}
)
AS partition_value
`;
};
export const sqlWrap = (s:string) => {
return Prisma.sql([s])
}
export const batchedKeysToSQL = (batchedKeys?: BatchedKeys) => {
if (!batchedKeys || Object.keys(batchedKeys).length == 0) return Prisma.empty;
return Prisma.join(
Object.keys(batchedKeys).map((field) => handleOperator("in", field, Array.from(batchedKeys[field]))
),
" AND "
);
};
export const selectFields = (fields?: string[], fmt: ("%%" | "%I" | "%L" | "%s") = "%I") => {
if (fields == undefined) return [];
const joined = fields.map((field) => sqlWrap(format(`${fmt}`, field)));
return joined;
};
export type FieldOptionsString = {
is?: String;
not?: String;
in?: String[];
not_in?: String[];
lt?: String;
lte?: String;
gt?: String;
gte?: String;
contains?: String;
not_contains?: String;
starts_with?: String;
not_starts_with?: String;
ends_with?: String;
not_ends_with?: String;
};
export type FieldOptionsInt = {
is?: number;
not?: number;
in?: number[];
not_in?: number[];
lt?: number;
lte?: number;
gt?: number;
gte?: number;
contains?: number;
not_contains?: number;
starts_with?: number;
not_starts_with?: number;
ends_with?: number;
not_ends_with?: number;
};
export type FieldOptionsBoolean = {
is?: Boolean;
};
export const handleOperator = (
operation: keyof FieldOptionsString | keyof FieldOptionsInt,
field: string,
value: ValueOf<FieldOptionsString> | ValueOf<FieldOptionsInt>
) => {
switch (operation) {
case "is": {
return sqlWrap(format("%I = %L", field, value));
}
case "not": {
return sqlWrap(format("%I != %L", field, value))
}
case "in": {
return sqlWrap(format("%I IN (%L)", field, value))
}
case "not_in": {
return sqlWrap(format("%I NOT IN (%L)", field, value))
}
case "lt": {
return sqlWrap(format("%I < %L", field, value));
}
case "lte": {
return sqlWrap(format("%I <= %L", field, value));
}
case "gt": {
return sqlWrap(format("%I > %L", field, value));
}
case "gte": {
return sqlWrap(format("%I >= %L", field, value));
}
case "contains": {
return sqlWrap(format("%I ILIKE %L", field, `%${value}%`))
}
case "not_contains": {
return sqlWrap(format("%I NOT ILIKE %L", field, `%${value}%`))
}
case "starts_with": {
return sqlWrap(format("%I ILIKE %L", field, `${value}%`))
}
case "not_starts_with": {
return sqlWrap(format("%I NOT ILIKE %L", field, `${value}%`))
}
case "ends_with": {
return sqlWrap(format("%I ILIKE %L", field, `%${value}`))
}
case "not_ends_with": {
return sqlWrap(format("%I NOT ILIKE %L", field, `%${value}`))
}
default: {
throw new Error(`Invalid operator ${operation}`);
}
}
};
export type Where = {
[field in string | number | "OR" | "AND"]:
| FieldOptionsString
| FieldOptionsInt
| Where[];
};
export const prismaWhere = (where?: Where) => {
return prismaWhereHelper(where);
};
const prismaWhereHelper = (where?: Where | Where[], key?: string): Sql => {
if (where == null || Object.keys(where).length == 0) return Prisma.empty;
if (Array.isArray(where)) {
return Prisma.join(
(where as Where[]).filter((arrItem)=>arrItem != null).map((arrItem) => prismaWhereHelper(arrItem)),
key
);
} else {
// one key (field, AND, OR)
const keys = Object.keys(where);
if (keys.length !== 1) throw new Error("Invalid where object");
const key = keys[0];
if (key == "AND" || key == "OR") {
return Prisma.sql`(${prismaWhereHelper(where[key] as Where[], key)})`;
}
const field = key;
const operation = where[key] as {
[field: string]: FieldOptionsString | FieldOptionsInt;
};
const ops = Object.keys(operation);
if (ops.length !== 1)
throw new Error('Operation can only have one key. name: {is: "Bob"}');
const op = ops[0] as keyof FieldOptionsString | keyof FieldOptionsInt;
const value = operation[op] as ValueOf<FieldOptionsString> | ValueOf<FieldOptionsInt>;
return handleOperator(op, field, value);
}
};
export const whereGen = (args: QueryArgs) => {
const conditions = [
prismaWhere(args.where),
batchedKeysToSQL(args.batchedKeys),
].filter((item) => item != Prisma.empty);
if (conditions.length == 0) return Prisma.empty;
return Prisma.sql`WHERE (${Prisma.join(conditions, " AND ")})`;
};
export const afterLimit = (sql: Prisma.Sql, args: QueryArgs) => {
const afterId = args.after
? parseInt(Buffer.from(args.after, "base64").toString("ascii"))
: 0;
const limit =
args.first == undefined || args.first > HARD_LIMIT
? HARD_LIMIT
: args.first;
const afterSQL = sqlWrap(format.withArray("%s > %s", [args.paginateFiled || "partition_value", afterId]))
const limitSQL = sqlWrap(format.withArray("%s < %s", [args.paginateFiled || "partition_value", limit]))
return Prisma.sql`SELECT * FROM ${sql} WHERE ${afterSQL} AND ${limitSQL}`;
};
export const selectCount = (sql: Prisma.Sql, args: QueryArgs) => {
return Prisma.sql`SELECT ${Prisma.join([
...args.partitionBy?.map((item) => sqlWrap(format('MIN(%I) AS %I', item,item))) || [],
Prisma.sql`AVG(partition_value) as count`,
])} FROM ${sql}`
}