@voltro/runtime 0.86.0 → 0.87.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +5321 -0
  2. package/README.md +43 -0
  3. package/THIRD-PARTY-NOTICES.md +860 -221
  4. package/dist/analyticsSink.js +8 -14
  5. package/dist/apiKeys.js +82 -78
  6. package/dist/apiRoleBucketSampler.js +64 -0
  7. package/dist/apiRoleWorkloadHistory.js +56 -0
  8. package/dist/apiRoleWorkloadMetrics.js +78 -0
  9. package/dist/apiRoleWorkloadStorage.js +95 -0
  10. package/dist/approvals.js +67 -74
  11. package/dist/authMiddlewareLayer.js +7 -6
  12. package/dist/bindEvent.js +48 -28
  13. package/dist/bootListener.js +106 -39
  14. package/dist/bootStoreCodec.js +38 -37
  15. package/dist/circuitBreaker.js +1 -72
  16. package/dist/composeAnalytics.js +31 -10
  17. package/dist/computedQueryCache.js +231 -0
  18. package/dist/connectionsFacade.js +32 -30
  19. package/dist/coordinatedSchedule.js +84 -78
  20. package/dist/dataStore.js +154 -121
  21. package/dist/declaredTablesGuard.js +2 -1
  22. package/dist/dispatcher.js +403 -328
  23. package/dist/effectStore.js +2 -2
  24. package/dist/engineClients.js +24 -0
  25. package/dist/externalizedTasks.js +19 -6
  26. package/dist/fieldCipher.js +109 -40
  27. package/dist/fieldKeyRegistry.js +295 -0
  28. package/dist/fieldKeyRekey.js +286 -0
  29. package/dist/fieldKeyScan.js +136 -0
  30. package/dist/fieldKeyring.js +77 -0
  31. package/dist/hot.d.ts +56 -0
  32. package/dist/hot.js +3 -0
  33. package/dist/hotDataStore.js +148 -0
  34. package/dist/hotDataStoreSnapshot.js +98 -0
  35. package/dist/hotDataStoreTables.js +18 -0
  36. package/dist/http-range.d.ts +53 -0
  37. package/dist/http-range.js +52 -0
  38. package/dist/httpClientSsrf.js +12 -37
  39. package/dist/httpClientTracing.js +12 -9
  40. package/dist/httpCompression.js +17 -17
  41. package/dist/httpInputStream.js +82 -0
  42. package/dist/httpRouteMounts.js +1 -0
  43. package/dist/idempotencyStore.js +92 -52
  44. package/dist/index.d.ts +1164 -451
  45. package/dist/index.js +143 -132
  46. package/dist/kvFacade.js +23 -17
  47. package/dist/memoryQueryJoins.js +16 -16
  48. package/dist/memorySetOperations.js +43 -0
  49. package/dist/metrics.js +38 -24
  50. package/dist/outbox.js +1 -1
  51. package/dist/readRecorder.js +35 -34
  52. package/dist/readScopePredicate.js +12 -0
  53. package/dist/replicatedDataStore.js +22 -4
  54. package/dist/rowFilter.js +56 -39
  55. package/dist/rpcConnections.js +8 -8
  56. package/dist/rpcServer.js +522 -449
  57. package/dist/rpcWorkloadAdmission.js +50 -0
  58. package/dist/rywPositionStore.js +82 -47
  59. package/dist/scheduler.js +6 -1
  60. package/dist/servePipeline.js +215 -146
  61. package/dist/setReadScopes.js +28 -0
  62. package/dist/snapshotValueCodec.js +46 -0
  63. package/dist/sourceResolution.js +34 -24
  64. package/dist/storeMiddleware.js +211 -209
  65. package/dist/subscriber.js +1 -0
  66. package/dist/tenantScope.js +8 -20
  67. package/dist/traceparent.js +8 -19
  68. package/dist/webSocketGatewayRuntime.js +111 -71
  69. package/dist/workflowRuntime.js +9 -9
  70. package/package.json +19 -7
package/dist/dataStore.js CHANGED
@@ -1,24 +1,25 @@
1
1
  import { memoryQueryJoins as e } from "./memoryQueryJoins.js";
2
- import { applyMemoryColumnDefaults as t } from "./memoryColumnDefaults.js";
3
- import { Effect as n } from "effect";
4
- import { applyJsonPatch as r, attachEagerLoads as i, attributionFields as a, collectSubqueries as o, compareDecimal as s, emitCommittedChanges as c, evaluatePredicate as l, externalChangeEvent as u, getTable as d, hasEagerLoads as f, isTableReactive as p, jsonPatchAssignments as m, preparePredicate as h, raiseChangeListenerCeiling as g, requireTable as _, rowKeyColumns as v, settleTransactionExit as y, stampGeneratedId as b, transactionRetrySchedule as x, upsertUpdatePatch as S, upsertUpdateValues as C } from "@voltro/database";
5
- import { EventEmitter as w } from "node:events";
2
+ import { memoryOutputColumns as t, memoryRowKey as n, memorySetRows as r } from "./memorySetOperations.js";
3
+ import { applyMemoryColumnDefaults as i } from "./memoryColumnDefaults.js";
4
+ import { Effect as a } from "effect";
5
+ import { applyJsonPatch as o, attachEagerLoads as s, attributionFields as c, collectSubqueries as l, compareDecimal as u, emitCommittedChanges as d, evaluatePredicate as f, externalChangeEvent as p, getTable as m, hasEagerLoads as h, isTableReactive as g, jsonPatchAssignments as _, preparePredicate as v, raiseChangeListenerCeiling as y, requireTable as b, rowKeyColumns as x, settleTransactionExit as S, stampGeneratedId as C, transactionRetrySchedule as w, upsertUpdatePatch as T, upsertUpdateValues as E } from "@voltro/database";
6
+ import { EventEmitter as D } from "node:events";
6
7
  //#region src/dataStore.ts
