Skip to content

Commit 5f58109

Browse files
committed
Update readme
1 parent 50671e6 commit 5f58109

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

README.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,14 @@ export class UserService {
255255
256256
## Seeding
257257
258-
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 seed script.
258+
Isn't it exhausting to create some sample data for your database, well this time is over!
259+
260+
How does it work? Just create a factory for your entities (models) and a seed script.
260261
261262
### 1. Create a factory for your entity
262263
263-
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+
For all 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 and return it. Those factory files should be in the `src/database/factories` folder and suffixed with `Factory` like `src/database/factories/UserFactory.ts`.
265+
264266
Settings can be used to pass some static value into the factory.
265267
266268
```typescript
@@ -279,29 +281,7 @@ define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
279281
});
280282
```
281283
282-
To deal with relations you can use the entity manager like this.
283-
284-
```typescript
285-
export class CreatePets implements SeedsInterface {
286-
287-
public async seed(factory: FactoryInterface, connection: Connection): Promise<any> {
288-
const connection = await factory.getConnection();
289-
const em = connection.createEntityManager();
290-
291-
await times(10, async (n) => {
292-
// This creates a pet in the database
293-
const pet = await factory(Pet)().create();
294-
// This only returns a entity with fake data
295-
const user = await factory(User)({ roles: ['admin'] }).make();
296-
user.pets = [pet];
297-
await em.save(user);
298-
});
299-
}
300-
301-
}
302-
```
303-
304-
Or you could do the relation in the entity factory like this.
284+
Handle relation in the entity factory like this.
305285
306286
```typescript
307287
define(Pet, (faker: typeof Faker, settings: undefined) => {
@@ -319,7 +299,7 @@ define(Pet, (faker: typeof Faker, settings: undefined) => {
319299
### 2. Create a seed file
320300
321301
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.
302+
With the second function, accepting your settings defined in the factories, you are able to create different variations of entities.
323303
324304
```typescript
325305
export class CreateUsers implements Seed {
@@ -346,6 +326,28 @@ await factory(User)()
346326
...
347327
```
348328
329+
To deal with relations you can use the entity manager like this.
330+
331+
```typescript
332+
export class CreatePets implements SeedsInterface {
333+
334+
public async seed(factory: FactoryInterface, connection: Connection): Promise<any> {
335+
const connection = await factory.getConnection();
336+
const em = connection.createEntityManager();
337+
338+
await times(10, async (n) => {
339+
// This creates a pet in the database
340+
const pet = await factory(Pet)().create();
341+
// This only returns a entity with fake data
342+
const user = await factory(User)({ roles: ['admin'] }).make();
343+
user.pets = [pet];
344+
await em.save(user);
345+
});
346+
}
347+
348+
}
349+
```
350+
349351
### 3. Run the seeder
350352
351353
The last step is the easiest, just hit the following command in your terminal, but be sure you are in the projects root folder.
@@ -359,11 +361,11 @@ npm start db.seed
359361
| Command | Description |
360362
| --------------------------------------------------- | ----------- |
361363
| `npm start "db.seed"` | Run all seeds |
362-
| `npm start "db.seed --list CreateBruce,CreatePets"` | List seeds to run |
363-
| `npm start "db.seed -L` | Log database queries to the terminal |
364-
| `npm start "db.seed --factories` | Add a different path to your factories (Default: `src/database/`) |
365-
| `npm start "db.seed --seeds` | Add a different path to your seeds (Default: `src/database/seeds/`) |
366-
| `npm start "db.seed --config` | Path to your ormconfig.json file |
364+
| `npm start "db.seed --run CreateBruce,CreatePets"` | Run specific seeds (file names without extension) |
365+
| `npm start "db.seed -L"` | Log database queries to the terminal |
366+
| `npm start "db.seed --factories <path>"` | Add a different path to your factories (Default: `src/database/`) |
367+
| `npm start "db.seed --seeds <path>"` | Add a different path to your seeds (Default: `src/database/seeds/`) |
368+
| `npm start "db.seed --config <file>"` | Path to your ormconfig.json file |
367369
368370
## Run in Docker container
369371

src/lib/seed/EntityFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class EntityFactory<Entity, Settings> {
2020
// -------------------------------------------------------------------------
2121

2222
/**
23-
* This fucntion is used to alter the generated values of entity, before it
23+
* This function is used to alter the generated values of entity, before it
2424
* is persist into the database
2525
*/
2626
public map(mapFunction: (entity: Entity) => Promise<Entity>): EntityFactory<Entity, Settings> {
@@ -29,7 +29,7 @@ export class EntityFactory<Entity, Settings> {
2929
}
3030

3131
/**
32-
* Make generate a new entity, but does not persist it
32+
* Make a new entity, but does not persist it
3333
*/
3434
public async make(): Promise<Entity> {
3535
if (this.factory) {
@@ -43,7 +43,7 @@ export class EntityFactory<Entity, Settings> {
4343
}
4444

4545
/**
46-
* Seed persist and generates a given entity
46+
* Seed makes a new entity and does persist it
4747
*/
4848
public async seed(): Promise<Entity> {
4949
const connection: Connection = (global as any).seeder.connection;

src/lib/seed/cli.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ commander
1414
.option('-L, --logging', 'enable sql query logging')
1515
.option('--factories <path>', 'add filepath for your factories')
1616
.option('--seeds <path>', 'add filepath for your seeds')
17-
.option('--list <seeds>', 'list seeds to seed', (val) => val.split(','))
18-
.option('--config <filepath>', 'add filepath to your database config (must be a json)')
17+
.option('--run <seeds>', 'run specific seeds (file names without extension)', (val) => val.split(','))
18+
.option('--config <file>', 'path to your ormconfig.json file (must be a json)')
1919
.parse(process.argv);
2020

2121
// Get cli parameter for a different factory path
@@ -29,8 +29,8 @@ const seedsPath = (commander.seeds)
2929
: 'src/database/seeds/';
3030

3131
// Get a list of seeds
32-
const listOfSeeds = (commander.list)
33-
? commander.list.map(l => l.trim()).filter(l => l.length > 0)
32+
const listOfSeeds = (commander.run)
33+
? commander.run.map(l => l.trim()).filter(l => l.length > 0)
3434
: [];
3535

3636
// Search for seeds and factories

0 commit comments

Comments
 (0)