jelly5798
jelly5798

Reputation: 349

Why does my NestJS seeding script want other modules?

I have a NestJS app and I want to create a seeding script for status. Here is my script:

import { NestFactory } from '@nestjs/core';
import { AppModule } from '../../app.module';
import { Status } from '../../status/entities/status.entity';
import { DataSource } from 'typeorm';

async function seed() {
  const app = await NestFactory.createApplicationContext(AppModule);

  const dataSource = app.get(DataSource);

  const statusRepository = dataSource.getRepository(Status);

  const statuses = [
    { name: 'Pending', codeStub: 'PENDING' },
    { name: 'Approved', codeStub: 'APPROVED' },
    { name: 'Processing', codeStub: 'PROCESSING' },
    { name: 'Completed', codeStub: 'COMPLETED' },
  ];

  await statusRepository.upsert(statuses, ['codeStub']);


  console.log('Statuses have been seeded');
  await app.close();
}

seed().catch((error) => {
  console.error(error);
  process.exit(1);
});

The script above resides in /src/database/seeds. The Status entity links to one other entity, which then goes on to link to other entities etc.

When I run the above script using the command below I get a lot of messages stating that modules cannot be found. These modules are not required for the status and in some cases are not directly associated with the status.

npx ts-node src/database/seeds/status.seed.ts

I'm not entirely sure why the seed script needs them and how to overcome the issue?

Upvotes: 0

Views: 39

Answers (0)

Related Questions