fix(backend): nodeNumber handmatig invullen

Om MikroORM bug die optreedt bij het persisteren van een kind van een entity met automatisch gegenereerde PK te vermijden.
This commit is contained in:
Gerald Schmittinger 2025-04-16 10:53:30 +02:00
parent ffa0915647
commit a803b45046
5 changed files with 28 additions and 11 deletions

View file

@ -5,12 +5,12 @@ import { Language } from '@dwengo-1/common/util/language';
@Entity()
export class LearningPathNode {
@ManyToOne({ entity: () => LearningPath, primary: true })
learningPath!: Rel<LearningPath>;
@PrimaryKey({ type: 'integer', autoincrement: true })
nodeNumber?: number;
@ManyToOne({ entity: () => LearningPath, primary: true })
learningPath!: Rel<LearningPath>;
@Property({ type: 'string' })
learningObjectHruid!: string;
@ -27,7 +27,7 @@ export class LearningPathNode {
startNode!: boolean;
@OneToMany({ entity: () => LearningPathTransition, mappedBy: 'node' })
transitions: Collection<LearningPathTransition> = new Collection<LearningPathTransition>(this);
transitions!: Collection<LearningPathTransition>;
@Property({ length: 3 })
createdAt: Date = new Date();

View file

@ -3,12 +3,12 @@ import { LearningPathNode } from './learning-path-node.entity.js';
@Entity()
export class LearningPathTransition {
@ManyToOne({ entity: () => LearningPathNode, primary: true })
node!: Rel<LearningPathNode>;
@PrimaryKey({ type: 'numeric' })
transitionNumber!: number;
@ManyToOne({ entity: () => LearningPathNode, primary: true })
node!: Rel<LearningPathNode>;
@Property({ type: 'string' })
condition!: string;

View file

@ -29,10 +29,11 @@ export function mapToLearningPath(
admins,
image: dto.image ? Buffer.from(base64ToArrayBuffer(dto.image)) : null
});
const nodes = dto.nodes.map((nodeDto: LearningObjectNode) =>
const nodes = dto.nodes.map((nodeDto: LearningObjectNode, i: number) =>
repo.createNode({
learningPath: path,
learningObjectHruid: nodeDto.learningobject_hruid,
nodeNumber: i,
language: nodeDto.language,
version: nodeDto.version,
startNode: nodeDto.start_node ?? false,

View file

@ -1,5 +1,9 @@
import fs from 'fs';
import path from 'node:path';
import {fileURLToPath} from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Load the asset at the given path.

View file

@ -49,12 +49,24 @@ export async function seedDatabase(): Promise<void> {
const answers = makeTestAnswers(em, teachers, questions);
const submissions = makeTestSubmissions(em, students, groups);
// Persist all entities
await em.persistAndFlush([
/*await em.persistAndFlush([
...students,
...teachers,
...learningObjects,
...learningPaths,
]);*/
try {
await em.persistAndFlush(learningPaths[0]);
} catch (e) {
"hey";
}
/*await em.persistAndFlush(learningPaths[1]);
await em.persistAndFlush(learningPaths[2]);
await em.persistAndFlush(learningPaths[3]);
await em.persistAndFlush([
...classes,
...assignments,
...groups,
@ -64,7 +76,7 @@ export async function seedDatabase(): Promise<void> {
...questions,
...answers,
...submissions,
]);
])*/
logger.info('Development database seeded successfully!');