Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/contest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ describe('createContext', () => {
const [ctx, prisma] = await makeContext('transactionStarted');
expect(prisma.meta).toEqual((ctx.client as PrismaClientStub).meta);
});

it('has trap returns true for properties on the transaction client', async () => {
const [ctx] = await makeContext('transactionStarted');
expect('$connect' in ctx.client).toBe(true);
expect('meta' in ctx.client).toBe(true);
expect('nonExistentProp' in ctx.client).toBe(false);
});

it('has trap returns false when no transaction is active', async () => {
const [ctx] = await makeContext('transactionPending');
expect('$connect' in ctx.client).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export function createContext(options: PrismaPostgresEnvironmentOptions) {
* error message.
*/
const client: PrismaClientLike = new Proxy({} as PrismaClientLike, {
has: (_target, name) => {
if (!transactionClient) return false;
return name in transactionClient;
},
get: (_target, name: keyof PrismaClientLike | symbol) => {
if (!transactionClient) {
throw new Error(
Expand Down