Skip to content

Commit 09fbca0

Browse files
author
hirsch88
committed
Refactor entity factory
1 parent 819fc05 commit 09fbca0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/lib/seeds/EntityFactory.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ export class EntityFactory<Entity> implements EntityFactoryInterface<Entity> {
2424
return await this.makeEntity(this.blueprint.create(this.faker, this.args));
2525
}
2626

27+
public async makeMany(amount: number): Promise<Entity[]> {
28+
const results: Entity[] = [];
29+
for (let i = 0; i < amount; i++) {
30+
const entity = await this.makeEntity(this.blueprint.create(this.faker, this.args));
31+
if (entity) {
32+
results.push(entity);
33+
}
34+
}
35+
return results;
36+
}
37+
2738
public async create(): Promise<Entity> {
2839
const entity = await this.build();
2940
if (typeof this.eachFn === 'function') {

src/lib/seeds/EntityFactoryInterface.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ export interface EntityFactoryInterface<Entity> {
1010
* Creates a entity with faked data, but not persisted to the database.
1111
*/
1212
make(): Promise<Entity>;
13+
makeMany(amount: number): Promise<Entity[]>;
1314
/**
1415
* Creates a new faked entity in the database.
1516
*/
1617
create(): Promise<Entity>;
17-
/**
18-
* Creates an amount (default 1) of the defined entity.
19-
*/
20-
createMany(amount: number): Promise<Entity[] | undefined>;
18+
createMany(amount: number): Promise<Entity[]>;
2119
/**
2220
* This is called after creating a enity to the database. Use this to
2321
* create other seeds but combined with this enitiy.

0 commit comments

Comments
 (0)