adonis-odm
Version:
A comprehensive MongoDB ODM for AdonisJS with Lucid-style API, type-safe relationships, embedded documents, and transaction support
33 lines (32 loc) • 1.08 kB
JavaScript
import { ModelQueryBuilder } from './query_builder/model_query_builder.js';
export class ConcreteMongoTransactionClient {
session;
manager;
connectionName;
constructor(session, manager, connectionName) {
this.session = session;
this.manager = manager;
this.connectionName = connectionName;
}
async commit() {
await this.session.commitTransaction();
this.session.endSession();
}
async rollback() {
await this.session.abortTransaction();
this.session.endSession();
}
collection(name) {
// Get the Db instance for the connection, then get collection
const dbInstance = this.manager.db(this.connectionName);
return dbInstance.collection(name);
}
query(modelConstructor) {
const collectionName = modelConstructor.getCollectionName();
const collectionInstance = this.collection(collectionName);
return new ModelQueryBuilder(collectionInstance, modelConstructor, this);
}
getSession() {
return this.session;
}
}