7
- var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) => {
8
+ var O = (e, t, n, r = m(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) => {
8
9
  for (let i of t) {
9
10
  let t = e[i.column], a = n[i.column];
10
11
  if (t === a || t instanceof Date && a instanceof Date && t.getTime() === a.getTime()) continue;
11
12
  if (t == null) return i.direction === "asc" ? -1 : 1;
12
13
  if (a == null) return i.direction === "asc" ? 1 : -1;
13
- let o = r?.[i.column]?.type === "decimal" ? s(t, a) ?? 0 : t < a ? -1 : 1;
14
+ let o = r?.[i.column]?.type === "decimal" ? u(t, a) ?? 0 : t < a ? -1 : 1;
14
15
  if (o !== 0) return i.direction === "asc" ? o : -o;
15
16
  }
16
17
  return 0;
17
- }), E = (e, t, n, r) => !e || l(e, t, n, r ? d(r)?.fields : void 0), D = (e, t) => {
18
+ }), k = (e, t, n, r) => !e || f(e, t, n, r ? m(r)?.fields : void 0), A = (e, t) => {
18
19
  let n = /* @__PURE__ */ new Map();
19
20
  if (!e) return n;
20
- for (let r of o(e)) {
21
- let e = t.get(r.descriptor.table) ?? /* @__PURE__ */ new Map(), i = D(r.descriptor.predicate, t), a = N(r.descriptor, e, t, i);
21
+ for (let r of l(e)) {
22
+ let e = t.get(r.descriptor.table) ?? /* @__PURE__ */ new Map(), i = A(r.descriptor.predicate, t), a = I(r.descriptor, e, t, i);
22
23
  if (r.kind === "exists") n.set(r.descriptor, a.length);
23
24
  else {
24
25
  let e = r.descriptor.projection, t = /* @__PURE__ */ new Set();
@@ -33,12 +34,12 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
33
34
  }
34
35
  }
35
36
  return n;
36
- }, O = {
37
+ }, j = {
37
38
  A: 4,
38
39
  B: 2,
39
40
  C: 1,
40
41
  D: .5
41
- }, k = (e) => (typeof e == "string" ? e : e == null ? "" : JSON.stringify(e)).toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [], A = (e) => {
42
+ }, M = (e) => (typeof e == "string" ? e : e == null ? "" : JSON.stringify(e)).toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [], N = (e) => {
42
43
  let t = {
43
44
  children: /* @__PURE__ */ new Map(),
44
45
  terms: []
@@ -55,23 +56,23 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
55
56
  r.terms.push(n);
56
57
  }
57
58
  return t;
58
- }, j = (e, t, n) => {
59
+ }, P = (e, t, n) => {
59
60
  let r = Array(n).fill(0);
60
61
  for (let n of t) {
61
62
  let t = e;
62
63
  for (let e = 0; e < n.length && (t = t.children.get(n[e]), t !== void 0); e++) for (let e of t.terms) r[e] = r[e] + 1;
63
64
  }
64
65
  return r;
65
- }, M = (e, t) => {
66
- let n = k(t.query);
66
+ }, F = (e, t) => {
67
+ let n = M(t.query);
67
68
  if (n.length === 0) return [];
68
69
  let r = [], i;
69
70
  for (let a of e) {
70
71
  let e = 0, o = /* @__PURE__ */ new Set();
71
72
  for (let r of t.columns) {
72
- let s = k(a[r]), c = O[t.weights?.[r] ?? ""] ?? 1;
73
- i === void 0 && n.length >= 16 && s.length >= 512 && (i = A(n));
74
- let l = i === void 0 ? void 0 : j(i, s, n.length);
73
+ let s = M(a[r]), c = j[t.weights?.[r] ?? ""] ?? 1;
74
+ i === void 0 && n.length >= 16 && s.length >= 512 && (i = N(n));
75
+ let l = i === void 0 ? void 0 : P(i, s, n.length);
75
76
  for (let t = 0; t < n.length; t++) {
76
77
  let r = n[t], i = l?.[t];
77
78
  if (i === void 0) {
@@ -87,24 +88,33 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
87
88
  });
88
89
  }
89
90
  return r;
90
- }, N = (t, n, r = /* @__PURE__ */ new Map(), i = null) => {
91
- let a = i ?? D(t.predicate, r), o = t.alias !== void 0 || (t.joins?.length ?? 0) > 0 || t.joinedProjection !== void 0 ? e(t, [...n.values()], r, (e) => D(e, r)) : void 0, s = t.predicate === void 0 ? void 0 : h(t.predicate, a, o?.columns ?? d(t.table)?.fields), c = o?.rows ?? [...n.values()], l = s === void 0 ? c : c.filter((e) => s(e) ?? !0);
92
- if (t.aggregations && t.aggregations.length > 0) {
93
- let e = P(l, t.aggregations, t.groupBy, t.table, o?.columns);
94
- if (!t.having) return e;
95
- let n = D(t.having, r);
96
- return e.filter((e) => E(t.having, e, n));
97
- }
98
- let u = T(t.fullTextSearch === void 0 ? l : M(l, t.fullTextSearch), t.order, t.table, o?.columns), f = t.fullTextSearch?.rank, p = f?.order === !0 ? u.sort((e, t) => Number(t[f.alias] ?? 0) - Number(e[f.alias] ?? 0)) : u, m = t.skip ? p.slice(t.skip) : p, g = t.take === void 0 ? m : m.slice(0, t.take);
99
- if (o) return g.map(o.project);
100
- if (!t.projection) return g;
101
- let _ = t.projection;
102
- return g.map((e) => {
103
- let t = {};
104
- for (let n of _) t[n] = e[n];
105
- return t;
106
- });
107
- }, P = (e, t, n, r, i = d(r)?.fields) => {
91
+ }, I = (i, a, o = /* @__PURE__ */ new Map(), s = null) => {
92
+ for (let e of [i.take, i.skip]) if (e !== void 0 && (!Number.isSafeInteger(e) || e < 0)) throw Error("Query limit and offset must be non-negative safe integers");
93
+ if (i.setOp && i.setOp.queries.length >= 2) {
94
+ let e = r(i, (e) => I(e, o.get(e.table) ?? /* @__PURE__ */ new Map(), o)), n = O(e, i.order, i.table, t(i, e)), a = i.skip === void 0 ? n : n.slice(i.skip);
95
+ return i.take === void 0 ? a : a.slice(0, i.take);
96
+ }
97
+ let c = s ?? A(i.predicate, o), l = i.alias !== void 0 || (i.joins?.length ?? 0) > 0 || i.joinedProjection !== void 0 ? e(i, [...a.values()], o, (e) => A(e, o)) : void 0, u = i.predicate === void 0 ? void 0 : v(i.predicate, c, l?.columns ?? m(i.table)?.fields), d = l?.rows ?? [...a.values()], f = u === void 0 ? d : d.filter((e) => u(e) ?? !0);
98
+ if (i.aggregations && i.aggregations.length > 0) {
99
+ let e = L(f, i.aggregations, i.groupBy, i.table, l?.columns);
100
+ if (!i.having) return e;
101
+ let t = A(i.having, o);
102
+ return e.filter((e) => k(i.having, e, t));
103
+ }
104
+ let p = O(i.fullTextSearch === void 0 ? f : F(f, i.fullTextSearch), i.order.map((e) => ({
105
+ ...e,
106
+ column: i.joinedProjection?.find((t) => t.outputKey === e.column)?.source ?? e.column
107
+ })), i.table, l?.columns), h = i.fullTextSearch?.rank, g = h?.order === !0 ? p.sort((e, t) => Number(t[h.alias] ?? 0) - Number(e[h.alias] ?? 0)) : p, _ = l?.project ?? (i.projection ? (e) => Object.fromEntries(i.projection.map((t) => [t, e[t]])) : (e) => e), y = g;
108
+ if (i.distinct) {
109
+ let e = t(i, g), r = Object.keys(e), a = /* @__PURE__ */ new Set();
110
+ y = g.filter((t) => {
111
+ let i = n(_(t), r, e);
112
+ return !a.has(i) && (a.add(i), !0);
113
+ });
114
+ }
115
+ let b = i.skip ? y.slice(i.skip) : y;
116
+ return (i.take === void 0 ? b : b.slice(0, i.take)).map(_);
117
+ }, L = (e, t, n, r, i = m(r)?.fields) => {
108
118
  let a = /* @__PURE__ */ new Map();
109
119
  for (let t of e) {
110
120
  let e = {};
@@ -122,22 +132,32 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
122
132
  let o = [];
123
133
  for (let e of a.values()) {
124
134
  let n = { ...e.keyVals };
125
- for (let r of t) n[r.alias] = L(r, e.rows, r.column === void 0 ? void 0 : i?.[r.column]?.type);
135
+ for (let r of t) n[r.alias] = B(r, e.rows, r.column === void 0 ? void 0 : i?.[r.column]?.type, r.column === void 0 ? void 0 : i?.[r.column]?.scale);
126
136
  o.push(n);
127
137
  }
128
138
  return o;
129
- }, F = /^-?\d+$/, I = (e, t) => {
139
+ }, R = /^-?\d+$/, z = (e, t) => {
130
140
  let n = !1;
131
141
  for (let r of e) {
132
142
  let e = r[t];
133
143
  if (e != null) {
134
- if (typeof e != "string" || !F.test(e)) return !1;
144
+ if (typeof e != "string" || !R.test(e)) return !1;
135
145
  n = !0;
136
146
  }
137
147
  }
138
148
  return n;
139
- }, L = (e, t, n) => {
140
- if (n === void 0 && (e.op === "sum" || e.op === "avg") && e.column !== void 0 && I(t, e.column) && (n = "bigint"), n === "bigint" && (e.op === "sum" || e.op === "avg")) {
149
+ }, B = (e, t, n, r) => {
150
+ if (n === void 0 && (e.op === "sum" || e.op === "avg") && e.column !== void 0 && z(t, e.column) && (n = "bigint"), n === "decimal" && (e.op === "sum" || e.op === "avg")) {
151
+ let n = t.map((t) => t[e.column]).filter((e) => e != null).map(String);
152
+ if (n.length === 0) return null;
153
+ if (e.op === "avg") return n.reduce((e, t) => e + Number(t), 0) / n.length;
154
+ let i = r ?? Math.max(0, ...n.map((e) => (e.split(".")[1] ?? "").length)), a = n.reduce((e, t) => {
155
+ let n = t.startsWith("-"), [r = "0", a = ""] = t.replace(/^[-+]/, "").split("."), o = BigInt((r || "0") + a.slice(0, i).padEnd(i, "0"));
156
+ return e + (n ? -o : o);
157
+ }, 0n), o = (a < 0n ? -a : a).toString().padStart(i + 1, "0");
158
+ return (a < 0n ? "-" : "") + (i === 0 ? o : `${o.slice(0, -i)}.${o.slice(-i)}`);
159
+ }
160
+ if (n === "bigint" && (e.op === "sum" || e.op === "avg")) {
141
161
  let n = 0n, r = 0;
142
162
  for (let i of t) {
143
163
  let t = i[e.column];
@@ -186,18 +206,18 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
186
206
  continue;
187
207
  }
188
208
  if (n === "decimal" || n === "bigint") {
189
- let n = s(t, r);
209
+ let n = u(t, r);
190
210
  n !== null && (e.op === "min" && n < 0 || e.op === "max" && n > 0) && (r = t);
191
211
  continue;
192
212
  }
193
- let a = t instanceof Date ? t.getTime() : t, o = r instanceof Date ? r.getTime() : r, c = e.op === "min";
194
- (c && a < o || !c && a > o) && (r = t);
213
+ let a = t instanceof Date ? t.getTime() : t, o = r instanceof Date ? r.getTime() : r, s = e.op === "min";
214
+ (s && a < o || !s && a > o) && (r = t);
195
215
  }
196
216
  return r;
197
217
  }
198
218
  default: throw Error(`computeAggregates: unknown op '${e.op}'`);
199
219
  }
200
- }, R = (e, t) => {
220
+ }, V = (e, t) => {
201
221
  let n = /* @__PURE__ */ new Set();
202
222
  for (let e of t.values()) for (let t of Object.keys(e)) n.add(t);
203
223
  return {
@@ -205,31 +225,31 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
205
225
  columns: [...n],
206
226
  rowCount: t.size
207
227
  };
208
- }, z = "\0", B = (e, t) => e === null ? t : `${e}${z}${t}`, V = class extends Error {
228
+ }, H = "\0", U = (e, t) => e === null ? t : `${e}${H}${t}`, W = class extends Error {
209
229
  constructor() {
210
230
  super("InMemoryDataStore.transactional: concurrent table change; transaction attempt was discarded."), this.name = "MemoryTransactionConflict";
211
231
  }
212
- }, H = (e, t) => {
232
+ }, G = (e, t) => {
213
233
  if (typeof t == "string" || typeof t == "number") return e.has(String(t)) ? String(t) : void 0;
214
- let n = v(t);
234
+ let n = x(t);
215
235
  for (let [t, r] of e) if (n.every(([e, t]) => r[e] === t)) return t;
216
- }, U = /* @__PURE__ */ new WeakSet(), W = class e {
236
+ }, K = /* @__PURE__ */ new WeakSet(), q = class e {
217
237
  #e;
218
238
  #t;
219
239
  #n;
220
240
  #r(e, t = !0) {
221
- t && this.#l(this.#c(e.table)), p(e.table) && this.#n.emit("change", {
222
- ...a(),
241
+ t && this.#l(this.#c(e.table)), g(e.table) && this.#n.emit("change", {
242
+ ...c(),
223
243
  ...e
224
244
  });
225
245
  }
