Skip to content

fix: bug in enhancement proxy for detecting nested transactions #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { lowerCaseFirst } from 'lower-case-first';
import invariant from 'tiny-invariant';
import { upperCaseFirst } from 'upper-case-first';
import { fromZodError } from 'zod-validation-error';
import { CrudFailureReason, PRISMA_TX_FLAG } from '../../constants';
import { CrudFailureReason } from '../../constants';
import {
ModelDataVisitor,
NestedWriteVisitor,
Expand Down 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
9 changes: 6 additions & 3 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 @@ -183,6 +183,7 @@ export function makeProxy<T extends PrismaProxyHandler>(
errorTransformer?: ErrorTransformer
) {
const models = Object.keys(modelMeta.fields).map((k) => k.toLowerCase());

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

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

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

const txFunc = input;
return $transaction.bind(target)((tx: any) => {
// create a proxy for the transaction function
const txProxy = makeProxy(tx, modelMeta, makeHandler, name + '$tx');
txProxy[PRISMA_TX_FLAG] = true;

// call the transaction function with the proxy
return txFunc(txProxy);
}, ...rest);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe('With Policy: with postgres', () => {

beforeEach(async () => {
dbUrl = await createPostgresDb(DB_NAME);
const { prisma: _prisma, withPolicy } = await loadSchemaFromFile(
const { prisma: _prisma, enhance } = await loadSchemaFromFile(
path.join(__dirname, '../../schema/todo-pg.zmodel'),
{
provider: 'postgresql',
dbUrl,
}
);
getDb = withPolicy;
getDb = enhance;
prisma = _prisma;
});

Expand Down