@tanstack/db
Version:
A reactive client store for building super fast apps on sync
90 lines (89 loc) • 3.06 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ir = require("../ir.cjs");
const refProxyIdentity = require("./ref-proxy-identity.cjs");
function cloneQueryForPlacement(query) {
return cloneQuery(query, /* @__PURE__ */ new WeakMap());
}
function cloneQuery(query, clones) {
const existing = clones.get(query);
if (existing) return existing;
const cloned = {
...query
};
clones.set(query, cloned);
cloned.from = cloneFromForPlacement(query.from, clones);
cloned.join = query.join?.map((join) => ({
...join,
from: cloneSourceForPlacement(join.from, clones)
}));
cloned.select = query.select === void 0 || ir.isExpressionLike(query.select) ? query.select : cloneSelectForPlacement(query.select, clones);
return cloned;
}
function cloneFromForPlacement(from, clones) {
if (from.type === `unionFrom`) {
return new ir.UnionFrom(
from.sources.map((source) => cloneSourceForPlacement(source, clones))
);
}
if (from.type === `unionAll`) {
return new ir.UnionAll(from.queries.map((query) => cloneQuery(query, clones)));
}
return cloneSourceForPlacement(from, clones);
}
function cloneSourceForPlacement(source, clones) {
if (source.type === `collectionRef`) {
return new ir.CollectionRef(source.collection, source.alias);
}
return new ir.QueryRef(cloneQuery(source.query, clones), source.alias);
}
function cloneSelectForPlacement(select, clones) {
const existing = clones.get(select);
if (existing) return existing;
const cloned = {};
clones.set(select, cloned);
for (const [field, value] of Object.entries(select)) {
cloned[field] = cloneSelectValueForPlacement(value, clones);
}
return cloned;
}
function cloneSelectValueForPlacement(value, clones) {
if (value instanceof ir.IncludesSubquery) {
const existing = clones.get(value);
if (existing) return existing;
const cloned = new ir.IncludesSubquery(
cloneQuery(value.query, clones),
value.correlationField,
value.childCorrelationField,
value.fieldName,
value.parentFilters,
value.parentProjection,
value.materialization,
value.scalarField
);
clones.set(value, cloned);
return cloned;
}
if (value instanceof ir.ConditionalSelect) {
const existing = clones.get(value);
if (existing) return existing;
const cloned = new ir.ConditionalSelect(
value.branches.map((branch) => ({
...branch,
value: cloneSelectValueForPlacement(branch.value, clones)
})),
value.defaultValue !== void 0 ? cloneSelectValueForPlacement(value.defaultValue, clones) : void 0
);
clones.set(value, cloned);
return cloned;
}
if (value === null || typeof value !== `object` || Array.isArray(value)) {
return value;
}
if (refProxyIdentity.isRefProxy(value)) {
return value;
}
return ir.isExpressionLike(value) ? value : cloneSelectForPlacement(value, clones);
}
exports.cloneQueryForPlacement = cloneQueryForPlacement;
//# sourceMappingURL=clone-query.cjs.map