Skip to content

Commit c83b40f

Browse files
authored
Merge pull request #107 from sfelix-martins/master
Add typeorm SaveOptions to `create` and `createMany` factory methods
2 parents a7a53ff + e52faab commit c83b40f

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,11 @@ await factory(User)().makeMany(10, { email: '[email protected]' })
308308
the create and createMany method is similar to the make and makeMany method, but at the end the created entity instance gets persisted in the database.
309309

310310
**overrideParams** - Override some of the attributes of the entity.
311+
**saveOptions** - [Save options](https://github.com/typeorm/typeorm/blob/master/src/repository/SaveOptions.ts) from typeorm
311312

312313
```typescript
313-
create(overrideParams: EntityProperty<Entity> = {}): Promise<Entity>
314+
create(overrideParams: EntityProperty<Entity> = {}, saveOptions?: SaveOptions): Promise<Entity>
315+
createMany(amount: number, overrideParams: EntityProperty<Entity> = {}, saveOptions?: SaveOptions): Promise<Entity>
314316
```
315317

316318
```typescript
@@ -320,6 +322,10 @@ await factory(User)().createMany(10)
320322
// override the email
321323
await factory(User)().create({ email: '[email protected]' })
322324
await factory(User)().createMany(10, { email: '[email protected]' })
325+
326+
// using save options
327+
await factory(User)().create({ email: '[email protected]' }, { listeners: false })
328+
await factory(User)().createMany(10, { email: '[email protected]' }, { listeners: false })
323329
```
324330

325331
## ❯ Seeding Data in Testing

src/entity-factory.ts

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Faker from 'faker'
2-
import { ObjectType } from 'typeorm'
2+
import { ObjectType, SaveOptions } from 'typeorm'
33
import { FactoryFunction, EntityProperty } from './types'
44
import { isPromiseLike } from './utils/factory.util'
55
import { printError, printWarning } from './utils/log.util'
@@ -38,14 +38,14 @@ export class EntityFactory<Entity, Context> {
3838
/**
3939
* Create makes a new entity and does persist it
4040
*/
41-
public async create(overrideParams: EntityProperty<Entity> = {}): Promise<Entity> {
41+
public async create(overrideParams: EntityProperty<Entity> = {}, saveOptions?: SaveOptions): Promise<Entity> {
4242
const option = await getConnectionOptions()
4343
const connection = await createConnection(option)
4444
if (connection && connection.isConnected) {
4545
const em = connection.createEntityManager()
4646
try {
4747
const entity = await this.makeEnity(overrideParams, true)
48-
return await em.save<Entity>(entity)
48+
return await em.save<Entity>(entity, saveOptions)
4949
} catch (error) {
5050
const message = 'Could not save entity'
5151
printError(message, error)
@@ -66,10 +66,14 @@ export class EntityFactory<Entity, Context> {
6666
return list
6767
}
6868

69-
public async createMany(amount: number, overrideParams: EntityProperty<Entity> = {}): Promise<Entity[]> {
69+
public async createMany(
70+
amount: number,
71+
overrideParams: EntityProperty<Entity> = {},
72+
saveOptions?: SaveOptions,
73+
): Promise<Entity[]> {
7074
const list = []
7175
for (let index = 0; index < amount; index++) {
72-
list[index] = await this.create(overrideParams)
76+
list[index] = await this.create(overrideParams, saveOptions)
7377
}
7478
return list
7579
}
@@ -85,50 +89,52 @@ export class EntityFactory<Entity, Context> {
8589
}
8690

8791
// -------------------------------------------------------------------------
88-
// Prrivat Helpers
92+
// Private Helpers
8993
// -------------------------------------------------------------------------
9094

9195
private async makeEnity(overrideParams: EntityProperty<Entity> = {}, isSeeding = false): Promise<Entity> {
92-
if (this.factory) {
93-
let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding)
94-
if (this.mapFunction) {
95-
entity = await this.mapFunction(entity)
96-
}
96+
if (!this.factory) {
97+
throw new Error('Could not found entity')
98+
}
9799

98-
for (const key in overrideParams) {
99-
if (overrideParams.hasOwnProperty(key)) {
100-
entity[key] = overrideParams[key]
101-
}
102-
}
100+
let entity = await this.resolveEntity(this.factory(Faker, this.context), isSeeding)
101+
if (this.mapFunction) {
102+
entity = await this.mapFunction(entity)
103+
}
103104

104-
return entity
105+
for (const key in overrideParams) {
106+
if (overrideParams.hasOwnProperty(key)) {
107+
entity[key] = overrideParams[key]
108+
}
105109
}
106-
throw new Error('Could not found entity')
110+
111+
return entity
107112
}
108113

109114
private async resolveEntity(entity: Entity, isSeeding = false): Promise<Entity> {
110115
for (const attribute in entity) {
111-
if (entity.hasOwnProperty(attribute)) {
112-
if (isPromiseLike(entity[attribute])) {
113-
entity[attribute] = await entity[attribute]
114-
}
115-
if (
116-
entity[attribute] &&
117-
typeof entity[attribute] === 'object' &&
118-
entity[attribute].constructor.name === EntityFactory.name
119-
) {
120-
const subEntityFactory = entity[attribute]
121-
try {
122-
if (isSeeding) {
123-
entity[attribute] = await (subEntityFactory as any).create()
124-
} else {
125-
entity[attribute] = await (subEntityFactory as any).make()
126-
}
127-
} catch (error) {
128-
const message = `Could not make ${(subEntityFactory as any).name}`
129-
printError(message, error)
130-
throw new Error(message)
116+
if (!entity.hasOwnProperty(attribute)) {
117+
continue
118+
}
119+
if (isPromiseLike(entity[attribute])) {
120+
entity[attribute] = await entity[attribute]
121+
}
122+
if (
123+
entity[attribute] &&
124+
typeof entity[attribute] === 'object' &&
125+
entity[attribute].constructor.name === EntityFactory.name
126+
) {
127+
const subEntityFactory = entity[attribute]
128+
try {
129+
if (isSeeding) {
130+
entity[attribute] = await (subEntityFactory as any).create()
131+
} else {
132+
entity[attribute] = await (subEntityFactory as any).make()
131133
}
134+
} catch (error) {
135+
const message = `Could not make ${(subEntityFactory as any).name}`
136+
printError(message, error)
137+
throw new Error(message)
132138
}
133139
}
134140
}

0 commit comments

Comments
 (0)