@forklaunch/implementation-worker-database 1.0.41 → 1.1.0

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.
@@ -1,5 +1,9 @@
1
1
  import { WorkerEventEntity } from '@forklaunch/interfaces-worker/types';
2
- import { EntityManager } from '@mikro-orm/core';
2
+ import {
3
+ EntityManager,
4
+ EntitySchema,
5
+ RequiredEntityData
6
+ } from '@mikro-orm/core';
3
7
  import { DatabaseWorkerOptions } from '../domain/types/databaseWorker.types';
4
8
 
5
9
  export class DatabaseWorkerProducer<
@@ -8,17 +12,42 @@ export class DatabaseWorkerProducer<
8
12
  > {
9
13
  private readonly em: EntityManager;
10
14
  private readonly options: Options;
15
+ private readonly entityName?: EntitySchema<EventEntity>;
11
16
 
12
- constructor(em: EntityManager, options: Options) {
17
+ /**
18
+ * @param entityName the event record's entity, the one the matching
19
+ * `DatabaseWorkerConsumer` reads. MikroORM 7 only persists entity
20
+ * instances, so without it callers must pass entities (`em.create`)
21
+ * rather than plain event objects.
22
+ */
23
+ constructor(
24
+ em: EntityManager,
25
+ options: Options,
26
+ entityName?: EntitySchema<EventEntity>
27
+ ) {
13
28
  this.em = em;
14
29
  this.options = options;
30
+ this.entityName = entityName;
31
+ }
32
+
33
+ private toEntity(event: EventEntity): EventEntity {
34
+ // An event is a complete record of its own entity; MikroORM's generic
35
+ // RequiredEntityData cannot see that through the EventEntity parameter.
36
+ return this.entityName
37
+ ? this.em.create(
38
+ this.entityName,
39
+ event as RequiredEntityData<EventEntity>
40
+ )
41
+ : event;
15
42
  }
16
43
 
17
44
  async enqueueJob(event: EventEntity): Promise<void> {
18
- await this.em.persist(event).flush();
45
+ await this.em.persist(this.toEntity(event)).flush();
19
46
  }
20
47
 
21
48
  async enqueueBatchJobs(events: EventEntity[]): Promise<void> {
22
- await this.em.persist(events).flush();
49
+ await this.em
50
+ .persist(events.map((event) => this.toEntity(event)))
51
+ .flush();
23
52
  }
24
53
  }
@@ -1,10 +1,18 @@
1
1
  import { WorkerEventEntity } from '@forklaunch/interfaces-worker/types';
2
- import { EntityManager } from '@mikro-orm/core';
2
+ import { EntityManager, EntitySchema } from '@mikro-orm/core';
3
3
  import { DatabaseWorkerOptions } from '../domain/types/databaseWorker.types';
4
4
  export declare class DatabaseWorkerProducer<EventEntity extends WorkerEventEntity, Options extends DatabaseWorkerOptions> {
5
5
  private readonly em;
6
6
  private readonly options;
7
- constructor(em: EntityManager, options: Options);
7
+ private readonly entityName?;
8
+ /**
9
+ * @param entityName the event record's entity, the one the matching
10
+ * `DatabaseWorkerConsumer` reads. MikroORM 7 only persists entity
11
+ * instances, so without it callers must pass entities (`em.create`)
12
+ * rather than plain event objects.
13
+ */
14
+ constructor(em: EntityManager, options: Options, entityName?: EntitySchema<EventEntity>);
15
+ private toEntity;
8
16
  enqueueJob(event: EventEntity): Promise<void>;
9
17
  enqueueBatchJobs(events: EventEntity[]): Promise<void>;
10
18
  }
@@ -1 +1 @@
1
- {"version":3,"file":"databaseWorker.producer.d.ts","sourceRoot":"","sources":["../../producers/databaseWorker.producer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAE7E,qBAAa,sBAAsB,CACjC,WAAW,SAAS,iBAAiB,EACrC,OAAO,SAAS,qBAAqB;IAErC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAgB;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAElC,YAAY,EAAE,EAAE,aAAa,EAAE,OAAO,EAAE,OAAO,EAG9C;IAEK,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAElD;IAEK,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3D;CACF"}
1
+ {"version":3,"file":"databaseWorker.producer.d.ts","sourceRoot":"","sources":["../../producers/databaseWorker.producer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AACxE,OAAO,EACL,aAAa,EACb,YAAY,EAEb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAE7E,qBAAa,sBAAsB,CACjC,WAAW,SAAS,iBAAiB,EACrC,OAAO,SAAS,qBAAqB;IAErC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAgB;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAA4B;IAExD;;;;;OAKG;IACH,YACE,EAAE,EAAE,aAAa,EACjB,OAAO,EAAE,OAAO,EAChB,UAAU,CAAC,EAAE,YAAY,CAAC,WAAW,CAAC,EAKvC;IAED,OAAO,CAAC,QAAQ;IAWV,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAElD;IAEK,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAI3D;CACF"}
@@ -28,15 +28,29 @@ module.exports = __toCommonJS(producers_exports);
28
28
  var DatabaseWorkerProducer = class {
29
29
  em;
30
30
  options;
31
- constructor(em, options) {
31
+ entityName;
32
+ /**
33
+ * @param entityName the event record's entity, the one the matching
34
+ * `DatabaseWorkerConsumer` reads. MikroORM 7 only persists entity
35
+ * instances, so without it callers must pass entities (`em.create`)
36
+ * rather than plain event objects.
37
+ */
38
+ constructor(em, options, entityName) {
32
39
  this.em = em;
33
40
  this.options = options;
41
+ this.entityName = entityName;
42
+ }
43
+ toEntity(event) {
44
+ return this.entityName ? this.em.create(
45
+ this.entityName,
46
+ event
47
+ ) : event;
34
48
  }
35
49
  async enqueueJob(event) {
36
- await this.em.persist(event).flush();
50
+ await this.em.persist(this.toEntity(event)).flush();
37
51
  }
38
52
  async enqueueBatchJobs(events) {
39
- await this.em.persist(events).flush();
53
+ await this.em.persist(events.map((event) => this.toEntity(event))).flush();
40
54
  }
41
55
  };
42
56
  // Annotate the CommonJS export names for ESM import in node:
@@ -2,15 +2,29 @@
2
2
  var DatabaseWorkerProducer = class {
3
3
  em;
4
4
  options;
5
- constructor(em, options) {
5
+ entityName;
6
+ /**
7
+ * @param entityName the event record's entity, the one the matching
8
+ * `DatabaseWorkerConsumer` reads. MikroORM 7 only persists entity
9
+ * instances, so without it callers must pass entities (`em.create`)
10
+ * rather than plain event objects.
11
+ */
12
+ constructor(em, options, entityName) {
6
13
  this.em = em;
7
14
  this.options = options;
15
+ this.entityName = entityName;
16
+ }
17
+ toEntity(event) {
18
+ return this.entityName ? this.em.create(
19
+ this.entityName,
20
+ event
21
+ ) : event;
8
22
  }
9
23
  async enqueueJob(event) {
10
- await this.em.persist(event).flush();
24
+ await this.em.persist(this.toEntity(event)).flush();
11
25
  }
12
26
  async enqueueBatchJobs(events) {
13
- await this.em.persist(events).flush();
27
+ await this.em.persist(events.map((event) => this.toEntity(event))).flush();
14
28
  }
15
29
  };
16
30
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/implementation-worker-database",
3
- "version": "1.0.41",
3
+ "version": "1.1.0",
4
4
  "description": "Database implementation for forklaunch workers",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -54,21 +54,21 @@
54
54
  "lib/**"
55
55
  ],
56
56
  "dependencies": {
57
- "@forklaunch/core": "^3.1.0",
58
- "@forklaunch/internal": "^1.2.34",
59
- "@forklaunch/validator": "^1.2.32",
57
+ "@forklaunch/core": "^3.3.0",
58
+ "@forklaunch/interfaces-worker": "^1.0.37",
59
+ "@forklaunch/internal": "^1.2.35",
60
+ "@forklaunch/validator": "^1.3.0",
60
61
  "@mikro-orm/core": "7.2.5",
61
62
  "@sinclair/typebox": "^0.34.52",
62
- "zod": "^4.6.7",
63
- "@forklaunch/interfaces-worker": "^1.0.36"
63
+ "zod": "^4.6.7"
64
64
  },
65
65
  "devDependencies": {
66
66
  "depcheck": "^1.4.7",
67
- "eslint": "^10.12.0",
67
+ "eslint": "^10.13.0",
68
68
  "prettier": "^3.9.10",
69
69
  "typedoc": "^0.28.20",
70
70
  "typescript": "^7.0.2",
71
- "typescript-eslint": "^8.71.2"
71
+ "typescript-eslint": "^8.72.0"
72
72
  },
73
73
  "publishConfig": {
74
74
  "access": "public"