From 3cffed1065a3948ed04ec21fe0834a3560b97fd4 Mon Sep 17 00:00:00 2001 From: myfunc Date: Thu, 27 Jun 2024 13:06:45 +0000 Subject: [PATCH] replace executeIsolated with PrismaTransactional.prismaRoot --- README.md | 10 ++++++---- examples/express/index.ts | 6 +++--- package.json | 2 +- src/core.ts | 2 +- src/decorator.ts | 19 +++++++++---------- src/manager.ts | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 8d6fcb1..d96773e 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,11 @@ Execute a query out of current transaction context. const { balance } = await this.getBalance(userId); // userLog item will be created even if current transaction will be rolled back. - await PrismaTransactional.executeIsolated(async (rootClient: PrismaClient) => { - await rootClient.userLog.create({ note: `Attempt to add balance for user ${userId} with balance ${balance}`}); - }); + await PrismaTransactional.prismaRoot.userLog.create( + { + note: `Attempt to add balance for user ${userId} with balance ${balance}` + } + ); const newBalance = await this.prisma.user.update({ select: { @@ -147,7 +149,7 @@ Execute a query out of current transaction context. ## Plans -- [x] Add `PrismaTransactional.executeIsolated` method for running queries out of transaction context. +- [x] Add `PrismaTransactional.prismaRoot` method for running queries out of transaction context. - [ ] Add tests. - [x] Add express.js example. - [ ] Add nestjs example. diff --git a/examples/express/index.ts b/examples/express/index.ts index 26a7a1f..d8eeb1c 100644 --- a/examples/express/index.ts +++ b/examples/express/index.ts @@ -133,11 +133,11 @@ class PostService { console.log('txCreate30PostsAndThrow PrismaTransactional.onSuccess'); }); await this.postRepository.createPost({ title: 'Not existing post' }); - await PrismaTransactional.executeIsolated(async (client: PrismaClient) => { - await client.post.create({ data: { title: 'Isolated post' } }); - }); + await PrismaTransactional.prismaRoot.post.create({ data: { title: 'Isolated post' } }); await WaitAsync(100); + PrismaTransactional; + this.throwError(); } diff --git a/package.json b/package.json index e7ad124..a44b4a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@myfunc/prisma-transactional", - "version": "0.2.0", + "version": "0.3.0", "author": "myfunc", "description": "Decorator that wraps all prisma queries along the whole call stack to a single transaction.", "license": "MIT", diff --git a/src/core.ts b/src/core.ts index 06b8594..bc302c8 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,4 +1,4 @@ -import { Prisma, PrismaClient } from '@prisma/client/extension'; +import { Prisma, PrismaClient } from '@prisma/client'; import { ClsService, ClsServiceManager } from 'nestjs-cls'; import { TX_CLIENT_KEY, TX_CLIENT_SUCCESS_CALLBACKS } from './const'; import { Manager } from './manager'; diff --git a/src/decorator.ts b/src/decorator.ts index d84de7f..b8b7712 100644 --- a/src/decorator.ts +++ b/src/decorator.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client/extension'; +import { PrismaClient } from '@prisma/client'; import { ClsService, ClsServiceManager } from 'nestjs-cls'; import { TX_CLIENT_KEY, TRANSACTION_TIMEOUT, TX_CLIENT_SUCCESS_CALLBACKS } from './const'; import { Manager } from './manager'; @@ -25,7 +25,7 @@ export function PrismaTransactional(isolationLevel?: string): MethodDecorator { } // Utility to manage success callback queue -PrismaTransactional.onSuccess = (callback: () => void | Promise) => { +PrismaTransactional.onSuccess = (callback: () => T | Promise): T | Promise | undefined => { const clsService = ClsServiceManager.getClsService(); const isActiveTransaction = clsService.get(TX_CLIENT_KEY); @@ -58,11 +58,10 @@ PrismaTransactional.execute = ( } }; -// Run callback with isolated PrismaClient with no transaction. -// Only for parameter prismaClient, if other client will be used it will be executed in transaction if exists -PrismaTransactional.executeIsolated = ( - callback: (prismaClient: PrismaClient) => Promise, -): Promise => { - const prismaRoot = Manager.prismaClient['$root'] as PrismaClient; - return callback(prismaRoot); -}; +PrismaTransactional.prismaRoot = null as unknown as PrismaClient; +// Run query with no transaction even if it exists. +Object.defineProperty(PrismaTransactional, 'prismaRoot', { + get(): PrismaClient { + return Manager.prismaClient['$root'] as PrismaClient; + }, +}); diff --git a/src/manager.ts b/src/manager.ts index 8e96a73..76b4b2e 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client/extension'; +import { PrismaClient } from '@prisma/client'; import { ILoggerService, PrismaTransactionalConfig } from './type'; import { ConsoleLogger } from './logger/console-logger'; import { EmptyLogger } from './logger/empty-logger';