-
Notifications
You must be signed in to change notification settings - Fork 15
/
script.ts
76 lines (68 loc) · 2.01 KB
/
script.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
import { Prisma, PrismaClient } from "@prisma/client";
const WRITE_METHODS = [
"create",
"update",
"upsert",
"delete",
"createMany",
"createManyAndReturn",
"updateMany",
"deleteMany",
] as const;
const GLOBAL_WRITE_METHODS = [
'$executeRaw',
'$queryRawUnsafe',
'$executeRawUnsafe',
'$runCommandRaw',
] as const;
const ReadonlyClient = Prisma.defineExtension({
name: "ReadonlyClient",
model: {
$allModels: Object.fromEntries(
WRITE_METHODS.map((method) => [
method,
function (args: never) {
throw new Error(
`Calling the \`${method}\` method on a readonly client is not allowed`
);
},
])
) as {
[K in typeof WRITE_METHODS[number]]: (
args: `Calling the \`${K}\` method on a readonly client is not allowed`
) => never;
},
},
query: Object.fromEntries(
GLOBAL_WRITE_METHODS.map((method) => [
method,
function (args: never) {
throw new Error(`Calling the \`${method}\` method on a readonly client is not allowed`);
}
])) as {
[K in typeof GLOBAL_WRITE_METHODS[number]]: (args: `Calling the \`${K}\` method on a readonly client is not allowed`) => never;
}
});
const prisma = new PrismaClient();
const readonlyPrisma = prisma.$extends(ReadonlyClient);
async function main() {
const posts = await readonlyPrisma.post.findMany({ take: 5 });
console.log(posts);
// @ts-expect-error:
// Argument of type '{ data: { title: string; published: boolean; }; }'
// is not assignable to parameter of type '"Calling the `create` method
// on a readonly client is not allowed"'.
await readonlyPrisma.post.create({
data: { title: "New post", published: false },
});
await readonlyPrisma.$executeRaw`INSERT INTO post(id,title, published) VALUES(12345,'New post', false)`
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});