Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add prisma logging support #38

Merged
merged 1 commit into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev --turbopack",
"dev:opt": "cross-env OPTIMIZE=true next dev --turbopack",
"build": "npx prisma generate --no-engine && next build",
"start": "next start",
"lint": "next lint",
Expand Down Expand Up @@ -35,6 +36,7 @@
"@tiptap/starter-kit": "^2.11.5",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cross-env": "^7.0.3",
"lucide-react": "^0.475.0",
"next": "^15.2.1",
"next-auth": "^5.0.0-beta.25",
Expand Down
72 changes: 55 additions & 17 deletions utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,68 @@
import { remember } from "@epic-web/remember";
import { PrismaClient, type User, type Post } from "@prisma/client";
import {
PrismaClient,
type User,
type Post,
type Prisma,
} from "@prisma/client/index.js";
import { withAccelerate } from "@prisma/extension-accelerate";
import { withOptimize } from "@prisma/extension-optimize";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { redirect } from "next/navigation";
import { styleText } from "util";

const isProd = process.env.NODE_ENV === "production";
const isOptimizeMode = !!process.env.OPTIMIZE;
const enableOptimize = !(isProd || !isOptimizeMode);

const generateClient = () => {
return isProd
? new PrismaClient().$extends(withAccelerate())
: new PrismaClient()
.$extends(withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY! }))
.$extends(withAccelerate());
};
export const prisma = remember("prisma", () => {
const logThreshold = 20;

export const prisma = remember("prisma", generateClient);
async function queryOutput(e: Prisma.QueryEvent) {
if (e.duration < logThreshold) return;
const color =
e.duration < logThreshold * 1.1
? "green"
: e.duration < logThreshold * 1.2
? "blue"
: e.duration < logThreshold * 1.3
? "yellow"
: e.duration < logThreshold * 1.4
? "redBright"
: "red";
const dur = styleText(color, `${e.duration}ms`);
console.info(`prisma:query - ${dur} - ${e.query}`);
}

const client = new PrismaClient({
log: [
{ level: "query", emit: "event" },
{ level: "error", emit: "stdout" },
{ level: "warn", emit: "stdout" },
],
})
.$on("query", queryOutput)
.$extends(
withOptimize({
enable: enableOptimize,
apiKey: process.env.OPTIMIZE_API_KEY!,
})
)
.$extends(withAccelerate());

client.$connect();

return client;
});

export const authAdapter = PrismaAdapter(prisma);

export async function getUsersPosts(userId: string) {
const user = await prisma.user.findUnique({
// cacheStrategy: {
// swr: 120,
// ttl: 120,
// },
cacheStrategy: {
swr: 120,
ttl: 120,
},
where: { id: userId },
select: {
id: true,
Expand Down Expand Up @@ -60,10 +98,10 @@ export async function getPost(slug: string): Promise<
> {
try {
const data = await prisma.post.findUnique({
cacheStrategy: {
swr: 240,
ttl: 240,
},
// cacheStrategy: {
// swr: 240,
// ttl: 240,
// },
where: { slug },
select: {
id: true,
Expand Down
4 changes: 1 addition & 3 deletions utils/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export function formatValue(value: string) {
return generateHTML(parsedValue, [StarterKit]);
}
return value; // Return as-is if parsed but not proper doc format
} catch (error) {
console.log("error", error);
// If parsing fails, it's not JSON, so return the original value
} catch {
return value;
}
}