Skip to content

Commit 7324279

Browse files
author
Gery Hirschfeld
authored
Merge branch 'master' into docs/make-clearer
2 parents bc55097 + 9b614d4 commit 7324279

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

README.md

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ or with yarn
4848
yarn add typeorm-seeding
4949
```
5050

51+
Optional, for `Faker` types
52+
```bash
53+
npm install -D @types/faker
54+
```
55+
5156
### Configuration
5257

5358
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 {
8388
}
8489
```
8590

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+
86108
### Using Model Factories
87109

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`.
89111

90112
Settings can be used to pass some static value into the factory.
91113

92114
```typescript
115+
import Faker from 'faker';
116+
import { define } from "typeorm-seeding";
117+
import { User } from '../entities'
118+
93119
define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
94120
const gender = faker.random.number(1)
95121
const firstName = faker.name.firstName(gender)
@@ -108,6 +134,10 @@ define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
108134
Handle relation in the entity factory like this.
109135

110136
```typescript
137+
import Faker from 'faker';
138+
import { define } from 'typeorm-seeding';
139+
import { Pet } from '../entities'
140+
111141
define(Pet, (faker: typeof Faker, settings: undefined) => {
112142
const gender = faker.random.number(1)
113143
const name = faker.name.firstName(gender)
@@ -130,7 +160,7 @@ import { User } from '../entities'
130160

131161
export default class CreateUsers implements Seeder {
132162
public async run(factory: Factory, connection: Connection): Promise<any> {
133-
await factory(User)({ roles: [] }).createMany(10)
163+
await factory(User)({ roles: [] }).seedMany(10)
134164
}
135165
}
136166
```
@@ -142,11 +172,11 @@ the generated value before they get persisted.
142172
...
143173
await factory(User)()
144174
.map(async (user: User) => {
145-
const pets: Pet[] = await factory(Pet)().createMany(2);
175+
const pets: Pet[] = await factory(Pet)().seedMany(2);
146176
const petIds = pets.map((pet: Pet) => pet.Id);
147177
await user.pets().attach(petIds);
148178
})
149-
.createMany(5);
179+
.seedMany(5);
150180
...
151181
```
152182

@@ -157,21 +187,20 @@ and `.seed()` methods, or as second argument in the `.makeMany()` and `.seedMany
157187
```typescript
158188
...
159189
await factory(User)()
160-
.createMany(10, { roles: ['admin'], firstName: 'John' });
190+
.seedMany(10, { roles: ['admin'], firstName: 'John' });
161191
...
162192
```
163193

164194
To deal with relations you can use the entity manager like this.
165195

166196
```typescript
167197
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> {
170199
const em = connection.createEntityManager()
171200

172201
await times(10, async n => {
173202
// This creates a pet in the database
174-
const pet = await factory(Pet)().create()
203+
const pet = await factory(Pet)().seed()
175204
// This only returns a entity with fake data
176205
const user = await factory(User)({ roles: ['admin'] }).make()
177206
user.pets = [pet]
@@ -181,17 +210,6 @@ export default class CreatePets implements Seeder {
181210
}
182211
```
183212

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-
195213
Now you are able to execute your seeds with this command `npm run seed`.
196214

197215
**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`.
203221
| `--class` or `--c` | null | Option to specify a specific seeder class to run individually |
204222
| `--config` | `ormconfig.js` | Path to the typeorm config file (json or js). |
205223

224+
206225
## ❯ License
207226

208227
[MIT](/LICENSE)

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "TypeORM seeding",
55
"main": "dist/typeorm-seeding.js",
66
"module": "dist/typeorm-seeding.es.js",
7+
"bin": {
8+
"typeorm-seeding": "dist/cli.js"
9+
},
710
"files": [
811
"dist"
912
],

src/connection.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ export const getConnectionOptions = async (configPath = 'ormconfig.js'): Promise
1717
}
1818

1919
export const createConnection = async (configPath: string): Promise<Connection> => {
20-
const options = await getConnectionOptions(configPath)
20+
let options = await getConnectionOptions(configPath)
21+
if (Array.isArray(options)) {
22+
options.forEach(item => {
23+
if (item.name === 'default') {
24+
options = item
25+
}
26+
})
27+
}
2128
return createTypeORMConnection(options as TypeORMConnectionOptions)
2229
}

src/entity-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ export class EntityFactory<Entity, Settings> {
9494
for (const attribute in entity) {
9595
if (entity.hasOwnProperty(attribute)) {
9696
if (isPromiseLike(entity[attribute])) {
97-
entity[attribute] = await entity[attribute]
97+
entity[attribute] = entity[attribute]
9898
}
9999

100-
if (typeof entity[attribute] === 'object' && !(entity[attribute] instanceof Date)) {
100+
if (entity[attribute] && typeof entity[attribute] === 'object' && !(entity[attribute] instanceof Date)) {
101101
const subEntityFactory = entity[attribute]
102102
try {
103103
if (typeof (subEntityFactory as any).make === 'function') {

0 commit comments

Comments
 (0)