I currently have a parent entity called CostCenter
, which contains an Array of Coordinators
.
The attribute
coordinators: Array<Coordinator>
in the CostCenter class has typeORM annotations specified as follows:
export class CostCenter {
...
@OneToMany(() => Coordinator, (coordinator) => coordinator.costCenter, {
eager: true,
cascade: ['insert', 'update'],
})
coordinators: Array<Coordinator>;
...
}
The Coordinator class is defined as:
export class Coordinator {
...
@ManyToOne(() => CostCenter, (costCenter) => costCenter.coordinators, {
cascade: ['insert', 'update'],
})
costCenter: CostCenter;
...
}
I am facing an issue while trying to save a CostCenter along with its array of coordinators. The code snippet for this operation is given below:
async create(createCostCenterDto: CreateCostCenterDto) {
const costCenter = new CostCenter(
createCostCenterDto.code,
createCostCenterDto.description,
createCostCenterDto.id,
);
var coordinators = new Array<Coordinator>();
await createCostCenterDto.coordinators.forEach((coordinator) => {
coordinators.push(
new Coordinator(
coordinator.name,
coordinator.email,
coordinator.password,
coordinator.id,
coordinator.telephone,
coordinator.registration,
coordinator.course,
coordinator.graduation,
),
);
this.coordinatorRepository.save(coordinator);
});
costCenter.coordinators = coordinators;
return this.costCentersRepository.insert(costCenter);
}
Despite following the typeORM documentation's instructions on cascades (), the code seems to have an issue where the coordinators are not properly linked to their corresponding costCenter entities. Even after trying different approaches like commenting out certain lines, the desired behavior was not achieved.
If anyone could provide assistance or suggestions on improving this functionality, it would be greatly appreciated.