@mikro-orm/seeder 6.4.6-dev.8 → 6.4.6-dev.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/Factory.d.ts +14 -14
  2. package/Factory.js +21 -18
  3. package/package.json +2 -2
package/Factory.d.ts CHANGED
@@ -1,41 +1,41 @@
1
1
  import type { EntityData, EntityManager, Constructor } from '@mikro-orm/core';
2
- export declare abstract class Factory<T extends object> {
2
+ export declare abstract class Factory<TEntity extends object, TInput = EntityData<TEntity>> {
3
3
  protected readonly em: EntityManager;
4
- abstract readonly model: Constructor<T>;
4
+ abstract readonly model: Constructor<TEntity>;
5
5
  private eachFunction?;
6
6
  constructor(em: EntityManager);
7
- protected abstract definition(): EntityData<T>;
7
+ protected abstract definition(input?: TInput): EntityData<TEntity>;
8
8
  /**
9
9
  * Make a single entity instance, without persisting it.
10
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
10
+ * @param input Object specifying what default attributes of the entity factory should be overridden
11
11
  */
12
- makeEntity(overrideParameters?: EntityData<T>, index?: number): T;
12
+ makeEntity(input?: TInput, index?: number): TEntity;
13
13
  /**
14
14
  * Make a single entity and persist (not flush)
15
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
15
+ * @param input Object specifying what default attributes of the entity factory should be overridden
16
16
  */
17
- makeOne(overrideParameters?: EntityData<T>): T;
17
+ makeOne(input?: TInput): TEntity;
18
18
  /**
19
19
  * Make multiple entities and then persist them (not flush)
20
20
  * @param amount Number of entities that should be generated
21
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
21
+ * @param input Object specifying what default attributes of the entity factory should be overridden
22
22
  */
23
- make(amount: number, overrideParameters?: EntityData<T>): T[];
23
+ make(amount: number, input?: TInput): TEntity[];
24
24
  /**
25
25
  * Create (and flush) a single entity
26
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
26
+ * @param input Object specifying what default attributes of the entity factory should be overridden
27
27
  */
28
- createOne(overrideParameters?: EntityData<T>): Promise<T>;
28
+ createOne(input?: TInput): Promise<TEntity>;
29
29
  /**
30
30
  * Create (and flush) multiple entities
31
31
  * @param amount Number of entities that should be generated
32
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
32
+ * @param input Object specifying what default attributes of the entity factory should be overridden
33
33
  */
34
- create(amount: number, overrideParameters?: EntityData<T>): Promise<T[]>;
34
+ create(amount: number, input?: TInput): Promise<TEntity[]>;
35
35
  /**
36
36
  * Set a function that is applied to each entity before it is returned
37
37
  * In case of `createOne` or `create` it is applied before the entity is persisted
38
38
  * @param eachFunction The function that is applied on every entity
39
39
  */
40
- each(eachFunction: (entity: T, index: number) => void): this;
40
+ each(eachFunction: (entity: TEntity, index: number) => void): this;
41
41
  }
package/Factory.js CHANGED
@@ -9,53 +9,56 @@ class Factory {
9
9
  }
10
10
  /**
11
11
  * Make a single entity instance, without persisting it.
12
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
12
+ * @param input Object specifying what default attributes of the entity factory should be overridden
13
13
  */
14
- makeEntity(overrideParameters, index = 0) {
15
- const entity = this.em.create(this.model, {
16
- ...this.definition(),
17
- ...overrideParameters,
18
- }, { persist: false });
14
+ makeEntity(input, index = 0) {
15
+ const data = this.definition.length === 0
16
+ ? {
17
+ ...this.definition(),
18
+ ...input,
19
+ }
20
+ : this.definition(input);
21
+ const entity = this.em.create(this.model, data, { persist: false });
19
22
  this.eachFunction?.(entity, index);
20
23
  return entity;
21
24
  }
22
25
  /**
23
26
  * Make a single entity and persist (not flush)
24
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
27
+ * @param input Object specifying what default attributes of the entity factory should be overridden
25
28
  */
26
- makeOne(overrideParameters) {
27
- const entity = this.makeEntity(overrideParameters);
29
+ makeOne(input) {
30
+ const entity = this.makeEntity(input);
28
31
  this.em.persist(entity);
29
32
  return entity;
30
33
  }
31
34
  /**
32
35
  * Make multiple entities and then persist them (not flush)
33
36
  * @param amount Number of entities that should be generated
34
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
37
+ * @param input Object specifying what default attributes of the entity factory should be overridden
35
38
  */
36
- make(amount, overrideParameters) {
39
+ make(amount, input) {
37
40
  const entities = [...Array(amount)].map((_, index) => {
38
- return this.makeEntity(overrideParameters, index);
41
+ return this.makeEntity(input, index);
39
42
  });
40
43
  this.em.persist(entities);
41
44
  return entities;
42
45
  }
43
46
  /**
44
47
  * Create (and flush) a single entity
45
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
48
+ * @param input Object specifying what default attributes of the entity factory should be overridden
46
49
  */
47
- async createOne(overrideParameters) {
48
- const entity = this.makeOne(overrideParameters);
50
+ async createOne(input) {
51
+ const entity = this.makeOne(input);
49
52
  await this.em.flush();
50
53
  return entity;
51
54
  }
52
55
  /**
53
56
  * Create (and flush) multiple entities
54
57
  * @param amount Number of entities that should be generated
55
- * @param overrideParameters Object specifying what default attributes of the entity factory should be overridden
58
+ * @param input Object specifying what default attributes of the entity factory should be overridden
56
59
  */
57
- async create(amount, overrideParameters) {
58
- const entities = this.make(amount, overrideParameters);
60
+ async create(amount, input) {
61
+ const entities = this.make(amount, input);
59
62
  await this.em.flush();
60
63
  return entities;
61
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/seeder",
3
- "version": "6.4.6-dev.8",
3
+ "version": "6.4.6-dev.9",
4
4
  "description": "Seeder package for MikroORM.",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -55,6 +55,6 @@
55
55
  "@mikro-orm/core": "^6.4.5"
56
56
  },
57
57
  "peerDependencies": {
58
- "@mikro-orm/core": "6.4.6-dev.8"
58
+ "@mikro-orm/core": "6.4.6-dev.9"
59
59
  }
60
60
  }