UNPKG

@sentzunhat/zacatl

Version:

A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.

35 lines 1.34 kB
import { Mongoose } from 'mongoose'; import { CustomError } from '../../../../error/index.js'; import { getContainer } from '../../../../dependency-injection/container.js'; export class MongooseAdapter { async connect(serviceName, config) { const { instance, connectionString, onDatabaseConnected } = config; const getMongoDbName = (uri) => { const match = uri.match(/^mongodb(?:\+srv)?:\/\/[^/]+\/([^?]+)(\?|$)/); return match?.[1] != null ? decodeURIComponent(match[1]) : undefined; }; const mongoose = instance; if (mongoose == null || typeof mongoose.connect !== 'function') { throw new CustomError({ message: 'database instance is not provided', code: 500, reason: 'database instance not provided', }); } const dbName = getMongoDbName(connectionString) ?? serviceName; await mongoose.connect(connectionString, { dbName, autoIndex: true, autoCreate: true, }); if (onDatabaseConnected != null) { await onDatabaseConnected(mongoose); } getContainer().register(Mongoose, { useValue: mongoose, }); } async disconnect() { } } //# sourceMappingURL=mongoose-adapter.js.map