forked from noxious/server
Greatly improved server code base
This commit is contained in:
5
src/application/base/baseCommand.ts
Normal file
5
src/application/base/baseCommand.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
export abstract class BaseCommand {
|
||||
constructor(readonly io: Server) {}
|
||||
}
|
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {}
|
||||
}
|
||||
|
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user