Skip to content

Commit 1df5b29

Browse files
authored
Merge pull request #55 from fabriciovergal/patch-1
Seed entity with lazy relations
2 parents d8f99f0 + 5de8c95 commit 1df5b29

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/lib/seeds/EntityFactory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class EntityFactory<Entity> implements EntityFactoryInterface<Entity> {
7171
private async makeEntity(entity: Entity): Promise<Entity> {
7272
for (const attribute in entity) {
7373
if (entity.hasOwnProperty(attribute)) {
74+
if (this.isPromiseLike(entity[attribute])) {
75+
entity[attribute] = await entity[attribute];
76+
}
77+
7478
if (typeof entity[attribute] === 'object' && entity[attribute] instanceof EntityFactory) {
7579
const subEntityFactory = entity[attribute];
7680
const subEntity = await (subEntityFactory as any).build();
@@ -81,4 +85,7 @@ export class EntityFactory<Entity> implements EntityFactoryInterface<Entity> {
8185
return entity;
8286
}
8387

88+
private isPromiseLike(object: any): boolean {
89+
return !!object && (typeof object === 'object' || typeof object === 'function') && typeof object.then === 'function';
90+
}
8491
}

0 commit comments

Comments
 (0)