mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
feat: support transactions mongodb
This commit is contained in:
parent
ae4bfcfe89
commit
e24d9d0ab3
62
api/src/transaction-manager/TransactionManager.ts
Normal file
62
api/src/transaction-manager/TransactionManager.ts
Normal file
@ -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
api/src/transaction-manager/TransactionManagerFactory.ts
Normal file
37
api/src/transaction-manager/TransactionManagerFactory.ts
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user