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

Property '$on' is missing in type #65

Closed
JoshuaM1995 opened this issue Jun 25, 2023 · 8 comments
Closed

Property '$on' is missing in type #65

JoshuaM1995 opened this issue Jun 25, 2023 · 8 comments

Comments

@JoshuaM1995
Copy link

JoshuaM1995 commented Jun 25, 2023

I'm using a custom prisma client, and for some reason I'm getting TypeScript errors. They appear to be related to the prisma client type not matching the type in the package.

Here's my custom prisma module:

CustomPrismaModule.forRootAsync({
  name: 'PrismaService',
  useFactory: () => prismaClient,
  isGlobal: true,
}),

My custom prisma client:

import { PrismaClient } from '@prisma/database';
import { userSubscriptionIsActiveModelExtension, userSubscriptionIsActiveResultExtension } from './extensions';

export const prismaClient = new PrismaClient(
  process.env.NODE_ENV !== 'dev'
    ? {
        datasources: {
          db: {
            url: process.env.DATABASE_URL,
          },
        },
      }
    : undefined,
)
  .$extends(userSubscriptionIsActiveResultExtension)
  .$extends(userSubscriptionIsActiveModelExtension);

export type ExtendedPrismaClient = typeof prismaClient;

And here's the error:

Property '$on' is missing in type 'DynamicClientExtensionThis<TypeMap<Args & { result: { userSubscription: { isActive: () => { needs: { endsAt: true; isLifetime: true; paymentStatus: true; isOnFreeTrial: true; }; compute({ endsAt, isLifetime, paymentStatus, isOnFreeTrial }: { endsAt: Date | null; isLifetime: boolean; paymentStatus: StripePaymentStatu...' but required in type 'PrismaClientLike'.ts(2741)

When I comment out the $extends in the custom prisma client, the error goes away. I followed the documentation and it was working at one point. I'm not sure if one of the latest updates broke it? Here are my versions:

@prisma/client: 4.16.1
prisma: 4.16.1
nestjs-prisma: 0.20.1

@marcjulian
Copy link
Member

Hi @JoshuaM1995 have a look at the extensions example which uses $on with an extended PrismaClient

Extended Prisma Client

export const extendedPrismaClient = new PrismaClient<
Prisma.PrismaClientOptions,
'query' | 'info' | 'warn' | 'error'
>({
log: [
{ level: 'query', emit: 'event' },
{ level: 'info', emit: 'event' },
{ level: 'warn', emit: 'event' },
{ level: 'error', emit: 'event' },
],
}).$extends({

Use $on

customPrismaService.client.$on('error', (e) => {});

@marcjulian
Copy link
Member

I tested the extensions example with the latest prisma version 4.16.1 and I am seeing the same error as you:

src/prisma.extension.ts:25:22 - error TS2339: Property '$on' does not exist on type 'DynamicClientExtensionThis<TypeMap<Args & { result: {}; model: { user: { findByEmail: () => (email: string) => Promise<GetResult<{ id: string; email: string; name: string; }, { [x: string]: () => unknown; }> & {}>; }; }; query: {}; client: {}; }>, TypeMapCb, { ...; }>'.

25 extendedPrismaClient.$on('query', (e) => {});

This might be an issue with the latest prisma version. Either $on is missing in the types or it is not available any longer for extended PrismaClients.

@marcjulian
Copy link
Member

marcjulian commented Jun 26, 2023

@JoshuaM1995 I found the following issue about disallowing $on for extended clients: prisma/prisma#19552 (comment)

You can find it also in the release notes from 4.16.0

CleanShot 2023-06-26 at 10 40 54

@JoshuaM1995
Copy link
Author

@JoshuaM1995 I found the following issue about disallowing $on for extended clients: prisma/prisma#19552 (comment)

You can find it also in the release notes from 4.16.0

CleanShot 2023-06-26 at 10 40 54

Thanks for looking into this. Are there any plans for supporting Prisma 4.16.0 any time soon? I can help contribute if possible.

If not I can try downgrading. That was the first thing I tried, but even after downgrading @prisma/client and prisma in my package.json files (I'm in a monorepo), I still see it generating the client with 4.16.0. I even tried using npx to generate it with a specific version of the cli, with no luck.

@JoshuaM1995
Copy link
Author

Also, maybe it would be safer to lock prisma to a patch version in the package.json? @marcjulian

"@prisma/client": "~4.13.0"

@JoshuaM1995
Copy link
Author

Update: I was able to solve this temporarily with PNPM by adding this to my package.json:

"pnpm": {
    "overrides": {
      "@prisma/client": "4.15.0",
      "prisma": "4.15.0"
    }
  }

marcjulian added a commit that referenced this issue Jul 3, 2023
- $on is not available for extended PrismaClients
@marcjulian
Copy link
Member

@JoshuaM1995 I have update it to the latest prisma client and added support for extension GA. Please try it out with the latest dev release:

npm i nestjs-prisma@0.21.0-dev.3

@marcjulian
Copy link
Member

This has been released with 0.21.0

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

No branches or pull requests

2 participants