-
Notifications
You must be signed in to change notification settings - Fork 15
/
script.ts
71 lines (58 loc) · 1.66 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
import { Prisma, PrismaClient } from "@prisma/client";
function forUser(userId: number) {
return Prisma.defineExtension((prisma) =>
prisma.$extends({
query: {
$allModels: {
async $allOperations({ args, query }) {
const [, result] = await prisma.$transaction([
prisma.$executeRaw`SELECT set_config('app.current_user_id', ${userId.toString()}, TRUE)`,
query(args),
]);
return result;
},
},
},
})
);
}
const prisma = new PrismaClient();
async function main() {
const user = await prisma.user.findFirstOrThrow();
const product = await prisma.product.findFirstOrThrow();
const userPrisma = prisma.$extends(forUser(user.id));
await userPrisma.product.update({
where: { id: product.id },
data: { name: "New Slurm" },
});
await userPrisma.product.update({
where: { id: product.id },
data: { name: "Slurm Classic" },
});
const order = await userPrisma.order.create({
data: { userId: user.id, productId: product.id, quantity: 10 },
});
await userPrisma.order.update({
where: { id: order.id },
data: { quantity: 20 },
});
await userPrisma.order.delete({
where: { id: order.id },
});
const productVersions = await userPrisma.productVersion.findMany({
where: { id: product.id },
});
const orderVersions = await userPrisma.orderVersion.findMany({
where: { id: order.id },
});
console.log({ productVersions, orderVersions });
}
main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});