Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
389 stars 23 forks source link

How to use with extended client? #125

Closed darr1s closed 4 months ago

darr1s commented 4 months ago

Hi! Thanks for the lib.

The question is as title. I have extended client (available in ts, or as nest service)

Papooch commented 4 months ago

Hi, I am not exactly sure which client or library you're referring to, as this GitHub repository is a monorepo with multiple nestjs-cls related libs, but from the working, I'm going to assume you're talking about an extended Prisma client and the library @nestjs-cls/transactional-adapter-prisma.

Unfortunately, it is not (easily) possible to use an extended client (that is, an another type than the one imported from @prisma/client) with the prisma adapter. I'm planning to add that option to the library, but for the time being, to achieve what you want, you'll have to create a custom adapter.

Fortunately, it is not that hard as the entire source-code of the prisma adapter can be found here

And basically the only thing that needs changing is the import at the very top(and the related uses) from import { PrismaClient } from '@prisma/client' to import { MyPrismaClient } from '../my/prisma-client.

Also please keep in mind, that since the propagated transaction object is the one that gets passed to prisma.$transaction(( THIS_ONE ) => ...), if you use a custom service that extends the PrismaClient with new class methods, those won't be available on the transaction object.

Papooch commented 4 months ago

It it now possible to use a custom Prisma Client type since @nestjs-cls/transactional-adapter-prisma v1.1.0. Please see updated docs.