Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 packages/orm/src/client/client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ export class ClientImpl {
newClient.inputValidator = new InputValidator(newClient as any, {
enabled: newOptions.validateInput !== false,
});
// preserve transaction state so the new client stays in the same transaction
if (this.kysely.isTransaction) {
Comment thread
ymc9 marked this conversation as resolved.
Outdated
newClient.kysely = this.kysely;
}
return newClient;
}

Expand All @@ -399,6 +403,10 @@ export class ClientImpl {
newClient.inputValidator = new InputValidator(newClient as any, {
enabled: newClient.$options.validateInput !== false,
});
// preserve transaction state so the new client stays in the same transaction
if (this.kysely.isTransaction) {
newClient.kysely = this.kysely;
}
return newClient;
}

Expand All @@ -414,6 +422,10 @@ export class ClientImpl {
newClient.inputValidator = new InputValidator(newClient as any, {
enabled: newOptions.validateInput !== false,
});
// preserve transaction state so the new client stays in the same transaction
if (this.kysely.isTransaction) {
newClient.kysely = this.kysely;
}
return newClient;
}

Expand Down
39 changes: 39 additions & 0 deletions tests/e2e/orm/client-api/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,45 @@ describe('Client raw query tests', () => {

await expect(client.user.findMany()).toResolveWithLength(0);
});

it('$unuseAll preserves transaction isolation', async () => {
await expect(
client.$transaction(async (tx) => {
await tx.$unuseAll().user.create({
data: { email: 'u1@test.com' },
});
throw new Error('rollback');
}),
).rejects.toThrow('rollback');

await expect(client.user.findMany()).toResolveWithLength(0);
});

it('$unuse preserves transaction isolation', async () => {
await expect(
client.$transaction(async (tx) => {
await tx.$unuse('nonexistent').user.create({
data: { email: 'u1@test.com' },
});
throw new Error('rollback');
}),
).rejects.toThrow('rollback');

await expect(client.user.findMany()).toResolveWithLength(0);
});

it('$use preserves transaction isolation', async () => {
await expect(
client.$transaction(async (tx) => {
await (tx as any).$use({ id: 'noop', handle: (_node: any, proceed: any) => proceed(_node) }).user.create({
data: { email: 'u1@test.com' },
});
throw new Error('rollback');
}),
).rejects.toThrow('rollback');

await expect(client.user.findMany()).toResolveWithLength(0);
});
});

describe('sequential transaction', () => {
Expand Down
Loading