@voltro/database 0.85.0 → 0.86.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 (169) hide show
  1. package/CHANGELOG.md +666 -0
  2. package/THIRD-PARTY-NOTICES.md +5 -33
  3. package/dist/actorSnapshot.js +24 -0
  4. package/dist/arrayCodec.js +32 -0
  5. package/dist/branchExecute.js +92 -0
  6. package/dist/branching.js +106 -0
  7. package/dist/bulkInsertLimits.js +25 -0
  8. package/dist/cdcOffsetsTables.js +19 -0
  9. package/dist/changeEvent.js +41 -0
  10. package/dist/changeIdentity.js +65 -0
  11. package/dist/changeListenerCeiling.js +6 -0
  12. package/dist/columnReference.js +17 -0
  13. package/dist/columns.js +326 -0
  14. package/dist/committedChanges.js +12 -0
  15. package/dist/connectionIdentity.js +36 -0
  16. package/dist/coreTables.js +16 -0
  17. package/dist/coreTablesRegistry.js +10 -0
  18. package/dist/dataTransfers.js +22 -0
  19. package/dist/dbCause.js +222 -0
  20. package/dist/dbMetrics.js +65 -0
  21. package/dist/decimalComparison.js +27 -0
  22. package/dist/derivedRowId.js +5 -0
  23. package/dist/dialect.js +13 -0
  24. package/dist/enumRenames.js +14 -0
  25. package/dist/expiresMixin.js +10 -0
  26. package/dist/fieldEncryption.js +98 -0
  27. package/dist/frameworkTables.js +130 -0
  28. package/dist/ginIndex.js +10 -0
  29. package/dist/hybridSearch.js +32 -0
  30. package/dist/idGenerator.js +57 -0
  31. package/dist/identifierLimits.js +32 -0
  32. package/dist/index.d.ts +18 -7
  33. package/dist/index.js +81 -2183
  34. package/dist/indexAudit.js +61 -0
  35. package/dist/indexStorage.js +58 -0
  36. package/dist/joinCompiler.js +160 -0
  37. package/dist/jsonEagerCompiler.js +436 -0
  38. package/dist/jsonPatch.js +78 -0
  39. package/dist/jsonPatchAssignments.js +13 -0
  40. package/dist/jsonPath.js +31 -0
  41. package/dist/localFirstMixin.js +12 -0
  42. package/dist/migrate.js +624 -0
  43. package/dist/migration.js +18 -0
  44. package/dist/migrations/appliedAtValue.js +8 -0
  45. package/dist/migrations/applier.js +1085 -0
  46. package/dist/migrations/boot.js +196 -0
  47. package/dist/migrations/branchRehearsal.js +122 -0
  48. package/dist/migrations/columnEquivalence.js +85 -0
  49. package/dist/migrations/ddlError.js +63 -0
  50. package/dist/migrations/declaredSchema.js +162 -0
  51. package/dist/migrations/deployLedger.js +19 -0
  52. package/dist/migrations/fileBased.js +57 -0
  53. package/dist/migrations/fileContext.js +482 -0
  54. package/dist/migrations/fileDiscovery.js +318 -0
  55. package/dist/migrations/fileSkipProbes.js +117 -0
  56. package/dist/migrations/fingerprint.js +46 -0
  57. package/dist/migrations/fkCycles.js +13 -0
  58. package/dist/migrations/frameworkLiveTables.js +25 -0
  59. package/dist/migrations/idempotentDdl.js +37 -0
  60. package/dist/migrations/indexPredicate.js +50 -0
  61. package/dist/migrations/introspect.js +799 -0
  62. package/dist/migrations/ledgerRows.js +4 -0
  63. package/dist/migrations/lock.js +91 -0
  64. package/dist/migrations/migrationTableExists.js +9 -0
  65. package/dist/migrations/mssqlColumnDependencies.js +76 -0
  66. package/dist/migrations/mysqlEngine.js +11 -0
  67. package/dist/migrations/namespaceCharset.js +51 -0
  68. package/dist/migrations/planner.js +775 -0
  69. package/dist/migrations/postgresEnumCheck.js +19 -0
  70. package/dist/migrations/reactiveTriggerDrift.js +58 -0
  71. package/dist/migrations/reactiveTriggerName.js +7 -0
  72. package/dist/migrations/renameMssqlCheck.js +21 -0
  73. package/dist/migrations/restoreSnapshots.js +206 -0
  74. package/dist/migrations/resumeLedger.js +86 -0
  75. package/dist/migrations/rollingDeploy.js +106 -0
  76. package/dist/migrations/schemaOwner.js +46 -0
  77. package/dist/migrations/schemaOwnerTable.js +4 -0
  78. package/dist/migrations/softDropSnapshot.js +20 -0
  79. package/dist/migrations/sqlLimit.js +8 -0
  80. package/dist/migrations/sqliteConstraintIdentity.js +97 -0
  81. package/dist/migrations/sqliteDecimalBoot.js +22 -0
  82. package/dist/migrations/sqliteDecimalPublication.js +257 -0
  83. package/dist/migrations/sqliteDecimalReview.js +30 -0
  84. package/dist/migrations/sqliteDecimalReviewStore.js +188 -0
  85. package/dist/migrations/sqliteDecimalReviewWitness.js +38 -0
  86. package/dist/migrations/sqliteDecimalSource.js +69 -0
  87. package/dist/migrations/sqliteDecimalStage.js +135 -0
  88. package/dist/migrations/sqliteForeignKeys.js +38 -0
  89. package/dist/migrations/sqliteMigrationBatch.js +37 -0
  90. package/dist/migrations/sqliteRetainedSource.js +106 -0
  91. package/dist/migrations/sqliteRowIdentity.js +63 -0
  92. package/dist/migrations/sqliteSchemaEpoch.js +12 -0
  93. package/dist/migrations/sqliteSnapshotGuard.js +10 -0
  94. package/dist/migrations/sqliteTableContents.js +34 -0
  95. package/dist/migrations/sqliteTableCopy.js +25 -0
  96. package/dist/migrations/sqliteTableSchema.js +19 -0
  97. package/dist/migrations/sqliteWitnessProtection.js +8 -0
  98. package/dist/migrations/squash.js +81 -0
  99. package/dist/migrations/storeCharset.js +66 -0
  100. package/dist/migrations/transactionCause.js +10 -0
  101. package/dist/migrations/undeclaredCharset.js +40 -0
  102. package/dist/migrations/uniqueActiveLowering.js +39 -0
  103. package/dist/mixins.js +22 -0
  104. package/dist/mssqlDecimalDistinct.js +51 -0
  105. package/dist/mssqlJsonValueCheck.js +42 -0
  106. package/dist/mssqlTextStorage.js +9 -0
  107. package/dist/namespace.js +21 -0
  108. package/dist/{wire-CG2j7J3f.js → optimisticVersion.js} +2 -2
  109. package/dist/pendingAttribution.js +79 -0
  110. package/dist/pluginRef.js +17 -0
  111. package/dist/predicate.js +266 -0
  112. package/dist/predicateCoercion.js +13 -0
  113. package/dist/projectedColumn.js +37 -0
  114. package/dist/queryBuilder.js +382 -0
  115. package/dist/rawSql.js +8 -0
  116. package/dist/relations.js +69 -0
  117. package/dist/relationsRegistry.js +38 -0
  118. package/dist/replaceInProgress.js +53 -0
  119. package/dist/requiredColumns.js +26 -0
  120. package/dist/residency.js +52 -0
  121. package/dist/retention.js +62 -0
  122. package/dist/rowDecoder.js +123 -0
  123. package/dist/rowKey.js +8 -0
  124. package/dist/rowSchema.js +31 -0
  125. package/dist/scopeKey.js +8 -0
  126. package/dist/seed.js +33 -0
  127. package/dist/snowflake.js +26 -0
  128. package/dist/sql.d.ts +27 -6
  129. package/dist/sql.js +41 -6905
  130. package/dist/sqlCompiler.js +343 -0
  131. package/dist/sqlError.js +8 -0
  132. package/dist/sqlPoolCapacity.js +12 -0
  133. package/dist/sqliteDecimal.js +22 -0
  134. package/dist/sqliteDecimalAggregateSql.js +61 -0
  135. package/dist/sqliteDecimalColumnSql.js +108 -0
  136. package/dist/sqliteDecimalConversion.js +15 -0
  137. package/dist/sqliteDecimalCte.js +154 -0
  138. package/dist/sqliteDecimalEncodeSql.js +32 -0
  139. package/dist/sqliteDecimalOutputs.js +74 -0
  140. package/dist/sqliteDecimalPredicate.js +66 -0
  141. package/dist/sqliteDecimalQuery.js +183 -0
  142. package/dist/sqliteDecimalQueryPlan.js +37 -0
  143. package/dist/sqliteDecimalResultSql.js +44 -0
  144. package/dist/sqliteDecimalRowQuery.js +63 -0
  145. package/dist/sqliteDecimalSetQuery.js +50 -0
  146. package/dist/sqliteDecimalSource.js +63 -0
  147. package/dist/sqliteDecimalStorage.js +29 -0
  148. package/dist/sqliteDecimalSubquery.js +19 -0
  149. package/dist/sqliteDecimalWindowQuery.js +196 -0
  150. package/dist/sqliteValueCoercion.js +4 -0
  151. package/dist/stampId.js +21 -0
  152. package/dist/storeErrors.js +9 -0
  153. package/dist/storeRefusal.js +20 -0
  154. package/dist/stream.js +36 -0
  155. package/dist/subjectGraph.js +175 -0
  156. package/dist/table.js +394 -0
  157. package/dist/tableRegistry.js +16 -0
  158. package/dist/tenantSelection.js +27 -0
  159. package/dist/transactionOutcome.js +97 -0
  160. package/dist/transientContention.js +36 -0
  161. package/dist/typedStore.js +16 -0
  162. package/dist/upsertUpdate.js +20 -0
  163. package/dist/vectorEmbedding.js +31 -0
  164. package/dist/view.js +39 -0
  165. package/dist/wire.js +6 -2
  166. package/dist/writeAttribution.js +16 -0
  167. package/dist/writeRecorder.js +37 -0
  168. package/package.json +3 -3
  169. package/dist/replaceInProgress-DC88Pdiy.js +0 -3735
