npm i loopback4-seeders -D
Now go to package.json, and add a new script for seeding
"scripts": {
"seed": "lb4-seed"
}
By default the app will get seeders files from src/seeders
folder
- First we need to create a new folder called
seeders
insrc
folder - After that we need to create
seeders/index.ts
to export every seeder we will create - Create a new file ends with
.seeder.ts
, in my case will betest.seeder.ts
- In our new file lets create a new class extending
SeedersBase
like the following code
export class TestSeeder extends SeedersBase {
//repository
public repository: EntityCrudRepository<Test, typeof Test.prototype.id> = TestRepository as any;
constructor(app: any) {
super(app);
}
async run() {
// logic
}
}
- In repository property, just add your repository
- Now we need to export that class in
index.ts
we have created before
export * from "./test.seeder";
Finally lets seed our database by hitting the following command :
npm run build && npm run seed
You must see a seeding data message, then a successfull message
Started to seeding data
Seeded data successfull