🔭 I’m currently learning Blockchain
zhuravlevma / prisma-unit-of-work Goto Github PK
View Code? Open in Web Editor NEWDDD, Unit of work pattern for Typescript, Nest.js, Prisma ⚡
License: MIT License
DDD, Unit of work pattern for Typescript, Nest.js, Prisma ⚡
License: MIT License
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.