Code Monkey home page Code Monkey logo

prisma-unit-of-work's Introduction

🔭 I’m currently learning Blockchain

Activity

Experience work

Tinkoff LeroyMerlin X5 Group MTS

Top Langs

Skills

Rust Kubernetes Typescript JavaScript PostgreSQL NodeJS MongoDB Docker Redis

Contact

LinkedIn

prisma-unit-of-work's People

Contributors

zhuravlevma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

prisma-unit-of-work's Issues

Transaction scope can work with nest-cls

Hi @zhuravlevma , Thank you for sharing this amazing piece of work with us. You have helped me crack unit of work with Prisma.

I opened this issue to let you know that your first approach with cls-hooked also works with a few modifications. You would need to add nestjs-cls and enable it globally and we can easily share the prisma transaction object without the need to pass it as an arg into each repository.

I'm sharing some of the code here for your reference.

export abstract class UnitOfWork { abstract runInTransaction<R>(fn: () => Promise<R>): Promise<R>; }

`import { ClsService, Terminal } from "nestjs-cls";

@Injectable({ scope: Scope.REQUEST })
export class PrismaTransactionScope implements UnitOfWork {
constructor(
private readonly prisma: PrismaService,
private readonly cls: ClsService<{
prisma: Terminal<Prisma.TransactionClient>;
}>,
) {}

get manager() {
return this.cls.get("prisma");
}

async runInTransaction(fn: () => Promise): Promise {
return await this.prisma.$transaction(async (manager) => {
this.cls.set("prisma", manager);
return await fn();
});
}
}`

`import { ClsModule } from "nestjs-cls";

@module({
imports: [
ClsModule.forRoot({ global: true, middleware: { mount: true } }),
],
controllers: [AppController],
})
export class AppModule {}`

Kindly let me know if it works for you.

Regards,
Atmeshwar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.