Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.

Home Page:https://papooch.github.io/nestjs-cls/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use with extended client?

darr1s opened this issue · comments

Hi! Thanks for the lib.

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

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.

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