Implemented mechanism to easily retrieve repositories.
This commit is contained in:
		
							parent
							
								
									0679c16b7d
								
							
						
					
					
						commit
						357a3fce67
					
				
					 4 changed files with 26 additions and 11 deletions
				
			
		
							
								
								
									
										16
									
								
								backend/src/data/repositories.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								backend/src/data/repositories.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | ||||||
|  | import {AnyEntity, EntityName, EntityRepository} from "@mikro-orm/core"; | ||||||
|  | import {forkEntityManager} from "../orm"; | ||||||
|  | import {StudentRepository} from "./users/student-repository"; | ||||||
|  | import {Student} from "../entities/users/student.entity"; | ||||||
|  | 
 | ||||||
|  | function repositoryGetter<T extends AnyEntity, R extends EntityRepository<T>>(entity: EntityName<T>): () => R { | ||||||
|  |     let cachedRepo: R | undefined; | ||||||
|  |     return (): R => { | ||||||
|  |         if (!cachedRepo) { | ||||||
|  |             cachedRepo = forkEntityManager().getRepository(entity) as R; | ||||||
|  |         } | ||||||
|  |         return cachedRepo; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const getStudentRepository = repositoryGetter<Student, StudentRepository>(Student); | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| import {AnyEntity, EntityName, EntityRepository, MikroORM} from '@mikro-orm/core'; | import { EntityManager, MikroORM} from '@mikro-orm/core'; | ||||||
| import config from './mikro-orm.config.js'; | import config from './mikro-orm.config.js'; | ||||||
| import {EnvVars, getEnvVar} from "./util/envvars"; | import {EnvVars, getEnvVar} from "./util/envvars"; | ||||||
| 
 | 
 | ||||||
|  | @ -17,10 +17,9 @@ export async function initORM(testingMode: boolean = false) { | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | export function forkEntityManager(): EntityManager { | ||||||
| export function getRepository<T extends AnyEntity>(entityName: EntityName<T>): EntityRepository<T> { |     if (!orm) { | ||||||
|     if (orm === undefined) { |         throw Error("Accessing the Entity Manager before the ORM is fully initialized.") | ||||||
|         throw new Error("ORM is not initialized yet"); |  | ||||||
|     } |     } | ||||||
|     return orm.em.fork().getRepository(entityName); |     return orm.em.fork(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,8 +1,8 @@ | ||||||
| import {initializeTests} from "../testutils" | import {setupTestApp} from "../setup-tests" | ||||||
| import {Student} from "../../src/entities/users/student.entity"; | import {Student} from "../../src/entities/users/student.entity"; | ||||||
| import {describe, it, expect, beforeAll} from "vitest"; | import {describe, it, expect, beforeAll} from "vitest"; | ||||||
| import {getRepository} from "../../src/orm"; |  | ||||||
| import {StudentRepository} from "../../src/data/users/student-repository"; | import {StudentRepository} from "../../src/data/users/student-repository"; | ||||||
|  | import {getStudentRepository} from "../../src/data/repositories"; | ||||||
| 
 | 
 | ||||||
| const username = "teststudent"; | const username = "teststudent"; | ||||||
| const firstName = "John"; | const firstName = "John"; | ||||||
|  | @ -11,8 +11,8 @@ describe("StudentRepository", () => { | ||||||
|     let studentRepository: StudentRepository; |     let studentRepository: StudentRepository; | ||||||
| 
 | 
 | ||||||
|     beforeAll(async () => { |     beforeAll(async () => { | ||||||
|         await initializeTests() |         setupTestApp(); | ||||||
|         studentRepository = getRepository(Student) as StudentRepository; |         studentRepository = getStudentRepository(); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it("should return the queried student after he was added", async () => { |     it("should return the queried student after he was added", async () => { | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import {initORM} from "../src/orm"; | import {initORM} from "../src/orm"; | ||||||
| import dotenv from "dotenv"; | import dotenv from "dotenv"; | ||||||
| 
 | 
 | ||||||
| export async function initializeTests() { | export async function setupTestApp() { | ||||||
|     dotenv.config({path: ".env.test"}); |     dotenv.config({path: ".env.test"}); | ||||||
|     await initORM(true); |     await initORM(true); | ||||||
| } | } | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger