typeorm-seeding-fixed v1.0.0-beta.6
❯ Why
Isn't it exhausting to create some sample data for your database, well this time is over!
How does it work? Just create a factory for your entities (models) and a seed script.
❯ Installation
npm i typeorm-seeding
or with yarn
yarn add typeorm-seeding
❯ Usage
1. Create a factory for your entity
For all entities we want to seed, we need to define a factory. To do so we give you the awesome faker library as a parameter into your factory. Then create your "fake" entity and return it. Those factory files should be in the src/database/factories
folder and suffixed with Factory
like src/database/factories/UserFactory.ts
.
Settings can be used to pass some static value into the factory.
define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
const gender = faker.random.number(1);
const firstName = faker.name.firstName(gender);
const lastName = faker.name.lastName(gender);
const email = faker.internet.email(firstName, lastName);
const user = new User();
user.firstName = firstName;
user.lastName = lastName;
user.email = email;
user.roles = settings.roles;
return user;
});
Handle relation in the entity factory like this.
define(Pet, (faker: typeof Faker, settings: undefined) => {
const gender = faker.random.number(1);
const name = faker.name.firstName(gender);
const pet = new Pet();
pet.name = name;
pet.age = faker.random.number();
pet.user = factory(User)({ roles: ['admin'] })
return pet;
});
2. Create a seed file
The seeds files define how much and how the data are connected with each other. The files will be executed alphabetically. With the second function, accepting your settings defined in the factories, you are able to create different variations of entities.
export class CreateUsers implements Seed {
public async seed(factory: Factory, connection: Connection): Promise<any> {
await factory(User)({ roles: [] }).createMany(10);
}
}
Here an example with nested factories. You can use the .map()
function to alter
the generated value before they get persisted.
...
await factory(User)()
.map(async (user: User) => {
const pets: Pet[] = await factory(Pet)().createMany(2);
const petIds = pets.map((pet: Pet) => pet.Id);
await user.pets().attach(petIds);
})
.createMany(5);
...
To deal with relations you can use the entity manager like this.
export class CreatePets implements SeedsInterface {
public async seed(factory: FactoryInterface, connection: Connection): Promise<any> {
const connection = await factory.getConnection();
const em = connection.createEntityManager();
await times(10, async (n) => {
// This creates a pet in the database
const pet = await factory(Pet)().create();
// This only returns a entity with fake data
const user = await factory(User)({ roles: ['admin'] }).make();
user.pets = [pet];
await em.save(user);
});
}
}
❯ Example
A good example is in the express-typescript-boilerplate repository.
❯ License
6 years ago