@voltro/runtime 0.85.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.
- package/CHANGELOG.md +5987 -0
- package/README.md +43 -0
- package/THIRD-PARTY-NOTICES.md +864 -253
- package/dist/aggregate.js +42 -0
- package/dist/aggregateExtrema.js +80 -0
- package/dist/aggregateIvm.js +115 -0
- package/dist/analyticsMirror.js +228 -0
- package/dist/analyticsSink.js +32 -0
- package/dist/apiKeyUsage.js +33 -0
- package/dist/apiKeys.js +227 -0
- package/dist/apiKeysWiring.js +114 -0
- package/dist/apiRoleBucketSampler.js +64 -0
- package/dist/apiRoleWorkloadHistory.js +56 -0
- package/dist/apiRoleWorkloadMetrics.js +78 -0
- package/dist/apiRoleWorkloadStorage.js +95 -0
- package/dist/approvalBuiltins.js +10 -0
- package/dist/approvals.js +262 -0
- package/dist/authMiddlewareLayer.js +48 -0
- package/dist/bindEvent.js +92 -0
- package/dist/bootListener.js +143 -0
- package/dist/bootRefusal.js +7 -0
- package/dist/bootStoreCodec.js +71 -0
- package/dist/businessRules.js +67 -0
- package/dist/changeClaimsTable.js +12 -0
- package/dist/circuitBreaker.js +1 -0
- package/dist/columnOnUpdate.js +23 -0
- package/dist/composeAnalytics.js +129 -0
- package/dist/computedQuery.js +4 -0
- package/dist/computedQueryCache.js +231 -0
- package/dist/connectionBuiltins.js +81 -0
- package/dist/connectionCredentials.js +66 -0
- package/dist/connectionState.js +34 -0
- package/dist/connectionVault.js +410 -0
- package/dist/connectionsFacade.js +56 -0
- package/dist/coordinatedSchedule.js +145 -0
- package/dist/crdtHistory.js +138 -0
- package/dist/crdtMerge.js +179 -0
- package/dist/crdtVersionsTable.js +25 -0
- package/dist/credentialBound.js +12 -0
- package/dist/crud.js +72 -0
- package/dist/dataStore.js +712 -0
- package/dist/dataloader.js +61 -0
- package/dist/declaredTablesGuard.js +49 -0
- package/dist/defineExecutor.js +4 -0
- package/dist/dependencyGraph.js +32 -0
- package/dist/devices.js +228 -0
- package/dist/devicesWiring.js +168 -0
- package/dist/dialectReplicationAdapter.js +29 -0
- package/dist/dispatcher.js +522 -0
- package/dist/effectStore.js +71 -0
- package/dist/engineClients.js +24 -0
- package/dist/eventBus.js +260 -0
- package/dist/eventPublish.js +49 -0
- package/dist/events.js +193 -0
- package/dist/expectation.js +52 -0
- package/dist/expectationEvaluator.js +308 -0
- package/dist/experiment.js +96 -0
- package/dist/experimentEvaluator.js +245 -0
- package/dist/externalizedTasks.js +38 -0
- package/dist/fieldCipher.js +130 -0
- package/dist/fieldKeyRegistry.js +295 -0
- package/dist/fieldKeyRekey.js +286 -0
- package/dist/fieldKeyScan.js +136 -0
- package/dist/fieldKeyring.js +77 -0
- package/dist/fingerprint.js +21 -0
- package/dist/finops.js +42 -0
- package/dist/finopsAccountant.js +216 -0
- package/dist/handlerBody.js +8 -0
- package/dist/healthProbes.js +55 -0
- package/dist/hot.d.ts +56 -0
- package/dist/hot.js +3 -0
- package/dist/hotDataStore.js +148 -0
- package/dist/hotDataStoreSnapshot.js +98 -0
- package/dist/hotDataStoreTables.js +18 -0
- package/dist/http-range.d.ts +53 -0
- package/dist/http-range.js +52 -0
- package/dist/httpClientSsrf.js +24 -0
- package/dist/httpClientTracing.js +14 -0
- package/dist/httpCompression.js +76 -0
- package/dist/httpInputStream.js +82 -0
- package/dist/httpKeepAlive.js +12 -0
- package/dist/httpRouteMounts.js +48 -0
- package/dist/idempotencyStore.js +110 -0
- package/dist/idleDetector.js +21 -0
- package/dist/index.d.ts +1249 -517
- package/dist/index.js +155 -15292
- package/dist/insertRequiredColumns.js +19 -0
- package/dist/inspectWorkflow.js +1 -0
- package/dist/instanceMembership.js +98 -0
- package/dist/ivm.js +125 -0
- package/dist/kvDataStore.js +86 -0
- package/dist/kvFacade.js +38 -0
- package/dist/matcher.js +304 -0
- package/dist/memoryColumnDefaults.js +11 -0
- package/dist/memoryQueryJoins.js +114 -0
- package/dist/memorySetOperations.js +43 -0
- package/dist/metrics.js +175 -0
- package/dist/observability.js +123 -0
- package/dist/observedGraph.js +52 -0
- package/dist/originGuard.js +116 -0
- package/dist/outbox.js +352 -0
- package/dist/pluginRefRules.js +65 -0
- package/dist/policyGuardResolver.js +55 -0
- package/dist/preview.js +62 -0
- package/dist/projectedRows.js +12 -0
- package/dist/provenance.js +63 -0
- package/dist/rangeIndex.js +89 -0
- package/dist/rawReadScope.js +17 -0
- package/dist/reaction.js +85 -0
- package/dist/reactiveConfig.js +11 -0
- package/dist/readClassification.js +57 -0
- package/dist/readRecorder.js +67 -0
- package/dist/readScopePredicate.js +12 -0
- package/dist/rebac.js +104 -0
- package/dist/rebacInterceptor.js +81 -0
- package/dist/registry.js +29 -0
- package/dist/relevantFields.js +65 -0
- package/dist/replicaHealth.js +42 -0
- package/dist/replicaLocality.js +44 -0
- package/dist/replicaObservations.js +121 -0
- package/dist/replicaSelection.js +32 -0
- package/dist/replicatedDataStore.js +257 -0
- package/dist/requestLoader.js +7 -0
- package/dist/resumeCensus.js +28 -0
- package/dist/revalidation.js +22 -0
- package/dist/rowFilter.js +82 -0
- package/dist/rowStateBytes.js +45 -0
- package/dist/rpcBodyCap.js +63 -0
- package/dist/rpcConnections.js +83 -0
- package/dist/rpcDefectDiagnostics.js +15 -0
- package/dist/rpcServer.js +1065 -0
- package/dist/rpcWorkloadAdmission.js +50 -0
- package/dist/runAsSystem.js +23 -0
- package/dist/rywPositionStore.js +119 -0
- package/dist/schedule.js +23 -0
- package/dist/scheduleBackfillRequests.js +151 -0
- package/dist/scheduleClaimsTable.js +11 -0
- package/dist/scheduleRunsTable.js +37 -0
- package/dist/scheduler.js +840 -0
- package/dist/schemaRegistry.js +65 -0
- package/dist/secretsResolver.js +35 -0
- package/dist/securityHeaders.js +33 -0
- package/dist/servePipeline.js +374 -0
- package/dist/serverOnlyAudit.js +66 -0
- package/dist/serverTracer.js +15 -0
- package/dist/serviceCredentials.js +40 -0
- package/dist/setReadScopes.js +28 -0
- package/dist/singleNodeOrchestrator.js +91 -0
- package/dist/snapshotValueCodec.js +46 -0
- package/dist/sourceResolution.js +52 -0
- package/dist/storeMiddleware.js +882 -0
- package/dist/storeQuery.js +204 -0
- package/dist/storeRead.js +10 -0
- package/dist/subscriber.js +8 -0
- package/dist/subscriptionOutbox.js +139 -0
- package/dist/subscriptionRegistry.js +82 -0
- package/dist/subscriptionResume.js +132 -0
- package/dist/subscriptionSocketMetrics.js +65 -0
- package/dist/systemSubject.js +5 -0
- package/dist/tenantScope.js +11 -0
- package/dist/tenantSelection.js +27 -0
- package/dist/timeline.js +105 -0
- package/dist/traceContext.js +5 -0
- package/dist/traceSink.js +63 -0
- package/dist/traceSpansTable.js +29 -0
- package/dist/traceparent.js +23 -0
- package/dist/transactionSubject.js +12 -0
- package/dist/trustedProxy.js +155 -0
- package/dist/undo.js +129 -0
- package/dist/undoBuiltins.js +69 -0
- package/dist/undoCapture.js +74 -0
- package/dist/undoLogTable.js +16 -0
- package/dist/upsertBinding.js +20 -0
- package/dist/wakeOrchestrator.js +34 -0
- package/dist/wakeRegistry.js +71 -0
- package/dist/wakeRouter.js +63 -0
- package/dist/wakeSupervisor.js +62 -0
- package/dist/wakeupsTable.js +12 -0
- package/dist/webSocketGatewayRuntime.js +154 -0
- package/dist/webhookVerification.js +18 -0
- package/dist/wireColumnStrip.js +84 -0
- package/dist/workflowReadBuiltins.js +87 -0
- package/dist/workflowRuntime.js +708 -0
- package/package.json +19 -7
|
@@ -0,0 +1,882 @@
|
|
|
1
|
+
import { isolateRawReadScope as e, rawSqlPreview as t, recordRawRead as n } from "./rawReadScope.js";
|
|
2
|
+
import { applySealedWrite as r, assertCrdtUpdateAdmissible as i, mergeCrdtColumnsIntoPatch as a, patchTouchesCrdt as o } from "./crdtMerge.js";
|
|
3
|
+
import { mapSetReadScopes as s } from "./setReadScopes.js";
|
|
4
|
+
import { NO_ROW_FILTER as c, applyRowFilter as l, rowFilterRegistered as u } from "./rowFilter.js";
|
|
5
|
+
import { addReadScopePredicate as d } from "./readScopePredicate.js";
|
|
6
|
+
import { readMaybeOne as f } from "./storeRead.js";
|
|
7
|
+
import { CRDT_VERSIONS_TABLE as p } from "./crdtVersionsTable.js";
|
|
8
|
+
import { getFieldCipher as m } from "./fieldCipher.js";
|
|
9
|
+
import { observeStore as h } from "./observedGraph.js";
|
|
10
|
+
import { upsertConflictId as g } from "./upsertBinding.js";
|
|
11
|
+
import { applyColumnOnUpdate as _ } from "./columnOnUpdate.js";
|
|
12
|
+
import { prepareInsertRequiredColumns as v } from "./insertRequiredColumns.js";
|
|
13
|
+
import { makeFluentStore as ee, missingRowError as te } from "./storeQuery.js";
|
|
14
|
+
import { Schema as ne, SchemaIssue as re } from "effect";
|
|
15
|
+
import { INITIAL_VERSION as ie, VersionConflict as ae, and as y, classifyConstraintViolation as oe, decryptFieldsOnRead as se, deserializeArraysOnRead as ce, encryptFieldsForWrite as le, encryptFieldsForWriteBatch as ue, encryptedColumnsOf as de, eq as b, extractDbCause as fe, generateId as pe, getRelations as me, getTable as x, gt as he, inSet as ge, isCompositeRowKey as _e, isNull as S, jsonPatchAssignments as ve, missingConflictColumns as ye, missingRequiredColumns as be, or as xe, planVersionedUpdate as Se, refuseIdChangeOfSealedRow as Ce, rowKeyColumns as we, rowKeyIdentity as C, serializeArraysForWrite as Te, settleVersionedUpdate as Ee, storeRefusalOf as De, upsertUpdateValues as Oe } from "@voltro/database";
|
|
16
|
+
import { ConstraintViolation as ke, TableValidationFailed as w, TenantRowNotFound as Ae, TenantScopeViolation as je, ValidationError as Me, isMachineSubject as Ne, subjectAuditActorId as T } from "@voltro/protocol";
|
|
17
|
+
import { createLogger as Pe } from "@voltro/logger";
|
|
18
|
+
import { defaultCrdtBackend as E } from "@voltro/local-first";
|
|
19
|
+
//#region src/storeMiddleware.ts
|
|
20
|
+
var Fe = (e, t, n) => {
|
|
21
|
+
let r = me(e);
|
|
22
|
+
if (r === void 0) return t;
|
|
23
|
+
let i = {};
|
|
24
|
+
for (let [a, o] of Object.entries(t)) {
|
|
25
|
+
if (o === !1 || o === void 0) {
|
|
26
|
+
i[a] = o;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
let t = r[a];
|
|
30
|
+
if (t === void 0) {
|
|
31
|
+
i[a] = o;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let s = t.through;
|
|
35
|
+
if (s !== void 0) {
|
|
36
|
+
let t;
|
|
37
|
+
try {
|
|
38
|
+
t = s().tableName;
|
|
39
|
+
} catch {
|
|
40
|
+
t = void 0;
|
|
41
|
+
}
|
|
42
|
+
if (t !== void 0 && n(t) !== void 0) throw Error(`row filter: the junction table '${t}' behind relation '${a}' on '${e}' is row-filtered, and a many-to-many junction is read below the seam that could narrow it — its rows would be joined unfiltered. Refusing the read rather than serving it.\n → read '${t}' as its own query (it is filtered there), or\n → drop '${a}' from this .with(...).`);
|
|
43
|
+
}
|
|
44
|
+
let c;
|
|
45
|
+
try {
|
|
46
|
+
c = t.target().tableName;
|
|
47
|
+
} catch {
|
|
48
|
+
c = void 0;
|
|
49
|
+
}
|
|
50
|
+
let l = o === !0 ? {} : { ...o };
|
|
51
|
+
if (c !== void 0) {
|
|
52
|
+
let e = n(c);
|
|
53
|
+
if (e !== void 0) {
|
|
54
|
+
let t = l.where;
|
|
55
|
+
l.where = t === void 0 ? e : y(t, e);
|
|
56
|
+
}
|
|
57
|
+
let t = l.with;
|
|
58
|
+
t !== void 0 && (l.with = Fe(c, t, n));
|
|
59
|
+
}
|
|
60
|
+
i[a] = l;
|
|
61
|
+
}
|
|
62
|
+
return i;
|
|
63
|
+
}, D = "id", Ie = 3, O = 8, Le = (e) => "descriptor" in e ? e.descriptor : e, Re = (e, t) => {
|
|
64
|
+
let n = {};
|
|
65
|
+
for (let r of t) e[r] !== void 0 && (n[r] = e[r]);
|
|
66
|
+
return n;
|
|
67
|
+
}, k = "cannot insert into tenant-scoped table without an authenticated tenant — subject.tenantId is null. Either authenticate first or pass tenantId explicitly in the row (admin tooling).", ze = (e) => {
|
|
68
|
+
throw new je({
|
|
69
|
+
table: e,
|
|
70
|
+
reason: k
|
|
71
|
+
});
|
|
72
|
+
}, Be = (e) => T(e), Ve = (me, T) => {
|
|
73
|
+
let k = h(me);
|
|
74
|
+
e();
|
|
75
|
+
let A = Be(T.subject), M = T.subject.tenantId;
|
|
76
|
+
if (T.rowFilter === void 0 && !Ne(T.subject) && u()) throw Error("row filter registered, but this store was scoped without one — refusing to serve an unfiltered read.\n\nThis app called `setRowFilter`, so every scoped store must receive a resolved `rowFilter` (the serve pipeline's `withRowFilter` does this per request). Reaching here with none means a read path was built outside that spine, and continuing would serve rows the filter exists to hide — silently, which is how this was found in the first place.\n\nIf this path is deliberately unfiltered (a system sweep, a migration), pass `rowFilter: NO_ROW_FILTER` explicitly.");
|
|
77
|
+
let N = T.rowFilter ?? c, P = (e, t) => {
|
|
78
|
+
let n = { ...t }, r = T.schemaRegistry, i = r.idScheme(e);
|
|
79
|
+
if (i !== void 0 && n[D] == null) {
|
|
80
|
+
if (i.kind === "numeric") delete n[D];
|
|
81
|
+
else {
|
|
82
|
+
let t = pe(i, e);
|
|
83
|
+
t !== null && (n[D] = t);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
let a = r.versionColumn(e);
|
|
87
|
+
if (a !== void 0) {
|
|
88
|
+
let e = n[a];
|
|
89
|
+
(typeof e != "number" || !Number.isFinite(e)) && (n[a] = ie);
|
|
90
|
+
}
|
|
91
|
+
if (r.hasAudit(e)) {
|
|
92
|
+
let e = /* @__PURE__ */ new Date();
|
|
93
|
+
"createdAt" in n || (n.createdAt = e), "updatedAt" in n || (n.updatedAt = e), A !== null && ("createdBy" in n || (n.createdBy = A), "updatedBy" in n || (n.updatedBy = A));
|
|
94
|
+
}
|
|
95
|
+
r.hasTenant(e) && ("tenantId" in n || (M === null && ze(e), n.tenantId = M));
|
|
96
|
+
let o = r.defaultFactories(e);
|
|
97
|
+
if (o.size > 0) for (let [e, t] of o) (!(e in n) || n[e] === void 0) && (n[e] = t());
|
|
98
|
+
let s = r.computedFields(e);
|
|
99
|
+
if (s.size > 0) for (let [e, t] of s) n[e] = t(n);
|
|
100
|
+
let c = r.generatedColumns(e);
|
|
101
|
+
if (c.size > 0) for (let e of c) delete n[e];
|
|
102
|
+
return n;
|
|
103
|
+
}, F = (e, t, n = []) => {
|
|
104
|
+
let r = _(T.schemaRegistry, e, t, n);
|
|
105
|
+
if (!T.schemaRegistry.hasAudit(e)) return r;
|
|
106
|
+
let i = { ...r };
|
|
107
|
+
return "updatedAt" in i || (i.updatedAt = /* @__PURE__ */ new Date()), A !== null && !("updatedBy" in i) && (i.updatedBy = A), i;
|
|
108
|
+
}, I = async (e, t, n, r) => {
|
|
109
|
+
try {
|
|
110
|
+
return await r();
|
|
111
|
+
} catch (r) {
|
|
112
|
+
let i = De(r);
|
|
113
|
+
if (i !== void 0) throw new ke({
|
|
114
|
+
kind: i.kind,
|
|
115
|
+
table: e,
|
|
116
|
+
operation: t,
|
|
117
|
+
...i.constraint === void 0 ? {} : { constraint: i.constraint },
|
|
118
|
+
...i.column === void 0 ? {} : { column: i.column },
|
|
119
|
+
detail: i.detail
|
|
120
|
+
});
|
|
121
|
+
let a = fe(r), o = oe(a, t);
|
|
122
|
+
if (o === void 0) throw r;
|
|
123
|
+
if (T.schemaRegistry.hasTenant(e) && (o.kind === "foreignKey" || o.kind === "foreignKeyInUse")) {
|
|
124
|
+
let t = n?.tenantId, r = `${a.dbConstraint ?? ""} ${a.dbColumn ?? ""} ${a.dbMessage ?? ""} ${a.dbCauseChain ?? ""}`.toLowerCase();
|
|
125
|
+
if (r.includes("tenantid") || /\btenants\b/.test(r) || t != null && r.includes(String(t).toLowerCase())) throw new je({
|
|
126
|
+
table: e,
|
|
127
|
+
reason: t == null ? "write references a tenant that does not exist" : `tenant '${String(t)}' does not exist — seed the row in \`tenants\`, or fix how the request resolved this tenant (subject.tenantId / the x-tenant header / the dev TENANT fallback)`
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
throw new ke({
|
|
131
|
+
kind: o.kind,
|
|
132
|
+
table: e,
|
|
133
|
+
operation: t,
|
|
134
|
+
...o.constraint === void 0 ? {} : { constraint: o.constraint },
|
|
135
|
+
...o.column === void 0 ? {} : { column: o.column }
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}, He = (e, t, n) => {
|
|
139
|
+
let r = T.schemaRegistry.computedFields(e);
|
|
140
|
+
if (r.size === 0) return n;
|
|
141
|
+
let i = {
|
|
142
|
+
...t,
|
|
143
|
+
...n
|
|
144
|
+
}, a = { ...n };
|
|
145
|
+
for (let [e, t] of r) a[e] = t(i);
|
|
146
|
+
return a;
|
|
147
|
+
}, L = (e, t) => {
|
|
148
|
+
let n = T.schemaRegistry.validatePatchSchema(e);
|
|
149
|
+
if (n === void 0) return t;
|
|
150
|
+
let r = ne.decodeUnknownResult(n)(t);
|
|
151
|
+
if (r._tag === "Success") return t;
|
|
152
|
+
let i = re.makeFormatterStandardSchemaV1()(r.failure.issue).issues.map((e) => ({
|
|
153
|
+
path: (e.path ?? []).length === 0 ? "<root>" : (e.path ?? []).join("."),
|
|
154
|
+
message: e.message
|
|
155
|
+
})), a = i[0];
|
|
156
|
+
throw new w({
|
|
157
|
+
table: e,
|
|
158
|
+
summary: a === void 0 ? "patch validation failed" : `${a.path}: ${a.message}`,
|
|
159
|
+
issues: i
|
|
160
|
+
});
|
|
161
|
+
}, Ue = () => {
|
|
162
|
+
let e = { deletedAt: /* @__PURE__ */ new Date() };
|
|
163
|
+
return A !== null && (e.deletedBy = A), e;
|
|
164
|
+
}, We = (e, t, n) => {
|
|
165
|
+
let r = ye(x(e), n, t);
|
|
166
|
+
if (r.length !== 0) throw new w({
|
|
167
|
+
table: e,
|
|
168
|
+
summary: `upsert conflictColumns [${n.map((e) => `'${e}'`).join(", ")}] cannot match: ` + r.map((e) => e.reason === "null" ? `'${e.column}' is NULL` : `'${e.column}' is absent`).join(", "),
|
|
169
|
+
issues: r.map((e) => ({
|
|
170
|
+
path: e.column,
|
|
171
|
+
message: e.reason === "null" ? "named in conflictColumns but NULL — NULL never matches a unique conflict target, so this write would always INSERT. Use a plain insert for NULL-keyed rows, or make the column NOT NULL." : "named in conflictColumns but absent from the payload"
|
|
172
|
+
}))
|
|
173
|
+
});
|
|
174
|
+
}, R = (e, t, n) => {
|
|
175
|
+
let i = T.schemaRegistry.crdtColumns(e);
|
|
176
|
+
o(i, t) && (t = a(i, void 0, t, e));
|
|
177
|
+
for (let n of T.schemaRegistry.sealedColumns(e)) {
|
|
178
|
+
let i = t[n];
|
|
179
|
+
i instanceof Uint8Array && (t = {
|
|
180
|
+
...t,
|
|
181
|
+
[n]: r(e, n, void 0, i).log
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
let s = x(e);
|
|
185
|
+
if (s !== void 0) {
|
|
186
|
+
let r = n === void 0 ? be(s, t) : n.filter((e) => t[e] === void 0 || t[e] === null);
|
|
187
|
+
if (r.length > 0) throw new w({
|
|
188
|
+
table: e,
|
|
189
|
+
summary: `missing required column${r.length > 1 ? "s" : ""} ${r.map((e) => `'${e}'`).join(", ")} — NOT NULL with no default and not auto-stamped`,
|
|
190
|
+
issues: r.map((e) => ({
|
|
191
|
+
path: e,
|
|
192
|
+
message: "required (NOT NULL, no default) but absent from the payload"
|
|
193
|
+
}))
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
let c = T.schemaRegistry.insertSchema(e);
|
|
197
|
+
if (c === void 0) return t;
|
|
198
|
+
let l = ne.decodeUnknownResult(c)(t);
|
|
199
|
+
if (l._tag === "Success") return t;
|
|
200
|
+
let u = re.makeFormatterStandardSchemaV1()(l.failure.issue).issues.map((e) => ({
|
|
201
|
+
path: (e.path ?? []).length === 0 ? "<root>" : (e.path ?? []).join("."),
|
|
202
|
+
message: e.message
|
|
203
|
+
})), d = u[0];
|
|
204
|
+
throw new w({
|
|
205
|
+
table: e,
|
|
206
|
+
summary: d === void 0 ? "row validation failed" : `${d.path}: ${d.message}`,
|
|
207
|
+
issues: u
|
|
208
|
+
});
|
|
209
|
+
}, z = T.dialect, B = (e, t, n) => {
|
|
210
|
+
let r = x(e), i = z ? Te(t, r, z) : t;
|
|
211
|
+
return le(i, r, m(), n);
|
|
212
|
+
}, V = (e, t, n) => (Ce(t, x(e), n), B(e, t, n)), H = (e, t) => {
|
|
213
|
+
let n = x(e);
|
|
214
|
+
return n !== void 0 && de(n).some((e) => t[e] !== void 0 && t[e] !== null);
|
|
215
|
+
}, Ge = (e) => {
|
|
216
|
+
let t = e.projection;
|
|
217
|
+
if (t === void 0 || t.includes(D)) return {
|
|
218
|
+
descriptor: e,
|
|
219
|
+
stripId: !1
|
|
220
|
+
};
|
|
221
|
+
let n = x(e.table);
|
|
222
|
+
return n === void 0 || de(n).length === 0 ? {
|
|
223
|
+
descriptor: e,
|
|
224
|
+
stripId: !1
|
|
225
|
+
} : {
|
|
226
|
+
descriptor: {
|
|
227
|
+
...e,
|
|
228
|
+
projection: [...t, D]
|
|
229
|
+
},
|
|
230
|
+
stripId: !0
|
|
231
|
+
};
|
|
232
|
+
}, Ke = process.env.VOLTRO_FIELD_DECRYPT_ON_ERROR === "null" ? "null" : "throw", qe = Pe({ scope: "store.fieldEncryption" }), Je = /* @__PURE__ */ new Set(), Ye = (e) => {
|
|
233
|
+
let t = `${e.table}.${e.column}`;
|
|
234
|
+
Je.has(t) || (Je.add(t), qe.warn("encrypted column degraded to null — could not decrypt with the active key", {
|
|
235
|
+
table: e.table,
|
|
236
|
+
column: e.column,
|
|
237
|
+
reason: e.reason,
|
|
238
|
+
hint: "VOLTRO_FIELD_DECRYPT_ON_ERROR=null is set — this row was likely encrypted under a different key"
|
|
239
|
+
}));
|
|
240
|
+
}, U = (e, t, n) => {
|
|
241
|
+
let r = x(e), i = se(t, r, m(), {
|
|
242
|
+
onError: Ke,
|
|
243
|
+
...Ke === "null" ? { warn: Ye } : {},
|
|
244
|
+
...n === void 0 ? {} : { rowId: n }
|
|
245
|
+
});
|
|
246
|
+
return z ? ce(i, r, z) : i;
|
|
247
|
+
}, Xe = (e, t, n = N) => {
|
|
248
|
+
let r = e, i = r.table;
|
|
249
|
+
!r.crossTenant && M != null && T.schemaRegistry.hasTenant(i) && (r = d(r, (e) => b(e("tenantId"), M))), !r.includeDeleted && T.schemaRegistry.hasSoftDelete(i) && (r = d(r, (e) => S(e("deletedAt")))), !r.includeExpired && T.schemaRegistry.hasExpires(i) && (r = d(r, (e) => xe(S(e("expiresAt")), he(e("expiresAt"), t))));
|
|
250
|
+
let a = r.predicate;
|
|
251
|
+
a = l(n, i, a);
|
|
252
|
+
let o = r.eager === void 0 ? void 0 : Fe(i, r.eager, n);
|
|
253
|
+
return o === void 0 ? a === r.predicate ? r : {
|
|
254
|
+
...r,
|
|
255
|
+
predicate: a
|
|
256
|
+
} : {
|
|
257
|
+
...r,
|
|
258
|
+
predicate: a,
|
|
259
|
+
eager: o
|
|
260
|
+
};
|
|
261
|
+
}, Ze = (e, t = N) => {
|
|
262
|
+
let n = /* @__PURE__ */ new Date();
|
|
263
|
+
return s(e, (e) => Xe(e, n, t));
|
|
264
|
+
}, Qe = async (e, t, n) => {
|
|
265
|
+
let r = Ge(e), i = await t(r.descriptor), a = U(e.table, i);
|
|
266
|
+
return n && et(e.table, a), r.stripId ? a.map((e) => {
|
|
267
|
+
let { [D]: t, ...n } = e;
|
|
268
|
+
return n;
|
|
269
|
+
}) : a;
|
|
270
|
+
}, $e = (e) => Qe(e, k.query.bind(k), !0), W = (e) => $e(Ze(e)), G = (e, t, n) => {
|
|
271
|
+
if (_e(n)) return;
|
|
272
|
+
let r = T.schemaRegistry.declaredPrimaryKey(e);
|
|
273
|
+
if (r.length === 0 || r.length === 1 && r[0] === "id") return;
|
|
274
|
+
let i = r.map((e) => `${e}: …`).join(", ");
|
|
275
|
+
throw Error(`${t}('${e}', <${typeof n}>, …) cannot address a row: '${e}' has no \`id\` column — its primary key is (${r.join(", ")}). Pass those columns instead:\n store.${t}('${e}', { ${i} }${t === "delete" ? "" : ", patch"})\nThis used to return ${t === "delete" ? "`false`" : "`null`"}, which is also what a missing row returns.`);
|
|
276
|
+
}, K = async (e, t) => {
|
|
277
|
+
if (M === null || !T.schemaRegistry.hasTenant(e)) return;
|
|
278
|
+
let n = we(t).map(([e, t]) => b(e, t)).reduce((e, t) => e === void 0 ? t : y(e, t), void 0);
|
|
279
|
+
if (!((await k.query({
|
|
280
|
+
table: e,
|
|
281
|
+
lock: "update",
|
|
282
|
+
predicate: y(n, b("tenantId", M)),
|
|
283
|
+
order: [],
|
|
284
|
+
take: 1,
|
|
285
|
+
skip: void 0,
|
|
286
|
+
projection: we(t).map(([e]) => e)
|
|
287
|
+
})).length > 0)) throw new Ae({
|
|
288
|
+
table: e,
|
|
289
|
+
id: C(t),
|
|
290
|
+
reason: "no row with this id in your tenant — it does not exist, or it belongs to another tenant. These are deliberately indistinguishable: reporting them differently would let a caller probe for row ids in other tenants."
|
|
291
|
+
});
|
|
292
|
+
}, q = Symbol.for("@voltro/runtime:tx-fold-releases"), J = Symbol.for("@voltro/runtime:tx-read-versions"), Y = (e) => e[J], X = (e, t) => `${e}\u0000${String(t)}`, et = (e, t) => {
|
|
293
|
+
let n = Y(k);
|
|
294
|
+
if (n === void 0 || t.length === 0) return;
|
|
295
|
+
let r = T.schemaRegistry.versionColumn(e);
|
|
296
|
+
if (r !== void 0) for (let i of t) {
|
|
297
|
+
let t = i[D], a = i[r];
|
|
298
|
+
typeof t == "string" && typeof a == "number" && Number.isFinite(a) && n.set(X(e, t), a);
|
|
299
|
+
}
|
|
300
|
+
}, Z = (() => {
|
|
301
|
+
let e = globalThis, t = Symbol.for("@voltro/runtime:crdt-fold-locks");
|
|
302
|
+
return e[t] ??= /* @__PURE__ */ new Map();
|
|
303
|
+
})(), tt = async (e, t, n) => {
|
|
304
|
+
let r = `${e}\u0000${C(t)}`, i = Z.get(r) ?? Promise.resolve(), a, o = new Promise((e) => {
|
|
305
|
+
a = e;
|
|
306
|
+
});
|
|
307
|
+
Z.set(r, i.then(() => o)), await i;
|
|
308
|
+
try {
|
|
309
|
+
return await n();
|
|
310
|
+
} finally {
|
|
311
|
+
a(), Z.get(r) === o && Z.delete(r);
|
|
312
|
+
}
|
|
313
|
+
}, Q = async (e, t, n) => {
|
|
314
|
+
let r = T.schemaRegistry.sealedColumns(e);
|
|
315
|
+
if (o(r, n)) {
|
|
316
|
+
let i = {}, a = {};
|
|
317
|
+
for (let [e, t] of Object.entries(n)) (r.has(e) ? i : a)[e] = t;
|
|
318
|
+
let o = await at(e, t, i);
|
|
319
|
+
return o === null || Object.keys(a).length === 0 ? o : Q(e, t, a);
|
|
320
|
+
}
|
|
321
|
+
let a = T.schemaRegistry.crdtColumns(e);
|
|
322
|
+
if (o(a, n)) {
|
|
323
|
+
for (let t of a) t in n && i(e, t, n[t]);
|
|
324
|
+
let r = k[q];
|
|
325
|
+
if (r !== void 0) {
|
|
326
|
+
let i = {}, o = {};
|
|
327
|
+
for (let [e, t] of Object.entries(n)) a.has(e) ? i[e] = t : o[e] = t;
|
|
328
|
+
if (r.push({
|
|
329
|
+
table: e,
|
|
330
|
+
pk: t,
|
|
331
|
+
patch: i
|
|
332
|
+
}), Object.keys(o).length > 0) return $(e, t, o);
|
|
333
|
+
await K(e, t);
|
|
334
|
+
let s = await k.query({
|
|
335
|
+
table: e,
|
|
336
|
+
predicate: {
|
|
337
|
+
column: D,
|
|
338
|
+
op: "eq",
|
|
339
|
+
value: t
|
|
340
|
+
},
|
|
341
|
+
order: [],
|
|
342
|
+
take: 1,
|
|
343
|
+
skip: void 0,
|
|
344
|
+
projection: void 0
|
|
345
|
+
});
|
|
346
|
+
return s[0] === void 0 ? null : U(e, [s[0]])[0];
|
|
347
|
+
}
|
|
348
|
+
return tt(e, t, () => $(e, t, n));
|
|
349
|
+
}
|
|
350
|
+
return $(e, t, n);
|
|
351
|
+
}, $ = async (e, t, n) => {
|
|
352
|
+
G(e, "update", t), await K(e, t);
|
|
353
|
+
let r = F(e, n), i = T.schemaRegistry.crdtColumns(e);
|
|
354
|
+
if (o(i, r)) return ot(e, t, r, i);
|
|
355
|
+
if (T.schemaRegistry.computedFields(e).size > 0) {
|
|
356
|
+
let n = (await k.query({
|
|
357
|
+
table: e,
|
|
358
|
+
predicate: {
|
|
359
|
+
column: D,
|
|
360
|
+
op: "eq",
|
|
361
|
+
value: t
|
|
362
|
+
},
|
|
363
|
+
order: [],
|
|
364
|
+
take: 1,
|
|
365
|
+
skip: void 0,
|
|
366
|
+
projection: void 0
|
|
367
|
+
}))[0];
|
|
368
|
+
if (n === void 0) return null;
|
|
369
|
+
let i = U(e, [n])[0];
|
|
370
|
+
r = He(e, i, r);
|
|
371
|
+
}
|
|
372
|
+
r = L(e, r);
|
|
373
|
+
let a = T.schemaRegistry.versionColumn(e), s;
|
|
374
|
+
if (a !== void 0) {
|
|
375
|
+
let n = Se({
|
|
376
|
+
versionColumn: a,
|
|
377
|
+
patch: r
|
|
378
|
+
}), i = Y(k)?.get(X(e, C(t))), o = n.expected === void 0 && i !== void 0, c = n.expected ?? i, l = (e) => {
|
|
379
|
+
throw o ? new j(e) : e;
|
|
380
|
+
}, u = async () => {
|
|
381
|
+
let n = await k.query({
|
|
382
|
+
table: e,
|
|
383
|
+
predicate: {
|
|
384
|
+
column: D,
|
|
385
|
+
op: "eq",
|
|
386
|
+
value: t
|
|
387
|
+
},
|
|
388
|
+
order: [],
|
|
389
|
+
take: 1,
|
|
390
|
+
skip: void 0,
|
|
391
|
+
projection: void 0
|
|
392
|
+
}), r = n[0] === void 0 ? void 0 : n[0][a];
|
|
393
|
+
return typeof r == "number" && Number.isFinite(r) ? r : null;
|
|
394
|
+
}, d = await u(), f = Ee({
|
|
395
|
+
table: e,
|
|
396
|
+
id: C(t),
|
|
397
|
+
expected: c,
|
|
398
|
+
current: d
|
|
399
|
+
});
|
|
400
|
+
if (f !== void 0 && l(f), d === null) return null;
|
|
401
|
+
s = null;
|
|
402
|
+
for (let i = 0;; i += 1) {
|
|
403
|
+
let o = d;
|
|
404
|
+
if (r = {
|
|
405
|
+
...n.patch,
|
|
406
|
+
[a]: o + 1
|
|
407
|
+
}, await I(e, "update", r, () => k.updateMany(e, V(e, r, t), { where: y(b(D, t), b(a, o)) })) > 0) {
|
|
408
|
+
s = (await k.query({
|
|
409
|
+
table: e,
|
|
410
|
+
predicate: {
|
|
411
|
+
column: D,
|
|
412
|
+
op: "eq",
|
|
413
|
+
value: t
|
|
414
|
+
},
|
|
415
|
+
order: [],
|
|
416
|
+
take: 1,
|
|
417
|
+
skip: void 0,
|
|
418
|
+
projection: void 0
|
|
419
|
+
}))[0] ?? null, Y(k)?.set(X(e, C(t)), o + 1);
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
let f = await u();
|
|
423
|
+
(c !== void 0 || f === null || i >= Ie) && l(new ae({
|
|
424
|
+
table: e,
|
|
425
|
+
id: C(t),
|
|
426
|
+
expected: c ?? o,
|
|
427
|
+
actual: f
|
|
428
|
+
})), d = f;
|
|
429
|
+
}
|
|
430
|
+
} else s = await I(e, "update", r, () => k.update(e, t, V(e, r, t)));
|
|
431
|
+
return s === null ? null : U(e, [s], t)[0];
|
|
432
|
+
}, nt = async (e, t, n) => {
|
|
433
|
+
let r = await k.query({
|
|
434
|
+
table: p,
|
|
435
|
+
predicate: y(b("tableName", e), b("rowId", C(t)), b("columnName", n), b("reason", "rebase")),
|
|
436
|
+
order: [{
|
|
437
|
+
column: "createdAt",
|
|
438
|
+
direction: "desc"
|
|
439
|
+
}],
|
|
440
|
+
take: void 0,
|
|
441
|
+
skip: void 0,
|
|
442
|
+
projection: void 0
|
|
443
|
+
});
|
|
444
|
+
return U(p, r).map((e) => e.state).filter((e) => e instanceof Uint8Array);
|
|
445
|
+
}, rt = async (e, t, n, r, i) => {
|
|
446
|
+
let a = P(p, {
|
|
447
|
+
tableName: e,
|
|
448
|
+
rowId: C(t),
|
|
449
|
+
columnName: n,
|
|
450
|
+
state: r,
|
|
451
|
+
reason: "rebase",
|
|
452
|
+
label: null,
|
|
453
|
+
epoch: i === void 0 ? E.epochOf(r) ?? null : i,
|
|
454
|
+
createdBy: null
|
|
455
|
+
});
|
|
456
|
+
await I(p, "insert", a, () => k.insert(p, B(p, R(p, a))));
|
|
457
|
+
}, it = async (e, t, n, r, i) => {
|
|
458
|
+
if (!(r instanceof Uint8Array) || !(i instanceof Uint8Array) || r.length === 0) return i;
|
|
459
|
+
let a = E.authorsOf(r), o = [...E.authorsOf(i)].filter((e) => !a.has(e));
|
|
460
|
+
if (o.length === 0 || E.epochOf(r) === void 0) return i;
|
|
461
|
+
for (let a of await nt(e, t, n)) {
|
|
462
|
+
let t = E.authorsOf(a);
|
|
463
|
+
if (!o.some((e) => t.has(e))) continue;
|
|
464
|
+
let s = E.translate(a, i, r);
|
|
465
|
+
if (s === void 0) throw new Me({
|
|
466
|
+
field: n,
|
|
467
|
+
message: "crdt.staleLineage",
|
|
468
|
+
params: { table: e }
|
|
469
|
+
});
|
|
470
|
+
return s;
|
|
471
|
+
}
|
|
472
|
+
return i;
|
|
473
|
+
}, at = async (e, t, n) => {
|
|
474
|
+
await K(e, t);
|
|
475
|
+
for (let i = 0;; i += 1) {
|
|
476
|
+
let a = (await k.query({
|
|
477
|
+
table: e,
|
|
478
|
+
predicate: {
|
|
479
|
+
column: D,
|
|
480
|
+
op: "eq",
|
|
481
|
+
value: t
|
|
482
|
+
},
|
|
483
|
+
order: [],
|
|
484
|
+
take: 1,
|
|
485
|
+
skip: void 0,
|
|
486
|
+
projection: void 0
|
|
487
|
+
}))[0];
|
|
488
|
+
if (a === void 0) return null;
|
|
489
|
+
let o = U(e, [a])[0], s = {}, c = [b(D, t)], l = [];
|
|
490
|
+
for (let [t, i] of Object.entries(n)) {
|
|
491
|
+
if (i === null) s[t] = null;
|
|
492
|
+
else {
|
|
493
|
+
let n = r(e, t, o[t], i);
|
|
494
|
+
s[t] = n.log, n.replaced !== void 0 && l.push({
|
|
495
|
+
column: t,
|
|
496
|
+
...n.replaced
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
let n = a[t];
|
|
500
|
+
c.push(n == null ? S(t) : b(t, n));
|
|
501
|
+
}
|
|
502
|
+
let u = F(e, s);
|
|
503
|
+
if (await I(e, "update", u, () => k.updateMany(e, V(e, u, t), { where: y(...c) })) > 0) {
|
|
504
|
+
for (let n of l) await rt(e, t, n.column, n.log, n.epoch === "" ? null : n.epoch);
|
|
505
|
+
let n = await k.query({
|
|
506
|
+
table: e,
|
|
507
|
+
predicate: {
|
|
508
|
+
column: D,
|
|
509
|
+
op: "eq",
|
|
510
|
+
value: t
|
|
511
|
+
},
|
|
512
|
+
order: [],
|
|
513
|
+
take: 1,
|
|
514
|
+
skip: void 0,
|
|
515
|
+
projection: void 0
|
|
516
|
+
});
|
|
517
|
+
return n[0] === void 0 ? null : U(e, [n[0]], t)[0];
|
|
518
|
+
}
|
|
519
|
+
if (i + 1 >= O) throw Error(`sealed crdt append on ${e} lost ${O} compare-and-swap rounds to concurrent writers; the frame was not written and can be retried`);
|
|
520
|
+
await new Promise((e) => setTimeout(e, Math.random() * 5 * (i + 1)));
|
|
521
|
+
}
|
|
522
|
+
}, ot = async (e, t, n, r) => {
|
|
523
|
+
let i = async () => (await k.query({
|
|
524
|
+
table: e,
|
|
525
|
+
predicate: {
|
|
526
|
+
column: D,
|
|
527
|
+
op: "eq",
|
|
528
|
+
value: t
|
|
529
|
+
},
|
|
530
|
+
order: [],
|
|
531
|
+
take: 1,
|
|
532
|
+
skip: void 0,
|
|
533
|
+
projection: void 0
|
|
534
|
+
}))[0], o = [...r].filter((e) => e in n), s = T.schemaRegistry.versionColumn(e), c = s === void 0 ? void 0 : Se({
|
|
535
|
+
versionColumn: s,
|
|
536
|
+
patch: n
|
|
537
|
+
}), l = s === void 0 ? void 0 : Y(k)?.get(X(e, C(t))), u = c !== void 0 && c.expected === void 0 && l !== void 0, d = c?.expected ?? l, f = c?.patch ?? n, p = await i();
|
|
538
|
+
for (let n = 0;; n += 1) {
|
|
539
|
+
if (p === void 0) return null;
|
|
540
|
+
let c = U(e, [p])[0], l = f;
|
|
541
|
+
for (let n of o) {
|
|
542
|
+
let r = await it(e, t, n, c[n], f[n]);
|
|
543
|
+
r !== f[n] && (l = {
|
|
544
|
+
...l,
|
|
545
|
+
[n]: r
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
let m = [], h = a(r, c, l, e, (e, t) => {
|
|
549
|
+
m.push({
|
|
550
|
+
column: e,
|
|
551
|
+
archived: t
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
T.schemaRegistry.computedFields(e).size > 0 && (h = He(e, c, h));
|
|
555
|
+
let g = [b(D, t)];
|
|
556
|
+
for (let e of o) {
|
|
557
|
+
let t = p[e];
|
|
558
|
+
g.push(t == null ? S(e) : b(e, t));
|
|
559
|
+
}
|
|
560
|
+
let _;
|
|
561
|
+
if (s !== void 0) {
|
|
562
|
+
let n = p[s];
|
|
563
|
+
_ = typeof n == "number" && Number.isFinite(n) ? n : void 0;
|
|
564
|
+
let r = Ee({
|
|
565
|
+
table: e,
|
|
566
|
+
id: C(t),
|
|
567
|
+
expected: d,
|
|
568
|
+
current: _ ?? null
|
|
569
|
+
});
|
|
570
|
+
if (r !== void 0) throw u ? new j(r) : r;
|
|
571
|
+
_ !== void 0 && (h = {
|
|
572
|
+
...h,
|
|
573
|
+
[s]: _ + 1
|
|
574
|
+
}, g.push(b(s, _)));
|
|
575
|
+
}
|
|
576
|
+
h = L(e, h);
|
|
577
|
+
let v = h;
|
|
578
|
+
if (await I(e, "update", v, () => k.updateMany(e, V(e, v, t), { where: y(...g) })) > 0) {
|
|
579
|
+
for (let { column: n, archived: r } of m) await rt(e, t, n, r);
|
|
580
|
+
let n = await i();
|
|
581
|
+
return n === void 0 ? null : (s !== void 0 && _ !== void 0 && Y(k)?.set(X(e, C(t)), _ + 1), U(e, [n], t)[0]);
|
|
582
|
+
}
|
|
583
|
+
if (n + 1 >= O) throw Error(`crdt fold on ${e} lost ${O} compare-and-swap rounds to concurrent writers; the update was not written and can be retried`);
|
|
584
|
+
await new Promise((e) => setTimeout(e, Math.random() * 5 * (n + 1))), p = await i();
|
|
585
|
+
}
|
|
586
|
+
}, st = async (e, t) => (G(e, "delete", t), await K(e, t), T.schemaRegistry.hasSoftDelete(e) ? await Q(e, t, Ue()) !== null : I(e, "delete", void 0, () => k.delete(e, t))), ct = async (e, t, n) => {
|
|
587
|
+
let r = F(e, t), i = T.schemaRegistry.versionColumn(e);
|
|
588
|
+
if (i === void 0) {
|
|
589
|
+
if (!H(e, r)) return I(e, "updateMany", r, () => k.updateMany(e, B(e, r), n));
|
|
590
|
+
let t = await k.query({
|
|
591
|
+
table: e,
|
|
592
|
+
predicate: n.where,
|
|
593
|
+
order: [],
|
|
594
|
+
take: void 0,
|
|
595
|
+
skip: void 0,
|
|
596
|
+
projection: [D]
|
|
597
|
+
}), i = 0;
|
|
598
|
+
for (let a of t) {
|
|
599
|
+
let t = a[D];
|
|
600
|
+
t != null && (i += await I(e, "updateMany", r, () => k.updateMany(e, V(e, r, t), { where: y(n.where, b(D, t)) })));
|
|
601
|
+
}
|
|
602
|
+
return i;
|
|
603
|
+
}
|
|
604
|
+
let a = await k.query({
|
|
605
|
+
table: e,
|
|
606
|
+
predicate: n.where,
|
|
607
|
+
order: [],
|
|
608
|
+
take: void 0,
|
|
609
|
+
skip: void 0,
|
|
610
|
+
projection: [D, i]
|
|
611
|
+
}), { [i]: o, ...s } = r, c = 0;
|
|
612
|
+
for (let t of a) {
|
|
613
|
+
let r = t[D];
|
|
614
|
+
if (typeof r != "string") continue;
|
|
615
|
+
let a = t[i];
|
|
616
|
+
for (let t = 0; typeof a == "number" && Number.isFinite(a); t += 1) {
|
|
617
|
+
let o = a, l = {
|
|
618
|
+
...s,
|
|
619
|
+
[i]: o + 1
|
|
620
|
+
}, u = await I(e, "updateMany", l, () => k.updateMany(e, V(e, l, r), { where: y(n.where, b(D, r), b(i, o)) }));
|
|
621
|
+
if (u > 0) {
|
|
622
|
+
c += u;
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
if (t >= Ie) throw new ae({
|
|
626
|
+
table: e,
|
|
627
|
+
id: r,
|
|
628
|
+
expected: o,
|
|
629
|
+
actual: null
|
|
630
|
+
});
|
|
631
|
+
a = (await k.query({
|
|
632
|
+
table: e,
|
|
633
|
+
predicate: y(n.where, b(D, r)),
|
|
634
|
+
order: [],
|
|
635
|
+
take: 1,
|
|
636
|
+
skip: void 0,
|
|
637
|
+
projection: [i]
|
|
638
|
+
}))[0]?.[i];
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return c;
|
|
642
|
+
}, lt = async (e, t) => I(e, "deleteMany", void 0, () => k.deleteMany(e, t)), ut = (e, t) => M != null && T.schemaRegistry.hasTenant(e) ? y(t, b("tenantId", M)) : t, dt = async (e, t, n) => ct(e, t, {
|
|
643
|
+
...n,
|
|
644
|
+
where: ut(e, n.where)
|
|
645
|
+
}), ft = async (e, t) => lt(e, {
|
|
646
|
+
...t,
|
|
647
|
+
where: ut(e, t.where)
|
|
648
|
+
}), pt = ee({
|
|
649
|
+
query: $e,
|
|
650
|
+
updateMany: ct,
|
|
651
|
+
deleteMany: lt,
|
|
652
|
+
delete: (e, t) => k.delete(e, t)
|
|
653
|
+
}, {
|
|
654
|
+
subject: T.subject,
|
|
655
|
+
schemaRegistry: T.schemaRegistry,
|
|
656
|
+
rowFilter: N
|
|
657
|
+
}), mt = ((e, t, n) => t === void 0 ? pt.updateWhere(e) : Q(e, t, n)), ht = ((e, t) => t === void 0 ? pt.deleteWhere(e) : st(e, t)), gt = async (e, t, n) => {
|
|
658
|
+
let r = t.map((t) => P(e, t)), i = x(e), a = t.length >= 32 && i !== void 0 && T.schemaRegistry.insertSchema(e) === void 0 && T.schemaRegistry.crdtColumns(e).size === 0 && T.schemaRegistry.sealedColumns(e).size === 0 ? v(i) : void 0, o = r.map((t) => R(e, t, a)), s = z ? o.map((e) => Te(e, i, z)) : o, c = ue(s, i, m()), l = await I(e, "insertMany", r[0], () => k.insertMany(e, c, n));
|
|
659
|
+
return U(e, l);
|
|
660
|
+
}, _t = (e, t) => {
|
|
661
|
+
let n = Object.keys(t);
|
|
662
|
+
if (n.length !== 1) throw Error(`store.links('${e}'): anchor must name exactly one source column, got [${n.join(", ")}]`);
|
|
663
|
+
let r = n[0], i = t[r], a = x(e);
|
|
664
|
+
if (a === void 0) throw Error(`store.links('${e}'): unknown table (not registered) — declare it and its relation first`);
|
|
665
|
+
let o = Object.entries(a.fields).filter(([, e]) => e.type === "reference").map(([e]) => e);
|
|
666
|
+
if (o.length !== 2) throw Error(`store.links('${e}'): expected a junction with exactly two reference() columns, found ${o.length} [${o.join(", ")}]. Use plain insert/deleteMany for a non-standard junction.`);
|
|
667
|
+
if (!o.includes(r)) throw Error(`store.links('${e}'): '${r}' is not a reference column of the junction [${o.join(", ")}]`);
|
|
668
|
+
return {
|
|
669
|
+
sourceCol: r,
|
|
670
|
+
sourceId: i,
|
|
671
|
+
targetCol: o.find((e) => e !== r)
|
|
672
|
+
};
|
|
673
|
+
}, vt = (e, t) => {
|
|
674
|
+
let n = x(e);
|
|
675
|
+
if (n === void 0) throw Error(`store.relationLinks('${e}'): unknown table (not registered) — declare it and its relation first`);
|
|
676
|
+
let r = Object.entries(n.fields).filter(([, e]) => e.type === "reference").filter(([, e]) => {
|
|
677
|
+
let n = e.references;
|
|
678
|
+
try {
|
|
679
|
+
return n !== void 0 && n().tableName === t;
|
|
680
|
+
} catch {
|
|
681
|
+
return !1;
|
|
682
|
+
}
|
|
683
|
+
}).map(([e]) => e);
|
|
684
|
+
if (r.length === 1) return r[0];
|
|
685
|
+
throw r.length === 0 ? Error(`store.relationLinks('${e}'): no reference column points at '${t}' — the junction's reference() targets decide the anchor, and none matches.`) : Error(`store.relationLinks('${e}'): ${r.length} reference columns point at '${t}' [${r.join(", ")}] — a self-junction is ambiguous; use store.links('${e}', { <column>: id }) and name the side.`);
|
|
686
|
+
}, yt = (e, t) => {
|
|
687
|
+
let { sourceCol: n, sourceId: r, targetCol: i } = _t(e, t), a = async () => (await W({
|
|
688
|
+
table: e,
|
|
689
|
+
predicate: b(n, r),
|
|
690
|
+
order: [],
|
|
691
|
+
projection: void 0,
|
|
692
|
+
skip: void 0,
|
|
693
|
+
take: void 0
|
|
694
|
+
})).map((e) => String(e[i])), o = async (t) => {
|
|
695
|
+
t.length !== 0 && await gt(e, t.map((e) => ({
|
|
696
|
+
[n]: r,
|
|
697
|
+
[i]: e
|
|
698
|
+
})), { returning: "none" });
|
|
699
|
+
}, s = async (t) => {
|
|
700
|
+
t.length !== 0 && await ft(e, { where: y(b(n, r), ge(i, t)) });
|
|
701
|
+
};
|
|
702
|
+
return {
|
|
703
|
+
columns: {
|
|
704
|
+
anchorColumn: n,
|
|
705
|
+
targetColumn: i
|
|
706
|
+
},
|
|
707
|
+
list: a,
|
|
708
|
+
set: async (e) => {
|
|
709
|
+
let t = new Set(e), n = new Set(await a()), r = [...t].filter((e) => !n.has(e)), i = [...n].filter((e) => !t.has(e));
|
|
710
|
+
return await s(i), await o(r), {
|
|
711
|
+
added: r,
|
|
712
|
+
removed: i
|
|
713
|
+
};
|
|
714
|
+
},
|
|
715
|
+
add: async (e) => {
|
|
716
|
+
let t = new Set(await a()), n = [...new Set(e)].filter((e) => !t.has(e));
|
|
717
|
+
return await o(n), n;
|
|
718
|
+
},
|
|
719
|
+
remove: async (e) => {
|
|
720
|
+
let t = new Set(await a()), n = [...new Set(e)].filter((e) => t.has(e));
|
|
721
|
+
return await s(n), n;
|
|
722
|
+
},
|
|
723
|
+
setRows: async (t) => {
|
|
724
|
+
let a = /* @__PURE__ */ new Map(), o = await W({
|
|
725
|
+
table: e,
|
|
726
|
+
predicate: b(n, r),
|
|
727
|
+
order: [],
|
|
728
|
+
projection: void 0,
|
|
729
|
+
skip: void 0,
|
|
730
|
+
take: void 0
|
|
731
|
+
});
|
|
732
|
+
for (let e of o) a.set(String(e[i]), e);
|
|
733
|
+
let c = /* @__PURE__ */ new Map();
|
|
734
|
+
for (let e of t) c.set(String(e[i]), e);
|
|
735
|
+
let l = [...a.keys()].filter((e) => !c.has(e));
|
|
736
|
+
await s(l);
|
|
737
|
+
let u = [], d = [], f = [];
|
|
738
|
+
for (let [t, o] of c) if (!a.has(t)) u.push(t), f.push({
|
|
739
|
+
...o,
|
|
740
|
+
[n]: r,
|
|
741
|
+
[i]: t
|
|
742
|
+
});
|
|
743
|
+
else {
|
|
744
|
+
let s = a.get(t), c = {}, l = !1;
|
|
745
|
+
for (let [e, t] of Object.entries(o)) e !== n && e !== i && s[e] !== t && (c[e] = t, l = !0);
|
|
746
|
+
l && (d.push(t), await dt(e, c, { where: y(b(n, r), b(i, t)) }));
|
|
747
|
+
}
|
|
748
|
+
return f.length > 0 && await gt(e, f, { returning: "none" }), {
|
|
749
|
+
added: u,
|
|
750
|
+
removed: l,
|
|
751
|
+
updated: d
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
}, bt = async (e, t, n, r) => {
|
|
756
|
+
let i = t[D] !== void 0 && t[D] !== null, a = P(e, t), o = Oe(n), s = n;
|
|
757
|
+
if (We(e, a, n.conflictColumns), typeof n.update == "function") {
|
|
758
|
+
let t = n.update;
|
|
759
|
+
s = {
|
|
760
|
+
conflictColumns: n.conflictColumns,
|
|
761
|
+
update: (n) => {
|
|
762
|
+
let r = n.id, i = U(e, [n], r)[0], a = F(e, {
|
|
763
|
+
...t(i),
|
|
764
|
+
...o
|
|
765
|
+
});
|
|
766
|
+
return V(e, L(e, a), r);
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
} else {
|
|
770
|
+
let r = n.update ?? Object.keys(t).filter((t) => t !== "id" && !n.conflictColumns.includes(t) && (!T.schemaRegistry.hasAudit(e) || t !== "createdAt" && t !== "createdBy"));
|
|
771
|
+
s = {
|
|
772
|
+
...n,
|
|
773
|
+
update: r
|
|
774
|
+
};
|
|
775
|
+
let i = {
|
|
776
|
+
...Object.fromEntries(r.filter((e) => e in t).map((e) => [e, t[e]])),
|
|
777
|
+
...o
|
|
778
|
+
}, a = F(e, i);
|
|
779
|
+
o = Oe({
|
|
780
|
+
conflictColumns: n.conflictColumns,
|
|
781
|
+
updateValues: {
|
|
782
|
+
...o,
|
|
783
|
+
...Object.fromEntries(Object.entries(a).filter(([e]) => i[e] === void 0))
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
let c = !i && typeof a[D] == "string" ? a[D] : void 0, l = H(e, a) || H(e, o) || r && c === void 0, u = l ? await g(k, e, a, n.conflictColumns) : void 0, d = l ? u === void 0 ? a[D] : u : void 0, f = l ? u === void 0 ? "inserted" : "updated" : void 0, p = await I(e, "upsert", a, () => k.upsert(e, B(e, R(e, a), d), {
|
|
788
|
+
...s,
|
|
789
|
+
...typeof n.update != "function" && (n.updateValues !== void 0 || Object.keys(o).length > 0) ? { updateValues: B(e, L(e, o), d) } : {}
|
|
790
|
+
}));
|
|
791
|
+
return {
|
|
792
|
+
row: U(e, [p], d)[0],
|
|
793
|
+
outcome: f ?? (c !== void 0 && p[D] !== c ? "updated" : "inserted")
|
|
794
|
+
};
|
|
795
|
+
}, xt = async (e, t, n) => {
|
|
796
|
+
let r = P(e, t);
|
|
797
|
+
We(e, r, n.conflictColumns);
|
|
798
|
+
let i = await I(e, "insertIgnore", r, () => k.insertIgnoreWithOutcome(e, B(e, R(e, r)), n));
|
|
799
|
+
return {
|
|
800
|
+
row: U(e, [i.row])[0],
|
|
801
|
+
outcome: i.outcome
|
|
802
|
+
};
|
|
803
|
+
};
|
|
804
|
+
return {
|
|
805
|
+
query: W,
|
|
806
|
+
...k.integrityQuery === void 0 ? {} : { integrityQuery: (e) => Qe(Ze({
|
|
807
|
+
...e,
|
|
808
|
+
crossTenant: !1,
|
|
809
|
+
includeDeleted: !1,
|
|
810
|
+
includeExpired: !1
|
|
811
|
+
}, c), k.integrityQuery.bind(k), !1) },
|
|
812
|
+
one: (async (e, t) => {
|
|
813
|
+
let n = Le(e), r = await W({
|
|
814
|
+
...n,
|
|
815
|
+
take: 2
|
|
816
|
+
}), i = r[0];
|
|
817
|
+
if (i === void 0) throw te({
|
|
818
|
+
table: n.table,
|
|
819
|
+
found: 0
|
|
820
|
+
}, t);
|
|
821
|
+
if (r.length > 1) throw te({
|
|
822
|
+
table: n.table,
|
|
823
|
+
found: r.length
|
|
824
|
+
}, t);
|
|
825
|
+
return i;
|
|
826
|
+
}),
|
|
827
|
+
first: ((e) => f({ query: W }, e)),
|
|
828
|
+
maybeOne: ((e) => f({ query: W }, e)),
|
|
829
|
+
insert: async (e, t) => {
|
|
830
|
+
let n = P(e, t), r = await I(e, "insert", n, () => k.insert(e, B(e, R(e, n))));
|
|
831
|
+
return U(e, [r])[0];
|
|
832
|
+
},
|
|
833
|
+
insertMany: (async (e, t, n) => gt(e, t, n)),
|
|
834
|
+
links: yt,
|
|
835
|
+
relationLinks: (e, t, n) => yt(e, { [vt(e, t)]: n }),
|
|
836
|
+
patchJson: async (e, t, n, r, i) => {
|
|
837
|
+
G(e, "patchJson", t), await K(e, t);
|
|
838
|
+
let a = ve(t, n, F(e, ve(t, n, i), [n.split(".")[0]])), o = await I(e, "patchJson", a, () => k.patchJson(e, t, n, r, V(e, L(e, a), t)));
|
|
839
|
+
return o === null ? null : U(e, [o], t)[0];
|
|
840
|
+
},
|
|
841
|
+
select: pt.select,
|
|
842
|
+
update: mt,
|
|
843
|
+
delete: ht,
|
|
844
|
+
applyDefined: Re,
|
|
845
|
+
hardDelete: async (e, t) => (G(e, "hardDelete", t), await K(e, t), I(e, "hardDelete", void 0, () => k.delete(e, t))),
|
|
846
|
+
updateMany: dt,
|
|
847
|
+
deleteMany: ft,
|
|
848
|
+
upsert: async (e, t, n) => (await bt(e, t, n, !1)).row,
|
|
849
|
+
upsertWithOutcome: (e, t, n) => bt(e, t, n, !0),
|
|
850
|
+
insertIgnore: async (e, t, n) => (await xt(e, t, n)).row,
|
|
851
|
+
insertIgnoreWithOutcome: (e, t, n) => xt(e, t, n),
|
|
852
|
+
transactional: async (e) => {
|
|
853
|
+
let t = await k.transactional(async (t) => {
|
|
854
|
+
let n = [];
|
|
855
|
+
return t[q] = n, t[J] = /* @__PURE__ */ new Map(), {
|
|
856
|
+
result: await e(Ve(t, T)),
|
|
857
|
+
jobs: n
|
|
858
|
+
};
|
|
859
|
+
});
|
|
860
|
+
for (let e of t.jobs) await Q(e.table, e.pk, e.patch);
|
|
861
|
+
return t.result;
|
|
862
|
+
},
|
|
863
|
+
...k.raw ? { raw: ((e, r) => (n(Object.freeze({
|
|
864
|
+
sql: t(e.strings),
|
|
865
|
+
dependsOn: r?.dependsOn ?? e.dependsOn ?? []
|
|
866
|
+
})), k.raw(e, r))) } : {},
|
|
867
|
+
...k.savepoint ? { savepoint: (e) => k.savepoint(e) } : {},
|
|
868
|
+
onChange: (e) => k.onChange(e),
|
|
869
|
+
injectExternalChange: (e) => k.injectExternalChange(e),
|
|
870
|
+
...k.changeScope === void 0 ? {} : { changeScope: k.changeScope },
|
|
871
|
+
...k[q] === void 0 ? {} : { [q]: k[q] },
|
|
872
|
+
...Y(k) === void 0 ? {} : { [J]: Y(k) }
|
|
873
|
+
};
|
|
874
|
+
}, A = "VoltroTransactionReadConflict", j = class extends Error {
|
|
875
|
+
conflict;
|
|
876
|
+
name = A;
|
|
877
|
+
constructor(e) {
|
|
878
|
+
super(e.message), this.conflict = e;
|
|
879
|
+
}
|
|
880
|
+
}, M = (e) => typeof e == "object" && !!e && e.name === "VoltroTransactionReadConflict" && "conflict" in e, N = Symbol.for("@voltro/runtime:tx-read-versions");
|
|
881
|
+
//#endregion
|
|
882
|
+
export { A as TRANSACTION_READ_CONFLICT_NAME, N as TX_READ_VERSIONS_KEY, j as TransactionReadConflict, Be as actingUserId, Re as applyDefined, M as isTransactionReadConflict, Ve as wrapStoreWithMixinBehaviour };
|