import { MikroORM, IDatabaseDriver, Connection, EntityRepository } from '@mikro-orm/core' export type ObjectType = { new (): T } | Function let orm: MikroORM> export abstract class BaseEntity { static getRepo(this: ObjectType) { if (!orm) throw new Error('Orm not initialized') return orm.em.getRepository(this) } // Native Functions static count(this: ObjectType, ...args: Parameters['count']>): ReturnType['count']> { return (this as any).getRepo().count(...args) } static create(this: ObjectType, ...args: Parameters['create']>): ReturnType['create']> { return (this as any).getRepo().create(...args) } static find(this: ObjectType, ...args: Parameters['find']>): ReturnType['find']> { return (this as any).getRepo().find(...args) } static findAll(this: ObjectType, ...args: Parameters['findAll']>): ReturnType['findAll']> { return (this as any).getRepo().findAll(...args) } static findAndCount(this: ObjectType, ...args: Parameters['findAndCount']>): ReturnType['findAndCount']> { return (this as any).getRepo().findAndCount(...args) } static findOne(this: ObjectType, ...args: Parameters['findOne']>): ReturnType['findOne']> { return (this as any).getRepo().findOne(...args) } static findOneOrFail(this: ObjectType, ...args: Parameters['findOneOrFail']>): ReturnType['findOneOrFail']> { return (this as any).getRepo().findOneOrFail(...args) } static flush(this: ObjectType, ...args: Parameters['flush']>): ReturnType['flush']> { return (this as any).getRepo().flush(...args) } static getReference(this: ObjectType, ...args: Parameters['getReference']>): ReturnType['getReference']> { return (this as any).getRepo().getReference(...args) } static map(this: ObjectType, ...args: Parameters['map']>): ReturnType['findOneOrFail']> { return (this as any).getRepo().map(...args) } static nativeDelete(this: ObjectType, ...args: Parameters['nativeDelete']>): ReturnType['nativeDelete']> { return (this as any).getRepo().nativeDelete(...args) } static nativeInsert(this: ObjectType, ...args: Parameters['nativeInsert']>): ReturnType['nativeInsert']> { return (this as any).getRepo().nativeInsert(...args) } static nativeUpdate(this: ObjectType, ...args: Parameters['nativeUpdate']>): ReturnType['nativeUpdate']> { return (this as any).getRepo().nativeUpdate(...args) } static persist(this: ObjectType, ...args: Parameters['persist']>): ReturnType['persist']> { return (this as any).getRepo().persist(...args) } static persistAndFlush(this: ObjectType, ...args: Parameters['persistAndFlush']>): ReturnType['persistAndFlush']> { return (this as any).getRepo().persist(...args) } static populate(this: ObjectType, ...args: Parameters['populate']>): ReturnType['populate']> { return (this as any).getRepo().populate(...args) } static remove(this: ObjectType, ...args: Parameters['remove']>): ReturnType['remove']> { return (this as any).getRepo().remove(...args) } } export const register = >(db: MikroORM) => (orm = db)