Skip to content

Commit 0ad98b3

Browse files
authored
Merge pull request #38 from danielvanc/upgrade-node-and-enable-prisma-logging
Re-add prisma logging support
2 parents f6cd4fb + fdd728c commit 0ad98b3

File tree

4 files changed

+78
-27
lines changed

4 files changed

+78
-27
lines changed

package-lock.json

+20-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev --turbopack",
7+
"dev:opt": "cross-env OPTIMIZE=true next dev --turbopack",
78
"build": "npx prisma generate --no-engine && next build",
89
"start": "next start",
910
"lint": "next lint",
@@ -35,6 +36,7 @@
3536
"@tiptap/starter-kit": "^2.11.5",
3637
"class-variance-authority": "^0.7.1",
3738
"clsx": "^2.1.1",
39+
"cross-env": "^7.0.3",
3840
"lucide-react": "^0.475.0",
3941
"next": "^15.2.1",
4042
"next-auth": "^5.0.0-beta.25",

utils/db.ts

+55-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
11
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";
38
import { withAccelerate } from "@prisma/extension-accelerate";
49
import { withOptimize } from "@prisma/extension-optimize";
510
import { PrismaAdapter } from "@auth/prisma-adapter";
611
import { redirect } from "next/navigation";
12+
import { styleText } from "util";
713

814
const isProd = process.env.NODE_ENV === "production";
15+
const isOptimizeMode = !!process.env.OPTIMIZE;
16+
const enableOptimize = !(isProd || !isOptimizeMode);
917

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;
1720

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+
});
1957

2058
export const authAdapter = PrismaAdapter(prisma);
2159

2260
export async function getUsersPosts(userId: string) {
2361
const user = await prisma.user.findUnique({
24-
// cacheStrategy: {
25-
// swr: 120,
26-
// ttl: 120,
27-
// },
62+
cacheStrategy: {
63+
swr: 120,
64+
ttl: 120,
65+
},
2866
where: { id: userId },
2967
select: {
3068
id: true,
@@ -60,10 +98,10 @@ export async function getPost(slug: string): Promise<
6098
> {
6199
try {
62100
const data = await prisma.post.findUnique({
63-
cacheStrategy: {
64-
swr: 240,
65-
ttl: 240,
66-
},
101+
// cacheStrategy: {
102+
// swr: 240,
103+
// ttl: 240,
104+
// },
67105
where: { slug },
68106
select: {
69107
id: true,

utils/posts.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export function formatValue(value: string) {
4040
return generateHTML(parsedValue, [StarterKit]);
4141
}
4242
return value; // Return as-is if parsed but not proper doc format
43-
} catch (error) {
44-
console.log("error", error);
45-
// If parsing fails, it's not JSON, so return the original value
43+
} catch {
4644
return value;
4745
}
4846
}

0 commit comments

Comments
 (0)