Skip to content

Commit 85a0525

Browse files
authored
fix: bug in enhancement proxy for detecting nested transactions (#941)
1 parent 9e840ea commit 85a0525

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

packages/runtime/src/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ export enum PrismaErrorCode {
5353
DEPEND_ON_RECORD_NOT_FOUND = 'P2025',
5454
}
5555

56-
/**
57-
* Field name for storing in-transaction flag
58-
*/
59-
export const PRISMA_TX_FLAG = '$__zenstack_tx';
60-
6156
/**
6257
* Field name for getting current enhancer
6358
*/

packages/runtime/src/enhancements/policy/handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { lowerCaseFirst } from 'lower-case-first';
44
import invariant from 'tiny-invariant';
55
import { upperCaseFirst } from 'upper-case-first';
66
import { fromZodError } from 'zod-validation-error';
7-
import { CrudFailureReason, PRISMA_TX_FLAG } from '../../constants';
7+
import { CrudFailureReason } from '../../constants';
88
import {
99
ModelDataVisitor,
1010
NestedWriteVisitor,
@@ -1232,11 +1232,11 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
12321232
}
12331233

12341234
private transaction(action: (tx: Record<string, DbOperations>) => Promise<any>) {
1235-
if (this.prisma[PRISMA_TX_FLAG]) {
1235+
if (this.prisma['$transaction']) {
1236+
return this.prisma.$transaction((tx) => action(tx), { maxWait: 100000, timeout: 100000 });
1237+
} else {
12361238
// already in transaction, don't nest
12371239
return action(this.prisma);
1238-
} else {
1239-
return this.prisma.$transaction((tx) => action(tx), { maxWait: 100000, timeout: 100000 });
12401240
}
12411241
}
12421242

packages/runtime/src/enhancements/proxy.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22

3-
import { PRISMA_PROXY_ENHANCER, PRISMA_TX_FLAG } from '../constants';
3+
import { PRISMA_PROXY_ENHANCER } from '../constants';
44
import type { ModelMeta } from '../cross';
55
import type { DbClientContract } from '../types';
66
import { createDeferredPromise } from './policy/promise';
@@ -183,6 +183,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
183183
errorTransformer?: ErrorTransformer
184184
) {
185185
const models = Object.keys(modelMeta.fields).map((k) => k.toLowerCase());
186+
186187
const proxy = new Proxy(prisma, {
187188
get: (target: any, prop: string | symbol, receiver: any) => {
188189
// enhancer metadata
@@ -191,7 +192,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
191192
}
192193

193194
if (prop === 'toString') {
194-
return () => `$zenstack_${name}[${target.toString()}]`;
195+
return () => `$zenstack_prisma_${prisma._clientVersion}`;
195196
}
196197

197198
if (prop === '$transaction') {
@@ -213,8 +214,10 @@ export function makeProxy<T extends PrismaProxyHandler>(
213214

214215
const txFunc = input;
215216
return $transaction.bind(target)((tx: any) => {
217+
// create a proxy for the transaction function
216218
const txProxy = makeProxy(tx, modelMeta, makeHandler, name + '$tx');
217-
txProxy[PRISMA_TX_FLAG] = true;
219+
220+
// call the transaction function with the proxy
218221
return txFunc(txProxy);
219222
}, ...rest);
220223
};

tests/integration/tests/enhancements/with-policy/postgres.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ describe('With Policy: with postgres', () => {
1616

1717
beforeEach(async () => {
1818
dbUrl = await createPostgresDb(DB_NAME);
19-
const { prisma: _prisma, withPolicy } = await loadSchemaFromFile(
19+
const { prisma: _prisma, enhance } = await loadSchemaFromFile(
2020
path.join(__dirname, '../../schema/todo-pg.zmodel'),
2121
{
2222
provider: 'postgresql',
2323
dbUrl,
2424
}
2525
);
26-
getDb = withPolicy;
26+
getDb = enhance;
2727
prisma = _prisma;
2828
});
2929

0 commit comments

Comments
 (0)