Skip to content

Commit

Permalink
feat(web): add planetscale db driver for prisma (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt authored Oct 11, 2023
1 parent 9fae556 commit e63fab5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
8 changes: 5 additions & 3 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"@monaco-editor/react": "^4.5.1",
"@next-auth/prisma-adapter": "1.0.5",
"@next/mdx": "13.3.4",
"@prisma/client": "5.2.0-integration-fix-enums-browser.1",
"@planetscale/database": "^1.11.0",
"@prisma/adapter-planetscale": "5.4.2",
"@prisma/client": "5.4.2",
"@radix-ui/react-avatar": "^1.0.2",
"@radix-ui/react-dropdown-menu": "^2.0.4",
"@radix-ui/react-navigation-menu": "^1.1.3",
Expand Down Expand Up @@ -123,7 +125,7 @@
"postcss": "^8.4.21",
"prettier": "^2.8.7",
"prettier-plugin-tailwindcss": "^0.1.13",
"prisma": "5.2.0-integration-fix-enums-browser.1",
"prisma": "5.4.2",
"tailwindcss": "^3.3.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
Expand All @@ -132,4 +134,4 @@
"ct3aMetadata": {
"initVersion": "6.11.1"
}
}
}
3 changes: 2 additions & 1 deletion apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/env/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const serverSchema = z.object({
GOOGLE_CLIENT_ID: z.string().optional(),
GOOGLE_CLIENT_SECRET: z.string().optional(),
HASHING_SECRET: z.string().min(1),
USE_PLANETSCALE: z.boolean().default(true),
});

/**
Expand Down
15 changes: 12 additions & 3 deletions apps/web/src/server/db/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PrismaClient } from "@prisma/client";
import { connect } from "@planetscale/database";
import { PrismaPlanetScale } from "@prisma/adapter-planetscale";

import { env } from "../../env/server.mjs";

Expand All @@ -9,9 +11,16 @@ declare global {

export const prisma =
global.prisma ||
new PrismaClient({
log: ["error"],
});
// enable planetscale adapter in production if enabled
(env.NODE_ENV === "production" && env.USE_PLANETSCALE
? (() => {
const connection = connect({ url: env.DATABASE_URL });
const adapter = new PrismaPlanetScale(connection);
return new PrismaClient({ adapter, log: ["error"] });
})()
: new PrismaClient({
log: ["error"],
}));

if (env.NODE_ENV !== "production") {
global.prisma = prisma;
Expand Down
66 changes: 48 additions & 18 deletions pnpm-lock.yaml

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

1 comment on commit e63fab5

@vercel
Copy link

@vercel vercel bot commented on e63fab5 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.