Skip to content

Commit

Permalink
fix(mongo): fix transaction (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined-moe authored Jun 7, 2024
1 parent 308229e commit 08540c0
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class MongoDriver extends Driver<MongoDriver.Config> {
this.db.admin().serverInfo().then((doc) => this.version = +doc.version.split('.')[0]).catch(noop)
await this.client.withSession((session) => session.withTransaction(
() => this.db.collection('_fields').findOne({}, { session }),
{ readPreference: 'primary' },
)).catch(() => {
this._replSet = false
this.logger.warn(`MongoDB is currently running as standalone server, transaction is disabled.
Expand Down Expand Up @@ -485,7 +486,7 @@ export class MongoDriver extends Driver<MongoDriver.Config> {

async withTransaction(callback: (session: any) => Promise<void>) {
if (this._replSet) {
await this.client.withSession((session) => session.withTransaction(() => callback(session)))
await this.client.withSession((session) => session.withTransaction(() => callback(session), { readPreference: 'primary' }))
} else {
await callback(undefined)
}
Expand Down

0 comments on commit 08540c0

Please sign in to comment.