forked from noxious/server
DB init. improvements
This commit is contained in:
@ -1,25 +1,12 @@
|
||||
import { EntityManager, MikroORM } from '@mikro-orm/core'
|
||||
|
||||
import { Database } from '../database'
|
||||
import { appLogger } from '../logger'
|
||||
import Database from '../database'
|
||||
|
||||
export abstract class BaseRepository {
|
||||
protected orm!: MikroORM
|
||||
protected em!: EntityManager
|
||||
|
||||
constructor() {
|
||||
this.initializeORM().catch((error) => {
|
||||
appLogger.error(`Failed to initialize Repository: ${error instanceof Error ? error.message : String(error)}`)
|
||||
})
|
||||
protected get orm(): MikroORM {
|
||||
return Database.getORM()
|
||||
}
|
||||
|
||||
private async initializeORM() {
|
||||
try {
|
||||
this.orm = await Database.getInstance()
|
||||
this.em = this.orm.em.fork()
|
||||
} catch (error: any) {
|
||||
appLogger.error(`Failed to initialize ORM: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
protected get em(): EntityManager {
|
||||
return Database.getEntityManager()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user