4

I have my application with this directory structure.

App
|-- src
    |-- modules
        |-- user
        |-- role
        |-- company
        |-- ...
        |-- app.module.ts
        |-- main.js (Application Bootstrap)
        |-- seed.js
    |-- ...other files

I created a file with the following content:

seed.ts

enter image description here

I want to insert data at the beginning of the application to complete with data some tables of my databases that are needed for the application to work.

Thanks for the help!

  • 9
    Please don't post pictures, but rather code snippets here on StackOverflow. Code snippets can be copied and checked locally while screenshots and pictures can't be. Also, what's wrong with above? What is it not doing that you want it to? Do you get any errors? Does something just not happen? There's not really much of a question or problem at the moment – Jay McDoniel Jan 18 '21 at 05:35

1 Answers1

22

Well, let's make it easier, You have two options for seeding, use queryRunner.manager.insert() in your migration files, and make application to run migrations every time you run it!

In AppService implement OnApplicationBootstrap and then invoke its function, whatever you write in that function will be called each time you run the application, here's a sample:


@Injectable()
export class AppService implements OnApplicationBootstrap{
  onApplicationBootstrap(): any {
  // add a functionality to check if the data already exists, if not add it manually
  }
}

Hossein Heydari
  • 1,439
  • 9
  • 28