loopback4-seeders
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Loopback 4 Seeders

LoopBack

Installation

npm i loopback4-seeders -D

Now go to package.json, and add a new script for seeding

"scripts": {
    "seed": "lb4-seed"
}

Now lets create our first seeder

By default the app will get seeders files from src/seeders folder

  • First we need to create a new folder called seeders in src 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 be test.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

License

MIT

Package Sidebar

Install

npm i loopback4-seeders

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

11.9 kB

Total Files

13

Last publish

Collaborators

  • hemdan-dev