refactor(backend): Splits seeding
This commit is contained in:
		
							parent
							
								
									f8035099c9
								
							
						
					
					
						commit
						52efb03ecd
					
				
					 3 changed files with 81 additions and 71 deletions
				
			
		|  | @ -1,80 +1,20 @@ | |||
| import { forkEntityManager, initORM } from '../src/orm.js'; | ||||
| import { initORM } from '../src/orm.js'; | ||||
| import dotenv from 'dotenv'; | ||||
| import { makeTestAssignemnts } from '../tests/test_assets/assignments/assignments.testdata.js'; | ||||
| import { makeTestGroups } from '../tests/test_assets/assignments/groups.testdata.js'; | ||||
| import { makeTestSubmissions } from '../tests/test_assets/assignments/submission.testdata.js'; | ||||
| import { makeTestClassJoinRequests } from '../tests/test_assets/classes/class-join-requests.testdata.js'; | ||||
| import { makeTestClasses } from '../tests/test_assets/classes/classes.testdata.js'; | ||||
| import { makeTestTeacherInvitations } from '../tests/test_assets/classes/teacher-invitations.testdata.js'; | ||||
| import { makeTestAttachments } from '../tests/test_assets/content/attachments.testdata.js'; | ||||
| import { makeTestLearningObjects } from '../tests/test_assets/content/learning-objects.testdata.js'; | ||||
| import { makeTestLearningPaths } from '../tests/test_assets/content/learning-paths.testdata.js'; | ||||
| import { makeTestAnswers } from '../tests/test_assets/questions/answers.testdata.js'; | ||||
| import { makeTestQuestions } from '../tests/test_assets/questions/questions.testdata.js'; | ||||
| import { makeTestStudents } from '../tests/test_assets/users/students.testdata.js'; | ||||
| import { makeTestTeachers } from '../tests/test_assets/users/teachers.testdata.js'; | ||||
| import { getLogger, Logger } from '../src/logging/initalize.js'; | ||||
| import { Collection, MikroORM } from '@mikro-orm/core'; | ||||
| import { Group } from '../src/entities/assignments/group.entity'; | ||||
| import { seedORM } from './seedORM.js'; | ||||
| 
 | ||||
| const logger: Logger = getLogger(); | ||||
| 
 | ||||
| export async function seedORM(orm: MikroORM): Promise<void> { | ||||
|     await orm.schema.clearDatabase(); | ||||
| 
 | ||||
|     const em = forkEntityManager(); | ||||
| 
 | ||||
|     logger.info('seeding database...'); | ||||
| 
 | ||||
|     const students = makeTestStudents(em); | ||||
|     const teachers = makeTestTeachers(em); | ||||
|     const learningObjects = makeTestLearningObjects(em); | ||||
|     const learningPaths = makeTestLearningPaths(em); | ||||
|     const classes = makeTestClasses(em, students, teachers); | ||||
|     const assignments = makeTestAssignemnts(em, classes); | ||||
| 
 | ||||
|     const groups = makeTestGroups(em, students, assignments); | ||||
| 
 | ||||
|     assignments[0].groups = new Collection<Group>(groups.slice(0, 3)); | ||||
|     assignments[1].groups = new Collection<Group>(groups.slice(3, 4)); | ||||
| 
 | ||||
|     const teacherInvitations = makeTestTeacherInvitations(em, teachers, classes); | ||||
|     const classJoinRequests = makeTestClassJoinRequests(em, students, classes); | ||||
|     const attachments = makeTestAttachments(em, learningObjects); | ||||
| 
 | ||||
|     learningObjects[1].attachments = attachments; | ||||
| 
 | ||||
|     const questions = makeTestQuestions(em, students, groups); | ||||
|     const answers = makeTestAnswers(em, teachers, questions); | ||||
|     const submissions = makeTestSubmissions(em, students, groups); | ||||
| 
 | ||||
|     // Persist all entities
 | ||||
|     await em.persistAndFlush([ | ||||
|         ...students, | ||||
|         ...teachers, | ||||
|         ...learningObjects, | ||||
|         ...learningPaths, | ||||
|         ...classes, | ||||
|         ...assignments, | ||||
|         ...groups, | ||||
|         ...teacherInvitations, | ||||
|         ...classJoinRequests, | ||||
|         ...attachments, | ||||
|         ...questions, | ||||
|         ...answers, | ||||
|         ...submissions, | ||||
|     ]); | ||||
| 
 | ||||
|     logger.info('Development database seeded successfully!'); | ||||
| } | ||||
| 
 | ||||
