@iterativeflow/mysql 2.0.0-alpha.9 → 2.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ahmed Rowaihi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.mts CHANGED
@@ -18,8 +18,18 @@ interface Sql {
18
18
  exec(text: string, params?: readonly unknown[]): Promise<WriteResult>;
19
19
  tx<T>(fn: (t: Sql) => Promise<T>): Promise<T>;
20
20
  }
21
+ /** Options for {@link mysqlPool}. */
22
+ interface MysqlPoolOpts {
23
+ /**
24
+ * Set READ COMMITTED per transaction (default `true`). The engine needs READ COMMITTED so a
25
+ * first-writer-wins checkpoint's re-read sees a concurrent winner's just-committed row. On
26
+ * PlanetScale/Vitess a per-connection `SET` taints the connection into a reserved pool slot, so
27
+ * set this `false` there and configure the server's default isolation to READ COMMITTED instead.
28
+ */
29
+ setIsolation?: boolean;
30
+ }
21
31
  /** Adapt a `mysql2/promise` {@link Pool} to {@link Sql}. `tx` checks out one connection for the unit. */
22
- declare const mysqlPool: (pool: Pool) => Sql;
32
+ declare const mysqlPool: (pool: Pool, opts?: MysqlPoolOpts) => Sql;
23
33
  //#endregion
24
34
  //#region src/backend.d.ts
25
35
  interface MysqlBackendOpts {
package/dist/index.mjs CHANGED
@@ -77,11 +77,26 @@ const createMysqlQueue = (sql, t, id) => {
77
77
  await sql.exec(`UPDATE ${t.job} SET lease_token = NULL, lease_expires = NULL, run_at = 0
78
78
  WHERE run_id = ? AND lease_token = ? AND lease_expires > ? AND version <> ?`, params);
79
79
  },
80
- async depth(now) {
81
- return queueDepthOf((await sql.query(`SELECT run_at, lease_expires FROM ${t.job}`)).map((r) => ({
82
- runAt: Number(r.run_at),
83
- leaseExpires: r.lease_expires == null ? void 0 : Number(r.lease_expires)
84
- })), now.getTime());
80
+ async depth(now, names) {
81
+ if (names && names.length === 0) return queueDepthOf([], now.getTime());
82
+ const nowMs = now.getTime();
83
+ const namePredicate = names ? ` WHERE r.name IN (${names.map(() => "?").join(",")})` : "";
84
+ const r = (await sql.query(`SELECT SUM(claimable) AS claimable, SUM(leased) AS leased,
85
+ MIN(CASE WHEN claimable = 1 THEN run_at END) AS oldest
86
+ FROM (SELECT j.run_at AS run_at,
87
+ (j.run_at <= ? AND (j.lease_expires IS NULL OR j.lease_expires <= ?)) AS claimable,
88
+ (j.lease_expires > ?) AS leased
89
+ FROM ${t.job} j LEFT JOIN ${t.run} r ON r.id = j.run_id${namePredicate}) x`, [
90
+ nowMs,
91
+ nowMs,
92
+ nowMs,
93
+ ...names ?? []
94
+ ]))[0];
95
+ return {
96
+ claimable: Number(r?.claimable ?? 0),
97
+ leased: Number(r?.leased ?? 0),
98
+ oldestClaimableAgeMs: r?.oldest == null ? null : nowMs - Number(r.oldest)
99
+ };
85
100
  }
86
101
  };
87
102
  };
@@ -536,6 +551,13 @@ const createMysqlStore = (sql, t, id) => {
536
551
  async dueCrons(now, limit) {
537
552
  return (await sql.query(`SELECT * FROM ${t.cron} WHERE next_run_at <= ? ORDER BY next_run_at LIMIT ?`, [now.getTime(), limit])).map(mapCron);
538
553
  },
554
+ async dueCronCount(now, names) {
555
+ if (names && names.length === 0) return 0;
556
+ const namePredicate = names ? ` AND flow_name IN ${inList(names.length)}` : "";
557
+ const params = names ? [now.getTime(), ...names] : [now.getTime()];
558
+ const rows = await sql.query(`SELECT count(*) AS n FROM ${t.cron} WHERE next_run_at <= ?${namePredicate}`, params);
559
+ return Number(rows[0]?.n ?? 0);
560
+ },
539
561
  async advanceCron(name, expectedNextRunAt, nextRunAt, lastRunAt) {
540
562
  return (await sql.exec(`UPDATE ${t.cron} SET next_run_at = ?, last_run_at = ?
541
563
  WHERE name = ? AND next_run_at = ?`, [
@@ -567,6 +589,14 @@ const createMysqlTimer = (sql, t) => {
567
589
  return ids;
568
590
  });
569
591
  },
592
+ async dueCount(now, names) {
593
+ if (names && names.length === 0) return 0;
594
+ const namePredicate = names ? ` AND r.name IN (${names.map(() => "?").join(",")})` : "";
595
+ const params = names ? [now.getTime(), ...names] : [now.getTime()];
596
+ const rows = await sql.query(`SELECT count(*) AS n FROM ${t.timer} tm LEFT JOIN ${t.run} r ON r.id = tm.run_id
597
+ WHERE tm.fire_at <= ?${namePredicate}`, params);
598
+ return Number(rows[0]?.n ?? 0);
599
+ },
570
600
  async cancel(runId) {
571
601
  await sql.exec(`DELETE FROM ${t.timer} WHERE run_id = ?`, [runId]);
572
602
  },
@@ -611,17 +641,14 @@ const onConn = (c) => ({
611
641
  tx: (fn) => fn(onConn(c))
612
642
  });
613
643
  /** Adapt a `mysql2/promise` {@link Pool} to {@link Sql}. `tx` checks out one connection for the unit. */
614
- const mysqlPool = (pool) => {
615
- const configured = /* @__PURE__ */ new WeakSet();
644
+ const mysqlPool = (pool, opts = {}) => {
645
+ const setIsolation = opts.setIsolation ?? true;
616
646
  return {
617
647
  ...bind(pool),
618
648
  async tx(fn) {
619
649
  const c = await pool.getConnection();
620
650
  try {
621
- if (!configured.has(c)) {
622
- await c.query("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED");
623
- configured.add(c);
624
- }
651
+ if (setIsolation) await c.query("SET TRANSACTION ISOLATION LEVEL READ COMMITTED");
625
652
  await c.beginTransaction();
626
653
  const out = await fn(onConn(c));
627
654
  await c.commit();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iterativeflow/mysql",
3
- "version": "2.0.0-alpha.9",
3
+ "version": "2.0.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -24,13 +24,13 @@
24
24
  "publishConfig": {
25
25
  "access": "public"
26
26
  },
27
- "scripts": {
28
- "build": "tsdown"
29
- },
30
27
  "dependencies": {
31
- "@iterativeflow/core": "2.0.0-alpha.9"
28
+ "@iterativeflow/core": "2.0.1"
32
29
  },
33
30
  "peerDependencies": {
34
31
  "mysql2": ">=3.0.0"
32
+ },
33
+ "scripts": {
34
+ "build": "tsdown"
35
35
  }
36
- }
36
+ }