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

Extension's $connect method is not used by Prisma query methods unless it's invoked explicitly #23124

Open
nicolo-tito opened this issue Feb 14, 2024 · 3 comments
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/improvement An improvement to existing feature and code. topic: clientExtensions topic: connect

Comments

@nicolo-tito
Copy link

nicolo-tito commented Feb 14, 2024

Bug description

When using a Prisma extension with a client $connect method, this is not invoked by the automatic connection of query methods (findFirst, findMany etc...).
It is invoked only by explicitly calling prismaExtension.$connect().

How to reproduce

const prisma = new PrismaClient()

const prismaExtension = prisma.$extends({
  client: {
    async $connect() {
      console.warn('Connecting to database')
      await prisma.$connect()
    }
  }
})

prismaExtension.user.findFirst() // Won't log 'Connecting to database'
// prismaExtension.$connect() // Will log 'Connecting to database'

Expected behavior

Prisma query methods should use the provided extension $connect method on automatic connection

Prisma information

const prisma = new PrismaClient()

const prismaExtension = prisma.$extends({
  client: {
    async $connect() {
      console.warn('Connecting to database')
      await prisma.$connect()
    }
  }
})

prismaExtension.user.findFirst() // Won't log 'Connecting to database'
// prismaExtension.$connect() // Will log 'Connecting to database'

Environment & setup

Operating System:
Platform: win32
Arch: x64
Version: Windows 11 Pro
Binaries:
Node: 20.11.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 14.1.0
react: 18.2.0
react-dom: 18.2.0
typescript: 5.3.3
Next.js Config:
output: standalone
Database: SQL Server

Prisma Version

5.9.1

@nicolo-tito nicolo-tito added the kind/bug A reported bug. label Feb 14, 2024
@nicolo-tito

This comment was marked as outdated.

@SevInf
Copy link
Member

SevInf commented Feb 14, 2024

This is generally true for non-extended $connect too - executing request will establish the connection to the database, but it will not do so by calling the same public $connect method. What is a usecase for overriding $connect? Generally, client extensions are meant for additions on top of public client API, not for altering internal behaviours.

@nicolo-tito
Copy link
Author

nicolo-tito commented Feb 14, 2024

In my case, I was just testing more effective ways to send connection exceptions to a logging service, which in the end I did by just leveraging the $on error event.
I opened the bug anyway because I noticed some official Prisma extensions do override $connect in some cases (e.g. https://github.com/prisma/extension-read-replicas).

@jkomyno jkomyno added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: clientExtensions labels Feb 14, 2024
@janpio janpio changed the title Extensions $connect method is not used by Prisma query methods unless it's invoked explicitly Extensions $connect method is not used by Prisma query methods unless it's invoked explicitly Feb 14, 2024
@Weakky Weakky added kind/improvement An improvement to existing feature and code. topic: connect and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. labels May 16, 2024
@janpio janpio changed the title Extensions $connect method is not used by Prisma query methods unless it's invoked explicitly Extension's $connect method is not used by Prisma query methods unless it's invoked explicitly Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/improvement An improvement to existing feature and code. topic: clientExtensions topic: connect
Projects
None yet
Development

No branches or pull requests

4 participants