Support serverless deployment#11
Conversation
| ], | ||
| prisma: { | ||
| client: prisma, | ||
| dmmf: getDatamodel(), |
There was a problem hiding this comment.
I believe this is safe to add regardless of deployment runtime. cc @hayes correct me if I'm wrong
There was a problem hiding this comment.
According to https://pothos-graphql.dev/docs/plugins/prisma/setup#set-up-the-builder:
This give pothos information about your tables, relations, and indexes to help it generate optimal queries at runtime. This used to be attached to the prisma client, but has been removed in most runtimes/modes to reduce bundle size.
| import { PrismaPg } from '@prisma/adapter-pg'; | ||
| import { PrismaClient } from './prisma-client/client.ts'; | ||
|
|
||
| const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL }); |
There was a problem hiding this comment.
| importFileExtension = "ts" | ||
| output = "./prisma-client" | ||
| provider = "prisma-client" | ||
| engineType = "client" |
There was a problem hiding this comment.
| if (process.env.npm_lifecycle_event === 'dev') { | ||
| try { | ||
| await prisma.$connect(); | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error( | ||
| `${styleText(['red', 'bold'], 'Prisma Database Connection Error')}\n`, | ||
| error, | ||
| ); | ||
| process.exit(1); | ||
| } |
There was a problem hiding this comment.
Interesting! This is actually purely for prod since you want to fail fast when the db is not available in any other hosting scenario.
There was a problem hiding this comment.
I see. Lemme move this out.
|
This is pretty great! I'm inclined to merge this (and would appreciate any details about deployment in the README too, since it's something people have asked for). I'm a bit scared about switching to the new Prisma engine since when it first launched it was super broken. I have not migrated any projects to it yet, but I assume it is stable enough for new projects? |
Sure, will add some guidance around deployment in the README
I haven't used the new engine for any serious projects yet, but it works for this repo at least (tested live on Vercel) 🤣 |
|
Updated the PR |
|
I separated the app server from dev server here: 3a37572 |
Main changes necessary to support deployment to Vercel:
process.env.npm_lifecycle_event === 'dev'block so they don't run in a serverless function environmentengineType = "client"toschema.prismaas this mode doesn't require the engine binaries, but you'll need to use a pg driver adapter. Docs: https://www.prisma.io/docs/orm/prisma-client/deployment/serverless/deploy-to-vercel@pothos-corewhen Vercel tries to run the raw entry file. Selectdistas the output directory in the Vercel projectdmmf: getDatamodel()toprismaoptions when initializing the PothosSchemaBuilder. I believe this is needed because of theengineType = "client"changepnpm dev:setupas apostinstallcommand so that the generated files will be presentWhile this PR works, it contains quite a bit of changes and you might have opinions on how they're done, so feel free to use it as a reference and implement it the way you like.