feat(backend): Repositories toegevoegd, databank unit-testbaar gemaakt.

This commit is contained in:
Gerald Schmittinger 2025-02-24 01:13:20 +01:00
parent 2657e49ad6
commit 374de3b21a
15 changed files with 1672 additions and 45 deletions

View file

@ -1,10 +1,10 @@
import { MikroORM } from '@mikro-orm/core';
import {AnyEntity, EntityName, EntityRepository, MikroORM} from '@mikro-orm/core';
import config from './mikro-orm.config.js';
import {EnvVars, getEnvVar} from "./util/envvars";
export default async function initORM() {
const orm = await MikroORM.init(config);
let orm: MikroORM | undefined;
export async function initORM(testingMode: boolean = false) {
orm = await MikroORM.init(config(testingMode));
// Update the database scheme if necessary and enabled.
if (getEnvVar(EnvVars.DbUpdate)) {
await orm.schema.updateSchema();
@ -17,3 +17,10 @@ export default async function initORM() {
}
}
}
export function getRepository<T extends AnyEntity>(entityName: EntityName<T>): EntityRepository<T> {
if (orm === undefined) {
throw new Error("ORM is not initialized yet");
}
return orm.em.fork().getRepository(entityName);
}