Loopback-next: Add support for seeders

Created on 1 Jul 2019  路  6Comments  路  Source: strongloop/loopback-next

Suggestion

This feature is inspired by Laravel seeding system.
We can have CLI command to generate seeders for repositories, for example in src/seeders directory.

Examples

Example for seeder file

import {UserRepository} from '../repositories';

interface Seeder {
  run(): Promise<void>;
}

export class UserSeeder implements Seeder {
  constructor(private userRepo: UserRepository) {}

  async run() {
    await this.userRepo.create({
      username: 'admin',
      password: 'password',
      email: '[email protected]',
      name: 'John Doe',
    });
  }
}

In application.ts, following what was stated in migrations docs

async migrateSchema(options?: SchemaMigrationOptions) {
  await super.migrateSchema(options);
  new UserSeeder(await this.getRepository(UserRepository)).run();
}

Of course we can enhance this feature by adding more flags to npm run migrate to allow seeding --seed.

And to achieve the maximum benefit of the seeding system, We can define factory feature in the Entity model to allow auto generated instances from models.

Acceptance criteria

TBD - will be filled by the team.

feature

Most helpful comment

@amrsalama, I've also created a blog post on how to seed a database using the life cycle observers in LoopBack 4. https://medium.com/loopback/loopback-quick-tip-seeding-database-using-life-cycle-observer-8ddca55fea5c.

All 6 comments

There is also the option of seeding via docker that is worth considering.

@amrsalama, I've also created a blog post on how to seed a database using the life cycle observers in LoopBack 4. https://medium.com/loopback/loopback-quick-tip-seeding-database-using-life-cycle-observer-8ddca55fea5c.

@amrsalama, I've also created a blog post on how to seed a database using the life cycle observers in LoopBack 4. https://medium.com/loopback/loopback-quick-tip-seeding-database-using-life-cycle-observer-8ddca55fea5c.

@dhmlau Good article. You could expand this example by using faker for other data types.

Thanks @dougal83! Good to know about this package. :)

I think this seeder capability would make a good LoopBack extension (instead of putting as part of the core). @strongloop/loopback-maintainers , what's your thoughts on that?

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marioestradarosa picture marioestradarosa  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

mhdawson picture mhdawson  路  3Comments

zero-bugs picture zero-bugs  路  3Comments