@@ -49,6 +49,7 @@ yarn add typeorm-seeding
4949```
5050
5151Optional, for ` Faker ` types
52+
5253``` bash
5354npm install -D @types/faker
5455```
@@ -60,11 +61,18 @@ To configure the path to your seeds and factories change the TypeORM config file
6061``` JavaScript
6162module .exports = {
6263 ...
63- seeds: [' src/seeds/**/*.seed.ts ' ],
64- factories: [' src/factories/**/*.factory.ts ' ],
64+ seeds: [' src/seeds/**/*{.ts,.js} ' ],
65+ factories: [' src/factories/**/*{.ts,.js} ' ],
6566}
6667```
6768
69+ or add the following variables to your ` .env ` file.
70+
71+ ```
72+ TYPEORM_SEEDING_FACTORIES=src/seeds/**/*{.ts,.js}
73+ TYPEORM_SEEDING_SEEDS=src/factories/**/*{.ts,.js}
74+ ```
75+
6876![ divider] ( ./w3tec-divider.png )
6977
7078## ❯ Writing Seeders
@@ -82,7 +90,10 @@ export default class CreateUsers implements Seeder {
8290 .createQueryBuilder ()
8391 .insert ()
8492 .into (User )
85- .values ([{ firstName: ' Timber' , lastName: ' Saw' }, { firstName: ' Phantom' , lastName: ' Lancer' }])
93+ .values ([
94+ { firstName: ' Timber' , lastName: ' Saw' },
95+ { firstName: ' Phantom' , lastName: ' Lancer' },
96+ ])
8697 .execute ()
8798 }
8899}
@@ -99,7 +110,7 @@ Once you have written your seeder, you can add this script to your `package.json
99110 }
100111```
101112
102- And then
113+ And then
103114
104115``` bash
105116npm run seed
@@ -112,8 +123,8 @@ For all entities we want to seed, we need to define a factory. To do so we give
112123Settings can be used to pass some static value into the factory.
113124
114125``` typescript
115- import Faker from ' faker' ;
116- import { define } from " typeorm-seeding" ;
126+ import Faker from ' faker'
127+ import { define } from ' typeorm-seeding'
117128import { User } from ' ../entities'
118129
119130define (User , (faker : typeof Faker , settings : { roles: string [] }) => {
@@ -134,8 +145,8 @@ define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
134145Handle relation in the entity factory like this.
135146
136147``` typescript
137- import Faker from ' faker' ;
138- import { define } from ' typeorm-seeding' ;
148+ import Faker from ' faker'
149+ import { define } from ' typeorm-seeding'
139150import { Pet } from ' ../entities'
140151
141152define (Pet , (faker : typeof Faker , settings : undefined ) => {
@@ -145,7 +156,7 @@ define(Pet, (faker: typeof Faker, settings: undefined) => {
145156 const pet = new Pet ()
146157 pet .name = name
147158 pet .age = faker .random .number ()
148- pet .user = factory (User )({ roles: [' admin' ] })
159+ pet .user = factory (User )({ roles: [' admin' ] }) as any
149160 return pet
150161})
151162```
@@ -198,7 +209,7 @@ export default class CreatePets implements Seeder {
198209 public async run(factory : Factory , connection : Connection ): Promise <any > {
199210 const em = connection .createEntityManager ()
200211
201- await times (10 , async n => {
212+ await times (10 , async ( n ) => {
202213 // This creates a pet in the database
203214 const pet = await factory (Pet )().seed ()
204215 // This only returns a entity with fake data
@@ -216,11 +227,12 @@ Now you are able to execute your seeds with this command `npm run seed`.
216227
217228### CLI Options
218229
219- | Option | Default | Description |
220- | ------------------ | -------------- | ------------------------------------------------------------- |
221- | ` --class ` or ` --c ` | null | Option to specify a specific seeder class to run individually |
222- | ` --config ` | ` ormconfig.js ` | Path to the typeorm config file (json or js). |
223-
230+ | Option | Default | Description |
231+ | ---------------------- | --------------- | --------------------------------------------------------------------------- |
232+ | ` --seed ` or ` -s ` | null | Option to specify a specific seeder class to run individually. |
233+ | ` --connection ` or ` -c ` | null | Name of the typeorm connection. Required if there are multiple connections. |
234+ | ` --configName ` or ` -n ` | ` ormconfig.js ` | Name to the typeorm config file. |
235+ | ` --root ` or ` -r ` | ` process.cwd() ` | Path to the typeorm config file. |
224236
225237## ❯ License
226238
0 commit comments