-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Adds Prisma setup fn #2693
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
base: main
Are you sure you want to change the base?
Adds Prisma setup fn #2693
Conversation
Signed-off-by: Mihovil Ilakovac <mihovil@ilakovac.com>
{=# prismaSetupFn.isDefined =} | ||
const _waspSetupPrisma = {= prismaSetupFn.importIdentifier =} | ||
{=/ prismaSetupFn.isDefined =} | ||
{=^ prismaSetupFn.isDefined =} | ||
const _waspSetupPrisma = () => new PrismaClient() | ||
{=/ prismaSetupFn.isDefined =} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either we use the default init, or the user defined init.
@@ -19,5 +32,8 @@ function createDbClient(): null { | |||
|
|||
const dbClient = createDbClient() | |||
|
|||
// PUBLIC API | |||
export type ResolvedPrismaClient = typeof dbClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user defined init fn will return a type with more type info than just the plain PrismaClient
// PUBLIC API | ||
// TODO: We can't use this type becuase prisma.$extends() doesn't return a PrismaClient? | ||
export type PrismaSetupFn = () => PrismaClient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence if it makes sense to provide this type:
- we usually provided it and it enabled users to have some type safety (it would make sense to include it now as well)
- the problem is that if the user extends the client with client extensions for some reason the returned type is incompatible with
PrismaClient
const prisma = new PrismaClient({
log: ['query'],
}).$extends({
query: {
task: {
async findMany({ args, query }) {
args.where = {
...args.where,
description: { not: { contains: 'hidden by setupPrisma' } },
}
return query(args)
},
},
},
})
// This is an type error ❌
const someOtherObj: PrismaClient = prisma
await page.goto('/') | ||
|
||
// Create a new task that will be hidden by the Prisma setup function | ||
const specificTask = `hidden by setupPrisma` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We filter out all the tasks that contain this string. This enables us to test out the Prisma setup fn with headless tests. I couldn't come up with something better 🤷
This PR enables users to override the default
PrismaClient
init and setup e.g. logging of SQL that Prisma is sending to the DB. It's also useful to add Prisma client extensions.Left to do:
PrismaClient
type (maybe we will need to infer the type from the user returnedPrismaClient
)Closes #2116