-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Factory.ts
85 lines (72 loc) · 2.83 KB
/
Factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { Faker } from '@faker-js/faker';
import { faker } from '@faker-js/faker';
import type { RequiredEntityData, EntityData, EntityManager, Constructor } from '@mikro-orm/core';
export abstract class Factory<T extends object> {
abstract readonly model: Constructor<T>;
private eachFunction?: (entity: T) => void;
constructor(private readonly em: EntityManager) { }
protected abstract definition(faker: Faker): EntityData<T>;
/**
* Make a single entity instance, without persisting it.
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
makeEntity(overrideParameters?: EntityData<T>): T {
const entity = this.em.create(this.model, {
...this.definition(faker),
...overrideParameters,
} as unknown as RequiredEntityData<T>, { persist: false });
if (this.eachFunction) {
this.eachFunction(entity);
}
return entity;
}
/**
* Make a single entity and persist (not flush)
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
makeOne(overrideParameters?: EntityData<T>): T {
const entity = this.makeEntity(overrideParameters);
this.em.persist(entity);
return entity;
}
/**
* Make multiple entities and then persist them (not flush)
* @param amount Number of entities that should be generated
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
make(amount: number, overrideParameters?: EntityData<T>): T[] {
const entities = [...Array(amount)].map(() => {
return this.makeEntity(overrideParameters);
});
this.em.persist(entities);
return entities;
}
/**
* Create (and flush) a single entity
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
async createOne(overrideParameters?: EntityData<T>): Promise<T> {
const entity = this.makeOne(overrideParameters);
await this.em.flush();
return entity;
}
/**
* Create (and flush) multiple entities
* @param amount Number of entities that should be generated
* @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
*/
async create(amount: number, overrideParameters?: EntityData<T>): Promise<T[]> {
const entities = this.make(amount, overrideParameters);
await this.em.flush();
return entities;
}
/**
* Set a function that is applied to each entity before it is returned
* In case of `createOne` or `create` it is applied before the entity is persisted
* @param eachFunction The function that is applied on every entity
*/
each(eachFunction: (entity: T) => void) {
this.eachFunction = eachFunction;
return this;
}
}