-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries.ts
49 lines (42 loc) · 1.35 KB
/
queries.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
import { PrismaClient } from "@prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
const prisma = new PrismaClient().$extends(withAccelerate());
/**
*
* This function queries your database as you typically would to retrieve a list of posts.
* It then caches the result for 300 seconds with Accelerate.
*/
async function postsView(requestsCount: number) {
for (let i = 0; i < requestsCount; i++) {
const startTime = performance.now();
const { info } = await prisma.post
.findMany({
cacheStrategy: {
ttl: 300,
},
})
.withAccelerateInfo();
const duration = performance.now() - startTime;
console.log(
`Query ${i + 1}: ${duration.toFixed(2)}ms - Cache Status: ${
info?.cacheStatus || "unknown"
}`
);
}
}
async function main() {
const requestsCount = 50; // Accelerate scales your database queries. 50 inidicates the number of requests to make to your database. You can change this number.
console.log(`Querying your database with ${requestsCount} requests`);
const startTime = Date.now();
await postsView(requestsCount);
const endTime = Date.now();
console.log(`\nTotal execution time: ${endTime - startTime}ms`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});