@@ -48,6 +48,11 @@ or with yarn
48
48
yarn add typeorm-seeding
49
49
```
50
50
51
+ Optional, for ` Faker ` types
52
+ ``` bash
53
+ npm install -D @types/faker
54
+ ```
55
+
51
56
### Configuration
52
57
53
58
To configure the path to your seeds and factories change the TypeORM config file(ormconfig.js or ormconfig.json).
@@ -83,13 +88,34 @@ export default class CreateUsers implements Seeder {
83
88
}
84
89
```
85
90
91
+ ## ❯ Running Seeders
92
+
93
+ Once you have written your seeder, you can add this script to your ` package.json ` .
94
+
95
+ ```
96
+ "scripts": {
97
+ "seed": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
98
+ ...
99
+ }
100
+ ```
101
+
102
+ And then
103
+
104
+ ``` bash
105
+ npm run seed
106
+ ```
107
+
86
108
### Using Model Factories
87
109
88
- 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 ` .
110
+ 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/User.factory .ts ` .
89
111
90
112
Settings can be used to pass some static value into the factory.
91
113
92
114
``` typescript
115
+ import Faker from ' faker' ;
116
+ import { define } from " typeorm-seeding" ;
117
+ import { User } from ' ../entities'
118
+
93
119
define (User , (faker : typeof Faker , settings : { roles: string [] }) => {
94
120
const gender = faker .random .number (1 )
95
121
const firstName = faker .name .firstName (gender )
@@ -108,6 +134,10 @@ define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
108
134
Handle relation in the entity factory like this.
109
135
110
136
``` typescript
137
+ import Faker from ' faker' ;
138
+ import { define } from ' typeorm-seeding' ;
139
+ import { Pet } from ' ../entities'
140
+
111
141
define (Pet , (faker : typeof Faker , settings : undefined ) => {
112
142
const gender = faker .random .number (1 )
113
143
const name = faker .name .firstName (gender )
@@ -130,7 +160,7 @@ import { User } from '../entities'
130
160
131
161
export default class CreateUsers implements Seeder {
132
162
public async run(factory : Factory , connection : Connection ): Promise <any > {
133
- await factory (User )({ roles: [] }).createMany (10 )
163
+ await factory (User )({ roles: [] }).seedMany (10 )
134
164
}
135
165
}
136
166
```
@@ -142,11 +172,11 @@ the generated value before they get persisted.
142
172
...
143
173
await factory (User )()
144
174
.map (async (user : User ) => {
145
- const pets: Pet [] = await factory (Pet )().createMany (2 );
175
+ const pets: Pet [] = await factory (Pet )().seedMany (2 );
146
176
const petIds = pets .map ((pet : Pet ) => pet .Id );
147
177
await user .pets ().attach (petIds );
148
178
})
149
- .createMany (5 );
179
+ .seedMany (5 );
150
180
...
151
181
```
152
182
@@ -157,21 +187,20 @@ and `.seed()` methods, or as second argument in the `.makeMany()` and `.seedMany
157
187
``` typescript
158
188
...
159
189
await factory (User )()
160
- .createMany (10 , { roles: [' admin' ], firstName: ' John' });
190
+ .seedMany (10 , { roles: [' admin' ], firstName: ' John' });
161
191
...
162
192
```
163
193
164
194
To deal with relations you can use the entity manager like this.
165
195
166
196
``` typescript
167
197
export default class CreatePets implements Seeder {
168
- public async run(factory : FactoryInterface , connection : Connection ): Promise <any > {
169
- const connection = await factory .getConnection ()
198
+ public async run(factory : Factory , connection : Connection ): Promise <any > {
170
199
const em = connection .createEntityManager ()
171
200
172
201
await times (10 , async n => {
173
202
// This creates a pet in the database
174
- const pet = await factory (Pet )().create ()
203
+ const pet = await factory (Pet )().seed ()
175
204
// This only returns a entity with fake data
176
205
const user = await factory (User )({ roles: [' admin' ] }).make ()
177
206
user .pets = [pet ]
@@ -181,17 +210,6 @@ export default class CreatePets implements Seeder {
181
210
}
182
211
```
183
212
184
- ## ❯ Running Seeders
185
-
186
- Once you have written your seeder, you can add this script to your ` package.json ` .
187
-
188
- ```
189
- "scripts": {
190
- "seed": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
191
- ...
192
- }
193
- ```
194
-
195
213
Now you are able to execute your seeds with this command ` npm run seed ` .
196
214
197
215
** Note:** Be sure to specify which config file you are using (` ormconfig.json ` or ` ormconfig.js ` ) with the ` --config ` cli option.
@@ -203,6 +221,7 @@ Now you are able to execute your seeds with this command `npm run seed`.
203
221
| ` --class ` or ` --c ` | null | Option to specify a specific seeder class to run individually |
204
222
| ` --config ` | ` ormconfig.js ` | Path to the typeorm config file (json or js). |
205
223
224
+
206
225
## ❯ License
207
226
208
227
[ MIT] ( /LICENSE )
0 commit comments