In my database i have a table Applications, and i created a new table ApplicationStatus that have a OneToOne relationship to Application using TypeORM.
To seed the status for all applications, i use nest-commander and i created a Command that gets all the applications, and for each one, creates an applicationStatus instance and links it to it. This command is part if the ApplicationModule that is imported in AppModule. The command and its dependencies are as follows:
seed-applications-status.command.ts
constructor(
@InjectRepository(Application)
private applicationRepository: Repository<Application>,
private readonly applicationService: ApplicationService,
private readonly applicationStatusService: ApplicationStatusService,
) {
super();
}
async run(inputs: string[], options: Record<string, any>): Promise<void> {
/* Works.. */
const applications = await this.applicationService.findAll();
/* doesn't work.. */
applications.forEach(async (application: Application) => {
const applicationStatus = await this.applicationStatusService.create(
application,
);
application.status = applicationStatus;
this.applicationRepository.save(application);
});
return;
}
application-status.service.ts
constructor(
@InjectRepository(ApplicationStatus)
private readonly applicationStatusRepository: Repository<ApplicationStatus>,
) {}
async create(application: Application) {
const applicationStatus = this.applicationStatusRepository.create({
status: 'PENDING',
cnieStatus: 'PENDING',
schoolCertificateStatus: 'PENDING',
regulationsStatus: 'PENDING',
gradesStatus: 'PENDING',
});
applicationStatus.application = application;
await this.applicationStatusRepository.save(applicationStatus);
return applicationStatus;
}
I run the command with ts-node thourgh a separate main.ts file:
main-cli.ts
async function bootstrap() {
await CommandFactory.run(AppModule, {
logger: ['warn', 'error'],
});
}
bootstrap();
The command gets me the applications without problems, but when i try to create an applicationStatus for each one of them, i have the following error:
it seems that even when i get all the instances of applicationStatus it works fine, but it does not work when creating an instance. I seem to have missed something. Thank you for your insights.