226
246
  #i(e, t = !1) {
227
247
  if (t) for (let t of e) this.#l(this.#c(t.table));
228
- let n = e.filter((e) => p(e.table)).map((e) => ({
229
- ...a(),
248
+ let n = e.filter((e) => g(e.table)).map((e) => ({
249
+ ...c(),
230
250
  ...e
231
251
  }));
232
- c(n, (e) => this.#n.emit("change", e));
252
+ d(n, (e) => this.#n.emit("change", e));
233
253
  }
234
254
  #a;
235
255
  #o;
@@ -238,7 +258,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
238
258
  return r.#e = t.tables, r.#t = t.revisions, r.#n = t.emitter, r.#a = t.numericCounters, r;
239
259
  }
240
260
  constructor(e, t = null) {
241
- if (this.#o = t, this.#e = /* @__PURE__ */ new Map(), this.#t = /* @__PURE__ */ new Map(), this.#n = new w(), g(this.#n), this.#a = /* @__PURE__ */ new Map(), e) for (let [n, r] of Object.entries(e)) {
261
+ if (this.#o = t, this.#e = /* @__PURE__ */ new Map(), this.#t = /* @__PURE__ */ new Map(), this.#n = new D(), y(this.#n), this.#a = /* @__PURE__ */ new Map(), e) for (let [n, r] of Object.entries(e)) {
242
262
  let e = /* @__PURE__ */ new Map(), i = 0n;
243
263
  for (let [t, n] of r.entries()) {
244
264
  let r = n.id, a = r == null ? `__voltro_seed_${t}` : String(r);
@@ -247,7 +267,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
247
267
  e > i && (i = e);
248
268
  } catch {}
249
269
  }
250
- let a = B(t, n);
270
+ let a = U(t, n);
251
271
  this.#e.set(a, e), i > 0n && this.#a.set(a, i);
252
272
  }
253
273
  }
@@ -260,35 +280,38 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
260
280
  }, t);
261
281
  }
