1
0
forked from noxious/server

Greatly improved server code base

This commit is contained in:
2024-12-28 17:26:17 +01:00
parent bd85908014
commit bd3bf6f580
39 changed files with 227 additions and 246 deletions

View File

@@ -0,0 +1,5 @@
import { Server } from 'socket.io'
export abstract class BaseCommand {
constructor(readonly io: Server) {}
}

View File

@@ -1,4 +1,5 @@
import { EntityManager } from '@mikro-orm/core'
import Database from '#application/database'
import { appLogger } from '#application/logger'
@@ -8,18 +9,18 @@ export abstract class BaseEntity {
}
async save(): Promise<this> {
return this.performDbOperation('persist', 'save entity')
return this.execute('persist', 'save entity')
}
async update(): Promise<this> {
return this.performDbOperation('merge', 'update entity')
return this.execute('merge', 'update entity')
}
async delete(): Promise<this> {
return this.performDbOperation('remove', 'remove entity')
return this.execute('remove', 'remove entity')
}
private async performDbOperation(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
private async execute(method: 'persist' | 'merge' | 'remove', actionDescription: string): Promise<this> {
try {
const em = this.getEntityManager()
@@ -38,4 +39,4 @@ export abstract class BaseEntity {
throw error
}
}
}
}

View File

@@ -1,4 +1,10 @@
import { Server } from 'socket.io'
import { TSocket } from '#application/types'
export abstract class BaseEvent {
}
constructor(
readonly io: Server,
readonly socket: TSocket
) {}
}

View File

@@ -1,12 +1,9 @@
import { EntityManager, MikroORM } from '@mikro-orm/core'
import { EntityManager } from '@mikro-orm/core'
import Database from '../database'
export abstract class BaseRepository {
protected get orm(): MikroORM {
return Database.getORM()
}
protected get em(): EntityManager {
return Database.getEntityManager()
}
}
}