Skip to content

Commit b7e832a

Browse files
authored
Block the executeRaw, queryRawUnsafe and executeRawUnsafe in readonly-client. (#37)
* Block executeRaw,queryRawUnsafe and executeRawUnsafe in readonly-client. * Applied suggestion, replaced `any` with `unknown`. * Mark `$runCommandRaw` as unsafe. * Also mark createManyAndReturn as writeable.
1 parent 451509f commit b7e832a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

readonly-client/script.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ const WRITE_METHODS = [
66
"upsert",
77
"delete",
88
"createMany",
9+
"createManyAndReturn",
910
"updateMany",
1011
"deleteMany",
1112
] as const;
1213

14+
const GLOBAL_WRITE_METHODS = [
15+
'$executeRaw',
16+
'$queryRawUnsafe',
17+
'$executeRawUnsafe',
18+
'$runCommandRaw',
19+
] as const;
20+
1321
const ReadonlyClient = Prisma.defineExtension({
1422
name: "ReadonlyClient",
1523
model: {
@@ -28,6 +36,15 @@ const ReadonlyClient = Prisma.defineExtension({
2836
) => never;
2937
},
3038
},
39+
query: Object.fromEntries(
40+
GLOBAL_WRITE_METHODS.map((method) => [
41+
method,
42+
function (args: never) {
43+
throw new Error(`Calling the \`${method}\` method on a readonly client is not allowed`);
44+
}
45+
])) as {
46+
[K in typeof GLOBAL_WRITE_METHODS[number]]: (args: `Calling the \`${K}\` method on a readonly client is not allowed`) => never;
47+
}
3148
});
3249

3350
const prisma = new PrismaClient();
@@ -44,6 +61,8 @@ async function main() {
4461
await readonlyPrisma.post.create({
4562
data: { title: "New post", published: false },
4663
});
64+
65+
await readonlyPrisma.$executeRaw`INSERT INTO post(id,title, published) VALUES(12345,'New post', false)`
4766
}
4867

4968
main()

0 commit comments

Comments
 (0)