forked from ps73/feathers-prisma
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
243 lines (215 loc) · 7.74 KB
/
utils.ts
File metadata and controls
243 lines (215 loc) · 7.74 KB
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
234
235
236
237
238
239
240
241
242
243
import { OPERATORS_MAP } from './constants';
import { EagerQuery, FeathersQueryData, IdField, QueryParam, QueryParamRecordFilters } from './types';
export const castToNumberBooleanStringOrNull = (value: string | boolean | number) => {
const isNumber = typeof value === 'number';
const isBoolean = value === 'true' || value === 'false';
if (isBoolean || typeof value === 'boolean') {
return typeof value === 'string' ? value === 'true' : value;
}
if (isNumber) {
return value;
}
if (value === 'null') {
return null;
}
return value;
};
export const castFeathersQueryToPrismaFilters = (p: QueryParamRecordFilters, whitelist: string[]) => {
const filters: Record<string, any> = {};
Object.keys(p).forEach((k: string) => {
const key = (k as keyof typeof OPERATORS_MAP);
const prismaKey = OPERATORS_MAP[key];
if (prismaKey && key !== '$eager' && whitelist.includes(key)) {
const value = p[key];
if (Array.isArray(value)) {
filters[prismaKey] = value.map((v) => castToNumberBooleanStringOrNull(v));
} else if (key === '$rawWhere' && typeof value === 'object') {
Object.keys(value).forEach((rawKey) => {
filters[rawKey] = value[rawKey];
});
} else if (value !== undefined && typeof value !== 'object') {
filters[prismaKey] = castToNumberBooleanStringOrNull(value);
}
}
});
return filters;
};
export const castEagerQueryToPrismaInclude = (value: EagerQuery, whitelist: string[], idField: string) => {
// we don't care about feathers compliance, we want where queries in our include
// thus just returning the $eager value as include 1:1
return value;
// const include: Record<string, any> = {};
// if (Array.isArray(value)) {
// value.forEach((v) => {
// if (Array.isArray(v) && typeof v[0] === 'string' && v.length > 1) {
// const [key, ...includes] = v;
// const subinclude = castEagerQueryToPrismaInclude(includes, whitelist, idField);
// include[key] = {
// include: subinclude,
// };
// } else if (Array.isArray(v) && typeof v[0] === 'string' && v.length === 1) {
// const [key] = v;
// include[key] = true;
// } else if (typeof v[0] !== 'string') {
// throw {
// code: 'FP1001',
// message: 'First Array Item in a sub-array must be a string!',
// };
// } else if (typeof v === 'string') {
// include[v] = true;
// }
// });
// } else {
// Object.keys(value).forEach((key) => {
// const val = value[key];
// if (typeof val === 'boolean') {
// include[key] = val;
// } else if (Array.isArray(val)) {
// include[key] = {
// select: {
// [idField]: true,
// ...buildSelect(val),
// },
// };
// }
// });
// }
// return include;
};
/**
* WARN: This method is not safe for Feathers queries because unwanted queries can reach the Prisma-Client.
**/
export const buildIdField = (value: IdField, whitelist: string[]) => {
if (value !== null && typeof value === 'object') {
const filters = castFeathersQueryToPrismaFilters(value, whitelist);
Object.keys(OPERATORS_MAP).forEach((key) => {
key in value && delete value[key];
});
return {
...value,
...filters,
};
}
return value;
};
export const buildWhereAndInclude = (query: QueryParam, whitelist: string[], idField: string) => {
const where: Record<string, any> = {};
let include: Record<string, any> = {};
Object.keys(query).forEach((k: string | '$or' | '$and') => {
const value: any = query[k];
if (value === null) {
where[k] = null;
} else if (k === idField) {
where[k] = buildIdField(value, whitelist);
} else if (k === '$or' && Array.isArray(value)) {
where.OR = value.map((v) => buildWhereAndInclude(v, whitelist, idField).where);
} else if (k === '$and' && Array.isArray(value)) {
where.AND = value.map((v) => buildWhereAndInclude(v, whitelist, idField).where);
} else if (k !== '$eager' && typeof value === 'object' && !Array.isArray(value)) {
where[k] = castFeathersQueryToPrismaFilters(value, whitelist);
} else if (k !== '$eager' && typeof value !== 'object' && !Array.isArray(value)) {
where[k] = castToNumberBooleanStringOrNull(value);
} else if (k === '$eager' && whitelist.includes(k)) {
const eager = value as EagerQuery;
include = castEagerQueryToPrismaInclude(eager, whitelist, idField);
}
});
return { where, include };
};
export const buildSelect = ($select: string[]) => {
const select: Record<string, boolean> = {};
$select.forEach((f: string) => { select[f] = true; });
return select;
};
const buildOrderByItem = ($sort: Record<string, any>) => {
const orderByObj: Record<string, any> = {};
for (const key in $sort) {
const value = $sort[key];
const valueAsNumber = Number(value);
if (valueAsNumber === 1) {
orderByObj[key] = 'asc';
} else if (valueAsNumber === -1) {
orderByObj[key] = 'desc';
} else if (value !== null && typeof value === 'object') {
orderByObj[key] = buildOrderByItem(value);
}
}
return Object.keys(orderByObj).length > 0 ? orderByObj : undefined;
};
const buildOrderByArray = ($sort: Record<string, any>[]) => {
const orderBy = [];
for (const sortItem of $sort) {
const orderByItem = buildOrderByItem(sortItem);
if (orderByItem) {
orderBy.push(orderByItem);
}
}
return orderBy;
};
export const buildOrderBy = ($sort: Record<string, any> | Record<string, any>[]) => {
if (Array.isArray($sort)) {
return buildOrderByArray($sort);
} else if ($sort !== null && typeof $sort === 'object') {
return buildOrderByItem($sort);
}
return [];
};
export const buildPagination = ($skip: number, $limit: number) => {
return {
skip: $skip || 0,
take: $limit,
};
};
export const hasIdObject = (where: Record<string, any>, id?: IdField) =>
id && !where.id && id !== null && typeof id === 'object';
export const buildWhereWithId = (id: IdField | undefined, where: Record<string, any>, idField: string) => {
if (!id) {
return where;
} else if (Object.keys(where).length > 0) {
return { AND: [{ [idField]: id }, where] };
} else {
return { [idField]: id };
}
};
export const buildBasePrismaQueryParams = (
{ id, query, filters, whitelist }: FeathersQueryData,
idField: string,
) => {
const select = buildSelect(filters.$select || []);
const { where, include } = buildWhereAndInclude(query, whitelist, idField);
const orderBy = buildOrderBy(filters.$sort || {});
const { skip, take } = buildPagination(filters.$skip, filters.$limit);
const resultQuery: any = {
skip,
take,
orderBy,
where: buildWhereWithId(id, where, idField)
};
if (Object.keys(select).length > 0) {
resultQuery.select = {
[idField]: true,
...select,
...include,
};
} else if (Object.keys(include).length > 0) {
resultQuery.include = include;
}
return resultQuery;
};
export const buildPrismaQueryParams = (feathersQueryData: FeathersQueryData, idField: string, prismaQueryOverwrite?: Record<string, any>) => {
const basePrismaQuery = buildBasePrismaQueryParams(feathersQueryData, idField);
if (prismaQueryOverwrite) {
const whereOverwrite = prismaQueryOverwrite.where;
delete prismaQueryOverwrite.where;
const baseWhere = basePrismaQuery.where;
return Object.assign(basePrismaQuery, prismaQueryOverwrite, {
where: whereOverwrite ? { AND: [whereOverwrite, baseWhere] } : baseWhere
});
}
return basePrismaQuery;
};
export const buildSelectOrInclude = (
{ select, include }: { select?: Record<string, boolean>; include?: Record<string, any> },
) => {
return select ? { select } : include ? { include } : {};
};