Skip to content

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Adds Prisma setup fn #2693

wants to merge 8 commits into from

Conversation

infomiho
Copy link
Contributor

@infomiho infomiho commented Apr 23, 2025

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:

  • Investigate if we need to adjust how we use the PrismaClient type (maybe we will need to infer the type from the user returned PrismaClient)
  • Docs
  • Add a test (probably headless test)
  • Update the Typescript Spec

Closes #2116

@infomiho infomiho marked this pull request as ready for review April 25, 2025 12:45
@infomiho infomiho requested review from cprecioso and sodic April 25, 2025 12:45
Comment on lines +8 to +13
{=# prismaSetupFn.isDefined =}
const _waspSetupPrisma = {= prismaSetupFn.importIdentifier =}
{=/ prismaSetupFn.isDefined =}
{=^ prismaSetupFn.isDefined =}
const _waspSetupPrisma = () => new PrismaClient()
{=/ prismaSetupFn.isDefined =}
Copy link
Contributor Author

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
Copy link
Contributor Author

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

Comment on lines +23 to +25
// PUBLIC API
// TODO: We can't use this type becuase prisma.$extends() doesn't return a PrismaClient?
export type PrismaSetupFn = () => PrismaClient
Copy link
Contributor Author

@infomiho infomiho Apr 25, 2025

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:

  1. we usually provided it and it enabled users to have some type safety (it would make sense to include it now as well)
  2. 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`
Copy link
Contributor Author

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 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prisma setup hook
1 participant