bb-inspired
Version:
Core library for BB-inspired NestJS backend
97 lines • 3.96 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var MongodbService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongodbService = void 0;
const common_1 = require("@nestjs/common");
const mongodb_1 = require("mongodb");
const logger_1 = require("../../utils/logger");
let MongodbService = MongodbService_1 = class MongodbService {
constructor(options) {
this.options = options;
this.logger = new logger_1.AppLogger(MongodbService_1.name);
this.collections = new Map();
}
async onModuleInit() {
var _a;
try {
if (!((_a = this.options.mongodb) === null || _a === void 0 ? void 0 : _a.uri)) {
throw new Error('MongoDB URI is required');
}
this.client = new mongodb_1.MongoClient(this.options.mongodb.uri, this.options.mongodb.options);
await this.client.connect();
const dbName = this.options.mongodb.dbName || 'main';
this.db = this.client.db(dbName);
this.logger.log(`MongoDB connected to database: ${dbName}`);
}
catch (error) {
this.logger.error(`Failed to connect to MongoDB: ${error.message}`, error.stack);
throw error;
}
}
async onModuleDestroy() {
try {
if (this.client) {
await this.client.close();
this.logger.log('MongoDB client disconnected');
}
}
catch (error) {
this.logger.error(`Error disconnecting from MongoDB: ${error.message}`, error.stack);
}
}
getDatabase() {
if (!this.db) {
throw new Error('MongoDB is not connected');
}
return this.db;
}
getCollection(name) {
if (!this.collections.has(name)) {
const collection = this.db.collection(name);
this.collections.set(name, collection);
}
return this.collections.get(name);
}
async startSession() {
return this.client.startSession();
}
async executeInTransaction(operations) {
const session = await this.startSession();
try {
let result;
await session.withTransaction(async () => {
result = await operations(session);
});
return result;
}
finally {
await session.endSession();
}
}
async createIndexes(collectionName, indexes) {
const collection = this.getCollection(collectionName);
for (const index of indexes) {
await collection.createIndex(index.fields, index.options || {});
}
this.logger.verbose(`Created ${indexes.length} indexes for collection ${collectionName}`);
}
};
exports.MongodbService = MongodbService;
exports.MongodbService = MongodbService = MongodbService_1 = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)('DATABASE_OPTIONS')),
__metadata("design:paramtypes", [Object])
], MongodbService);
//# sourceMappingURL=mongodb.service.js.map