| export async function seedDatabase(envFile = '.env.development.local', testMode = false): Promise<void> { | ||||
| export async function seedDatabase(envFile = '.env.development.local', testMode = process.env.NODE_ENV !== undefined && process.env.NODE_ENV === 'test'): Promise<void> { | ||||
|     dotenv.config({ path: envFile }); | ||||
| 
 | ||||
|     try { | ||||
|         const orm = await initORM(testMode); | ||||
| 
 | ||||
|         await seedORM(orm); | ||||
| 
 | ||||
|         await orm.close(); | ||||
|     } catch (err) { | ||||
|         logger.error(`Error: ${err}`); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| seedDatabase().catch((err) => logger.error(`Seeding: ${err}`)); | ||||
|  |  | |||
							
								
								
									
										70
									
								
								backend/tool/seedORM.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								backend/tool/seedORM.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,70 @@ | |||
| import { Collection, MikroORM } from '@mikro-orm/core'; | ||||
| import { forkEntityManager } from '../src/orm'; | ||||
| import { makeTestStudents } from '../tests/test_assets/users/students.testdata'; | ||||
| import { makeTestTeachers } from '../tests/test_assets/users/teachers.testdata'; | ||||
| import { makeTestLearningObjects } from '../tests/test_assets/content/learning-objects.testdata'; | ||||
| import { makeTestLearningPaths } from '../tests/test_assets/content/learning-paths.testdata'; | ||||
| import { makeTestClasses } from '../tests/test_assets/classes/classes.testdata'; | ||||
| import { makeTestAssignemnts } from '../tests/test_assets/assignments/assignments.testdata'; | ||||
| import { makeTestGroups } from '../tests/test_assets/assignments/groups.testdata'; | ||||
| import { Group } from '../src/entities/assignments/group.entity'; | ||||
| import { makeTestTeacherInvitations } from '../tests/test_assets/classes/teacher-invitations.testdata'; | ||||
| import { makeTestClassJoinRequests } from '../tests/test_assets/classes/class-join-requests.testdata'; | ||||
| import { makeTestAttachments } from '../tests/test_assets/content/attachments.testdata'; | ||||
| import { makeTestQuestions } from '../tests/test_assets/questions/questions.testdata'; | ||||
| import { makeTestAnswers } from '../tests/test_assets/questions/answers.testdata'; | ||||
| import { makeTestSubmissions } from '../tests/test_assets/assignments/submission.testdata'; | ||||
| import { getLogger } from '../src/logging/initalize'; | ||||
| 
 | ||||
| export async function seedORM(orm: MikroORM): Promise<void> { | ||||
|     const logger = getLogger(); | ||||
| 
 | ||||
|     logger.debug('Clearing database...'); | ||||
|     await orm.schema.clearDatabase(); | ||||
| 
 | ||||
|     logger.debug('Forking entity manager...'); | ||||
|     const em = forkEntityManager(); | ||||
| 
 | ||||
|     logger.debug('Seeding database...'); | ||||
| 
 | ||||
|     const students = makeTestStudents(em); | ||||
|     const teachers = makeTestTeachers(em); | ||||
|     const learningObjects = makeTestLearningObjects(em); | ||||
|     const learningPaths = makeTestLearningPaths(em); | ||||
|     const classes = makeTestClasses(em, students, teachers); | ||||
|     const assignments = makeTestAssignemnts(em, classes); | ||||
| 
 | ||||
|     const groups = makeTestGroups(em, students, assignments); | ||||
| 
 | ||||
|     assignments[0].groups = new Collection<Group>(groups.slice(0, 3)); | ||||
|     assignments[1].groups = new Collection<Group>(groups.slice(3, 4)); | ||||
| 
 | ||||
|     const teacherInvitations = makeTestTeacherInvitations(em, teachers, classes); | ||||
|     const classJoinRequests = makeTestClassJoinRequests(em, students, classes); | ||||
|     const attachments = makeTestAttachments(em, learningObjects); | ||||
| 
 | ||||
|     learningObjects[1].attachments = attachments; | ||||
| 
 | ||||
|     const questions = makeTestQuestions(em, students, groups); | ||||
|     const answers = makeTestAnswers(em, teachers, questions); | ||||
|     const submissions = makeTestSubmissions(em, students, groups); | ||||
| 
 | ||||
|     // Persist all entities
 | ||||
|     await em.persistAndFlush([ | ||||
|         ...students, | ||||
|         ...teachers, | ||||
|         ...learningObjects, | ||||
|         ...learningPaths, | ||||
|         ...classes, | ||||
|         ...assignments, | ||||
|         ...groups, | ||||
|         ...teacherInvitations, | ||||
|         ...classJoinRequests, | ||||
|         ...attachments, | ||||
|         ...questions, | ||||
|         ...answers, | ||||
|         ...submissions, | ||||
|     ]); | ||||
| 
 | ||||
|     logger.info('Development database seeded successfully!'); | ||||
| } | ||||
|  | @ -5,7 +5,7 @@ import { errorHandler } from '../src/middleware/error-handling/error-handler.js' | |||
| import dotenv from 'dotenv'; | ||||
| import cors from '../src/middleware/cors'; | ||||
| import { authenticateUser } from '../src/middleware/auth/auth'; | ||||
| import { seedORM } from './seed'; | ||||
| import { seedORM } from './seedORM'; | ||||
| 
 | ||||
| const envFile = '../.env.test'; | ||||
| 
 | ||||
|  |  | |||
		Reference in a new issue