Skip to content

Commit

Permalink
Merge pull request #8 from clovis1122/master
Browse files Browse the repository at this point in the history
Add parameter to override the factory-generated properties
  • Loading branch information
Gery Hirschfeld authored Jun 1, 2019
2 parents 9e9c591 + fac4d26 commit 5f09a43
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ await factory(User)()
...
```

To override specific properties in all the generated entities, you can send an additional object
containing the properties that you want to override as the first argument in the `.make()`
and `.seed()` methods, or as second argument in the `.makeMany()` and `.seedMany()` methods.

```typescript
...
await factory(User)()
.createMany(10, { roles: ['admin'], firstName: 'John' });
...
```

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

```typescript
Expand Down
21 changes: 13 additions & 8 deletions src/entity-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Faker from 'faker';
import { Connection, ObjectType } from 'typeorm';

import { FactoryFunction } from './types';
import { FactoryFunction, EntityProperty } from './types';
import { isPromiseLike } from './utils';

export class EntityFactory<Entity, Settings> {
Expand Down Expand Up @@ -31,12 +31,17 @@ export class EntityFactory<Entity, Settings> {
/**
* Make a new entity, but does not persist it
*/
public async make(): Promise<Entity> {
public async make(overrideParams: EntityProperty<Entity> = {}): Promise<Entity> {
if (this.factory) {
let entity = await this.resolveEntity(this.factory(Faker, this.settings));
if (this.mapFunction) {
entity = await this.mapFunction(entity);
}

for (const key in overrideParams) {
entity[key] = overrideParams[key];
}

return entity;
}
throw new Error('Could not found entity');
Expand All @@ -45,12 +50,12 @@ export class EntityFactory<Entity, Settings> {
/**
* Seed makes a new entity and does persist it
*/
public async seed(): Promise<Entity> {
public async seed(overrideParams: EntityProperty<Entity> = {}): Promise<Entity> {
const connection: Connection = (global as any).seeder.connection;
if (connection) {
const em = connection.createEntityManager();
try {
const entity = await this.make();
const entity = await this.make(overrideParams);
return await em.save<Entity>(entity);
} catch (error) {
throw new Error('Could not save entity');
Expand All @@ -60,18 +65,18 @@ export class EntityFactory<Entity, Settings> {
}
}

public async makeMany(amount: number): Promise<Entity[]> {
public async makeMany(amount: number, overrideParams: EntityProperty<Entity> = {}): Promise<Entity[]> {
const list = [];
for (let index = 0; index < amount; index++) {
list[index] = await this.make();
list[index] = await this.make(overrideParams);
}
return list;
}

public async seedMany(amount: number): Promise<Entity[]> {
public async seedMany(amount: number, overrideParams: EntityProperty<Entity> = {}): Promise<Entity[]> {
const list = [];
for (let index = 0; index < amount; index++) {
list[index] = await this.seed();
list[index] = await this.seed(overrideParams);
}
return list;
}
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { EntityFactory } from './entity-factory';
*/
export type FactoryFunction<Entity, Settings> = (faker: typeof Faker, settings?: Settings) => Entity;

/**
* EntityProperty defines an object whose keys and values must be properties of the given Entity.
*/
export type EntityProperty<Entity> = { [Property in keyof Entity]?: Entity[Property] };

/**
* Factory gets the EntityFactory to the given Entity and pass the settings along
*/
Expand Down

0 comments on commit 5f09a43

Please sign in to comment.