@iterativeflow/mysql 2.0.0-alpha.4 → 2.0.0-alpha.6

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.d.mts CHANGED
@@ -48,4 +48,21 @@ declare const ddl: (prefix?: string) => string[];
48
48
  /** Apply the schema DDL (idempotent). Run once before use. */
49
49
  declare const applySchema: (sql: Sql, prefix?: string) => Promise<void>;
50
50
  //#endregion
51
- export { type MysqlBackendOpts, type Sql, applySchema, createMysqlBackend, ddl, mysqlPool };
51
+ //#region src/tx.d.ts
52
+ /**
53
+ * Run `fn` inside one MySQL transaction, handing it a {@link Backend} bound to that
54
+ * transaction plus the raw {@link Sql} for the caller's own writes. Every `submit` /
55
+ * `startRun` / `enqueue` on that backend commits ATOMICALLY with the caller's writes on
56
+ * `tx` — the transactional-enqueue guarantee: business work and workflow dispatch land
57
+ * together or not at all. A throw rolls back both, so a failed request never leaves an
58
+ * orphan run or a dangling job.
59
+ *
60
+ * @example
61
+ * await inTx(pool, async (backend, tx) => {
62
+ * await tx.query("INSERT INTO orders (id) VALUES (?)", [orderId]);
63
+ * await submit(backend, fulfilOrder, { orderId }); // enqueued iff the order commits
64
+ * });
65
+ */
66
+ declare const inTx: <T>(pool: Pool, fn: (backend: Backend, tx: Sql) => Promise<T>, opts?: MysqlBackendOpts) => Promise<T>;
67
+ //#endregion
68
+ export { type MysqlBackendOpts, type Sql, applySchema, createMysqlBackend, ddl, inTx, mysqlPool };
package/dist/index.mjs CHANGED
@@ -390,6 +390,16 @@ const createMysqlStore = (sql, t, id) => {
390
390
  checkpointStep(c, fx) {
391
391
  return sql.tx(async (tx) => {
392
392
  if (!(await tx.query(`SELECT 1 AS ok FROM ${t.run} WHERE id = ?`, [c.runId]))[0]) throw new Error(`checkpointStep: run ${c.runId} not found`);
393
+ if (fx?.requireVersion !== void 0) {
394
+ if ((await tx.query(`SELECT 1 AS ok FROM ${t.step} WHERE run_id = ? AND cursor_key = ?`, [c.runId, c.cursorKey])).length === 0) {
395
+ const job = await tx.query(`SELECT version FROM ${t.job} WHERE run_id = ? FOR UPDATE`, [c.runId]);
396
+ if (!job[0] || Number(job[0].version) !== fx.requireVersion) return {
397
+ status: c.status,
398
+ attempts: c.attempts,
399
+ committed: false
400
+ };
401
+ }
402
+ }
393
403
  if ((await tx.exec(`INSERT IGNORE INTO ${t.step} (run_id, cursor_key, status, result, error, attempts, shape)
394
404
  VALUES (?, ?, ?, ?, ?, ?, ?)`, [
395
405
  c.runId,
@@ -617,4 +627,21 @@ const mysqlPool = (pool) => {
617
627
  };
618
628
  };
619
629
  //#endregion
620
- export { applySchema, createMysqlBackend, ddl, mysqlPool };
630
+ //#region src/tx.ts
631
+ /**
632
+ * Run `fn` inside one MySQL transaction, handing it a {@link Backend} bound to that
633
+ * transaction plus the raw {@link Sql} for the caller's own writes. Every `submit` /
634
+ * `startRun` / `enqueue` on that backend commits ATOMICALLY with the caller's writes on
635
+ * `tx` — the transactional-enqueue guarantee: business work and workflow dispatch land
636
+ * together or not at all. A throw rolls back both, so a failed request never leaves an
637
+ * orphan run or a dangling job.
638
+ *
639
+ * @example
640
+ * await inTx(pool, async (backend, tx) => {
641
+ * await tx.query("INSERT INTO orders (id) VALUES (?)", [orderId]);
642
+ * await submit(backend, fulfilOrder, { orderId }); // enqueued iff the order commits
643
+ * });
644
+ */
645
+ const inTx = (pool, fn, opts) => mysqlPool(pool).tx((tx) => fn(createMysqlBackend(tx, opts), tx));
646
+ //#endregion
647
+ export { applySchema, createMysqlBackend, ddl, inTx, mysqlPool };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iterativeflow/mysql",
3
- "version": "2.0.0-alpha.4",
3
+ "version": "2.0.0-alpha.6",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  "build": "tsdown"
29
29
  },
30
30
  "dependencies": {
31
- "@iterativeflow/core": "2.0.0-alpha.4"
31
+ "@iterativeflow/core": "2.0.0-alpha.6"
32
32
  },
33
33
  "peerDependencies": {
34
34
  "mysql2": ">=3.0.0"