fix: problee m met mergen en zorgen student Gerald in db komt bij npm run dev runnen
This commit is contained in:
parent
1ffd77cce7
commit
8218aae6ec
10 changed files with 372 additions and 62 deletions
|
@ -7,7 +7,7 @@
|
|||
"main": "dist/app.js",
|
||||
"scripts": {
|
||||
"build": "cross-env NODE_ENV=production tsc --build",
|
||||
"dev": "cross-env NODE_ENV=development tsx watch --env-file=.env.development.local src/app.ts",
|
||||
"dev": "cross-env NODE_ENV=development tsx seed.ts; tsx watch --env-file=.env.development.local src/app.ts",
|
||||
"start": "cross-env NODE_ENV=production node --env-file=.env dist/app.js",
|
||||
"format": "prettier --write src/",
|
||||
"format-check": "prettier --check src/",
|
||||
|
|
69
backend/seed.ts
Normal file
69
backend/seed.ts
Normal file
|
@ -0,0 +1,69 @@
|
|||
import { forkEntityManager, 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";
|
||||
|
||||
export async function seedDatabase() {
|
||||
dotenv.config({ path: '.env.development.local' });
|
||||
const orm = await initORM();
|
||||
await orm.schema.clearDatabase();
|
||||
|
||||
const em = forkEntityManager();
|
||||
|
||||
console.log("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 = groups.slice(0, 3);
|
||||
assignments[1].groups = 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);
|
||||
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,
|
||||
]);
|
||||
|
||||
console.log('Development database seeded successfully!');
|
||||
|
||||
await orm.close();
|
||||
}
|
||||
|
||||
seedDatabase().catch(console.error);
|
|
@ -1,10 +1,10 @@
|
|||
import { EntityManager, MikroORM } from '@mikro-orm/core';
|
||||
import { Connection, EntityManager, IDatabaseDriver, MikroORM } from '@mikro-orm/core';
|
||||
import config from './mikro-orm.config.js';
|
||||
import { envVars, getEnvVar } from './util/envVars.js';
|
||||
import { getLogger, Logger } from './logging/initalize.js';
|
||||
|
||||
let orm: MikroORM | undefined;
|
||||
export async function initORM(testingMode = false): Promise<void> {
|
||||
export async function initORM(testingMode = false): Promise<MikroORM<IDatabaseDriver<Connection>, EntityManager<IDatabaseDriver<Connection>>>> {
|
||||
const logger: Logger = getLogger();
|
||||
|
||||
logger.info('Initializing ORM');
|
||||
|
@ -25,6 +25,8 @@ export async function initORM(testingMode = false): Promise<void> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
return orm;
|
||||
}
|
||||
export function forkEntityManager(): EntityManager {
|
||||
if (!orm) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { EntityManager } from '@mikro-orm/core';
|
||||
import { Student } from '../../../src/entities/users/student.entity';
|
||||
import { fixupRule } from '@eslint/compat';
|
||||
|
||||
// 🔓 Ruwe testdata array — herbruikbaar in assertions
|
||||
export const TEST_STUDENTS = [
|
||||
|
@ -11,6 +12,8 @@ export const TEST_STUDENTS = [
|
|||
{ username: 'TheDoors', firstName: 'Jim', lastName: 'Morisson' },
|
||||
// ⚠️ Deze mag niet gebruikt worden in elke test!
|
||||
{ username: 'Nirvana', firstName: 'Kurt', lastName: 'Cobain' },
|
||||
// makes sure when logged in as leerling1, there exists a corresponding user
|
||||
{ username: 'testleerling1', firstName: 'Gerald', lastName: 'Schmittinger'},
|
||||
];
|
||||
|
||||
// 🏗️ Functie die ORM entities maakt uit de data array
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue