forked from noxious/server
DB init. improvements
This commit is contained in:
parent
474683082d
commit
bd85908014
@ -1,57 +1,31 @@
|
||||
import { Database } from '#application/database'
|
||||
import { EntityManager } from '@mikro-orm/core'
|
||||
import Database from '#application/database'
|
||||
import { appLogger } from '#application/logger'
|
||||
|
||||
export abstract class BaseEntity {
|
||||
async save(): Promise<this> {
|
||||
try {
|
||||
const orm = await Database.getInstance()
|
||||
const em = orm.em.fork()
|
||||
private getEntityManager(): EntityManager {
|
||||
return Database.getEntityManager()
|
||||
}
|
||||
|
||||
await em.begin()
|
||||
try {
|
||||
em.persist(this)
|
||||
await em.flush()
|
||||
await em.commit()
|
||||
return this
|
||||
} catch (error) {
|
||||
await em.rollback()
|
||||
throw error
|
||||
}
|
||||
} catch (error) {
|
||||
appLogger.error(`Failed to save entity: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
async save(): Promise<this> {
|
||||
return this.performDbOperation('persist', 'save entity')
|
||||
}
|
||||
|
||||
async update(): Promise<this> {
|
||||
try {
|
||||
const orm = await Database.getInstance()
|
||||
const em = orm.em.fork()
|
||||
|
||||
await em.begin()
|
||||
try {
|
||||
em.merge(this)
|
||||
await em.flush()
|
||||
await em.commit()
|
||||
return this
|
||||
} catch (error) {
|
||||
await em.rollback()
|
||||
throw error
|
||||
}
|
||||
} catch (error) {
|
||||
appLogger.error(`Failed to update entity: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
return this.performDbOperation('merge', 'update entity')
|
||||
}
|
||||
|
||||
async delete(): Promise<this> {
|
||||
return this.performDbOperation('remove', 'remove entity')
|
||||
}
|
||||
|
||||
private async performDbOperation(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
|
||||
try {
|
||||
const orm = await Database.getInstance()
|
||||
const em = orm.em.fork()
|
||||
const em = this.getEntityManager()
|
||||
|
||||
await em.begin()
|
||||
try {
|
||||
em.remove(this)
|
||||
em[method](this)
|
||||
await em.flush()
|
||||
await em.commit()
|
||||
return this
|
||||
@ -60,7 +34,7 @@ export abstract class BaseEntity {
|
||||
throw error
|
||||
}
|
||||
} catch (error) {
|
||||
appLogger.error(`Failed to remove entity: ${error instanceof Error ? error.message : String(error)}`)
|
||||
appLogger.error(`Failed to ${actionDescription}: ${error instanceof Error ? error.message : String(error)}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
@ -1,28 +1,36 @@
|
||||
// import { MikroORM } from '@mikro-orm/mariadb'
|
||||
import { MikroORM } from '@mikro-orm/mysql'
|
||||
|
||||
import { EntityManager } from '@mikro-orm/core'
|
||||
import { appLogger } from './logger'
|
||||
import config from '../../mikro-orm.config'
|
||||
|
||||
/**
|
||||
* Singleton class for initializing and managing the database connection
|
||||
*/
|
||||
export class Database {
|
||||
private static instance: MikroORM | undefined
|
||||
class Database {
|
||||
private static orm: MikroORM
|
||||
private static em: EntityManager
|
||||
|
||||
private static async init(): Promise<MikroORM> {
|
||||
public static async initialize(): Promise<void> {
|
||||
try {
|
||||
return await MikroORM.init(config)
|
||||
Database.orm = await MikroORM.init(config)
|
||||
Database.em = Database.orm.em.fork()
|
||||
appLogger.info('Database connection initialized')
|
||||
} catch (error) {
|
||||
appLogger.error(`MikroORM connection failed: ${error}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
public static async getInstance(): Promise<MikroORM> {
|
||||
if (!Database.instance) {
|
||||
Database.instance = await Database.init()
|
||||
public static getORM(): MikroORM {
|
||||
if (!Database.orm) {
|
||||
throw new Error('Database not initialized. Call Database.initialize() first.')
|
||||
}
|
||||
return Database.instance
|
||||
return Database.orm
|
||||
}
|
||||
|
||||
public static getEntityManager(): EntityManager {
|
||||
if (!Database.em) {
|
||||
throw new Error('Database not initialized. Call Database.initialize() first.')
|
||||
}
|
||||
return Database.em
|
||||
}
|
||||
}
|
||||
|
||||
export default Database
|
@ -6,7 +6,7 @@ import express, { Application } from 'express'
|
||||
import { Server as SocketServer } from 'socket.io'
|
||||
|
||||
import config from '#application/config'
|
||||
import { Database } from '#application/database'
|
||||
import Database from '#application/database'
|
||||
import { appLogger, watchLogs } from '#application/logger'
|
||||
import { getAppPath } from '#application/storage'
|
||||
import { TSocket } from '#application/types'
|
||||
@ -53,9 +53,10 @@ export class Server {
|
||||
|
||||
// Connect to database
|
||||
try {
|
||||
await Database.getInstance()
|
||||
await Database.initialize()
|
||||
} catch (error: any) {
|
||||
appLogger.error(`Database connection failed: ${error.message}`)
|
||||
process.exit(1) // Exit if database connection fails
|
||||
}
|
||||
|
||||
// Start the server
|
||||
|
Loading…
x
Reference in New Issue
Block a user