wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.
https://wasp-lang.dev
MIT License
12.7k stars 1.13k forks source link

Prisma setup hook #2115

Closed infomiho closed 1 week ago

infomiho commented 1 week ago

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
infomiho commented 1 week ago

Duplicate