UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

90 lines (89 loc) • 2.99 kB
import { isExpressionLike, UnionFrom, UnionAll, CollectionRef, QueryRef, IncludesSubquery, ConditionalSelect } from "../ir.js"; import { isRefProxy } from "./ref-proxy-identity.js"; 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 || isExpressionLike(query.select) ? query.select : cloneSelectForPlacement(query.select, clones); return cloned; } function cloneFromForPlacement(from, clones) { if (from.type === `unionFrom`) { return new UnionFrom( from.sources.map((source) => cloneSourceForPlacement(source, clones)) ); } if (from.type === `unionAll`) { return new UnionAll(from.queries.map((query) => cloneQuery(query, clones))); } return cloneSourceForPlacement(from, clones); } function cloneSourceForPlacement(source, clones) { if (source.type === `collectionRef`) { return new CollectionRef(source.collection, source.alias); } return new 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 IncludesSubquery) { const existing = clones.get(value); if (existing) return existing; const cloned = new 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 ConditionalSelect) { const existing = clones.get(value); if (existing) return existing; const cloned = new 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 (isRefProxy(value)) { return value; } return isExpressionLike(value) ? value : cloneSelectForPlacement(value, clones); } export { cloneQueryForPlacement }; //# sourceMappingURL=clone-query.js.map