Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
29 lines
692 B
TypeScript
29 lines
692 B
TypeScript
import { Global, Inject, Module, type OnApplicationShutdown } from '@nestjs/common';
|
|
import { createDb, type Db, type DbHandle } from '@mosaic/db';
|
|
|
|
export const DB_HANDLE = 'DB_HANDLE';
|
|
export const DB = 'DB';
|
|
|
|
@Global()
|
|
@Module({
|
|
providers: [
|
|
{
|
|
provide: DB_HANDLE,
|
|
useFactory: (): DbHandle => createDb(),
|
|
},
|
|
{
|
|
provide: DB,
|
|
useFactory: (handle: DbHandle): Db => handle.db,
|
|
inject: [DB_HANDLE],
|
|
},
|
|
],
|
|
exports: [DB],
|
|
})
|
|
export class DatabaseModule implements OnApplicationShutdown {
|
|
constructor(@Inject(DB_HANDLE) private readonly handle: DbHandle) {}
|
|
|
|
async onApplicationShutdown(): Promise<void> {
|
|
await this.handle.close();
|
|
}
|
|
}
|