-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdefault-auth.ts
239 lines (209 loc) · 8.95 KB
/
default-auth.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
234
235
236
237
238
239
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ACTIONS_WITH_WRITE_PAYLOAD } from '../../constants';
import {
FieldInfo,
NestedWriteVisitor,
NestedWriteVisitorContext,
PrismaWriteActionType,
clone,
enumerate,
getFields,
getModelInfo,
getTypeDefInfo,
requireField,
} from '../../cross';
import { DbClientContract, EnhancementContext } from '../../types';
import { InternalEnhancementOptions } from './create-enhancement';
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy';
import { isUnsafeMutate, prismaClientValidationError } from './utils';
/**
* Gets an enhanced Prisma client that supports `@default(auth())` attribute.
*
* @private
*/
export function withDefaultAuth<DbClient extends object>(
prisma: DbClient,
options: InternalEnhancementOptions,
context: EnhancementContext = {}
): DbClient {
return makeProxy(
prisma,
options.modelMeta,
(_prisma, model) => new DefaultAuthHandler(_prisma as DbClientContract, model, options, context),
'defaultAuth'
);
}
class DefaultAuthHandler extends DefaultPrismaProxyHandler {
private readonly userContext: any;
constructor(
prisma: DbClientContract,
model: string,
options: InternalEnhancementOptions,
private readonly context: EnhancementContext
) {
super(prisma, model, options);
this.userContext = this.context.user;
}
// base override
protected async preprocessArgs(action: PrismaProxyActions, args: any) {
if (args && ACTIONS_WITH_WRITE_PAYLOAD.includes(action)) {
const newArgs = await this.preprocessWritePayload(this.model, action as PrismaWriteActionType, args);
return newArgs;
}
return args;
}
private async preprocessWritePayload(model: string, action: PrismaWriteActionType, args: any) {
const newArgs = clone(args);
const processCreatePayload = (model: string, data: any, context: NestedWriteVisitorContext) => {
const fields = getFields(this.options.modelMeta, model);
for (const fieldInfo of Object.values(fields)) {
if (fieldInfo.isTypeDef) {
this.setDefaultValueForTypeDefData(fieldInfo.type, data[fieldInfo.name]);
continue;
}
if (fieldInfo.name in data) {
// create payload already sets field value
continue;
}
if (!fieldInfo.defaultValueProvider) {
// field doesn't have a runtime default value provider
continue;
}
const defaultValue = this.getDefaultValue(fieldInfo);
if (defaultValue !== undefined) {
// set field value extracted from `auth()`
this.setDefaultValueForModelData(fieldInfo, model, data, defaultValue, context);
}
}
};
// visit create payload and set default value to fields using `auth()` in `@default()`
const visitor = new NestedWriteVisitor(this.options.modelMeta, {
create: (model, data, context) => {
processCreatePayload(model, data, context);
},
upsert: (model, data, context) => {
processCreatePayload(model, data.create, context);
},
createMany: (model, args, context) => {
for (const item of enumerate(args.data)) {
processCreatePayload(model, item, context);
}
},
});
await visitor.visit(model, action, newArgs);
return newArgs;
}
private setDefaultValueForModelData(
fieldInfo: FieldInfo,
model: string,
data: any,
authDefaultValue: unknown,
context: NestedWriteVisitorContext
) {
if (fieldInfo.isForeignKey) {
// if the field being inspected is a fk field, there are several cases we should not
// set the default value or should not set directly
// if the field is a fk, and the relation field is already set, we should not override it
if (fieldInfo.relationField && fieldInfo.relationField in data) {
return;
}
if (context.field?.backLink && context.nestingPath.length > 1) {
// if the fk field is in a creation context where its implied by the parent,
// we should not set the default value, e.g.:
//
// ```
// parent.create({ data: { child: { create: {} } } })
// ```
//
// event if child's fk to parent has a default value, we should not set default
// value here
// fetch parent model from the parent context
const parentModel = getModelInfo(
this.options.modelMeta,
context.nestingPath[context.nestingPath.length - 2].model
);
if (parentModel) {
// get the opposite side of the relation for the current create context
const oppositeRelationField = requireField(this.options.modelMeta, model, context.field.backLink);
if (parentModel.name === oppositeRelationField.type) {
// if the opposite side matches the parent model, it means we currently in a creation context
// that implicitly sets this fk field
return;
}
}
}
if (!isUnsafeMutate(model, data, this.options.modelMeta)) {
// if the field is a fk, and the create payload is not unsafe, we need to translate
// the fk field setting to a `connect` of the corresponding relation field
const relFieldName = fieldInfo.relationField;
if (!relFieldName) {
throw new Error(
`Field \`${fieldInfo.name}\` is a foreign key field but no corresponding relation field is found`
);
}
const relationField = requireField(this.options.modelMeta, model, relFieldName);
// construct a `{ connect: { ... } }` payload
let connect = data[relationField.name]?.connect;
if (!connect) {
connect = {};
data[relationField.name] = { connect };
}
// sets the opposite fk field to value `authDefaultValue`
const oppositeFkFieldName = this.getOppositeFkFieldName(relationField, fieldInfo);
if (!oppositeFkFieldName) {
throw new Error(
`Cannot find opposite foreign key field for \`${fieldInfo.name}\` in relation field \`${relFieldName}\``
);
}
connect[oppositeFkFieldName] = authDefaultValue;
return;
}
}
// set default value directly
data[fieldInfo.name] = authDefaultValue;
}
private getOppositeFkFieldName(relationField: FieldInfo, fieldInfo: FieldInfo) {
if (!relationField.foreignKeyMapping) {
return undefined;
}
const entry = Object.entries(relationField.foreignKeyMapping).find(([, v]) => v === fieldInfo.name);
return entry?.[0];
}
private getDefaultValue(fieldInfo: FieldInfo) {
if (!this.userContext) {
throw prismaClientValidationError(
this.prisma,
this.options.prismaModule,
`Evaluating default value of field \`${fieldInfo.name}\` requires a user context`
);
}
return fieldInfo.defaultValueProvider?.(this.userContext);
}
private setDefaultValueForTypeDefData(type: string, data: any) {
if (!data || (typeof data !== 'object' && !Array.isArray(data))) {
return;
}
const typeDef = getTypeDefInfo(this.options.modelMeta, type);
if (!typeDef) {
return;
}
enumerate(data).forEach((item) => {
if (!item || typeof item !== 'object') {
return;
}
for (const fieldInfo of Object.values(typeDef.fields)) {
if (fieldInfo.isTypeDef) {
// recurse
this.setDefaultValueForTypeDefData(fieldInfo.type, item[fieldInfo.name]);
} else if (!(fieldInfo.name in item)) {
// set default value if the payload doesn't set the field
const defaultValue = this.getDefaultValue(fieldInfo);
if (defaultValue !== undefined) {
item[fieldInfo.name] = defaultValue;
}
}
}
});
}
}