@voltro/database 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/THIRD-PARTY-NOTICES.md +5 -257
- package/dist/actorSnapshot.js +24 -0
- package/dist/arrayCodec.js +32 -0
- package/dist/branchExecute.js +92 -0
- package/dist/branching.js +106 -0
- package/dist/bulkInsertLimits.js +25 -0
- package/dist/cdcOffsetsTables.js +19 -0
- package/dist/changeEvent.js +41 -0
- package/dist/changeIdentity.js +65 -0
- package/dist/changeListenerCeiling.js +6 -0
- package/dist/columnReference.js +17 -0
- package/dist/columns.js +326 -0
- package/dist/committedChanges.js +12 -0
- package/dist/connectionIdentity.js +36 -0
- package/dist/coreTables.js +16 -0
- package/dist/coreTablesRegistry.js +10 -0
- package/dist/dataTransfers.js +23 -0
- package/dist/dbCause.js +222 -0
- package/dist/dbMetrics.js +65 -0
- package/dist/decimalComparison.js +27 -0
- package/dist/derivedRowId.js +5 -0
- package/dist/dialect.js +13 -0
- package/dist/enumRenames.js +14 -0
- package/dist/expiresMixin.js +10 -0
- package/dist/fieldEncryption.js +121 -0
- package/dist/frameworkTables.js +143 -0
- package/dist/ginIndex.js +10 -0
- package/dist/hybridSearch.js +32 -0
- package/dist/idGenerator.js +57 -0
- package/dist/identifierLimits.js +32 -0
- package/dist/index.d.ts +166 -22
- package/dist/index.js +82 -2183
- package/dist/indexAudit.js +61 -0
- package/dist/indexStorage.js +58 -0
- package/dist/joinCompiler.js +160 -0
- package/dist/jsonEagerCompiler.js +436 -0
- package/dist/jsonPatch.js +78 -0
- package/dist/jsonPatchAssignments.js +13 -0
- package/dist/jsonPath.js +31 -0
- package/dist/localFirstMixin.js +12 -0
- package/dist/migrate.js +644 -0
- package/dist/migration.js +18 -0
- package/dist/migrations/appliedAtValue.js +8 -0
- package/dist/migrations/applier.js +1100 -0
- package/dist/migrations/boot.js +196 -0
- package/dist/migrations/branchRehearsal.js +122 -0
- package/dist/migrations/columnEquivalence.js +85 -0
- package/dist/migrations/ddlError.js +63 -0
- package/dist/migrations/ddlStatements.js +9 -0
- package/dist/migrations/declaredSchema.js +162 -0
- package/dist/migrations/deployLedger.js +19 -0
- package/dist/migrations/fileBased.js +57 -0
- package/dist/migrations/fileContext.js +482 -0
- package/dist/migrations/fileDiscovery.js +332 -0
- package/dist/migrations/fileSkipProbes.js +117 -0
- package/dist/migrations/fingerprint.js +46 -0
- package/dist/migrations/fkCycles.js +13 -0
- package/dist/migrations/frameworkLiveTables.js +25 -0
- package/dist/migrations/idempotentDdl.js +37 -0
- package/dist/migrations/indexPredicate.js +50 -0
- package/dist/migrations/introspect.js +801 -0
- package/dist/migrations/ledgerRows.js +4 -0
- package/dist/migrations/lock.js +91 -0
- package/dist/migrations/migrationTableExists.js +9 -0
- package/dist/migrations/mssqlColumnDependencies.js +76 -0
- package/dist/migrations/mysqlEngine.js +11 -0
- package/dist/migrations/namespaceCharset.js +51 -0
- package/dist/migrations/planner.js +785 -0
- package/dist/migrations/postgresEnumCheck.js +22 -0
- package/dist/migrations/reactiveTriggerDrift.js +58 -0
- package/dist/migrations/reactiveTriggerName.js +7 -0
- package/dist/migrations/renameMssqlCheck.js +21 -0
- package/dist/migrations/restoreSnapshots.js +206 -0
- package/dist/migrations/resumeLedger.js +86 -0
- package/dist/migrations/rollingDeploy.js +106 -0
- package/dist/migrations/schemaOwner.js +46 -0
- package/dist/migrations/schemaOwnerTable.js +4 -0
- package/dist/migrations/softDropSnapshot.js +20 -0
- package/dist/migrations/sqlLimit.js +8 -0
- package/dist/migrations/sqliteCatalogName.js +13 -0
- package/dist/migrations/sqliteConstraintIdentity.js +97 -0
- package/dist/migrations/sqliteDecimalBoot.js +22 -0
- package/dist/migrations/sqliteDecimalPublication.js +257 -0
- package/dist/migrations/sqliteDecimalReview.js +30 -0
- package/dist/migrations/sqliteDecimalReviewStore.js +188 -0
- package/dist/migrations/sqliteDecimalReviewWitness.js +38 -0
- package/dist/migrations/sqliteDecimalSource.js +69 -0
- package/dist/migrations/sqliteDecimalStage.js +135 -0
- package/dist/migrations/sqliteForeignKeys.js +38 -0
- package/dist/migrations/sqliteMigrationBatch.js +37 -0
- package/dist/migrations/sqliteRetainedSource.js +106 -0
- package/dist/migrations/sqliteRowIdentity.js +63 -0
- package/dist/migrations/sqliteSchemaEpoch.js +12 -0
- package/dist/migrations/sqliteSnapshotGuard.js +10 -0
- package/dist/migrations/sqliteTableContents.js +34 -0
- package/dist/migrations/sqliteTableCopy.js +25 -0
- package/dist/migrations/sqliteTableSchema.js +19 -0
- package/dist/migrations/sqliteWitnessProtection.js +8 -0
- package/dist/migrations/squash.js +81 -0
- package/dist/migrations/storeCharset.js +66 -0
- package/dist/migrations/transactionCause.js +10 -0
- package/dist/migrations/undeclaredCharset.js +40 -0
- package/dist/migrations/uniqueActiveLowering.js +39 -0
- package/dist/mixins.js +22 -0
- package/dist/mssqlDecimalDistinct.js +51 -0
- package/dist/mssqlJsonValueCheck.js +42 -0
- package/dist/mssqlTextStorage.js +9 -0
- package/dist/namespace.js +21 -0
- package/dist/{wire-CG2j7J3f.js → optimisticVersion.js} +2 -2
- package/dist/pendingAttribution.js +79 -0
- package/dist/pluginRef.js +17 -0
- package/dist/predicate.js +273 -0
- package/dist/predicateCoercion.js +13 -0
- package/dist/projectedColumn.js +37 -0
- package/dist/queryBuilder.js +388 -0
- package/dist/rawSql.js +8 -0
- package/dist/relations.js +69 -0
- package/dist/relationsRegistry.js +38 -0
- package/dist/replaceInProgress.js +53 -0
- package/dist/requiredColumns.js +26 -0
- package/dist/residency.js +52 -0
- package/dist/retention.js +62 -0
- package/dist/rowDecoder.js +173 -0
- package/dist/rowKey.js +8 -0
- package/dist/rowSchema.js +31 -0
- package/dist/scopeKey.js +8 -0
- package/dist/seed.js +33 -0
- package/dist/snowflake.js +26 -0
- package/dist/sql.d.ts +2792 -3773
- package/dist/sql.js +42 -6905
- package/dist/sqlCompiler.js +360 -0
- package/dist/sqlError.js +8 -0
- package/dist/sqlMutation.js +55 -0
- package/dist/sqlPoolCapacity.js +12 -0
- package/dist/sqliteDecimal.js +22 -0
- package/dist/sqliteDecimalAggregateSql.js +61 -0
- package/dist/sqliteDecimalColumnSql.js +108 -0
- package/dist/sqliteDecimalConversion.js +15 -0
- package/dist/sqliteDecimalCte.js +154 -0
- package/dist/sqliteDecimalEncodeSql.js +32 -0
- package/dist/sqliteDecimalOutputs.js +74 -0
- package/dist/sqliteDecimalPredicate.js +66 -0
- package/dist/sqliteDecimalQuery.js +183 -0
- package/dist/sqliteDecimalQueryPlan.js +37 -0
- package/dist/sqliteDecimalResultSql.js +44 -0
- package/dist/sqliteDecimalRowQuery.js +63 -0
- package/dist/sqliteDecimalSetQuery.js +50 -0
- package/dist/sqliteDecimalSource.js +63 -0
- package/dist/sqliteDecimalStorage.js +29 -0
- package/dist/sqliteDecimalSubquery.js +19 -0
- package/dist/sqliteDecimalWindowQuery.js +196 -0
- package/dist/sqliteValueCoercion.js +4 -0
- package/dist/stampId.js +21 -0
- package/dist/storeErrors.js +9 -0
- package/dist/storeRefusal.js +20 -0
- package/dist/stream.js +36 -0
- package/dist/subjectGraph.js +175 -0
- package/dist/table.js +394 -0
- package/dist/tableRegistry.js +16 -0
- package/dist/tenantSelection.js +27 -0
- package/dist/transactionOutcome.js +100 -0
- package/dist/transientContention.js +36 -0
- package/dist/typeId.js +51 -0
- package/dist/typedStore.js +16 -0
- package/dist/upsertUpdate.js +20 -0
- package/dist/vectorEmbedding.js +31 -0
- package/dist/view.js +39 -0
- package/dist/wire.js +6 -2
- package/dist/writeAttribution.js +16 -0
- package/dist/writeRecorder.js +38 -0
- package/package.json +3 -4
- package/dist/replaceInProgress-DC88Pdiy.js +0 -3735
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ansi as e } from "@voltro/logger";
|
|
2
|
+
//#region src/indexAudit.ts
|
|
3
|
+
var t = (e) => {
|
|
4
|
+
if (typeof e == "string") return e;
|
|
5
|
+
if ("jsonPath" in e) {
|
|
6
|
+
let { column: t, path: n, numeric: r } = e.jsonPath;
|
|
7
|
+
return `(json:${t}->${n.join(".")}${r ? "::num" : ""})`;
|
|
8
|
+
}
|
|
9
|
+
return `(${e.expr})`;
|
|
10
|
+
}, n = (e) => e.map(t), r = (e, n) => {
|
|
11
|
+
if (e.length !== n.length) return !1;
|
|
12
|
+
for (let r = 0; r < e.length; r++) if (t(e[r]) !== t(n[r])) return !1;
|
|
13
|
+
return !0;
|
|
14
|
+
}, i = (e, n) => {
|
|
15
|
+
if (e.length >= n.length) return !1;
|
|
16
|
+
for (let r = 0; r < e.length; r++) if (t(e[r]) !== t(n[r])) return !1;
|
|
17
|
+
return !0;
|
|
18
|
+
}, a = (e) => {
|
|
19
|
+
let t = [], a = e.appliedIndexes, o = e.tableName;
|
|
20
|
+
for (let e = 0; e < a.length; e++) {
|
|
21
|
+
let s = a[e];
|
|
22
|
+
for (let c = e + 1; c < a.length; c++) {
|
|
23
|
+
let e = a[c];
|
|
24
|
+
if (r(s.fields, e.fields)) {
|
|
25
|
+
let [r, i] = s.name < e.name ? [s, e] : [e, s];
|
|
26
|
+
t.push({
|
|
27
|
+
kind: "duplicate-fields",
|
|
28
|
+
table: o,
|
|
29
|
+
redundant: r,
|
|
30
|
+
coveredBy: i,
|
|
31
|
+
message: `'${o}' has two indexes on the same fields [${n(s.fields).join(", ")}]: '${s.name}' and '${e.name}'. The B-tree engine builds both, costing write throughput + disk space for no read benefit. Drop '${r.name}' (or '${i.name}' if you prefer the other name). Applies to every supported dialect (Postgres / MySQL / MariaDB / MSSQL / SQLite).`
|
|
32
|
+
});
|
|
33
|
+
} else i(s.fields, e.fields) ? t.push({
|
|
34
|
+
kind: "redundant-prefix",
|
|
35
|
+
table: o,
|
|
36
|
+
redundant: s,
|
|
37
|
+
coveredBy: e,
|
|
38
|
+
message: `'${o}' index '${s.name}' on [${n(s.fields).join(", ")}] is a leading prefix of '${e.name}' on [${n(e.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${n(s.fields).join(", ")}] query from '${e.name}' too via the leading-prefix rule, so '${s.name}' adds write cost without read benefit. Drop '${s.name}' — unless you specifically need a smaller index for cache locality on a write-heavy table (rare; profile first).`
|
|
39
|
+
}) : i(e.fields, s.fields) && t.push({
|
|
40
|
+
kind: "redundant-prefix",
|
|
41
|
+
table: o,
|
|
42
|
+
redundant: e,
|
|
43
|
+
coveredBy: s,
|
|
44
|
+
message: `'${o}' index '${e.name}' on [${n(e.fields).join(", ")}] is a leading prefix of '${s.name}' on [${n(s.fields).join(", ")}]. Every B-tree dialect we support (Postgres / MySQL / MariaDB / MSSQL / SQLite) can serve a [${n(e.fields).join(", ")}] query from '${s.name}' too via the leading-prefix rule, so '${e.name}' adds write cost without read benefit. Drop '${e.name}' — unless you specifically need a smaller index for cache locality on a write-heavy table (rare; profile first).`
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return t;
|
|
49
|
+
}, o = (e) => {
|
|
50
|
+
let t = [];
|
|
51
|
+
for (let n of e) t.push(...a(n));
|
|
52
|
+
return t;
|
|
53
|
+
}, s = "\x1B[", c = `${s}38;2;245;158;11m`, l = `${s}38;2;16;185;129m`, u = (t, n) => `${t}${n}${e.reset}`, d = (e, t, n) => e ? typeof t == "function" ? t(n) : u(t, n) : n, f = (t, r = {}) => {
|
|
54
|
+
let i = r.color ?? !0, a = r.indent ?? " ", o = (t) => d(i, e.dim, t.padEnd(11)), s = (t) => d(i, (t) => e.bold(e.brand(t)), t), u = (t) => d(i, e.brightRed24, t), f = (e) => d(i, l, e), p = (e) => d(i, c, e), m = (t) => d(i, e.dim, t), h = `${p("index audit")} ${m("·")} ${d(i, e.bold, t.kind)}`, g = t.redundant.fields.length, _ = n(t.coveredBy.fields.slice(g)), v = (e, t) => {
|
|
55
|
+
let n = e.slice(0, t), r = e.slice(t), i = [];
|
|
56
|
+
return n.length > 0 && i.push(f(`[${n.join(", ")}]`)), r.length > 0 && i.push(p(`+ [${r.join(", ")}]`)), i.join(" ");
|
|
57
|
+
}, y = [];
|
|
58
|
+
return y.push(h), y.push(""), y.push(`${o("table")}${s(t.table)}`), y.push(""), y.push(`${o("redundant")}${u(t.redundant.name)}`), y.push(`${o("")}${v(n(t.redundant.fields), g)}`), y.push(""), y.push(`${o("covered by")}${f(t.coveredBy.name)}`), y.push(`${o("")}${v(n(t.coveredBy.fields), g)}`), _.length > 0 && y.push(`${o("")}${m(`└ extra columns: ${_.join(", ")}`)}`), y.push(""), t.kind === "redundant-prefix" ? (y.push(`${o("why")}B-tree indexes serve queries that filter on a LEADING PREFIX of`), y.push(`${o("")}their column list. The wider index already covers every`), y.push(`${o("")}query the narrower one can serve.`), y.push(`${o("")}${m("Same rule on Postgres / MySQL / MariaDB / MSSQL / SQLite.")}`)) : (y.push(`${o("why")}Two indexes on the same columns under different names.`), y.push(`${o("")}The B-tree engine builds both — same coverage, double cost.`), y.push(`${o("")}${m("Same rule on Postgres / MySQL / MariaDB / MSSQL / SQLite.")}`)), y.push(""), y.push(`${o("cost")}Every ${s("INSERT")} / ${s("UPDATE")} / ${s("DELETE")} on this table writes`), y.push(`${o("")}to ${d(i, e.bold, "both")} B-trees. Pure write overhead, zero read benefit.`), y.push(""), y.push(`${o("fix")}Drop ${u(t.redundant.name)}`), y.push(`${o("")}${m("Keep only if profiling shows the smaller index gives a")}`), y.push(`${o("")}${m("measurable cache-locality boost on a write-heavy hot path.")}`), y.push(`${o("")}${m("Silence with VOLTRO_INDEX_AUDIT=off if intentional.")}`), y.map((e) => `${a}${e}`).join("\n");
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { o as auditAllTableIndexes, a as auditTableIndexes, f as formatIndexAuditIssue };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { resolveGinOpclass as e } from "./ginIndex.js";
|
|
2
|
+
import { vectorDistanceToOpclass as t } from "./table.js";
|
|
3
|
+
//#region src/indexStorage.ts
|
|
4
|
+
var n = (e, n = "float32") => {
|
|
5
|
+
let r = e?.hnsw, i = r?.m ?? 16, a = r?.efConstruction ?? 64;
|
|
6
|
+
for (let [e, t] of Object.entries({
|
|
7
|
+
m: i,
|
|
8
|
+
efConstruction: a
|
|
9
|
+
})) if (!Number.isSafeInteger(t) || t <= 0) throw Error(`HNSW ${e} must be a positive safe integer`);
|
|
10
|
+
let o = r?.opclass ?? t(r?.distance, n);
|
|
11
|
+
if (!/^[a-z_][a-z0-9_$]*(?:\.[a-z_][a-z0-9_$]*)?$/i.test(o)) throw Error("HNSW opclass must be an unquoted, optionally schema-qualified identifier");
|
|
12
|
+
return {
|
|
13
|
+
m: i,
|
|
14
|
+
efConstruction: a,
|
|
15
|
+
opclass: o.toLowerCase().replace(/^public\./, "")
|
|
16
|
+
};
|
|
17
|
+
}, r = (e, t) => t === void 0 || t === "postgres" ? e ?? "btree" : e === "hnsw" || e === "gin" && (t === "mysql" || t === "mariadb") ? null : "btree", i = (t, r) => {
|
|
18
|
+
let i = t === "gin" ? e(r) : void 0, a = i !== void 0 && [
|
|
19
|
+
"array_ops",
|
|
20
|
+
"jsonb_ops",
|
|
21
|
+
"tsvector_ops"
|
|
22
|
+
].includes(i);
|
|
23
|
+
return {
|
|
24
|
+
...t !== void 0 && t !== "btree" ? { kind: t } : {},
|
|
25
|
+
...t === "hnsw" ? { kindOptions: { hnsw: n(r) } } : {},
|
|
26
|
+
...i !== void 0 && !a ? { kindOptions: { gin: { opclass: i } } } : {}
|
|
27
|
+
};
|
|
28
|
+
}, a = (t, r, i = "float32") => {
|
|
29
|
+
if (t === void 0 || t === "btree") return {
|
|
30
|
+
using: "",
|
|
31
|
+
withSuffix: "",
|
|
32
|
+
skip: !1
|
|
33
|
+
};
|
|
34
|
+
if (!/^[a-z_][a-z0-9_]*$/i.test(t)) throw Error("Index kind must be an SQL access-method identifier");
|
|
35
|
+
if (t === "gin") {
|
|
36
|
+
let t = e(r);
|
|
37
|
+
return {
|
|
38
|
+
using: " USING GIN",
|
|
39
|
+
withSuffix: "",
|
|
40
|
+
...t === void 0 ? {} : { opclass: t },
|
|
41
|
+
skip: !1
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (t !== "hnsw") return {
|
|
45
|
+
using: ` USING ${t.toUpperCase()}`,
|
|
46
|
+
withSuffix: "",
|
|
47
|
+
skip: !1
|
|
48
|
+
};
|
|
49
|
+
let a = n(r, i);
|
|
50
|
+
return {
|
|
51
|
+
using: " USING hnsw",
|
|
52
|
+
withSuffix: ` WITH (m = ${a.m}, ef_construction = ${a.efConstruction})`,
|
|
53
|
+
opclass: a.opclass,
|
|
54
|
+
skip: !1
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
export { r as indexKindForDialect, i as indexStorageShape, a as pgIndexClauses, n as resolveHnswOptions };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { persistedColumns as e } from "./columns.js";
|
|
2
|
+
import { authoredError as t } from "./dbCause.js";
|
|
3
|
+
import { decimalEqualityKey as n } from "./decimalComparison.js";
|
|
4
|
+
import { inferForeignKey as r, validateRelationConfig as i } from "./relations.js";
|
|
5
|
+
import { getRelations as a } from "./relationsRegistry.js";
|
|
6
|
+
//#region src/joinCompiler.ts
|
|
7
|
+
var o = class extends Error {
|
|
8
|
+
_tag = "EagerCardinalityError";
|
|
9
|
+
constructor(e) {
|
|
10
|
+
super(e), this.name = "EagerCardinalityError";
|
|
11
|
+
}
|
|
12
|
+
}, s = (e) => {
|
|
13
|
+
let { relationName: t, sourceTableName: n, targetTableName: r, foreignKey: i, sourceKey: a } = e, o = r === n;
|
|
14
|
+
return `relation '${t}' on '${n}' is declared \`one\`, but ${r}.${i} matches MORE than one row for the same ${n}.${a} — so there is no single related row to return.` + (o ? ` Because this relation is self-referential, check which side the key is on: \`foreignKey: '${i}'\` means "rows whose ${i} points AT me" (my children — there can be many). If you meant "the row my ${i} points to" (my parent), that is \`sourceKey: '${i}'\`.` : " Use `many(...)` if several are expected, or narrow the key.");
|
|
15
|
+
}, c = async (e, n, r, o) => {
|
|
16
|
+
if (e.length === 0) return e;
|
|
17
|
+
let s = a(r.tableName);
|
|
18
|
+
if (s === void 0) throw t(`cannot eager-load on '${r.tableName}': no relations registered. Did you forget to declare \`relations(${r.tableName}, ...)\` or import the file that does?`);
|
|
19
|
+
let c = e.map((e) => ({ ...e }));
|
|
20
|
+
for (let [e, a] of Object.entries(n)) {
|
|
21
|
+
let n = s[e];
|
|
22
|
+
if (n === void 0) {
|
|
23
|
+
let n = Object.keys(s).join(", ") || "<none>";
|
|
24
|
+
throw t(`unknown relation '${e}' on '${r.tableName}'. Registered relations: ${n}.`);
|
|
25
|
+
}
|
|
26
|
+
i(r, e, n), await l(c, e, n, a === !0 ? {} : a, r, o);
|
|
27
|
+
}
|
|
28
|
+
return c;
|
|
29
|
+
}, l = async (e, t, n, r, i, a) => {
|
|
30
|
+
switch (n.kind) {
|
|
31
|
+
case "one":
|
|
32
|
+
await u(e, t, n, r, i, a);
|
|
33
|
+
return;
|
|
34
|
+
case "many":
|
|
35
|
+
await d(e, t, n, r, i, a);
|
|
36
|
+
return;
|
|
37
|
+
case "manyToMany":
|
|
38
|
+
await p(e, t, n, r, i, a);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
}, u = async (e, t, n, i, a, c) => {
|
|
42
|
+
let l = n.target(), u = n.sourceKey ?? h(a, l.tableName);
|
|
43
|
+
if (u !== void 0) {
|
|
44
|
+
let n = g(e, u), r = n.length === 0 ? [] : await _(l, "id", n, i, c), o = new Map(r.map((e) => [m(l, "id", e.id), e]));
|
|
45
|
+
for (let n of e) {
|
|
46
|
+
let e = m(a, u, n[u]);
|
|
47
|
+
n[t] = e == null ? null : o.get(e) ?? null;
|
|
48
|
+
}
|
|
49
|
+
await v(r, i.with, l, c);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let d = n.foreignKey ?? r(l, a.tableName, t), f = n.sourceKey ?? "id", p = g(e, f), y = p.length === 0 ? [] : await _(l, d, p, i, c), b = /* @__PURE__ */ new Map();
|
|
53
|
+
for (let e of y) {
|
|
54
|
+
let n = m(l, d, e[d]);
|
|
55
|
+
if (b.has(n)) throw new o(s({
|
|
56
|
+
relationName: t,
|
|
57
|
+
sourceTableName: a.tableName,
|
|
58
|
+
targetTableName: l.tableName,
|
|
59
|
+
foreignKey: d,
|
|
60
|
+
sourceKey: f
|
|
61
|
+
}));
|
|
62
|
+
b.set(n, e);
|
|
63
|
+
}
|
|
64
|
+
for (let n of e) {
|
|
65
|
+
let e = m(a, f, n[f]);
|
|
66
|
+
n[t] = e == null ? null : b.get(e) ?? null;
|
|
67
|
+
}
|
|
68
|
+
await v(y, i.with, l, c);
|
|
69
|
+
}, d = async (e, t, n, i, a, o) => {
|
|
70
|
+
let s = n.target(), c = n.foreignKey ?? r(s, a.tableName, t), l = n.sourceKey ?? "id", u = g(e, l), d = f(i), p = u.length === 0 ? [] : await _(s, c, u, d, o), h = /* @__PURE__ */ new Map(), y = i.offset ?? 0, b = Number.isSafeInteger(y) && y >= 0 && (i.limit === void 0 || Number.isSafeInteger(i.limit) && i.limit >= 0), x = /* @__PURE__ */ new Map();
|
|
71
|
+
for (let e of p) {
|
|
72
|
+
let t = m(s, c, e[c]);
|
|
73
|
+
if (b) {
|
|
74
|
+
let e = x.get(t) ?? 0;
|
|
75
|
+
if (x.set(t, e + 1), e < y || i.limit !== void 0 && e - y >= i.limit) continue;
|
|
76
|
+
}
|
|
77
|
+
let n = h.get(t);
|
|
78
|
+
n ? n.push(e) : h.set(t, [e]);
|
|
79
|
+
}
|
|
80
|
+
if (!b && (i.limit !== void 0 || i.offset !== void 0)) {
|
|
81
|
+
let e = i.limit;
|
|
82
|
+
for (let [t, n] of h) {
|
|
83
|
+
let r = e === void 0 ? n.slice(y) : n.slice(y, y + e);
|
|
84
|
+
h.set(t, r);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (let n of e) {
|
|
88
|
+
let e = m(a, l, n[l]);
|
|
89
|
+
n[t] = h.get(e) ?? [];
|
|
90
|
+
}
|
|
91
|
+
i.with !== void 0 && await v([...h.values()].flat(), i.with, s, o);
|
|
92
|
+
}, f = (e) => {
|
|
93
|
+
let { limit: t, offset: n, ...r } = e;
|
|
94
|
+
return r;
|
|
95
|
+
}, p = async (t, n, r, i, a, o) => {
|
|
96
|
+
let s = r.target(), c = r.through(), l = g(t, "id");
|
|
97
|
+
if (l.length === 0) {
|
|
98
|
+
for (let e of t) e[n] = [];
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let u = {
|
|
102
|
+
column: r.sourceKey,
|
|
103
|
+
op: "in",
|
|
104
|
+
value: l
|
|
105
|
+
}, d = i.onJunction === void 0 ? u : { and: [u, i.onJunction] }, f = await o({
|
|
106
|
+
table: c.tableName,
|
|
107
|
+
predicate: d,
|
|
108
|
+
order: [],
|
|
109
|
+
take: void 0,
|
|
110
|
+
skip: void 0,
|
|
111
|
+
projection: void 0
|
|
112
|
+
});
|
|
113
|
+
if (f.length === 0) {
|
|
114
|
+
for (let e of t) e[n] = [];
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
let p = await _(s, "id", g(f, r.targetKey), i, o), h = new Map(p.map((e) => [m(s, "id", e.id), e]));
|
|
118
|
+
await v(p, i.with, s, o);
|
|
119
|
+
let y = i.junction === void 0 ? void 0 : i.junction === !0 ? e(c) : i.junction, b = /* @__PURE__ */ new Map();
|
|
120
|
+
for (let e of f) {
|
|
121
|
+
let t = m(c, r.sourceKey, e[r.sourceKey]), n = m(c, r.targetKey, e[r.targetKey]), i = h.get(n);
|
|
122
|
+
if (i === void 0) continue;
|
|
123
|
+
let a = y === void 0 ? i : {
|
|
124
|
+
...i,
|
|
125
|
+
_junction: Object.fromEntries(y.map((t) => [t, e[t]]))
|
|
126
|
+
}, o = b.get(t);
|
|
127
|
+
o ? o.push(a) : b.set(t, [a]);
|
|
128
|
+
}
|
|
129
|
+
for (let e of t) e[n] = b.get(m(a, "id", e.id)) ?? [];
|
|
130
|
+
}, m = (e, t, r) => e.fields[t]?.type === "decimal" ? n(r) ?? r : r, h = (e, t) => {
|
|
131
|
+
for (let [n, r] of Object.entries(e.fields)) if (r.type === "reference" && r.references?.()?.tableName === t) return n;
|
|
132
|
+
}, g = (e, t) => {
|
|
133
|
+
let n = /* @__PURE__ */ new Set();
|
|
134
|
+
for (let r of e) {
|
|
135
|
+
let e = r[t];
|
|
136
|
+
e != null && n.add(e);
|
|
137
|
+
}
|
|
138
|
+
return [...n];
|
|
139
|
+
}, _ = (e, t, n, r, i) => {
|
|
140
|
+
let a = {
|
|
141
|
+
column: t,
|
|
142
|
+
op: "in",
|
|
143
|
+
value: n
|
|
144
|
+
}, o = r.where === void 0 ? a : { and: [r.where, a] };
|
|
145
|
+
return i({
|
|
146
|
+
table: e.tableName,
|
|
147
|
+
predicate: o,
|
|
148
|
+
order: r.orderBy ?? [],
|
|
149
|
+
take: r.limit,
|
|
150
|
+
skip: r.offset,
|
|
151
|
+
projection: void 0
|
|
152
|
+
});
|
|
153
|
+
}, v = async (e, t, n, r) => {
|
|
154
|
+
t !== void 0 && e.length !== 0 && (await c(e, t, n, r)).forEach((t, n) => {
|
|
155
|
+
let r = e[n];
|
|
156
|
+
for (let [e, n] of Object.entries(t)) e in r || (r[e] = n);
|
|
157
|
+
});
|
|
158
|
+
}, y = (e) => e.eager !== void 0 && Object.keys(e.eager).length > 0;
|
|
159
|
+
//#endregion
|
|
160
|
+
export { o as EagerCardinalityError, c as attachEagerLoads, y as hasEagerLoads, s as oneCardinalityMessage };
|