Files
adam-acs/providers/mongodb_provider.ts
T
Kevin L'HereecandCopilot d1c65dbf1e chore: initial commit — AdonisJS v7 TR-069 ACS
- Docker infrastructure (Dockerfile, docker-compose.yml, genieacs-sim)
- AdonisJS v7 app with Inertia.js + Vue 3
- MongoDB integration
- CWMP/TR-069 state machine engine
- Configuration engine with compliance tracking
- Branding: ADAM ACS (favicon, navbar, locales)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-29 16:46:37 +02:00

56 lines
1.2 KiB
TypeScript

import type { ApplicationService } from '@adonisjs/core/types'
import { Db, MongoClient } from 'mongodb'
declare module '@adonisjs/core/types' {
interface ContainerBindings {
mongodb: Db
}
}
/**
* Singleton reference to the MongoDB Db instance.
* Set during provider boot, used synchronously by BaseModel.
*/
let dbInstance: Db | null = null
export function getMongoDb(): Db {
if (!dbInstance) {
throw new Error('MongoDB is not connected. Ensure MongodbProvider has booted.')
}
return dbInstance
}
export default class MongodbProvider {
private client: MongoClient | null = null
constructor(protected app: ApplicationService) {}
async boot() {
const config = await import('#config/database')
const { uri, dbName } = config.default
this.client = new MongoClient(uri, {
monitorCommands: true,
serverSelectionTimeoutMS: 5000,
})
await this.client.connect()
const db = this.client.db(dbName)
dbInstance = db
this.app.container.bindValue('mongodb', db)
}
async shutdown() {
if (this.client) {
try {
await this.client.close()
} catch {
// Connection may already be closed
}
this.client = null
dbInstance = null
}
}
}