package/dist/table.js ADDED
@@ -0,0 +1,394 @@
1
+ import { SELF_REFERENCE_TARGET as e, materializeFields as t } from "./columns.js";
2
+ import { pluginRefSpecOf as n } from "./pluginRef.js";
3
+ import { validateColumnName as r, validateIndexName as i, validateTableName as a } from "./identifierLimits.js";
4
+ import { resolveIdScheme as o } from "./idGenerator.js";
5
+ import { resolveMixinGraph as s } from "./mixins.js";
6
+ import { resolveGinOpclass as c } from "./ginIndex.js";
7
+ import { Schema as l, Struct as u } from "effect";
8
+ //#region src/table.ts
9
+ var d = (e, ...t) => {
10
+ if (t.length === 0) throw Error(`jsonIndex("${e}"): at least one path segment is required`);
11
+ let n = (r) => ({
12
+ jsonPath: {
13
+ column: e,
14
+ path: t,
15
+ numeric: r
16
+ },
17
+ numeric: () => n(!0)
18
+ });
19
+ return n(!1);
20
+ }, f = (e = "cosine", t = "float32") => {
21
+ let n = t === "half" ? "halfvec" : "vector";
22
+ switch (e) {
23
+ case "cosine": return `${n}_cosine_ops`;
24
+ case "l2": return `${n}_l2_ops`;
25
+ case "inner": return `${n}_ip_ops`;
26
+ }
27
+ }, p = [
28
+ "id",
29
+ "tenantId",
30
+ "createdAt",
31
+ "updatedAt",
32
+ "createdBy",
33
+ "updatedBy",
34
+ "deletedAt",
35
+ "deletedBy"
36
+ ], m = (e) => e.id ?? `<anonymous mixin: ${Object.keys(e.fields).join(", ")}>`, h = (e, t) => `${e}_${t.join("_")}_idx`, g = (e, t) => `${e}_${t.join("_")}_uq`, _ = (e, t, n, r, i, a) => {
37
+ if (a?.gin !== void 0) {
38
+ if (n !== "gin") throw Error("GIN opclass requires kind: gin");
39
+ if (c(a), r.length !== 1 || typeof r[0] != "string") throw Error(`GIN opclass index '${t}' must reference exactly one plain column`);
40
+ }
41
+ if (n !== "hnsw") return;
42
+ if (r.length !== 1) throw Error(`hnsw index '${t}' on table '${e}' must cover exactly one vector column — got ${r.length} fields. pgvector's HNSW access method indexes a single vector column.`);
43
+ let o = r[0];
44
+ if (typeof o != "string") throw Error(`hnsw index '${t}' on table '${e}' must reference a vector COLUMN, not an expression. pgvector's HNSW access method indexes a column directly.`);
45
+ let s = i[o];
46
+ if (s === void 0) throw Error(`hnsw index '${t}' on table '${e}' references unknown column '${o}'.`);
47
+ if (s.type !== "vector") throw Error(`hnsw index '${t}' on table '${e}' requires a vector() column — column '${o}' is '${s.type}'. HNSW is the pgvector approximate-nearest-neighbour method and only applies to vectors.`);
48
+ }, v = (e, t) => {
49
+ let n;
50
+ for (let [r, i] of Object.entries(e)) i.type === "id" && i.idScheme === void 0 && (n === void 0 && (n = { ...e }), n[r] = {
51
+ ...i,
52
+ idScheme: o(i.idSchemeInput, t)
53
+ });
54
+ return n ?? e;
55
+ }, y = (e, t) => {
56
+ let n = (e) => Object.getPrototypeOf(e) === Object.prototype && Object.values(Object.getOwnPropertyDescriptors(e)).every((e) => "value" in e), r = (e) => {
57
+ if (Object.getPrototypeOf(e) !== Array.prototype || Object.hasOwn(e, Symbol.iterator)) return !1;
58
+ for (let t = 0; t < e.length; t++) {
59
+ let n = Object.getOwnPropertyDescriptor(e, String(t));
60
+ if (!n || !("value" in n) || typeof n.value != "string") return !1;
61
+ }
62
+ return !0;
63
+ }, i = 0, a = t.every((e) => {
64
+ let t = Object.getOwnPropertyDescriptor(e, "indexes");
65
+ if (t === void 0) return Object.getPrototypeOf(e) === Object.prototype;
66
+ if (!("value" in t)) return !1;
67
+ let n = t.value;
68
+ return n === void 0 ? !0 : Object.getPrototypeOf(n) !== Array.prototype || Object.hasOwn(n, Symbol.iterator) ? !1 : (i += n.length, !0);
69
+ });
70
+ if (!(i < 32 || !a)) {
71
+ for (let e of t) {
72
+ let t = e.indexes ?? [];
73
+ for (let e = 0; e < t.length; e++) {
74
+ let i = Object.getOwnPropertyDescriptor(t, String(e));
75
+ if (!i || !("value" in i) || !n(i.value) || !r(i.value.fields)) return;
76
+ }
77
+ }
78
+ return e.every(n) ? new Set(e.map((e) => e.name)) : void 0;
79
+ }
80
+ }, b = (e) => {
81
+ let t = e.appliedPrimaryKey ?? [], o = Object.entries(e.fields).flatMap(([t, r]) => {
82
+ let i = n(r);
83
+ if (i === void 0) return [];
84
+ if (i.orphanPolicy === "null" && r.nullable !== !0) throw Error(`pluginRef: ${e.tableName}.${t} orphanPolicy: 'null' requires .nullable().`);
85
+ return [{
86
+ column: t,
87
+ ...i
88
+ }];
89
+ });
90
+ return {
91
+ tableName: e.tableName,
92
+ fields: e.fields,
93
+ isReactive: e.isReactive,
94
+ appliedMixins: e.appliedMixins,
95
+ appliedIndexes: e.appliedIndexes,
96
+ appliedUniques: e.appliedUniques,
97
+ appliedFullText: e.appliedFullText,
98
+ appliedChecks: e.appliedChecks,
99
+ appliedRules: e.appliedRules ?? [],
100
+ appliedPluginRefs: o,
101
+ appliedPrimaryKey: t,
102
+ ...e.previousTableName === void 0 ? {} : { previousTableName: e.previousTableName },
103
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
104
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema },
105
+ fullTextIndex: ((t, n, r) => {
106
+ if (n.length === 0) throw Error(`fullTextIndex '${t}' on '${e.tableName}' has no columns.`);
107
+ if (i(e.tableName, t, !1, n), e.appliedFullText.some((e) => e.name === t)) throw Error(`duplicate full-text index '${t}' on table '${e.tableName}'.`);
108
+ return b({
109
+ ...e,
110
+ tableName: e.tableName,
111
+ fields: e.fields,
112
+ isReactive: e.isReactive,
113
+ appliedMixins: e.appliedMixins,
114
+ appliedIndexes: e.appliedIndexes,
115
+ appliedUniques: e.appliedUniques,
116
+ appliedChecks: e.appliedChecks,
117
+ appliedPrimaryKey: e.appliedPrimaryKey,
118
+ appliedFullText: [...e.appliedFullText, {
119
+ name: t,
120
+ columns: [...n],
121
+ ...r?.config === void 0 ? {} : { config: r.config },
122
+ ...r?.weights === void 0 ? {} : { weights: r.weights }
123
+ }],
124
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
125
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
126
+ });
127
+ }),
128
+ with: ((...t) => {
129
+ let n = s(t), a = { ...e.fields }, o = /* @__PURE__ */ new Map();
130
+ for (let e of Object.keys(a)) o.set(e, {
131
+ id: "<table>",
132
+ fields: {}
133
+ });
134
+ for (let t of n) for (let [n, i] of Object.entries(t.fields)) {
135
+ let s = o.get(n);
136
+ if (s !== void 0) throw Error(`mixin collision on column '${n}' in table '${e.tableName}': '${m(s)}' and '${m(t)}' both define it. If they're the same mixin reached via different paths, set a shared 'id' on both. If they're genuinely different concerns, rename one of the columns.`);
137
+ r(e.tableName, n), a[n] = i, o.set(n, t);
138
+ }
139
+ let c = [...e.appliedIndexes], l = y(c, n);
140
+ for (let t of n) for (let n of t.indexes ?? []) {
141
+ let t = n.name ?? h(e.tableName, n.fields);
142
+ (l?.has(t) ?? c.some((e) => e.name === t)) || (i(e.tableName, t, n.name === void 0, n.fields), _(e.tableName, t, n.kind, n.fields, a, n.kindOptions), c.push({
143
+ name: t,
144
+ fields: n.fields,
145
+ ...n.kind === void 0 ? {} : { kind: n.kind },
146
+ ...n.kindOptions === void 0 ? {} : { kindOptions: n.kindOptions }
147
+ }), l?.add(t));
148
+ }
149
+ return b({
150
+ ...e,
151
+ tableName: e.tableName,
152
+ fields: a,
153
+ isReactive: e.isReactive,
154
+ appliedMixins: [...e.appliedMixins, ...n],
155
+ appliedIndexes: c,
156
+ appliedUniques: e.appliedUniques,
157
+ appliedFullText: e.appliedFullText,
158
+ appliedChecks: e.appliedChecks,
159
+ appliedPrimaryKey: e.appliedPrimaryKey,
160
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
161
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
162
+ });
163
+ }),
164
+ index: ((...t) => {
165
+ let n, r, a, o = Array.isArray(t[0]);
166
+ if (o ? (r = t[0], a = t[1], n = h(e.tableName, r)) : (n = t[0], r = t[1], a = t[2]), r.length === 0) throw Error(`index '${n}' on table '${e.tableName}' has no fields — an index must cover at least one column.`);
167
+ let s = r.filter((e) => typeof e == "string");
168
+ if (i(e.tableName, n, o, s), _(e.tableName, n, a?.kind, r, e.fields, a?.kindOptions), e.appliedIndexes.some((e) => e.name === n)) throw t.length === 1 ? Error(`duplicate auto-named index '${n}' on table '${e.tableName}' — you've called .index(${JSON.stringify(r)}) twice. Pass an explicit name to the second one if both are intended (rare).`) : Error(`duplicate index '${n}' on table '${e.tableName}': each index name must be unique within a table.`);
169
+ return b({
170
+ ...e,
171
+ tableName: e.tableName,
172
+ fields: e.fields,
173
+ isReactive: e.isReactive,
174
+ appliedMixins: e.appliedMixins,
175
+ appliedIndexes: [...e.appliedIndexes, {
176
+ name: n,
177
+ fields: [...r],
178
+ ...a?.where === void 0 ? {} : { where: a.where },
179
+ ...a?.kind === void 0 ? {} : { kind: a.kind },
180
+ ...a?.kindOptions === void 0 ? {} : { kindOptions: a.kindOptions }
181
+ }],
182
+ appliedUniques: e.appliedUniques,
183
+ appliedFullText: e.appliedFullText,
184
+ appliedChecks: e.appliedChecks,
185
+ appliedPrimaryKey: e.appliedPrimaryKey,
186
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
187
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
188
+ });
189
+ }),
190
+ expressionIndex: ((t, n, r) => {
191
+ if (n.length === 0) throw Error(`expressionIndex '${t}' on table '${e.tableName}' has no fields — an index must cover at least one column or expression.`);
192
+ let a = n.filter((e) => typeof e == "string");
193
+ if (i(e.tableName, t, !1, a), _(e.tableName, t, r?.kind, n, e.fields, r?.kindOptions), e.appliedIndexes.some((e) => e.name === t)) throw Error(`duplicate index '${t}' on table '${e.tableName}': each index name must be unique within a table.`);
194
+ return b({
195
+ ...e,
196
+ tableName: e.tableName,
197
+ fields: e.fields,
198
+ isReactive: e.isReactive,
199
+ appliedMixins: e.appliedMixins,
200
+ appliedIndexes: [...e.appliedIndexes, {
201
+ name: t,
202
+ fields: [...n],
203
+ ...r?.where === void 0 ? {} : { where: r.where },
204
+ ...r?.kind === void 0 ? {} : { kind: r.kind },
205
+ ...r?.kindOptions === void 0 ? {} : { kindOptions: r.kindOptions }
206
+ }],
207
+ appliedUniques: e.appliedUniques,
208
+ appliedFullText: e.appliedFullText,
209
+ appliedChecks: e.appliedChecks,
210
+ appliedPrimaryKey: e.appliedPrimaryKey,
211
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
212
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
213
+ });
214
+ }),
215
+ unique: ((...t) => {
216
+ let n, r, a, o = Array.isArray(t[0]);
217
+ if (o ? (r = t[0], a = t[1], n = g(e.tableName, r)) : (n = t[0], r = t[1], a = t[2]), r.length === 0) throw Error(`unique '${n}' on table '${e.tableName}' has no fields — a UNIQUE constraint must cover at least one column.`);
218
+ if (i(e.tableName, n, o, r), e.appliedUniques.some((e) => e.name === n)) throw Error(o ? `duplicate auto-named UNIQUE '${n}' on table '${e.tableName}' — you've called .unique(${JSON.stringify(r)}) twice. Pass an explicit name to the second one if both are intended (very rare).` : `duplicate UNIQUE '${n}' on table '${e.tableName}': each constraint name must be unique within a table.`);
219
+ return b({
220
+ ...e,
221
+ tableName: e.tableName,
222
+ fields: e.fields,
223
+ isReactive: e.isReactive,
224
+ appliedMixins: e.appliedMixins,
225
+ appliedIndexes: e.appliedIndexes,
226
+ appliedUniques: [...e.appliedUniques, {
227
+ name: n,
228
+ fields: [...r],
229
+ ...a?.dedup === void 0 ? {} : { dedup: a.dedup }
230
+ }],
231
+ appliedFullText: e.appliedFullText,
232
+ appliedChecks: e.appliedChecks,
233
+ appliedPrimaryKey: e.appliedPrimaryKey,
234
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
235
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
236
+ });
237
+ }),
238
+ uniqueActive: ((...t) => {
239
+ let n, r, a, o = Array.isArray(t[0]);
240
+ if (o ? (r = t[0], a = t[1], n = h(e.tableName, r)) : (n = t[0], r = t[1], a = t[2]), r.length === 0) throw Error(`uniqueActive '${n}' on table '${e.tableName}' has no fields — a partial UNIQUE must cover at least one column.`);
241
+ if (i(e.tableName, n, o, r), e.appliedIndexes.some((e) => e.name === n)) throw Error(`duplicate index '${n}' on table '${e.tableName}': each index name must be unique within a table (uniqueActive shares the index namespace).`);
242
+ let s = a?.where ?? "\"deletedAt\" IS NULL";
243
+ return b({
244
+ ...e,
245
+ tableName: e.tableName,
246
+ fields: e.fields,
247
+ isReactive: e.isReactive,
248
+ appliedMixins: e.appliedMixins,
249
+ appliedIndexes: [...e.appliedIndexes, {
250
+ name: n,
251
+ fields: [...r],
252
+ where: s,
253
+ unique: !0
254
+ }],
255
+ appliedUniques: e.appliedUniques,
256
+ appliedFullText: e.appliedFullText,
257
+ appliedChecks: e.appliedChecks,
258
+ appliedPrimaryKey: e.appliedPrimaryKey,
259
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
260
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
261
+ });
262
+ }),
263
+ check: ((t, n) => {
264
+ if (n.trim().length === 0) throw Error(`CHECK '${t}' on table '${e.tableName}' has an empty expression.`);
265
+ if (i(e.tableName, t, !1, []), e.appliedChecks.some((e) => e.name === t)) throw Error(`duplicate CHECK '${t}' on table '${e.tableName}': each constraint name must be unique within a table.`);
266
+ return b({
267
+ ...e,
268
+ tableName: e.tableName,
269
+ fields: e.fields,
270
+ isReactive: e.isReactive,
271
+ appliedMixins: e.appliedMixins,
272
+ appliedIndexes: e.appliedIndexes,
273
+ appliedUniques: e.appliedUniques,
274
+ appliedFullText: e.appliedFullText,
275
+ appliedChecks: [...e.appliedChecks, {
276
+ name: t,
277
+ expr: n
278
+ }],
279
+ appliedPrimaryKey: e.appliedPrimaryKey,
280
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
281
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
282
+ });
283
+ }),
284
+ rule: ((t, n, r) => {
285
+ if (t.trim().length === 0) throw Error(`rule on table '${e.tableName}' has an empty name.`);
286
+ let i = e.appliedRules ?? [];
287
+ if (i.some((e) => e.name === t)) throw Error(`duplicate rule '${t}' on table '${e.tableName}': each rule name must be unique within a table.`);
288
+ return b({
289
+ ...e,
290
+ appliedRules: [...i, {
291
+ name: t,
292
+ predicate: n,
293
+ severity: r?.severity ?? "error"
294
+ }]
295
+ });
296
+ }),
297
+ renamedFrom: ((t) => {
298
+ if (t === e.tableName) throw Error(`renamedFrom('${t}') on table '${e.tableName}' names the table itself — drop the marker, there is nothing to rename.`);
299
+ return a(t), b({
300
+ ...e,
301
+ previousTableName: t
302
+ });
303
+ }),
304
+ primaryKey: ((n) => {
305
+ if (n.length === 0) throw Error(`primaryKey([...]) on table '${e.tableName}' needs at least one column.`);
306
+ let r = /* @__PURE__ */ new Set();
307
+ for (let t of n) {
308
+ if (!(t in e.fields)) throw Error(`primaryKey([...]) on table '${e.tableName}' references unknown column '${t}'.`);
309
+ if (r.has(t)) throw Error(`primaryKey([...]) on table '${e.tableName}' lists column '${t}' twice.`);
310
+ r.add(t);
311
+ }
312
+ if (t.length > 0) throw Error(`primaryKey([...]) declared twice on table '${e.tableName}'.`);
313
+ for (let [t, r] of Object.entries(e.fields)) if (r.type === "id" && !n.includes(t)) throw Error(`primaryKey([...]) on table '${e.tableName}' conflicts with the id() column '${t}'. The explicit primary key IS the key — drop the id() column, or include '${t}' in the composite key.`);
314
+ return b({
315
+ ...e,
316
+ tableName: e.tableName,
317
+ fields: e.fields,
318
+ isReactive: e.isReactive,
319
+ appliedMixins: e.appliedMixins,
320
+ appliedIndexes: e.appliedIndexes,
321
+ appliedUniques: e.appliedUniques,
322
+ appliedFullText: e.appliedFullText,
323
+ appliedChecks: e.appliedChecks,
324
+ appliedPrimaryKey: [...n],
325
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
326
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
327
+ });
328
+ }),
329
+ nonReactive: (() => b({
330
+ ...e,
331
+ tableName: e.tableName,
332
+ fields: e.fields,
333
+ isReactive: !1,
334
+ appliedMixins: e.appliedMixins,
335
+ appliedIndexes: e.appliedIndexes,
336
+ appliedUniques: e.appliedUniques,
337
+ appliedFullText: e.appliedFullText,
338
+ appliedChecks: e.appliedChecks,
339
+ appliedPrimaryKey: e.appliedPrimaryKey,
340
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
341
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
342
+ })),
343
+ validate: ((t) => b({
344
+ ...e,
345
+ tableName: e.tableName,
346
+ fields: e.fields,
347
+ isReactive: e.isReactive,
348
+ appliedMixins: e.appliedMixins,
349
+ appliedIndexes: e.appliedIndexes,
350
+ appliedUniques: e.appliedUniques,
351
+ appliedFullText: e.appliedFullText,
352
+ appliedChecks: e.appliedChecks,
353
+ appliedPrimaryKey: e.appliedPrimaryKey,
354
+ insertSchema: t,
355
+ ...e.validatePatchSchema === void 0 ? {} : { validatePatchSchema: e.validatePatchSchema }
356
+ })),
357
+ validatePatch: ((t) => b({
358
+ ...e,
359
+ tableName: e.tableName,
360
+ fields: e.fields,
361
+ isReactive: e.isReactive,
362
+ appliedMixins: e.appliedMixins,
363
+ appliedIndexes: e.appliedIndexes,
364
+ appliedUniques: e.appliedUniques,
365
+ appliedFullText: e.appliedFullText,
366
+ appliedChecks: e.appliedChecks,
367
+ appliedPrimaryKey: e.appliedPrimaryKey,
368
+ ...e.insertSchema === void 0 ? {} : { insertSchema: e.insertSchema },
369
+ validatePatchSchema: t.mapFields(u.map(l.optional))
370
+ }))
371
+ };
372
+ }, x = (n, i) => {
373
+ a(n);
374
+ let o = v(t(i), n);
375
+ for (let e of Object.keys(o)) r(n, e);
376
+ let s;
377
+ for (let [t, r] of Object.entries(o)) r.references === e && (r.references = () => {
378
+ if (s === void 0) throw Error(`selfReference() on '${n}.${t}' read before table() returned`);
379
+ return s;
380
+ });
381
+ return s = b({
382
+ tableName: n,
383
+ fields: o,
384
+ isReactive: !0,
385
+ appliedMixins: [],
386
+ appliedIndexes: [],
387
+ appliedUniques: [],
388
+ appliedFullText: [],
389
+ appliedChecks: [],
390
+ appliedRules: []
391
+ }), s;
392
+ };
393
+ //#endregion
394
+ export { p as AUTO_FILLED_COLUMNS, d as jsonIndex, x as table, f as vectorDistanceToOpclass };
@@ -0,0 +1,16 @@
1
+ //#region src/tableRegistry.ts
2
+ var e = Symbol.for("@voltro/database/tableRegistry"), t = globalThis, n = t[e] ?? (t[e] = /* @__PURE__ */ new Map()), r = (e) => {
3
+ let t = n.get(e.tableName);
4
+ if (t === void 0) n.set(e.tableName, e);
5
+ else if (t !== e) throw Error(`duplicate table '${e.tableName}' registration: two different table descriptors claim the same name. Rename one or import the shared module that defines it.`);
6
+ }, i = (e) => {
7
+ n.get(e.tableName) === void 0 && n.set(e.tableName, e);
8
+ }, a = (e) => n.get(e), o = (e) => {
9
+ let t = n.get(e);
10
+ if (t === void 0) throw Error(`table '${e}' is not registered. Register it by wrapping it in \`databaseHandle({ ... })\` (the usual path) or call \`registerTable(t)\` explicitly before this runs. Note a \`queryFor(t)\` query carries its own source table, so eager-loads don't hit this — this error means a reconstructed descriptor or by-name tooling reached an unregistered table.`);
11
+ return t;
12
+ }, s = () => [...n.values()], c = (e) => n.delete(e), l = () => {
13
+ n.clear();
14
+ }, u = (e) => n.get(e)?.isReactive !== !1;
15
+ //#endregion
16
+ export { s as allRegisteredTables, l as clearTableRegistry, i as ensureTableRegistered, a as getTable, u as isTableReactive, r as registerTable, o as requireTable, c as unregisterTable };
@@ -0,0 +1,27 @@
1
+ import { eq as e } from "./predicate.js";
2
+ import { _voltroTenantSelectionsTable as t } from "./frameworkTables.js";
3
+ import { createHash as n } from "node:crypto";
4
+ //#region src/tenantSelection.ts
5
+ var r = t.tableName, i = (e) => n("sha256").update(`${e.type}\u0000${e.id}`, "utf8").digest("hex"), a = async (t, n) => {
6
+ let [a] = await t.query({
7
+ table: r,
8
+ predicate: e("id", i(n)),
9
+ order: [],
10
+ take: 1,
11
+ skip: void 0,
12
+ projection: void 0
13
+ }), o = a?.tenantId;
14
+ return typeof o == "string" ? o : null;
15
+ }, o = async (e, t, n) => {
16
+ await e.upsert(r, {
17
+ id: i(t),
18
+ subjectType: t.type,
19
+ subjectId: t.id,
20
+ tenantId: n,
21
+ selectedAt: /* @__PURE__ */ new Date()
22
+ }, { conflictColumns: ["id"] });
23
+ }, s = async (e, t) => {
24
+ await e.delete(r, i(t));
25
+ };
26
+ //#endregion
27
+ export { r as TENANT_SELECTIONS_TABLE, s as deleteTenantSelection, a as readTenantSelection, i as tenantSelectionId, o as writeTenantSelection };
@@ -0,0 +1,97 @@
1
+ import { causeBranches as e } from "./dbCause.js";
2
+ import { currentWriteAttribution as t, runWithWriteAttribution as n } from "./writeAttribution.js";
3
+ import { observeDbOp as r } from "./dbMetrics.js";
4
+ import { Cause as i, Effect as a, Exit as o, Option as s, Schedule as c } from "effect";
5
+ import { SqlClient as l } from "effect/sql";
6
+ //#region src/transactionOutcome.ts
7
+ var u = (e) => {
8
+ if (o.isSuccess(e)) return e.value;
9
+ let t = i.squash(e.cause);
10
+ throw e.cause.reasons.length > 1 && typeof t == "object" && t && Object.isExtensible(t) && Object.defineProperty(t, "voltroCause", {
11
+ value: e.cause,
12
+ enumerable: !1,
13
+ configurable: !0
14
+ }), t;
15
+ }, d = "10 millis", f = 3, p = (e) => c.max([c.exponential(d), c.recurs(f)]).pipe(c.while(({ input: t }) => e(t))), m = l.TransactionConnection(0), h = /* @__PURE__ */ new WeakMap(), g = (e, t, n) => {
16
+ let r = h.get(n);
17
+ return r === void 0 ? a.provideService(e, t, n) : a.provideContext(e, r);
18
+ }, _ = async (e) => {
19
+ let t = 0, n = a.suspend(() => {
20
+ let n = ++t;
21
+ return e.withTransaction(a.flatMap(a.serviceOption(e.transactionService), (t) => {
22
+ if (s.isNone(t)) return a.fail(/* @__PURE__ */ Error(`${e.label}: TransactionConnection unexpectedly missing inside withTransaction.`));
23
+ let r = t.value, i = a.tryPromise({
24
+ try: () => e.body(r, n),
25
+ catch: (e) => e
26
+ });
27
+ return a.flatMap(a.context(), (t) => (h.set(r, t), e.prepare === void 0 ? i : a.flatMap(e.prepare(r), () => i)));
28
+ })).pipe(a.catchDefect((t) => e.isRetryable(t) ? a.fail(t) : a.die(t)));
29
+ }).pipe(a.retry(p(e.isRetryable)), a.withSpan(e.span.name, { attributes: { ...e.span.attributes } }));
30
+ return r(e.dialect, "transaction", async () => u(await e.runPromiseExit(e.wrapProgram === void 0 ? n : e.wrapProgram(n))));
31
+ }, v = class extends Error {
32
+ swallowed;
33
+ _tag = "TransactionAborted";
34
+ constructor(e, t) {
35
+ super(`${e}: a statement failed inside this transaction and the error was caught, but the database had already aborted the transaction — every later statement was refused, and COMMIT would have rolled back ALL of its writes, including the ones that succeeded. Nothing was committed.
36
+ the caught failure: ${S(t)}\n to tolerate a failing statement and keep the rest, run it inside \`tx.savepoint(() => …)\` — only that statement is rolled back. To keep all-or-nothing, let the error propagate.`, { cause: t }), this.swallowed = t, this.name = "TransactionAbortedError";
37
+ }
38
+ }, y = (e, t = "transactional()") => {
39
+ let n = 0, r, i = (e) => {
40
+ r === void 0 && (r = { error: e });
41
+ };
42
+ return {
43
+ track: (a) => r === void 0 ? n > 0 ? a() : a().catch((t) => {
44
+ throw e(t) && i(t), t;
45
+ }) : Promise.reject(new v(t, r.error)),
46
+ inSavepoint: async (e) => {
47
+ n++;
48
+ try {
49
+ return await e();
50
+ } finally {
51
+ n--;
52
+ }
53
+ },
54
+ record: i,
55
+ abortedBy: () => r
56
+ };
57
+ }, b = async (e, t) => {
58
+ let { tracker: n, name: r, exec: i, spelling: a } = e;
59
+ await n.track(() => i(a === "mssql" ? `SAVE TRANSACTION ${r}` : `SAVEPOINT ${r}`));
60
+ try {
61
+ let e = await n.inSavepoint(t);
62
+ return a === "standard" && await n.track(() => i(`RELEASE SAVEPOINT ${r}`)), e;
63
+ } catch (e) {
64
+ throw await i(a === "mssql" ? `ROLLBACK TRANSACTION ${r}` : `ROLLBACK TO SAVEPOINT ${r}`).catch((e) => n.record(e)), a === "standard" && await i(`RELEASE SAVEPOINT ${r}`).catch(() => void 0), e;
65
+ }
66
+ }, x = (e) => e instanceof v || typeof e == "object" && !!e && e._tag === "TransactionAborted", S = (t) => {
67
+ for (let n of e(t)) {
68
+ let e = n;
69
+ for (let t = 0; t < 5 && typeof e == "object" && e; t++) {
70
+ let t = e.code, n = e.message;
71
+ if (typeof t == "string" && typeof n == "string") return `${n} (${t})`;
72
+ e = e.cause;
73
+ }
74
+ }
75
+ return t instanceof Error ? t.message : String(t);
76
+ }, C = async (e) => {
77
+ let r = t(), i = r === void 0 ? (e) => e() : (e) => n(r, e), a = await _({
78
+ ...e,
79
+ body: (t) => {
80
+ let n = e.makeView(t, r);
81
+ return i(() => e.work(n)).then((t) => {
82
+ let r = n.abortedBy?.();
83
+ if (r !== void 0) throw e.isRetryable(r.error) ? r.error : new v(e.label, r.error);
84
+ return {
85
+ result: t,
86
+ view: n
87
+ };
88
+ }, (t) => {
89
+ let r = n.abortedBy?.();
90
+ throw r !== void 0 && e.isRetryable(r.error) ? r.error : t;
91
+ });
92
+ }
93
+ });
94
+ return a.view.commitEvents(), a.result;
95
+ };
96
+ //#endregion
97
+ export { m as SHARED_TRANSACTION_SERVICE, v as TransactionAbortedError, x as isTransactionAbortedError, y as makeTransactionAbortTracker, g as onTransaction, _ as runRetryingTransaction, b as runSavepoint, C as runStoreTransaction, u as settleTransactionExit, p as transactionRetrySchedule };
@@ -0,0 +1,36 @@
1
+ import { causeBranches as e } from "./dbCause.js";
2
+ //#region src/transientContention.ts
3
+ var t = /* @__PURE__ */ new Set([
4
+ "1213",
5
+ "ER_LOCK_DEADLOCK",
6
+ "1205",
7
+ "ER_LOCK_WAIT_TIMEOUT",
8
+ "1020",
9
+ "ER_CHECKREAD"
10
+ ]), n = /* @__PURE__ */ new Set(["40001", "40P01"]), r = /* @__PURE__ */ new Set(["1205"]), i = /* @__PURE__ */ new Set([
11
+ "SQLITE_BUSY",
12
+ "5",
13
+ "SQLITE_BUSY_SNAPSHOT",
14
+ "517",
15
+ "SQLITE_LOCKED",
16
+ "6"
17
+ ]), a = /write-write conflict|write-read conflict|database is locked|table is locked|database is busy/i, o = /* @__PURE__ */ new Set([
18
+ 1213,
19
+ 1205,
20
+ 1020
21
+ ]), s = /* @__PURE__ */ new Set([
22
+ ...t,
23
+ ...n,
24
+ ...r,
25
+ ...i
26
+ ]), c = (t) => e(t).some(l), l = (e) => {
27
+ let t = e;
28
+ for (let e = 0; e < 6 && typeof t == "object" && t; e++) {
29
+ let e = t;
30
+ if (typeof e.errno == "number" && o.has(e.errno) || typeof e.number == "number" && r.has(String(e.number)) || typeof e.code == "string" && s.has(e.code) || typeof e.sqlState == "string" && n.has(e.sqlState) || typeof e.message == "string" && a.test(e.message)) return !0;
31
+ t = e.cause;
32
+ }
33
+ return !1;
34
+ };
35
+ //#endregion
36
+ export { r as MSSQL_TRANSIENT_NUMBERS, t as MYSQL_TRANSIENT_CODES, n as POSTGRES_TRANSIENT_STATES, i as SQLITE_TRANSIENT_CODES, a as TURSO_TRANSIENT_MESSAGE, c as isTransientContention };
@@ -0,0 +1,16 @@
1
+ import { authoredError as e } from "./dbCause.js";
2
+ //#region src/typedStore.ts
3
+ var t = (e, t, n) => e.insert(t.tableName, n), n = (e, t, n) => e.insertMany(t.tableName, n), r = (e, t, n, r) => e.upsert(t.tableName, n, {
4
+ conflictColumns: r.conflictColumns,
5
+ ...r.update === void 0 ? {} : { update: r.update },
6
+ ...r.updateValues === void 0 ? {} : { updateValues: r.updateValues }
7
+ }), i = async (t, n, r, i) => {
8
+ if (t.upsertWithOutcome === void 0) throw e("upsertRowOutcome: this store does not implement `upsertWithOutcome`. It is provided by the framework's store wrapper, so `ctx.store` has it; a hand-written DataStore must add it.");
9
+ return t.upsertWithOutcome(n.tableName, r, {
10
+ conflictColumns: i.conflictColumns,
11
+ ...i.update === void 0 ? {} : { update: i.update },
12
+ ...i.updateValues === void 0 ? {} : { updateValues: i.updateValues }
13
+ });
14
+ }, a = async (e, t, n, r) => e.insertIgnoreWithOutcome(t.tableName, n, { conflictColumns: r.conflictColumns }), o = (e, t, n, r) => e.updateMany(t.tableName, n, { where: r.where });
15
+ //#endregion
16
+ export { a as insertIgnoreRowOutcome, n as insertManyRows, t as insertRow, o as updateManyRow, r as upsertRow, i as upsertRowOutcome };
@@ -0,0 +1,20 @@
1
+ import { authoredError as e } from "./dbCause.js";
2
+ //#region src/upsertUpdate.ts
3
+ var t = (t) => Object.fromEntries(Object.entries(t.updateValues ?? {}).filter(([n, r]) => {
4
+ if (r === void 0) return !1;
5
+ if (n === "id" || t.conflictColumns.includes(n)) throw e(`upsert: updateValues cannot rewrite row identity column '${n}'`);
6
+ return !0;
7
+ })), n = (e, n, r) => {
8
+ let i = t(r);
9
+ if (typeof r.update == "function") return {
10
+ ...r.update(n),
11
+ ...i
12
+ };
13
+ let a = r.update ?? Object.keys(e).filter((e) => e !== "id" && !r.conflictColumns.includes(e));
14
+ return {
15
+ ...Object.fromEntries(a.filter((t) => t in e).map((t) => [t, e[t]])),
16
+ ...i
17
+ };
18
+ };
19
+ //#endregion
20
+ export { n as upsertUpdatePatch, t as upsertUpdateValues };
@@ -0,0 +1,31 @@
1
+ import { vector as e } from "./columns.js";
2
+ import { defineMixin as t } from "./mixins.js";
3
+ //#region src/vectorEmbedding.ts
4
+ var n = async (e, t) => {
5
+ let n = e.as ?? "embedding", r = t.row[e.from];
6
+ if (typeof r != "string" || r.length === 0) return {};
7
+ let i = await t.embed(r);
8
+ return { [n]: i };
9
+ }, r = (r) => {
10
+ if (!Number.isInteger(r.dimensions) || r.dimensions <= 0) throw Error(`vectorEmbedding: dimensions must be a positive integer, got ${String(r.dimensions)}.`);
11
+ let i = r.as ?? "embedding", a = (e) => n(r, e);
12
+ return t({
13
+ id: "voltro/vectorEmbedding",
14
+ fields: { [i]: e(r.dimensions) },
15
+ ...r.index === !1 ? {} : { indexes: [{
16
+ fields: [i],
17
+ kind: "hnsw",
18
+ kindOptions: { hnsw: {
19
+ ...r.index?.m === void 0 ? {} : { m: r.index.m },
20
+ ...r.index?.efConstruction === void 0 ? {} : { efConstruction: r.index.efConstruction },
21
+ distance: r.distance ?? "cosine"
22
+ } }
23
+ }] },
24
+ behaviors: {
25
+ beforeInsert: a,
26
+ beforeUpdate: a
27
+ }
28
+ });
29
+ };
30
+ //#endregion
31
+ export { n as computeEmbedding, r as vectorEmbedding };