Skip to content

Prisma setup hook #2116

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
infomiho opened this issue Jun 24, 2024 · 2 comments · May be fixed by #2693
Open

Prisma setup hook #2116

infomiho opened this issue Jun 24, 2024 · 2 comments · May be fixed by #2693
Assignees
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
@infomiho
Copy link
Contributor Author

infomiho commented Sep 3, 2024

After we migrate to Prisma v5, this would be also useful to set up serverless DB adapters e.g. for Neon DB: https://www.prisma.io/docs/orm/overview/databases/neon#how-to-use-neons-serverless-driver-with-prisma-orm-preview

It requires from users to install certain adapters and then use them when initing the PrismaClient:

import { Pool, neonConfig } from '@neondatabase/serverless'
import { PrismaNeon } from '@prisma/adapter-neon'
import { PrismaClient } from '@prisma/client'
import dotenv from 'dotenv'
import ws from 'ws'

dotenv.config()
neonConfig.webSocketConstructor = ws
const connectionString = `${process.env.DATABASE_URL}`

const pool = new Pool({ connectionString })
const adapter = new PrismaNeon(pool)
const prisma = new PrismaClient({ adapter })

@infomiho
Copy link
Contributor Author

infomiho commented Apr 7, 2025

@infomiho infomiho self-assigned this Apr 7, 2025
@infomiho infomiho linked a pull request Apr 23, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant