Skip to content

feat: support transactions mongodb #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
62 changes: 62 additions & 0 deletions api/src/transaction-manager/TransactionManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { ClientSession } from 'mongoose';

// TODO: logging
export class TransactionManager {
private hasCommited = false;

private hasAborted = false;

private hasEnded = false;

private callbackHasErrored = false;

constructor(private session: ClientSession) {
this.session.startTransaction();
}

async withTransaction<T>(
callback: (session: ClientSession) => Promise<T>,
): Promise<T> {
try {
const result = await callback(this.session).catch((err) => {
this.callbackHasErrored = true;
throw err;
});

await this.commitTransaction();
return result;
} catch (error) {
await this.abortTransaction();
throw error;
} finally {
await this.endTransaction();
}
}

private async commitTransaction() {
if (this.session.inTransaction()) {
await this.session.commitTransaction();
this.hasCommited = true;
}
}

private async abortTransaction() {
if (this.session.inTransaction()) {
await this.session.abortTransaction();
this.hasAborted = true;
}
}

private async endTransaction() {
await this.session.endSession();
this.hasEnded = true;
}
}
37 changes: 37 additions & 0 deletions api/src/transaction-manager/TransactionManagerFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import {
HttpException,
HttpStatus,
Injectable,
LoggerService,
} from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import { Connection } from 'mongoose';

import { TransactionManager } from './TransactionManager';

// TODO: logging / update mongodb docker file replicas
@Injectable()
export class TransactionManagerFactory {
constructor(
@InjectConnection() private readonly connection: Connection,
private loggerService: LoggerService,
) {}

async create(): Promise<TransactionManager> {
const session = await this.connection.startSession().catch((_err) => {
throw new HttpException(
`Something went wrong while start transaction session`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
});
return new TransactionManager(session);
}
}