@dudousxd/nestjs-durable-store-mikro-orm 0.8.0 → 0.9.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.
package/dist/index.js CHANGED
@@ -1,20 +1,753 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/entities.ts
5
+ import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
6
+ function _ts_decorate(decorators, target, key, desc) {
7
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
11
+ }
12
+ __name(_ts_decorate, "_ts_decorate");
13
+ function _ts_metadata(k, v) {
14
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
15
+ }
16
+ __name(_ts_metadata, "_ts_metadata");
17
+ var WorkflowRunEntity = class {
18
+ static {
19
+ __name(this, "WorkflowRunEntity");
20
+ }
21
+ id;
22
+ workflow;
23
+ workflowVersion;
24
+ status;
25
+ input;
26
+ output;
27
+ error;
28
+ wakeAt;
29
+ lockedBy;
30
+ lockedUntil;
31
+ recoveryAttempts;
32
+ tags;
33
+ searchAttributes;
34
+ createdAt;
35
+ updatedAt;
36
+ };
37
+ _ts_decorate([
38
+ PrimaryKey(),
39
+ _ts_metadata("design:type", String)
40
+ ], WorkflowRunEntity.prototype, "id", void 0);
41
+ _ts_decorate([
42
+ Property(),
43
+ _ts_metadata("design:type", String)
44
+ ], WorkflowRunEntity.prototype, "workflow", void 0);
45
+ _ts_decorate([
46
+ Property(),
47
+ _ts_metadata("design:type", String)
48
+ ], WorkflowRunEntity.prototype, "workflowVersion", void 0);
49
+ _ts_decorate([
50
+ Property(),
51
+ _ts_metadata("design:type", typeof RunStatus === "undefined" ? Object : RunStatus)
52
+ ], WorkflowRunEntity.prototype, "status", void 0);
53
+ _ts_decorate([
54
+ Property({
55
+ type: "json",
56
+ nullable: true
57
+ }),
58
+ _ts_metadata("design:type", Object)
59
+ ], WorkflowRunEntity.prototype, "input", void 0);
60
+ _ts_decorate([
61
+ Property({
62
+ type: "json",
63
+ nullable: true
64
+ }),
65
+ _ts_metadata("design:type", Object)
66
+ ], WorkflowRunEntity.prototype, "output", void 0);
67
+ _ts_decorate([
68
+ Property({
69
+ type: "json",
70
+ nullable: true
71
+ }),
72
+ _ts_metadata("design:type", Object)
73
+ ], WorkflowRunEntity.prototype, "error", void 0);
74
+ _ts_decorate([
75
+ Property({
76
+ nullable: true
77
+ }),
78
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
79
+ ], WorkflowRunEntity.prototype, "wakeAt", void 0);
80
+ _ts_decorate([
81
+ Property({
82
+ nullable: true
83
+ }),
84
+ _ts_metadata("design:type", String)
85
+ ], WorkflowRunEntity.prototype, "lockedBy", void 0);
86
+ _ts_decorate([
87
+ Property({
88
+ nullable: true
89
+ }),
90
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
91
+ ], WorkflowRunEntity.prototype, "lockedUntil", void 0);
92
+ _ts_decorate([
93
+ Property({
94
+ type: "integer",
95
+ nullable: true
96
+ }),
97
+ _ts_metadata("design:type", Number)
98
+ ], WorkflowRunEntity.prototype, "recoveryAttempts", void 0);
99
+ _ts_decorate([
100
+ Property({
101
+ type: "json",
102
+ nullable: true
103
+ }),
104
+ _ts_metadata("design:type", Object)
105
+ ], WorkflowRunEntity.prototype, "tags", void 0);
106
+ _ts_decorate([
107
+ Property({
108
+ type: "json",
109
+ nullable: true
110
+ }),
111
+ _ts_metadata("design:type", Object)
112
+ ], WorkflowRunEntity.prototype, "searchAttributes", void 0);
113
+ _ts_decorate([
114
+ Property(),
115
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
116
+ ], WorkflowRunEntity.prototype, "createdAt", void 0);
117
+ _ts_decorate([
118
+ Property(),
119
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
120
+ ], WorkflowRunEntity.prototype, "updatedAt", void 0);
121
+ WorkflowRunEntity = _ts_decorate([
122
+ Entity({
123
+ tableName: "durable_workflow_runs"
124
+ })
125
+ ], WorkflowRunEntity);
126
+ var StepCheckpointEntity = class {
127
+ static {
128
+ __name(this, "StepCheckpointEntity");
129
+ }
130
+ runId;
131
+ seq;
132
+ name;
133
+ kind;
134
+ stepId;
135
+ status;
136
+ input;
137
+ output;
138
+ error;
139
+ events;
140
+ attempts;
141
+ workerGroup;
142
+ wakeAt;
143
+ enqueuedAt;
144
+ startedAt;
145
+ finishedAt;
146
+ };
147
+ _ts_decorate([
148
+ PrimaryKey(),
149
+ _ts_metadata("design:type", String)
150
+ ], StepCheckpointEntity.prototype, "runId", void 0);
151
+ _ts_decorate([
152
+ PrimaryKey(),
153
+ _ts_metadata("design:type", Number)
154
+ ], StepCheckpointEntity.prototype, "seq", void 0);
155
+ _ts_decorate([
156
+ Property(),
157
+ _ts_metadata("design:type", String)
158
+ ], StepCheckpointEntity.prototype, "name", void 0);
159
+ _ts_decorate([
160
+ Property(),
161
+ _ts_metadata("design:type", typeof StepKind === "undefined" ? Object : StepKind)
162
+ ], StepCheckpointEntity.prototype, "kind", void 0);
163
+ _ts_decorate([
164
+ Property(),
165
+ _ts_metadata("design:type", String)
166
+ ], StepCheckpointEntity.prototype, "stepId", void 0);
167
+ _ts_decorate([
168
+ Property(),
169
+ _ts_metadata("design:type", String)
170
+ ], StepCheckpointEntity.prototype, "status", void 0);
171
+ _ts_decorate([
172
+ Property({
173
+ type: "json",
174
+ nullable: true
175
+ }),
176
+ _ts_metadata("design:type", Object)
177
+ ], StepCheckpointEntity.prototype, "input", void 0);
178
+ _ts_decorate([
179
+ Property({
180
+ type: "json",
181
+ nullable: true
182
+ }),
183
+ _ts_metadata("design:type", Object)
184
+ ], StepCheckpointEntity.prototype, "output", void 0);
185
+ _ts_decorate([
186
+ Property({
187
+ type: "json",
188
+ nullable: true
189
+ }),
190
+ _ts_metadata("design:type", Object)
191
+ ], StepCheckpointEntity.prototype, "error", void 0);
192
+ _ts_decorate([
193
+ Property({
194
+ type: "json",
195
+ nullable: true
196
+ }),
197
+ _ts_metadata("design:type", Object)
198
+ ], StepCheckpointEntity.prototype, "events", void 0);
199
+ _ts_decorate([
200
+ Property(),
201
+ _ts_metadata("design:type", Number)
202
+ ], StepCheckpointEntity.prototype, "attempts", void 0);
203
+ _ts_decorate([
204
+ Property({
205
+ nullable: true
206
+ }),
207
+ _ts_metadata("design:type", String)
208
+ ], StepCheckpointEntity.prototype, "workerGroup", void 0);
209
+ _ts_decorate([
210
+ Property({
211
+ nullable: true
212
+ }),
213
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
214
+ ], StepCheckpointEntity.prototype, "wakeAt", void 0);
215
+ _ts_decorate([
216
+ Property({
217
+ nullable: true
218
+ }),
219
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
220
+ ], StepCheckpointEntity.prototype, "enqueuedAt", void 0);
221
+ _ts_decorate([
222
+ Property(),
223
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
224
+ ], StepCheckpointEntity.prototype, "startedAt", void 0);
225
+ _ts_decorate([
226
+ Property(),
227
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
228
+ ], StepCheckpointEntity.prototype, "finishedAt", void 0);
229
+ StepCheckpointEntity = _ts_decorate([
230
+ Entity({
231
+ tableName: "durable_step_checkpoints"
232
+ })
233
+ ], StepCheckpointEntity);
234
+ var RunAttributeEntity = class {
235
+ static {
236
+ __name(this, "RunAttributeEntity");
237
+ }
238
+ runId;
239
+ key;
240
+ strValue;
241
+ // `float`/`double` is portable across SQLite/MySQL/Postgres for numeric range scans.
242
+ numValue;
243
+ };
244
+ _ts_decorate([
245
+ PrimaryKey(),
246
+ _ts_metadata("design:type", String)
247
+ ], RunAttributeEntity.prototype, "runId", void 0);
248
+ _ts_decorate([
249
+ PrimaryKey(),
250
+ _ts_metadata("design:type", String)
251
+ ], RunAttributeEntity.prototype, "key", void 0);
252
+ _ts_decorate([
253
+ Property({
254
+ type: "string",
255
+ nullable: true
256
+ }),
257
+ _ts_metadata("design:type", Object)
258
+ ], RunAttributeEntity.prototype, "strValue", void 0);
259
+ _ts_decorate([
260
+ Property({
261
+ type: "float",
262
+ nullable: true
263
+ }),
264
+ _ts_metadata("design:type", Object)
265
+ ], RunAttributeEntity.prototype, "numValue", void 0);
266
+ RunAttributeEntity = _ts_decorate([
267
+ Entity({
268
+ tableName: "durable_run_attributes"
269
+ })
270
+ ], RunAttributeEntity);
271
+ var SignalWaiterEntity = class {
272
+ static {
273
+ __name(this, "SignalWaiterEntity");
274
+ }
275
+ token;
276
+ runId;
277
+ seq;
278
+ };
279
+ _ts_decorate([
280
+ PrimaryKey(),
281
+ _ts_metadata("design:type", String)
282
+ ], SignalWaiterEntity.prototype, "token", void 0);
283
+ _ts_decorate([
284
+ Property(),
285
+ _ts_metadata("design:type", String)
286
+ ], SignalWaiterEntity.prototype, "runId", void 0);
287
+ _ts_decorate([
288
+ Property(),
289
+ _ts_metadata("design:type", Number)
290
+ ], SignalWaiterEntity.prototype, "seq", void 0);
291
+ SignalWaiterEntity = _ts_decorate([
292
+ Entity({
293
+ tableName: "durable_signal_waiters"
294
+ })
295
+ ], SignalWaiterEntity);
296
+ var BufferedSignalEntity = class {
297
+ static {
298
+ __name(this, "BufferedSignalEntity");
299
+ }
300
+ id;
301
+ token;
302
+ payload;
303
+ };
304
+ _ts_decorate([
305
+ PrimaryKey({
306
+ autoincrement: true
307
+ }),
308
+ _ts_metadata("design:type", Number)
309
+ ], BufferedSignalEntity.prototype, "id", void 0);
310
+ _ts_decorate([
311
+ Property({
312
+ index: true
313
+ }),
314
+ _ts_metadata("design:type", String)
315
+ ], BufferedSignalEntity.prototype, "token", void 0);
316
+ _ts_decorate([
317
+ Property({
318
+ type: "json",
319
+ nullable: true
320
+ }),
321
+ _ts_metadata("design:type", Object)
322
+ ], BufferedSignalEntity.prototype, "payload", void 0);
323
+ BufferedSignalEntity = _ts_decorate([
324
+ Entity({
325
+ tableName: "durable_buffered_signals"
326
+ })
327
+ ], BufferedSignalEntity);
328
+ var ENTITIES = [
329
+ WorkflowRunEntity,
330
+ StepCheckpointEntity,
331
+ RunAttributeEntity,
332
+ SignalWaiterEntity,
333
+ BufferedSignalEntity
334
+ ];
335
+
336
+ // src/mikro-orm-state-store.ts
337
+ import { attributeColumnFor, attributeOperand, normalizeAttributeRows, sqlComparator } from "@dudousxd/nestjs-durable-core";
338
+
339
+ // src/schema.ts
340
+ async function ensureMikroOrmDurableSchema(orm) {
341
+ await orm.schema.updateSchema({
342
+ safe: true
343
+ });
344
+ }
345
+ __name(ensureMikroOrmDurableSchema, "ensureMikroOrmDurableSchema");
346
+
347
+ // src/mikro-orm-state-store.ts
348
+ var MikroOrmStateStore = class {
349
+ static {
350
+ __name(this, "MikroOrmStateStore");
351
+ }
352
+ orm;
353
+ constructor(orm) {
354
+ this.orm = orm;
355
+ }
356
+ async ensureSchema() {
357
+ await ensureMikroOrmDurableSchema(this.orm);
358
+ }
359
+ async createRun(run) {
360
+ const em = this.orm.em.fork();
361
+ em.create(WorkflowRunEntity, toRunEntity(run));
362
+ await em.flush();
363
+ await this.reindexAttributes(run.id, run.searchAttributes);
364
+ }
365
+ async updateRun(runId, patch) {
366
+ const em = this.orm.em.fork();
367
+ const entity = await em.findOneOrFail(WorkflowRunEntity, {
368
+ id: runId
369
+ });
370
+ Object.assign(entity, toRunEntity({
371
+ ...fromRunEntity(entity),
372
+ ...patch
373
+ }));
374
+ await em.flush();
375
+ if ("searchAttributes" in patch) await this.reindexAttributes(runId, patch.searchAttributes);
376
+ }
377
+ /** Rewrite a run's normalized attribute rows: delete the old set, insert the current one. Mirrors
378
+ * the in-memory store's reindex so the side-table always reflects the run's live searchAttributes. */
379
+ async reindexAttributes(runId, attributes, forked) {
380
+ const em = forked ?? this.orm.em.fork();
381
+ await em.nativeDelete(RunAttributeEntity, {
382
+ runId
383
+ });
384
+ const rows = normalizeAttributeRows(runId, attributes);
385
+ if (rows.length) await em.insertMany(RunAttributeEntity, rows);
386
+ }
387
+ async getRun(runId) {
388
+ const em = this.orm.em.fork();
389
+ const entity = await em.findOne(WorkflowRunEntity, {
390
+ id: runId
391
+ });
392
+ return entity ? fromRunEntity(entity) : null;
393
+ }
394
+ async getCheckpoint(runId, seq) {
395
+ const em = this.orm.em.fork();
396
+ const entity = await em.findOne(StepCheckpointEntity, {
397
+ runId,
398
+ seq
399
+ });
400
+ return entity ? fromCheckpointEntity(entity) : null;
401
+ }
402
+ async saveCheckpoint(checkpoint) {
403
+ const em = this.orm.em.fork();
404
+ await em.upsert(StepCheckpointEntity, toCheckpointEntity(checkpoint));
405
+ await em.flush();
406
+ }
407
+ async transaction(work) {
408
+ return this.orm.em.fork().transactional(async (em) => work({
409
+ raw: em,
410
+ saveCheckpoint: /* @__PURE__ */ __name(async (cp) => {
411
+ await em.upsert(StepCheckpointEntity, toCheckpointEntity(cp));
412
+ }, "saveCheckpoint")
413
+ }));
414
+ }
415
+ async listIncompleteRuns() {
416
+ const em = this.orm.em.fork();
417
+ const rows = await em.find(WorkflowRunEntity, {
418
+ status: "running"
419
+ });
420
+ return rows.map(fromRunEntity);
421
+ }
422
+ async listPendingRuns(limit) {
423
+ const em = this.orm.em.fork();
424
+ const rows = await em.find(WorkflowRunEntity, {
425
+ status: "pending"
426
+ }, {
427
+ orderBy: {
428
+ createdAt: "asc"
429
+ },
430
+ limit
431
+ });
432
+ return rows.map(fromRunEntity);
433
+ }
434
+ async listDueTimers(nowMs) {
435
+ const em = this.orm.em.fork();
436
+ const rows = await em.find(WorkflowRunEntity, {
437
+ status: "suspended",
438
+ wakeAt: {
439
+ $ne: null,
440
+ $lte: new Date(nowMs)
441
+ }
442
+ });
443
+ return rows.map(fromRunEntity);
444
+ }
445
+ async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
446
+ const em = this.orm.em.fork();
447
+ const affected = await em.nativeUpdate(WorkflowRunEntity, {
448
+ id: runId,
449
+ $or: [
450
+ {
451
+ lockedUntil: null
452
+ },
453
+ {
454
+ lockedUntil: {
455
+ $lte: new Date(nowMs)
456
+ }
457
+ }
458
+ ]
459
+ }, {
460
+ lockedBy: owner,
461
+ lockedUntil: new Date(leaseUntilMs)
462
+ });
463
+ return affected === 1;
464
+ }
465
+ async releaseRunLock(runId) {
466
+ const em = this.orm.em.fork();
467
+ await em.nativeUpdate(WorkflowRunEntity, {
468
+ id: runId
469
+ }, {
470
+ lockedBy: null,
471
+ lockedUntil: null
472
+ });
473
+ }
474
+ async renewRunLock(runId, owner, leaseUntilMs) {
475
+ const em = this.orm.em.fork();
476
+ const affected = await em.nativeUpdate(WorkflowRunEntity, {
477
+ id: runId,
478
+ lockedBy: owner
479
+ }, {
480
+ lockedUntil: new Date(leaseUntilMs)
481
+ });
482
+ return affected === 1;
483
+ }
484
+ async listRuns(query) {
485
+ const em = this.orm.em.fork();
486
+ const where = {};
487
+ if (query.workflow) where.workflow = query.workflow;
488
+ if (query.status && query.statuses) {
489
+ where.$and = [
490
+ {
491
+ status: query.status
492
+ },
493
+ {
494
+ status: {
495
+ $in: query.statuses
496
+ }
497
+ }
498
+ ];
499
+ } else if (query.status) {
500
+ where.status = query.status;
501
+ } else if (query.statuses) {
502
+ where.status = {
503
+ $in: query.statuses
504
+ };
505
+ }
506
+ const orderBy = {
507
+ createdAt: "desc"
508
+ };
509
+ if (query.tag || query.attributes?.length) {
510
+ const quote = this.idQuote(em);
511
+ const cols = this.attributeColumns(em);
512
+ const qb = em.createQueryBuilder(WorkflowRunEntity, "r").where(where).orderBy(orderBy);
513
+ if (query.tag) {
514
+ const tagsCol = this.tagsColumn(em);
515
+ const pattern = `%"${query.tag.replace(/'/g, "''")}"%`;
516
+ const colExpr = this.jsonAsText(em, `${quote("r")}.${quote(tagsCol)}`);
517
+ qb.andWhere(`${colExpr} LIKE '${pattern}'`);
518
+ }
519
+ for (const f of query.attributes ?? []) {
520
+ qb.andWhere(this.attributeExistsSql(f, quote, "r", cols));
521
+ }
522
+ if (query.limit != null) qb.limit(query.limit);
523
+ if (query.offset != null) qb.offset(query.offset);
524
+ const rows2 = await qb.getResultList();
525
+ return rows2.map(fromRunEntity);
7
526
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
527
+ const rows = await em.find(WorkflowRunEntity, where, {
528
+ ...query.limit != null ? {
529
+ limit: query.limit
530
+ } : {},
531
+ ...query.offset != null ? {
532
+ offset: query.offset
533
+ } : {},
534
+ orderBy
535
+ });
536
+ return rows.map(fromRunEntity);
537
+ }
538
+ /** One attribute predicate as a raw EXISTS subquery on the side-table, correlated to the outer run
539
+ * alias. `<>` (ne) also excludes runs where the attribute is absent (missing-key-never-matches):
540
+ * EXISTS already requires a present row, so EXISTS(... <> ...) is exactly ne-with-present. Numeric
541
+ * operands compare the num column, everything else the str column. Identifiers are quoted per
542
+ * driver and operands are inlined as literals (string operands are escaped by doubling quotes). */
543
+ attributeExistsSql(f, quote, alias, cols) {
544
+ const col = attributeColumnFor(f) === "numValue" ? cols.numValue : cols.strValue;
545
+ const cmp = sqlComparator(f.op);
546
+ const operand = attributeOperand(f);
547
+ const literal = typeof operand === "number" ? String(operand) : `'${String(operand).replace(/'/g, "''")}'`;
548
+ const a = quote(cols.table);
549
+ const sub = quote("a");
550
+ const outerId = `${quote(alias)}.${quote(cols.runPk)}`;
551
+ return `EXISTS (SELECT 1 FROM ${a} ${sub} WHERE ${sub}.${quote(cols.runId)} = ${outerId} AND ${sub}.${quote(cols.key)} = '${f.key.replace(/'/g, "''")}' AND ${sub}.${quote(col)} ${cmp} ${literal})`;
552
+ }
553
+ /** Resolve the side-table's actual column names from MikroORM metadata, so the raw EXISTS matches
554
+ * the active naming strategy (underscore vs camelCase) rather than guessing. The run PK column is
555
+ * reused for BOTH the outer correlation and the side-table FK (they share the same fieldName). */
556
+ attributeColumns(em) {
557
+ const meta = em.getMetadata().get(RunAttributeEntity.name);
558
+ const field = /* @__PURE__ */ __name((prop) => meta.properties[prop]?.fieldNames?.[0] ?? prop, "field");
559
+ const runMeta = em.getMetadata().get(WorkflowRunEntity.name);
560
+ const runPk = runMeta.properties.id?.fieldNames?.[0] ?? "id";
561
+ return {
562
+ table: meta.tableName,
563
+ runId: field("runId"),
564
+ runPk,
565
+ key: field("key"),
566
+ strValue: field("strValue"),
567
+ numValue: field("numValue")
568
+ };
569
+ }
570
+ /** Resolve the `tags` column name from metadata (per the active naming strategy), for the raw LIKE. */
571
+ tagsColumn(em) {
572
+ const runMeta = em.getMetadata().get(WorkflowRunEntity.name);
573
+ return runMeta.properties.tags?.fieldNames?.[0] ?? "tags";
574
+ }
575
+ /** Wrap a JSON column reference so it can be `LIKE`d as text. Postgres `jsonb`/`json` rejects `LIKE`
576
+ * directly (`operator does not exist: jsonb ~~ unknown`) — cast to `text`; MySQL JSON needs an
577
+ * explicit `CHAR` cast; SQLite stores JSON as text already, so no cast. */
578
+ jsonAsText(em, colRef) {
579
+ const platform = String(em.getPlatform().constructor.name).toLowerCase();
580
+ if (platform.includes("postgre")) return `${colRef}::text`;
581
+ if (platform.includes("mysql") || platform.includes("mariadb")) return `CAST(${colRef} AS CHAR)`;
582
+ return colRef;
583
+ }
584
+ /** Quote an identifier per the active SQL driver (MySQL/MariaDB backtick, others double-quote). */
585
+ idQuote(em) {
586
+ const platform = String(em.getPlatform().constructor.name).toLowerCase();
587
+ const isMysql = platform.includes("mysql") || platform.includes("mariadb");
588
+ const ch = isMysql ? "`" : '"';
589
+ return (id) => `${ch}${id}${ch}`;
590
+ }
591
+ async listCheckpoints(runId) {
592
+ const em = this.orm.em.fork();
593
+ const rows = await em.find(StepCheckpointEntity, {
594
+ runId
595
+ }, {
596
+ orderBy: {
597
+ seq: "asc"
598
+ }
599
+ });
600
+ return rows.map(fromCheckpointEntity);
601
+ }
602
+ async putSignalWaiter(waiter) {
603
+ const em = this.orm.em.fork();
604
+ await em.upsert(SignalWaiterEntity, {
605
+ ...waiter
606
+ });
607
+ await em.flush();
608
+ }
609
+ async takeSignalWaiter(token) {
610
+ const em = this.orm.em.fork();
611
+ const entity = await em.findOne(SignalWaiterEntity, {
612
+ token
613
+ });
614
+ if (!entity) return null;
615
+ const waiter = {
616
+ token: entity.token,
617
+ runId: entity.runId,
618
+ seq: entity.seq
619
+ };
620
+ await em.removeAndFlush(entity);
621
+ return waiter;
622
+ }
623
+ async listSignalWaiters(prefix) {
624
+ const em = this.orm.em.fork();
625
+ const rows = await em.find(SignalWaiterEntity, {
626
+ token: {
627
+ $like: `${prefix}%`
628
+ }
629
+ });
630
+ return rows.map((e) => ({
631
+ token: e.token,
632
+ runId: e.runId,
633
+ seq: e.seq
634
+ }));
635
+ }
636
+ async bufferSignal(token, payload) {
637
+ const em = this.orm.em.fork();
638
+ const e = new BufferedSignalEntity();
639
+ e.token = token;
640
+ e.payload = payload ?? null;
641
+ em.persist(e);
642
+ await em.flush();
643
+ }
644
+ async takeBufferedSignal(token) {
645
+ const em = this.orm.em.fork();
646
+ const entity = await em.findOne(BufferedSignalEntity, {
647
+ token
648
+ }, {
649
+ orderBy: {
650
+ id: "asc"
651
+ }
652
+ });
653
+ if (!entity) return null;
654
+ const payload = entity.payload ?? void 0;
655
+ await em.removeAndFlush(entity);
656
+ return {
657
+ payload
658
+ };
659
+ }
660
+ };
661
+ function toRunEntity(run) {
662
+ const e = new WorkflowRunEntity();
663
+ e.id = run.id;
664
+ e.workflow = run.workflow;
665
+ e.workflowVersion = run.workflowVersion;
666
+ e.status = run.status;
667
+ e.input = run.input ?? null;
668
+ e.output = run.output ?? null;
669
+ e.error = run.error ?? null;
670
+ if (run.wakeAt != null) e.wakeAt = new Date(run.wakeAt);
671
+ if (run.lockedBy !== void 0) e.lockedBy = run.lockedBy;
672
+ if (run.lockedUntil != null) e.lockedUntil = new Date(run.lockedUntil);
673
+ if (run.recoveryAttempts !== void 0) e.recoveryAttempts = run.recoveryAttempts;
674
+ e.tags = run.tags ?? null;
675
+ e.searchAttributes = run.searchAttributes ?? null;
676
+ e.createdAt = run.createdAt;
677
+ e.updatedAt = run.updatedAt;
678
+ return e;
679
+ }
680
+ __name(toRunEntity, "toRunEntity");
681
+ function fromRunEntity(e) {
682
+ return {
683
+ id: e.id,
684
+ workflow: e.workflow,
685
+ workflowVersion: e.workflowVersion,
686
+ status: e.status,
687
+ input: e.input ?? void 0,
688
+ output: e.output ?? void 0,
689
+ error: e.error ?? void 0,
690
+ wakeAt: e.wakeAt?.getTime(),
691
+ lockedBy: e.lockedBy ?? void 0,
692
+ lockedUntil: e.lockedUntil?.getTime(),
693
+ recoveryAttempts: e.recoveryAttempts ?? void 0,
694
+ tags: e.tags ?? void 0,
695
+ searchAttributes: e.searchAttributes ?? void 0,
696
+ createdAt: e.createdAt,
697
+ updatedAt: e.updatedAt
698
+ };
699
+ }
700
+ __name(fromRunEntity, "fromRunEntity");
701
+ function toCheckpointEntity(cp) {
702
+ const e = new StepCheckpointEntity();
703
+ e.runId = cp.runId;
704
+ e.seq = cp.seq;
705
+ e.name = cp.name;
706
+ e.kind = cp.kind;
707
+ e.stepId = cp.stepId;
708
+ e.status = cp.status;
709
+ e.input = cp.input ?? null;
710
+ e.output = cp.output ?? null;
711
+ e.error = cp.error ?? null;
712
+ e.events = cp.events ?? null;
713
+ e.attempts = cp.attempts;
714
+ if (cp.workerGroup !== void 0) e.workerGroup = cp.workerGroup;
715
+ if (cp.wakeAt != null) e.wakeAt = new Date(cp.wakeAt);
716
+ e.enqueuedAt = cp.enqueuedAt;
717
+ e.startedAt = cp.startedAt;
718
+ e.finishedAt = cp.finishedAt;
719
+ return e;
720
+ }
721
+ __name(toCheckpointEntity, "toCheckpointEntity");
722
+ function fromCheckpointEntity(e) {
723
+ return {
724
+ runId: e.runId,
725
+ seq: e.seq,
726
+ name: e.name,
727
+ kind: e.kind,
728
+ stepId: e.stepId,
729
+ status: e.status,
730
+ input: e.input ?? void 0,
731
+ output: e.output ?? void 0,
732
+ error: e.error ?? void 0,
733
+ events: e.events ?? void 0,
734
+ attempts: e.attempts,
735
+ workerGroup: e.workerGroup ?? void 0,
736
+ wakeAt: e.wakeAt?.getTime(),
737
+ enqueuedAt: e.enqueuedAt ?? e.startedAt,
738
+ startedAt: e.startedAt,
739
+ finishedAt: e.finishedAt
740
+ };
741
+ }
742
+ __name(fromCheckpointEntity, "fromCheckpointEntity");
743
+ export {
744
+ BufferedSignalEntity,
745
+ ENTITIES,
746
+ MikroOrmStateStore,
747
+ RunAttributeEntity,
748
+ SignalWaiterEntity,
749
+ StepCheckpointEntity,
750
+ WorkflowRunEntity,
751
+ ensureMikroOrmDurableSchema
15
752
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./entities"), exports);
18
- __exportStar(require("./mikro-orm-state-store"), exports);
19
- __exportStar(require("./schema"), exports);
20
753
  //# sourceMappingURL=index.js.map