Skip to content

Commit

Permalink
fix transaction detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Jan 10, 2024
1 parent ba763be commit f292d29
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
5 changes: 0 additions & 5 deletions packages/runtime/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ export enum PrismaErrorCode {
DEPEND_ON_RECORD_NOT_FOUND = 'P2025',
}

/**
* Field name for storing in-transaction flag
*/
export const PRISMA_TX_FLAG = '$__zenstack_tx';

/**
* Field name for getting current enhancer
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,11 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
}

private transaction(action: (tx: Record<string, DbOperations>) => Promise<any>) {
if (this.prisma[PRISMA_TX_FLAG]) {
if (this.prisma['$transaction']) {
return this.prisma.$transaction((tx) => action(tx), { maxWait: 100000, timeout: 100000 });
} else {
// already in transaction, don't nest
return action(this.prisma);
} else {
return this.prisma.$transaction((tx) => action(tx), { maxWait: 100000, timeout: 100000 });
}
}

Expand Down
15 changes: 2 additions & 13 deletions packages/runtime/src/enhancements/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { PRISMA_PROXY_ENHANCER, PRISMA_TX_FLAG } from '../constants';
import { PRISMA_PROXY_ENHANCER } from '../constants';
import type { ModelMeta } from '../cross';
import type { DbClientContract } from '../types';
import { createDeferredPromise } from './policy/promise';
Expand Down Expand Up @@ -184,9 +184,6 @@ export function makeProxy<T extends PrismaProxyHandler>(
) {
const models = Object.keys(modelMeta.fields).map((k) => k.toLowerCase());

// a store for saving fields that belong to the proxy (not the target)
const proxyStorage: Record<string, unknown> = {};

const proxy = new Proxy(prisma, {
get: (target: any, prop: string | symbol, receiver: any) => {
// enhancer metadata
Expand All @@ -195,11 +192,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
}

if (prop === 'toString') {
return () => `$zenstack_${name}[${target.toString()}]`;
}

if (typeof prop === 'string' && prop in proxyStorage) {
return proxyStorage[prop];
return () => `$zenstack_prisma_${prisma._clientVersion}`;
}

if (prop === '$transaction') {
Expand All @@ -224,10 +217,6 @@ export function makeProxy<T extends PrismaProxyHandler>(
// create a proxy for the transaction function
const txProxy = makeProxy(tx, modelMeta, makeHandler, name + '$tx');

// record in-transaction flag on the proxy (not the target)
// see logic in "set" handler below
txProxy[PRISMA_TX_FLAG] = true;

// call the transaction function with the proxy
return txFunc(txProxy);
}, ...rest);
Expand Down

0 comments on commit f292d29

Please sign in to comment.