262
282
  #c(e) {
263
- return B(this.#o, e);
283
+ return U(this.#o, e);
264
284
  }
265
285
  #l(e) {
266
286
  this.#t.set(e, (this.#t.get(e) ?? 0n) + 1n);
267
287
  }
268
288
  #u(e = this.#e) {
269
- let t = /* @__PURE__ */ new Map(), n = this.#o === null ? null : `${this.#o}${z}`;
270
- for (let [r, i] of e) n === null ? r.includes(z) || t.set(r, i) : r.startsWith(n) && t.set(r.slice(n.length), i);
289
+ let t = /* @__PURE__ */ new Map(), n = this.#o === null ? null : `${this.#o}${H}`;
290
+ for (let [r, i] of e) n === null ? r.includes(H) || t.set(r, i) : r.startsWith(n) && t.set(r.slice(n.length), i);
271
291
  return t;
272
292
  }
273
293
  #d(e) {
274
294
  let t = this.#c(e), n = this.#e.get(t);
275
- return n || (n = /* @__PURE__ */ new Map(), this.#e.set(t, n)), U.has(n) && (n = new Map(n), this.#e.set(t, n)), n;
295
+ return n || (n = /* @__PURE__ */ new Map(), this.#e.set(t, n)), K.has(n) && (n = new Map(n), this.#e.set(t, n)), n;
276
296
  }
277
297
  storedTables() {
278
- return [...this.#u()].map(([e, t]) => R(e, t));
298
+ return [...this.#u()].map(([e, t]) => V(e, t));
299
+ }
300
+ integrityQuery(e) {
301
+ return this.query(e);
279
302
  }
280
303
  async query(e) {
281
- let t = N(e, this.#e.get(this.#c(e.table)) ?? /* @__PURE__ */ new Map(), this.#u());
282
- return f(e) ? i(t, e.eager, _(e.table), (e) => this.query(e)) : t;
283
- }
284
- async insert(e, n) {
285
- n = t(e, b(e, n));
286
- let r = d(e), i = (r?.fields)?.id?.idScheme, a = n;
287
- if (a.id === void 0 && (r === void 0 || i !== void 0)) {
288
- let t = this.#f(e);
304
+ let t = I(e, this.#e.get(this.#c(e.table)) ?? /* @__PURE__ */ new Map(), this.#u());
305
+ return h(e) ? s(t, e.eager, b(e.table), (e) => this.query(e)) : t;
306
+ }
307
+ async insert(e, t) {
308
+ t = i(e, C(e, t));
309
+ let n = m(e), r = (n?.fields)?.id?.idScheme, a = t;
310
+ if (a.id === void 0 && (n === void 0 || r !== void 0)) {
311
+ let n = this.#f(e);
289
312
  a = {
290
- ...n,
291
- id: t
313
+ ...t,
314
+ id: n
292
315
  };
293
316
  }
294
317
  let o = a.id === void 0 ? `__voltro_row_${this.#f(e)}` : String(a.id);
@@ -299,19 +322,19 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
299
322
  new: a
300
323
  }), a;
301
324
  }
302
- async insertMany(e, t) {
303
- let n = [];
304
- for (let r of t) n.push(await this.insert(e, r));
305
- return n;
325
+ async insertMany(e, t, n) {
326
+ let r = [];
327
+ for (let n of t) r.push(await this.insert(e, n));
328
+ return n?.returning === "none" ? [] : r;
306
329
  }
307
- async patchJson(e, t, n, i, a) {
308
- let o = m(t, n, a), s = this.#e.get(this.#c(e));
330
+ async patchJson(e, t, n, r, i) {
331
+ let a = _(t, n, i), s = this.#e.get(this.#c(e));
309
332
  if (!s) return null;
310
- let c = H(s, t), l = c === void 0 ? void 0 : s.get(c);
333
+ let c = G(s, t), l = c === void 0 ? void 0 : s.get(c);
311
334
  if (!l || c === void 0) return null;
312
335
  let u = {
313
- ...r(l, n, i),
314
- ...o
336
+ ...o(l, n, r),
337
+ ...a
315
338
  };
316
339
  return this.#d(e).set(c, u), this.#r({
317
340
  table: e,
@@ -328,7 +351,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
328
351
  async update(e, t, n) {
329
352
  let r = this.#e.get(this.#c(e));
330
353
  if (!r) return null;
331
- let i = H(r, t), a = i === void 0 ? void 0 : r.get(i);
354
+ let i = G(r, t), a = i === void 0 ? void 0 : r.get(i);
332
355
  if (!a || i === void 0) return null;
333
356
  let o = {
334
357
  ...a,
@@ -344,7 +367,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
344
367
  async delete(e, t) {
345
368
  let n = this.#e.get(this.#c(e));
346
369
  if (!n) return !1;
347
- let r = H(n, t), i = r === void 0 ? void 0 : n.get(r);
370
+ let r = G(n, t), i = r === void 0 ? void 0 : n.get(r);
348
371
  return !i || r === void 0 ? !1 : (this.#d(e).delete(r), this.#r({
349
372
  table: e,
350
373
  op: "delete",
@@ -355,7 +378,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
355
378
  async updateMany(e, t, n) {
356
379
  let r = this.#e.get(this.#c(e));
357
380
  if (!r) return 0;
358
- let i = D(n.where, this.#u()), a = h(n.where, i, d(e)?.fields), o = 0, s = [];
381
+ let i = A(n.where, this.#u()), a = v(n.where, i, m(e)?.fields), o = 0, s = [];
359
382
  for (let [n, i] of r) {
360
383
  if (!a(i)) continue;
361
384
  let r = {
@@ -374,7 +397,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
374
397
  async deleteMany(e, t) {
375
398
  let n = this.#e.get(this.#c(e));
376
399
  if (!n) return 0;
377
- let r = D(t.where, this.#u()), i = h(t.where, r, d(e)?.fields), a = [];
400
+ let r = A(t.where, this.#u()), i = v(t.where, r, m(e)?.fields), a = [];
378
401
  for (let [e, t] of n) i(t) && a.push([e, t]);
379
402
  for (let [t, n] of a) this.#d(e).delete(t);
380
403
  return this.#i(a.map(([, t]) => ({
@@ -385,10 +408,10 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
385
408
  })), !0), a.length;
386
409
  }
387
410
  async upsert(e, t, n) {
388
- C(n);
411
+ E(n);
389
412
  let r = this.#p(e, t, n.conflictColumns);
390
413
  if (r) {
391
- let i = S(t, r, n), a = Object.fromEntries(n.conflictColumns.map((e) => [e, r[e]]));
414
+ let i = T(t, r, n), a = Object.fromEntries(n.conflictColumns.map((e) => [e, r[e]]));
392
415
  return await this.update(e, a, i) ?? r;
393
416
  }
394
417
  return this.insert(e, t);
@@ -409,14 +432,17 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
409
432
  #p(e, t, n) {
410
433
  let r = this.#e.get(this.#c(e));
411
434
  if (r && n.length !== 0) outer: for (let e of r.values()) {
412
- for (let r of n) if (e[r] !== t[r]) continue outer;
435
+ for (let r of n) {
436
+ let n = e[r], i = t[r];
437
+ if (n !== i && !(n instanceof Date && i instanceof Date && n.getTime() === i.getTime())) continue outer;
438
+ }
413
439
  return e;
414
440
  }
415
441
  }
416
442
  async transactional(e) {
417
- let t = n.tryPromise({
443
+ let t = a.tryPromise({
418
444
  try: async () => {
419
- let t = new G(this.#m());
445
+ let t = new J(this.#m());
420
446
  try {
421
447
  let n = await e(t);
422
448
  return t.commit(), n;
@@ -426,7 +452,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
426
452
  },
427
453
  catch: (e) => e
428
454
  });
429
- return y(await n.runPromiseExit(t.pipe(n.retry(x((e) => e instanceof V)))));
455
+ return S(await a.runPromiseExit(t.pipe(a.retry(w((e) => e instanceof W)))));
430
456
  }
431
457
  onChange(e) {
432
458
  return this.#n.on("change", e), () => {
@@ -434,11 +460,11 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
434
460
  };
435
461
  }
436
462
  injectExternalChange(e) {
437
- this.#r(u(e), !1);
463
+ this.#r(p(e), !1);
438
464
  }
439
465
  #m() {
440
466
  let e = new Map(this.#e);
441
- for (let t of e.values()) U.add(t);
467
+ for (let t of e.values()) K.add(t);
442
468
  let t = new Map(this.#t), n = /* @__PURE__ */ new Set();
443
469
  return {
444
470
  readTable: (t) => {
@@ -452,7 +478,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
452
478
  for (let e of r.keys()) i.add(this.#c(e));
453
479
  let a = new Set(n);
454
480
  for (let e of i) a.add(e);
455
- for (let e of a) if ((t.get(e) ?? 0n) !== (this.#t.get(e) ?? 0n)) throw new V();
481
+ for (let e of a) if ((t.get(e) ?? 0n) !== (this.#t.get(e) ?? 0n)) throw new W();
456
482
  for (let [e, t] of r) {
457
483
  if (!this.#e.get(this.#c(e))) continue;
458
484
  let n = this.#d(e);
@@ -468,7 +494,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
468
494
  allocateNumericId: (e) => this.#f(e)
469
495
  };
470
496
  }
471
- }, G = class {
497
+ }, J = class {
472
498
  parent;
473
499
  overlay = /* @__PURE__ */ new Map();
474
500
  tombstones = /* @__PURE__ */ new Map();
@@ -512,11 +538,12 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
512
538
  buildTablesView(e) {
513
539
  let t = /* @__PURE__ */ new Set([e.table]), n = (e) => {
514
540
  t.add(e.table);
541
+ for (let t of e.setOp?.queries ?? []) n(t);
515
542
  for (let r of e.joins ?? []) {
516
543
  t.add(r.table);
517
- for (let e of o(r.on)) n(e.descriptor);
544
+ for (let e of l(r.on)) n(e.descriptor);
518
545
  }
519
- if (e.predicate) for (let t of o(e.predicate)) n(t.descriptor);
546
+ if (e.predicate) for (let t of l(e.predicate)) n(t.descriptor);
520
547
  };
521
548
  n(e);
522
549
  let r = /* @__PURE__ */ new Map();
@@ -524,22 +551,25 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
524
551
  return r;
525
552
  }
526
553
  storedTables() {
527
- return [.../* @__PURE__ */ new Set([...this.parent.listTables(), ...this.overlay.keys()])].map((e) => R(e, this.materialise(e)));
554
+ return [.../* @__PURE__ */ new Set([...this.parent.listTables(), ...this.overlay.keys()])].map((e) => V(e, this.materialise(e)));
528
555
  }
529
556
  getOrCreateOverlay(e) {
530
557
  this.assertActive();
531
558
  let t = this.overlay.get(e);
532
559
  return t || (t = /* @__PURE__ */ new Map(), this.overlay.set(e, t)), t;
533
560
  }
561
+ integrityQuery(e) {
562
+ return this.query(e);
563
+ }
534
564
  async query(e) {
535
- let t = this.buildTablesView(e), n = N(e, this.materialise(e.table), t);
536
- return f(e) ? i(n, e.eager, _(e.table), (e) => this.query(e)) : n;
537
- }
538
- async insert(e, n) {
539
- n = t(e, b(e, n));
540
- let r = d(e), i = (r?.fields)?.id?.idScheme, a = n;
541
- a.id === void 0 && (r === void 0 || i !== void 0) && (a = {
542
- ...n,
565
+ let t = this.buildTablesView(e), n = I(e, this.materialise(e.table), t);
566
+ return h(e) ? s(n, e.eager, b(e.table), (e) => this.query(e)) : n;
567
+ }
568
+ async insert(e, t) {
569
+ t = i(e, C(e, t));
570
+ let n = m(e), r = (n?.fields)?.id?.idScheme, a = t;
571
+ a.id === void 0 && (n === void 0 || r !== void 0) && (a = {
572
+ ...t,
543
573
  id: this.parent.allocateNumericId(e)
544
574
  });
545
575
  let o = a.id === void 0 ? `__voltro_row_${this.parent.allocateNumericId(e)}` : String(a.id);
@@ -550,13 +580,13 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
550
580
  new: a
551
581
  }), a;
552
582
  }
553
- async insertMany(e, t) {
554
- let n = [];
555
- for (let r of t) n.push(await this.insert(e, r));
556
- return n;
583
+ async insertMany(e, t, n) {
584
+ let r = [];
585
+ for (let n of t) r.push(await this.insert(e, n));
586
+ return n?.returning === "none" ? [] : r;
557
587
  }
558
588
  async update(e, t, n) {
559
- let r = this.materialise(e), i = H(r, t), a = i === void 0 ? void 0 : r.get(i);
589
+ let r = this.materialise(e), i = G(r, t), a = i === void 0 ? void 0 : r.get(i);
560
590
  if (!a || i === void 0) return null;
561
591
  let o = {
562
592
  ...a,
@@ -569,12 +599,12 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
569
599
  new: o
570
600
  }), o;
571
601
  }
572
- async patchJson(e, t, n, i, a) {
573
- let o = m(t, n, a), s = this.materialise(e), c = H(s, t), l = c === void 0 ? void 0 : s.get(c);
602
+ async patchJson(e, t, n, r, i) {
603
+ let a = _(t, n, i), s = this.materialise(e), c = G(s, t), l = c === void 0 ? void 0 : s.get(c);
574
604
  if (!l || c === void 0) return null;
575
605
  let u = {
576
- ...r(l, n, i),
577
- ...o
606
+ ...o(l, n, r),
607
+ ...a
578
608
  };
579
609
  return this.getOrCreateOverlay(e).set(c, u), this.events.push({
580
610
  table: e,
@@ -584,7 +614,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
584
614
  }), u;
585
615
  }
586
616
  async delete(e, t) {
587
- let n = this.materialise(e), r = H(n, t), i = r === void 0 ? void 0 : n.get(r);
617
+ let n = this.materialise(e), r = G(n, t), i = r === void 0 ? void 0 : n.get(r);
588
618
  if (!i || r === void 0) return !1;
589
619
  this.overlay.get(e)?.delete(r);
590
620
  let a = this.tombstones.get(e);
@@ -596,7 +626,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
596
626
  }), !0;
597
627
  }
598
628
  async updateMany(e, t, n) {
599
- let r = this.materialise(e), i = D(n.where, this.buildTablesViewForPred(n.where)), a = h(n.where, i, d(e)?.fields), o = 0;
629
+ let r = this.materialise(e), i = A(n.where, this.buildTablesViewForPred(n.where)), a = v(n.where, i, m(e)?.fields), o = 0;
600
630
  for (let [n, i] of r) {
601
631
  if (!a(i)) continue;
602
632
  let r = {
@@ -613,7 +643,7 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
613
643
  return o;
614
644
  }
615
645
  async deleteMany(e, t) {
616
- let n = this.materialise(e), r = D(t.where, this.buildTablesViewForPred(t.where)), i = h(t.where, r, d(e)?.fields), a = 0, o = this.tombstones.get(e);
646
+ let n = this.materialise(e), r = A(t.where, this.buildTablesViewForPred(t.where)), i = v(t.where, r, m(e)?.fields), a = 0, o = this.tombstones.get(e);
617
647
  for (let [t, r] of n) i(r) && (this.overlay.get(e)?.delete(t), o || (o = /* @__PURE__ */ new Set(), this.tombstones.set(e, o)), o.add(t), this.events.push({
618
648
  table: e,
619
649
  op: "delete",
@@ -624,16 +654,16 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
624
654
  }
625
655
  buildTablesViewForPred(e) {
626
656
  let t = /* @__PURE__ */ new Set();
627
- for (let n of o(e)) t.add(n.descriptor.table);
657
+ for (let n of l(e)) t.add(n.descriptor.table);
628
658
  let n = /* @__PURE__ */ new Map();
629
659
  for (let e of t) n.set(e, this.materialise(e));
630
660
  return n;
631
661
  }
632
662
  async upsert(e, t, n) {
633
- C(n);
663
+ E(n);
634
664
  let r = this.findByConflictColumns(e, t, n.conflictColumns);
635
665
  if (r) {
636
- let i = S(t, r, n), a = Object.fromEntries(n.conflictColumns.map((e) => [e, r[e]]));
666
+ let i = T(t, r, n), a = Object.fromEntries(n.conflictColumns.map((e) => [e, r[e]]));
637
667
  return await this.update(e, a, i) ?? r;
638
668
  }
639
669
  return this.insert(e, t);
@@ -655,7 +685,10 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
655
685
  if (n.length === 0) return;
656
686
  let r = this.materialise(e);
657
687
  outer: for (let e of r.values()) {
658
- for (let r of n) if (e[r] !== t[r]) continue outer;
688
+ for (let r of n) {
689
+ let n = e[r], i = t[r];
690
+ if (n !== i && !(n instanceof Date && i instanceof Date && n.getTime() === i.getTime())) continue outer;
691
+ }
659
692
  return e;
660
693
  }
661
694
  }
@@ -676,4 +709,4 @@ var T = (e, t, n, r = d(n)?.fields) => t.length === 0 ? e : [...e].sort((e, n) =
676
709
  }
677
710
  };
678
711
  //#endregion
679
- export { W as InMemoryDataStore };
712
+ export { q as InMemoryDataStore };
@@ -14,8 +14,9 @@ var e = class extends Error {
14
14
  };
15
15
  return {
16
16
  query: async (e) => (i(e.table, "query"), n.query(e)),
17
+ ...n.integrityQuery === void 0 ? {} : { integrityQuery: async (e) => (i(e.table, "integrityQuery"), n.integrityQuery(e)) },
17
18
  insert: async (e, t) => (i(e, "insert"), n.insert(e, t)),
18
- insertMany: async (e, t) => (i(e, "insertMany"), n.insertMany(e, t)),
19
+ insertMany: async (e, t, r) => (i(e, "insertMany"), n.insertMany(e, t, r)),
19
20
  patchJson: async (e, t, r, a, o) => (i(e, "patchJson"), n.patchJson(e, t, r, a, o)),
20
21
  update: async (e, t, r) => (i(e, "update"), n.update(e, t, r)),
21
22
  updateMany: async (e, t, r) => (i(e, "updateMany"), n.updateMany(e, t, r)),