-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
262 lines (242 loc) · 8.57 KB
/
client.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { AsyncLocalStorage } from "node:async_hooks";
import { SerializedRecord, deserialize } from "@ungap/structured-clone";
import type { Prisma } from "@prisma/client/extension";
import { debug } from "./shared";
/* eslint-disable @typescript-eslint/no-explicit-any */
// This is a variable that Next.js will string replace during build with a string if run in an edge runtime from Next.js
// v12.2.1-canary.3 onwards:
// https://github.com/vercel/next.js/blob/166e5fb9b92f64c4b5d1f6560a05e2b9778c16fb/packages/next/build/webpack-config.ts#L206
declare const EdgeRuntime: string | undefined;
function isEdgeRuntime(): boolean {
return typeof EdgeRuntime === "string";
}
const defineExtension = ((ext) => {
if (typeof ext === "function") {
return ext;
}
return (client) => client.$extends(ext);
}) as typeof Prisma.defineExtension;
const inTransaction = new AsyncLocalStorage<{
options: any;
txId: string;
parentTxId?: string;
}>();
export function shouldUsePrismaLocalProxy(databaseUrl?: string): boolean {
return (
(isEdgeRuntime() || !!process.env.FORCE_PRISMA_LOCAL_PROXY) &&
!isPrismaLocalProxy() &&
(!databaseUrl || !databaseUrl?.startsWith("prisma://"))
);
}
export function getConnectionString(defaultUrl: string) {
if (shouldUsePrismaLocalProxy()) {
return `prisma://${process.env.PRISMA_LOCAL_PROXY?.replace(
"http://",
""
)}?api_key=dummy`;
}
return defaultUrl;
}
export function isPrismaLocalProxy(): boolean {
return !!globalThis.isPrismaLocalProxy;
}
// 1. first we override $transaction() to:
// - generate a transaction ID for the local proxy; passed via AsyncLocalStorage
// - make a final call to our local proxy to commit the transaction at the end
//
// this needs to be the first client extension applied, so that any subsequent extensions
// that use $transaction will use our override (e.g. Row Level Security)
export function withLocalProxyPre() {
return defineExtension((prisma) => {
let finalPrismaClient: typeof Prisma;
const addClientMethods = {
// callback to set a reference to the final Prisma client after all extensions have been applied
// so we pass it to interactive transactions correctly
$setFinalClient(prismaClient: typeof Prisma) {
finalPrismaClient = prismaClient;
},
async $transaction(batchOrFn: any, options?: any) {
const txId =
typeof EdgeRuntime === "string"
? crypto.randomUUID()
: (await import("crypto")).randomUUID();
const existingInteractiveTxId = inTransaction.getStore()?.txId;
const logInfo = `withLocalProxyPre: $transaction override: ${
Array.isArray(batchOrFn) ? "batch" : "interactive"
}; ${Array.isArray(batchOrFn) ? batchOrFn.length : ""}`;
if (existingInteractiveTxId) {
debug(`[${existingInteractiveTxId} -> nested tx:${txId}] ${logInfo}`);
} else {
debug(`[${txId}] ${logInfo}`);
}
return inTransaction.run(
{ options, txId, parentTxId: existingInteractiveTxId },
async () => {
let result: any;
try {
if (typeof batchOrFn === "function") {
await callLocalProxy({
operation: "$start",
parentTransactionUUID: inTransaction.getStore()?.parentTxId,
transactionUUID: inTransaction.getStore()?.txId,
transactionOptions: inTransaction.getStore()?.options,
});
result = await batchOrFn(finalPrismaClient);
} else {
result = [];
for (const request of batchOrFn) {
result.push(await request);
}
}
} catch (e) {
debug(`$transaction exception: ${e} - calling $cleanup`);
await callLocalProxy({
operation: "$cleanup",
parentTransactionUUID: inTransaction.getStore()?.parentTxId,
transactionUUID: inTransaction.getStore()?.txId,
});
throw e;
}
await callLocalProxy({
operation: "$commit",
parentTransactionUUID: inTransaction.getStore()?.parentTxId,
transactionUUID: inTransaction.getStore()?.txId,
});
return result;
}
);
},
};
return prisma.$extends({
name: "local-proxy",
// $setFinalClient() is for internal use by the withLocalProxyPost extension only
// so we exclude it from our extended Prisma client types
client: addClientMethods as Omit<
typeof addClientMethods,
"$setFinalClient"
>,
});
});
}
// 2. finally we override $allOperations() to call our local proxy instead of
// the usual Prisma client methods. This must be applied after all other
// client extensions, so that any other overrides are applied first and not bypassed
// (since we don't eventually call `query(args)` here, but instead call our local proxy directly)
export function withLocalProxyPost() {
return defineExtension((prisma) => {
const extended = prisma.$extends({
name: "local-proxy",
query: {
async $allOperations({ args, model, operation }) {
debug(`prisma $allOperations: ${model}.${operation}`);
const callback = async () => {
const result = await callLocalProxy({
args,
model,
operation,
parentTransactionUUID: inTransaction.getStore()?.parentTxId,
transactionUUID: inTransaction.getStore()?.txId,
transactionOptions: inTransaction.getStore()?.options,
});
debug(
`local proxy result (${model}.${operation}): ${JSON.stringify(
result
)}`
);
return result;
};
const result: Prisma.PrismaPromise<unknown> & {
requestTransaction: () => Promise<any>;
_model?: string;
_operation?: string;
} = {
then(onFulfilled, onRejected) {
return callback().then(onFulfilled, onRejected);
},
catch(onRejected) {
return callback().catch(onRejected);
},
finally(onFinally) {
return callback().finally(onFinally);
},
requestTransaction() {
const promise = callback();
// if (promise.requestTransaction) {
// // we want to have support for nested promises
// return promise.requestTransaction(batchTransaction);
// }
return promise;
},
[Symbol.toStringTag]: "PrismaPromise",
_model: model,
_operation: operation,
};
return result;
},
},
});
if ("$setFinalClient" in prisma) {
(prisma.$setFinalClient as any)(extended);
}
return extended;
});
}
async function callLocalProxy(body: any) {
const proxyEndpoint = process.env.PRISMA_LOCAL_PROXY;
if (!proxyEndpoint) {
throw new Error("env.PRISMA_LOCAL_PROXY must be set to use local proxy");
}
let result: Response;
try {
result = await fetch(proxyEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...body,
}),
});
} catch (e) {
throw new Error(
`Prisma Local Proxy: failed to get successful result from proxy, is the local proxy service running?\nfetch() error was: ${e}`
);
}
let resultData, bodyJson;
try {
bodyJson = await result.json();
resultData = deserialize(bodyJson);
} catch (e) {
try {
// example bodyJson in case of Error:
// [[2,[[1,2]]],[0,"error"],[7,{"name":"PrismaClientKnownRequestError","message":"..."}]]
if (Array.isArray(bodyJson)) {
const error = bodyJson[2][1];
error.name = "Error";
resultData = deserialize(bodyJson as SerializedRecord);
} else {
throw e;
}
} catch (e) {
debug(
`Prisma Local Proxy deserialize error: ${e}; ${JSON.stringify(
bodyJson
)}`
);
throw new Error(
`local proxy error (${body.model ?? ""}.${body.operation}): ${e}`
);
}
}
if (result.ok) {
return resultData;
} else {
const error = resultData.error;
debug(
`Prisma Local Proxy error: ${typeof error} ${error instanceof Error} ${
"name" in error ? error.name : ""
} ${"message" in error ? error.message : ""}`
);
throw error;
}
}