Skip to content

Commit d52c747

Browse files
committed
feat(mongo): allow passing transaction options to the mongo client
Closes #3703
1 parent 4a3d84e commit d52c747

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

packages/mongodb/src/MongoConnection.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
22
Collection, Db, MongoClientOptions, ClientSession, BulkWriteResult, Filter, UpdateFilter, OptionalUnlessRequiredId, UpdateResult,
3-
DeleteResult, InsertManyResult, InsertOneResult,
3+
DeleteResult, InsertManyResult, InsertOneResult, TransactionOptions,
44
} from 'mongodb';
55
import { MongoClient } from 'mongodb';
66
import { ObjectId } from 'bson';
@@ -199,7 +199,7 @@ export class MongoConnection extends Connection {
199199
return this.runQuery<T, number>('countDocuments', collection, undefined, where, ctx);
200200
}
201201

202-
async transactional<T>(cb: (trx: Transaction<ClientSession>) => Promise<T>, options: { isolationLevel?: IsolationLevel; ctx?: Transaction<ClientSession>; eventBroadcaster?: TransactionEventBroadcaster } = {}): Promise<T> {
202+
async transactional<T>(cb: (trx: Transaction<ClientSession>) => Promise<T>, options: { isolationLevel?: IsolationLevel; ctx?: Transaction<ClientSession>; eventBroadcaster?: TransactionEventBroadcaster } & TransactionOptions = {}): Promise<T> {
203203
const session = await this.begin(options);
204204

205205
try {
@@ -215,15 +215,16 @@ export class MongoConnection extends Connection {
215215
}
216216
}
217217

218-
async begin(options: { isolationLevel?: IsolationLevel; ctx?: ClientSession; eventBroadcaster?: TransactionEventBroadcaster } = {}): Promise<ClientSession> {
219-
if (!options.ctx) {
220-
await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
221-
}
218+
async begin(options: { isolationLevel?: IsolationLevel; ctx?: ClientSession; eventBroadcaster?: TransactionEventBroadcaster } & TransactionOptions = {}): Promise<ClientSession> {
219+
const { ctx, isolationLevel, eventBroadcaster, ...txOptions } = options;
222220

223-
const session = options.ctx || this.client.startSession();
224-
session.startTransaction();
221+
if (!ctx) {
222+
await eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart);
223+
}
224+
const session = ctx || this.client.startSession();
225+
session.startTransaction(txOptions);
225226
this.logQuery('db.begin();');
226-
await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, session);
227+
await eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, session);
227228

228229
return session;
229230
}

packages/mongodb/src/MongoEntityManager.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { EntityName, EntityRepository, GetRepository } from '@mikro-orm/core';
1+
import type { EntityName, EntityRepository, GetRepository, TransactionOptions, EntityManagerType } from '@mikro-orm/core';
22
import { EntityManager, Utils } from '@mikro-orm/core';
33
import type { MongoDriver } from './MongoDriver';
44
import type { MongoEntityRepository } from './MongoEntityRepository';
5-
import type { Collection, Document } from 'mongodb';
5+
import type { Collection, Document, TransactionOptions as MongoTransactionOptions } from 'mongodb';
66

77
/**
88
* @inheritDoc
@@ -21,8 +21,25 @@ export class MongoEntityManager<D extends MongoDriver = MongoDriver> extends Ent
2121
return this.getConnection().getCollection(entityName);
2222
}
2323

24+
/**
25+
* @inheritDoc
26+
*/
2427
getRepository<T extends object, U extends EntityRepository<T> = MongoEntityRepository<T>>(entityName: EntityName<T>): GetRepository<T, U> {
2528
return super.getRepository<T, U>(entityName);
2629
}
2730

31+
/**
32+
* @inheritDoc
33+
*/
34+
async begin(options: TransactionOptions & MongoTransactionOptions = {}): Promise<void> {
35+
return super.begin(options);
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
async transactional<T>(cb: (em: D[typeof EntityManagerType]) => Promise<T>, options: TransactionOptions & MongoTransactionOptions = {}): Promise<T> {
42+
return super.transactional(cb, options);
43+
}
44+
2845
}

tests/EntityManager.mongo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ describe('EntityManagerMongo', () => {
406406

407407
test('transactions', async () => {
408408
const god1 = new Author('God1', 'hello@heaven1.god');
409-
await orm.em.begin();
409+
await orm.em.begin({ readPreference: 'secondary' });
410410
await orm.em.persist(god1).flush();
411411
await orm.em.rollback();
412412
const res1 = await orm.em.findOne(Author, { name: 'God1' });

0 commit comments

Comments
 (0)