|
1 | 1 | import { remember } from "@epic-web/remember";
|
2 |
| -import { PrismaClient, type User, type Post } from "@prisma/client"; |
| 2 | +import { |
| 3 | + PrismaClient, |
| 4 | + type User, |
| 5 | + type Post, |
| 6 | + type Prisma, |
| 7 | +} from "@prisma/client/index.js"; |
3 | 8 | import { withAccelerate } from "@prisma/extension-accelerate";
|
4 | 9 | import { withOptimize } from "@prisma/extension-optimize";
|
5 | 10 | import { PrismaAdapter } from "@auth/prisma-adapter";
|
6 | 11 | import { redirect } from "next/navigation";
|
| 12 | +import { styleText } from "util"; |
7 | 13 |
|
8 | 14 | const isProd = process.env.NODE_ENV === "production";
|
| 15 | +const isOptimizeMode = !!process.env.OPTIMIZE; |
| 16 | +const enableOptimize = !(isProd || !isOptimizeMode); |
9 | 17 |
|
10 |
| -const generateClient = () => { |
11 |
| - return isProd |
12 |
| - ? new PrismaClient().$extends(withAccelerate()) |
13 |
| - : new PrismaClient() |
14 |
| - .$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY! })) |
15 |
| - .$extends(withAccelerate()); |
16 |
| -}; |
| 18 | +export const prisma = remember("prisma", () => { |
| 19 | + const logThreshold = 20; |
17 | 20 |
|
18 |
| -export const prisma = remember("prisma", generateClient); |
| 21 | + async function queryOutput(e: Prisma.QueryEvent) { |
| 22 | + if (e.duration < logThreshold) return; |
| 23 | + const color = |
| 24 | + e.duration < logThreshold * 1.1 |
| 25 | + ? "green" |
| 26 | + : e.duration < logThreshold * 1.2 |
| 27 | + ? "blue" |
| 28 | + : e.duration < logThreshold * 1.3 |
| 29 | + ? "yellow" |
| 30 | + : e.duration < logThreshold * 1.4 |
| 31 | + ? "redBright" |
| 32 | + : "red"; |
| 33 | + const dur = styleText(color, `${e.duration}ms`); |
| 34 | + console.info(`prisma:query - ${dur} - ${e.query}`); |
| 35 | + } |
| 36 | + |
| 37 | + const client = new PrismaClient({ |
| 38 | + log: [ |
| 39 | + { level: "query", emit: "event" }, |
| 40 | + { level: "error", emit: "stdout" }, |
| 41 | + { level: "warn", emit: "stdout" }, |
| 42 | + ], |
| 43 | + }) |
| 44 | + .$on("query", queryOutput) |
| 45 | + .$extends( |
| 46 | + withOptimize({ |
| 47 | + enable: enableOptimize, |
| 48 | + apiKey: process.env.OPTIMIZE_API_KEY!, |
| 49 | + }) |
| 50 | + ) |
| 51 | + .$extends(withAccelerate()); |
| 52 | + |
| 53 | + client.$connect(); |
| 54 | + |
| 55 | + return client; |
| 56 | +}); |
19 | 57 |
|
20 | 58 | export const authAdapter = PrismaAdapter(prisma);
|
21 | 59 |
|
22 | 60 | export async function getUsersPosts(userId: string) {
|
23 | 61 | const user = await prisma.user.findUnique({
|
24 |
| - // cacheStrategy: { |
25 |
| - // swr: 120, |
26 |
| - // ttl: 120, |
27 |
| - // }, |
| 62 | + cacheStrategy: { |
| 63 | + swr: 120, |
| 64 | + ttl: 120, |
| 65 | + }, |
28 | 66 | where: { id: userId },
|
29 | 67 | select: {
|
30 | 68 | id: true,
|
@@ -60,10 +98,10 @@ export async function getPost(slug: string): Promise<
|
60 | 98 | > {
|
61 | 99 | try {
|
62 | 100 | const data = await prisma.post.findUnique({
|
63 |
| - cacheStrategy: { |
64 |
| - swr: 240, |
65 |
| - ttl: 240, |
66 |
| - }, |
| 101 | + // cacheStrategy: { |
| 102 | + // swr: 240, |
| 103 | + // ttl: 240, |
| 104 | + // }, |
67 | 105 | where: { slug },
|
68 | 106 | select: {
|
69 | 107 | id: true,
|
|
0 commit comments