Skip to content

Commit 0bf3110

Browse files
author
hirsch88
committed
Adjust the documentation
1 parent e74adc9 commit 0bf3110

File tree

1 file changed

+31
-47
lines changed

1 file changed

+31
-47
lines changed

README.md

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,15 @@ export class UserService {
256256
## Seeding
257257
258258
Isn't exhausting to create some sample data into your fresh migrated database, well this time is over!
259-
How does it work? Just, create a factory for your entities and a seeds script.
259+
How does it work? Just, create a factory for your entities and a seed script.
260260
261261
### 1. Create a factory for your entity
262262
263263
For all the entities we want to seed, we need to define a factory. To do so we give you the awesome [faker](https://github.com/marak/Faker.js/) library as a parameter into your factory. Then create your "fake" entity as you would normally do and return it. Those factory files should be in the `src/database/factories` folder and suffixed with `Factory`. Example `src/database/factories/UserFactory.ts`.
264+
Settings can be used to pass some static value into the factory.
264265
265266
```typescript
266-
factory.define(User, (faker: typeof Faker) => {
267+
define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
267268
const gender = faker.random.number(1);
268269
const firstName = faker.name.firstName(gender);
269270
const lastName = faker.name.lastName(gender);
@@ -273,40 +274,25 @@ factory.define(User, (faker: typeof Faker) => {
273274
user.firstName = firstName;
274275
user.lastName = lastName;
275276
user.email = email;
277+
user.roles = settings.roles;
276278
return user;
277279
});
278280
```
279281
280-
This can be used to pass some dynamic value into the factory.
281-
282-
```typescript
283-
factory.define(Pet, (faker: typeof Faker, args: any[]) => {
284-
const type = args[0];
285-
return {
286-
name: faker.name.firstName(),
287-
type: type || 'dog'
288-
};
289-
});
290-
```
291-
292282
To deal with relations you can use the entity manager like this.
293283
294284
```typescript
295-
import { SeedsInterface, FactoryInterface, times } from '../../lib/seeds';
296-
import { Pet } from '../../../src/api/models/Pet';
297-
import { User } from '../../../src/api/models/User';
298-
299285
export class CreatePets implements SeedsInterface {
300286

301-
public async seed(factory: FactoryInterface): Promise<any> {
287+
public async seed(factory: FactoryInterface, connection: Connection): Promise<any> {
302288
const connection = await factory.getConnection();
303289
const em = connection.createEntityManager();
304290

305291
await times(10, async (n) => {
306292
// This creates a pet in the database
307-
const pet = await factory.get(Pet).create();
293+
const pet = await factory(Pet)().create();
308294
// This only returns a entity with fake data
309-
const user = await factory.get(User).make();
295+
const user = await factory(User)({ roles: ['admin'] }).make();
310296
user.pets = [pet];
311297
await em.save(user);
312298
});
@@ -315,51 +301,49 @@ export class CreatePets implements SeedsInterface {
315301
}
316302
```
317303
318-
### 2. Create a seed file
319-
320-
The seeds files define how much and how the data are connected with each other. The files will be executed alphabetically.
304+
Or you could do the relation in the entity factory like this.
321305
322306
```typescript
323-
export class CreateUsers implements SeedsInterface {
324-
325-
public async seed(factory: FactoryInterface): Promise<any> {
326-
await factory
327-
.get(User)
328-
.createMany(10);
329-
}
307+
define(Pet, (faker: typeof Faker, settings: undefined) => {
308+
const gender = faker.random.number(1);
309+
const name = faker.name.firstName(gender);
330310

331-
}
311+
const pet = new Pet();
312+
pet.name = name;
313+
pet.age = faker.random.number();
314+
pet.user = factory(User)({ roles: ['admin'] })
315+
return pet;
316+
});
332317
```
333318
334-
With the second parameter in the `.get(<Entity>, <args>)` you are able to create different variations of entities.
319+
### 2. Create a seed file
320+
321+
The seeds files define how much and how the data are connected with each other. The files will be executed alphabetically.
322+
With the second function you are able to create different variations of entities.
323+
335324
336325
```typescript
337-
export class CreateUsers implements SeedsInterface {
326+
export class CreateUsers implements Seed {
338327

339-
public async seed(factory: FactoryInterface): Promise<any> {
340-
await factory
341-
.get(User, 'admin')
342-
.create();
328+
public async seed(factory: Factory, connection: Connection): Promise<any> {
329+
await factory(User)({ roles: [] }).createMany(10);
343330
}
344331

345332
}
346333
```
347334
348-
Here an example with nested factories.
335+
Here an example with nested factories. You can use the `.map()` function to alter
336+
the generated value before they get persisted.
349337
350338
```typescript
351339
...
352-
await factory.get(User)
353-
.each(async (user: User) => {
354-
355-
const pets: Pet[] = await factory.get(Pet)
356-
.createMany(2);
357-
340+
await factory(User)()
341+
.map(async (user: User) => {
342+
const pets: Pet[] = await factory(Pet)().createMany(2);
358343
const petIds = pets.map((pet: Pet) => pet.Id);
359344
await user.pets().attach(petIds);
360-
361345
})
362-
.create(5);
346+
.createMany(5);
363347
...
364348
```
365349

0 commit comments

Comments
 (0)