#custom prisma method not recognized by TS

1 messages · Page 1 of 1 (latest)

main saffron
#

I would assume that you need to use the returned prisma client when extending it. Otherwise you can always declaration merge your types but they don't mention it in their docs so I'm confident that the former is what you need to do.

winged nacelle
#

At this time i'm really tired and already shut down for today
So i cant share any code atm from it, will have to do that tomorow

But this custom method defined directly in the prisma service

#

I'm going to need some help with this as it been days

main saffron
#

I am confident that when you use .$extends() on the client that the return value will be the correct type

#

so you need to either extract that type or reference the created instance directly.

winged nacelle
main saffron
#

Yeah well I dont see how thats going to work because typescript has no way of knowing that you extended the prisma client and changed it.

#
const client = new PrismaClient()
  .$extends({...})

client
  .users
  .findById(...)

so you could use typeof client wherever you inject it to get the correct type

#

but then again, and we discussed this last week, I dont consider this a solution to your "problem"

#

if you want an active record then just go typeorm or mikroorm

winged nacelle
main saffron
#

everything is possible but you are ducttaping something that shouldnt be ducttaped

winged nacelle
#

i really need some guide as my head is overloaded at this point

main saffron
#

I literally gave you an example?

#

But then again... I wouldnt do this and im done discussing it

winged nacelle
winged nacelle
# main saffron

i have to apologize for being brain dead sometimes 2iq
i can be slow to pickup things i struggle to understand, you should know this by now already from TPH 😉
it seems even prisma uses this method even in their docs

Prisma

Extend the functionality of Prisma Client

winged nacelle
#

so how would your suggestion apply in a situation like this?

@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit {
  private readonly logger = new Logger(DatabaseService.name)

  constructor() {
    super()
    // adds a prisma.model.findById(<number>) method
    // how can i create a out of this and be exported?
    this.$extends({
      model: {
        $allModels: {
          async findById<T, A>(
            this: T,
            id: Prisma.Exact<A, Prisma.Args<T, 'findUniqueOrThrow'['where']>,
            ): Promise<Prisma.Result<T, A, 'findUniqueOrThrow'>> {
            const context = Prisma.getExtensionContext(this)
            const result = await (context as any).findUniqueOrThrow({ where:                { id } })
            return result
          },
        },
      },
    })
  }
}
@Injectable()
export class AccountService {
  readonly logger = new Logger(AccountService.name)

  constructor(
    private readonly emailService: EmailService,
    private config: ApiConfigService,
    private readonly tokenService: TokenService,
    // use this export type here?
    private readonly prisma: DatabaseService,
  ) {}

async findById(id: number, includeUser = false): Promise<AccountUserRolesBundle | Account> {
    if (!includeUser) {
      const account = (await this.prisma.account.findById(id)) as Account
      if (!account) throw new NotFoundException()
      return account as Account
    } else {
      const account = await this.prisma.account.findUnique({
        where: { id },
        include: {
          user: {
            include: { roles: { select: { role: { select: { name: true,                    priority: true, permission: true } } } } },
          },
        },
      })
      if (!account) throw new NotFoundException()
      return account
    }
  }
}