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

Prisma setup hook #2116

Open
infomiho opened this issue Jun 24, 2024 · 0 comments
Open

Prisma setup hook #2116

infomiho opened this issue Jun 24, 2024 · 0 comments
Labels

Comments

@infomiho
Copy link
Contributor

Prisma supports client extensions: https://www.prisma.io/docs/orm/prisma-client/client-extensions which for:

You can use Prisma Client extensions to add functionality to your models, result objects, and queries, or to add client-level methods.

You can create an extension with one or more of the following component types:

model: add custom methods or fields to your models
client: add client-level methods to Prisma Client
query: create custom Prisma Client queries
result: add custom fields to your query results

Users can't do this with the existing Wasp setup since there is no way for them to give Wasp a new Prisma client to use throughout the app.

That's why having a Prisma setup hook that could influence which Prisma client instance Wasp uses is useful.

Implementation

It would probably be a db.prismaSetup hook whose implementation could look like this:

export function setupPrisma(prisma: PrismaClient): PrismaClient {
  // An example from Prisma docs
  return prisma.$extends({
    query: {
      user: {
        async findMany({ model, operation, args, query }) {
          // take incoming `where` and set `age`
          args.where = { ...args.where, age: { gt: 18 } }
  
          return query(args)
        },
      },
    },
  })
}

// Somewhere in the codebase
await prisma.user.findMany() // returns users whose age is greater than 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant