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
16 changes: 9 additions & 7 deletions packages/orm/src/client/client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ export class ClientImpl {
});
}

this.kysely = new Kysely(this.kyselyProps);
if (baseClient?.isTransaction && !executor) {
// if we're creating a derived client from a transaction client and not replacing
// the executor, reuse the current kysely instance to retain the transaction context
this.kysely = baseClient.$qb;
} else {
this.kysely = new Kysely(this.kyselyProps);
}

this.inputValidator =
baseClient?.inputValidator ??
new InputValidator(this as any, { enabled: this.$options.validateInput !== false });
Expand Down Expand Up @@ -956,12 +963,7 @@ function collectExtResultFieldDefs(
* - Injects `needs` fields into `select` when ext result fields are explicitly selected
* - Recurses into `include` and `select` for nested relation fields
*/
function prepareArgsForExtResult(
args: unknown,
model: string,
schema: SchemaDef,
plugins: AnyPlugin[],
): unknown {
function prepareArgsForExtResult(args: unknown, model: string, schema: SchemaDef, plugins: AnyPlugin[]): unknown {
if (!args || typeof args !== 'object') {
return args;
}
Expand Down
71 changes: 71 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,77 @@ 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',
onQuery: async ({ args, proceed }: { args: unknown; proceed: (args: unknown) => Promise<unknown> }) =>
proceed(args),
})
.user.create({
data: { email: 'u1@test.com' },
});
throw new Error('rollback');
}),
).rejects.toThrow('rollback');

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

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

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

it('$setOptions preserves transaction isolation', async () => {
await expect(
client.$transaction(async (tx) => {
await (tx as any).$setOptions((tx as any).$options).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