-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathquery-utils.ts
More file actions
297 lines (262 loc) · 11.2 KB
/
query-utils.ts
File metadata and controls
297 lines (262 loc) · 11.2 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
clone,
getIdFields,
getModelInfo,
getUniqueConstraints,
resolveField,
type FieldInfo,
type NestedWriteVisitorContext,
} from '../../cross';
import type { CrudContract, DbClientContract } from '../../types';
import { getVersion } from '../../version';
import { formatObject } from '../edge';
import { InternalEnhancementOptions } from './create-enhancement';
import { Logger } from './logger';
import { prismaClientUnknownRequestError, prismaClientValidationError } from './utils';
export class QueryUtils {
private readonly logger: Logger;
constructor(private readonly prisma: DbClientContract, protected readonly options: InternalEnhancementOptions) {
this.logger = new Logger(prisma);
}
getIdFields(model: string) {
return getIdFields(this.options.modelMeta, model, true);
}
makeIdSelection(model: string) {
const idFields = this.getIdFields(model);
return Object.assign({}, ...idFields.map((f) => ({ [f.name]: true })));
}
getEntityIds(model: string, entityData: any) {
const idFields = this.getIdFields(model);
const result: Record<string, unknown> = {};
for (const idField of idFields) {
result[idField.name] = entityData[idField.name];
}
return result;
}
/**
* Initiates a transaction.
*/
transaction<T>(db: CrudContract, action: (tx: CrudContract) => Promise<T>) {
const fullDb = db as DbClientContract;
if (fullDb['$transaction']) {
return fullDb.$transaction(
(tx) => {
(tx as any)[Symbol.for('nodejs.util.inspect.custom')] = 'PrismaClient$tx';
return action(tx);
},
{
maxWait: this.options.transactionMaxWait,
timeout: this.options.transactionTimeout,
isolationLevel: this.options.transactionIsolationLevel,
}
);
} else {
// already in transaction, don't nest
return action(db);
}
}
/**
* Builds a reversed query for the given nested path.
*/
async buildReversedQuery(
db: CrudContract,
context: NestedWriteVisitorContext,
forMutationPayload = false,
uncheckedOperation = false
) {
let result, currQuery: any;
let currField: FieldInfo | undefined;
for (let i = context.nestingPath.length - 1; i >= 0; i--) {
const { field, model, where } = context.nestingPath[i];
// never modify the original where because it's shared in the structure
const visitWhere = { ...where };
if (model && where) {
// make sure composite unique condition is flattened
this.flattenGeneratedUniqueField(model, visitWhere);
}
if (!result) {
// first segment (bottom), just use its where clause
result = currQuery = { ...visitWhere };
currField = field;
} else {
if (!currField) {
throw this.unknownError(`missing field in nested path`);
}
if (!currField.backLink) {
throw this.unknownError(`field ${currField.type}.${currField.name} doesn't have a backLink`);
}
const backLinkField = this.getModelField(currField.type, currField.backLink);
if (!backLinkField) {
throw this.unknownError(`missing backLink field ${currField.backLink} in ${currField.type}`);
}
if (backLinkField.isArray && !forMutationPayload) {
// many-side of relationship, wrap with "some" query
currQuery[currField.backLink] = { some: { ...visitWhere } };
currQuery = currQuery[currField.backLink].some;
} else {
const fkMapping = where && backLinkField.isRelationOwner && backLinkField.foreignKeyMapping;
// calculate if we should preserve the relation condition (e.g., { user: { id: 1 } })
const shouldPreserveRelationCondition =
// doing a mutation
forMutationPayload &&
// and it's not an unchecked mutate
!uncheckedOperation &&
// and the current segment is the direct parent (the last one is the mutate itself),
// the relation condition should be preserved and will be converted to a "connect" later
i === context.nestingPath.length - 2;
if (fkMapping && !shouldPreserveRelationCondition) {
// turn relation condition into foreign key condition, e.g.:
// { user: { id: 1 } } => { userId: 1 }
let parentPk = visitWhere;
if (Object.keys(fkMapping).some((k) => !(k in parentPk) || parentPk[k] === undefined)) {
// it can happen that the parent condition actually doesn't contain all id fields
// (when the parent condition is not a primary key but unique constraints)
// and in such case we need to load it to get the pks
if (this.options.logPrismaQuery && this.logger.enabled('info')) {
this.logger.info(
`[reverseLookup] \`findUniqueOrThrow\` ${model}: ${formatObject(where)}`
);
}
parentPk = await db[model].findUniqueOrThrow({
where,
select: this.makeIdSelection(model),
});
}
for (const [r, fk] of Object.entries<string>(fkMapping)) {
currQuery[fk] = parentPk[r];
}
if (i > 0) {
// prepare for the next segment
currQuery[currField.backLink] = {};
}
} else {
// preserve the original structure
currQuery[currField.backLink] = { ...visitWhere };
}
if (forMutationPayload && currQuery[currField.backLink]) {
// reconstruct compound unique field
currQuery[currField.backLink] = this.composeCompoundUniqueField(
backLinkField.type,
currQuery[currField.backLink]
);
}
currQuery = currQuery[currField.backLink];
}
currField = field;
}
}
return result;
}
/**
* Composes a compound unique field from multiple fields. E.g.: { a: '1', b: '1' } => { a_b: { a: '1', b: '1' } }.
*/
composeCompoundUniqueField(model: string, fieldData: any) {
const uniqueConstraints = getUniqueConstraints(this.options.modelMeta, model);
if (!uniqueConstraints) {
return fieldData;
}
const result: any = this.safeClone(fieldData);
for (const [name, constraint] of Object.entries(uniqueConstraints)) {
if (constraint.fields.length > 1 && constraint.fields.every((f) => fieldData[f] !== undefined)) {
// multi-field unique constraint, compose it
result[name] = constraint.fields.reduce<any>(
(prev, field) => ({ ...prev, [field]: fieldData[field] }),
{}
);
constraint.fields.forEach((f) => delete result[f]);
}
}
return result;
}
/**
* Flattens a generated unique field. E.g.: { a_b: { a: '1', b: '1' } } => { a: '1', b: '1' }.
*/
flattenGeneratedUniqueField(model: string, args: any) {
const uniqueConstraints = getUniqueConstraints(this.options.modelMeta, model);
if (uniqueConstraints && Object.keys(uniqueConstraints).length > 0) {
for (const [field, value] of Object.entries<any>(args)) {
if (
uniqueConstraints[field] &&
uniqueConstraints[field].fields.length > 1 &&
typeof value === 'object'
) {
// multi-field unique constraint, flatten it
delete args[field];
if (value) {
for (const [f, v] of Object.entries(value)) {
args[f] = v;
}
}
}
}
}
}
validationError(message: string) {
return prismaClientValidationError(this.prisma, this.options.prismaModule, message);
}
unknownError(message: string) {
return prismaClientUnknownRequestError(this.prisma, this.options.prismaModule, message, {
clientVersion: getVersion(),
});
}
getModelFields(model: string) {
return getModelInfo(this.options.modelMeta, model)?.fields;
}
/**
* Gets information for a specific model field.
*/
getModelField(model: string, field: string) {
return resolveField(this.options.modelMeta, model, field);
}
/**
* Clones an object and makes sure it's not empty.
*/
safeClone(value: unknown): any {
return value ? clone(value) : value === undefined || value === null ? {} : value;
}
getDelegateConcreteModel(model: string, data: any) {
if (!data || typeof data !== 'object') {
return model;
}
const modelInfo = getModelInfo(this.options.modelMeta, model);
if (modelInfo?.discriminator) {
// model has a discriminator so it can be a polymorphic base,
// need to find the concrete model
const concreteModelName = data[modelInfo.discriminator];
if (typeof concreteModelName === 'string' && concreteModelName) {
return concreteModelName;
}
}
return model;
}
/**
* Gets relation info for a foreign key field.
*/
getRelationForForeignKey(model: string, fkField: string) {
const modelInfo = getModelInfo(this.options.modelMeta, model);
if (!modelInfo) {
return undefined;
}
for (const field of Object.values(modelInfo.fields)) {
if (field.foreignKeyMapping) {
const entry = Object.entries(field.foreignKeyMapping).find(([, v]) => v === fkField);
if (entry) {
return { relation: field, idField: entry[0], fkField: entry[1] };
}
}
}
return undefined;
}
/**
* Gets fields of object with defined values.
*/
getFieldsWithDefinedValues(data: object) {
if (!data) {
return [];
}
return Object.entries(data)
.filter(([, v]) => v !== undefined)
.map(([k]) => k);
}
}