UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

1 lines • 7.55 kB
{"version":3,"file":"clone-query.cjs","names":["isExpressionLike","UnionFrom","UnionAll","CollectionRef","DescriptorRef","QueryRef","IncludesSubquery","ConditionalSelect","isRefProxy"],"sources":["../../../../src/query/builder/clone-query.ts"],"sourcesContent":["import {\n CollectionRef,\n ConditionalSelect,\n DescriptorRef,\n IncludesSubquery,\n QueryRef,\n UnionAll,\n UnionFrom,\n isExpressionLike,\n} from '../ir.js'\nimport { isRefProxy } from './ref-proxy-identity.js'\nimport type { CollectionImpl } from '../../collection/index.js'\nimport type { CollectionOptionsIdentity } from '../../collection-options.js'\nimport type {\n CollectionSourceRef,\n From,\n QueryIR,\n Select,\n SelectValueExpression,\n} from '../ir.js'\n\nexport type CollectionResolver = (\n options: CollectionOptionsIdentity<any, string | number, any, any, any>,\n) => CollectionImpl<any, string | number, any, any, any>\n\ntype CloneContext = {\n clones: WeakMap<object, object>\n resolveDescriptor?: CollectionResolver\n}\n\n/**\n * Gives every query-source placement its own runtime source identities.\n *\n * A reused builder describes the same query meaning, but each FROM, JOIN,\n * UNION, or include placement owns an independent position in the dataflow\n * graph. Expressions are immutable and can remain shared; source refs cannot\n * because their sourceId identifies that lexical position. A supplied\n * resolver also binds any retained descriptors while cloning the plan.\n */\nexport function cloneQueryForPlacement(\n query: QueryIR,\n resolveCollection?: CollectionResolver,\n): QueryIR {\n return cloneQuery(query, {\n clones: new WeakMap(),\n resolveDescriptor: resolveCollection,\n })\n}\n\nfunction cloneQuery(query: QueryIR, context: CloneContext): QueryIR {\n const { clones } = context\n const existing = clones.get(query)\n if (existing) return existing as QueryIR\n\n const cloned: QueryIR = {\n ...query,\n }\n clones.set(query, cloned)\n\n cloned.from = cloneFromForPlacement(query.from, context)\n cloned.join = query.join?.map((join) => ({\n ...join,\n from: cloneSourceForPlacement(join.from, context),\n }))\n cloned.select =\n query.select === undefined || isExpressionLike(query.select)\n ? query.select\n : cloneSelectForPlacement(query.select, context)\n return cloned\n}\n\nfunction cloneFromForPlacement(from: From, context: CloneContext): From {\n if (from.type === `unionFrom`) {\n return new UnionFrom(\n from.sources.map((source) => cloneSourceForPlacement(source, context)),\n )\n }\n\n if (from.type === `unionAll`) {\n return new UnionAll(from.queries.map((query) => cloneQuery(query, context)))\n }\n\n return cloneSourceForPlacement(from, context)\n}\n\nfunction cloneSourceForPlacement(\n source: CollectionSourceRef | QueryRef,\n context: CloneContext,\n): CollectionSourceRef | QueryRef {\n if (source.type === `collectionRef`) {\n return new CollectionRef(source.collection, source.alias, source.bindingId)\n }\n\n if (source.type === `descriptorRef`) {\n return context.resolveDescriptor\n ? new CollectionRef(\n context.resolveDescriptor(source.descriptor),\n source.alias,\n source.bindingId,\n )\n : new DescriptorRef(source.descriptor, source.alias, source.bindingId)\n }\n\n return new QueryRef(\n cloneQuery(source.query, context),\n source.alias,\n source.bindingId,\n )\n}\n\nfunction cloneSelectForPlacement(\n select: Select,\n context: CloneContext,\n): Select {\n const { clones } = context\n const existing = clones.get(select)\n if (existing) return existing as Select\n\n const cloned: Select = {}\n clones.set(select, cloned)\n for (const [field, value] of Object.entries(select)) {\n cloned[field] = cloneSelectValueForPlacement(value, context)\n }\n return cloned\n}\n\nfunction cloneSelectValueForPlacement(\n value: unknown,\n context: CloneContext,\n): SelectValueExpression {\n const { clones } = context\n if (value instanceof IncludesSubquery) {\n const existing = clones.get(value)\n if (existing) return existing as IncludesSubquery\n\n const cloned = new IncludesSubquery(\n cloneQuery(value.query, context),\n value.correlationField,\n value.childCorrelationField,\n value.fieldName,\n value.runtime,\n value.parentFilters,\n value.parentProjection,\n value.materialization,\n value.scalarField,\n )\n clones.set(value, cloned)\n return cloned\n }\n\n if (value instanceof ConditionalSelect) {\n const existing = clones.get(value)\n if (existing) return existing as ConditionalSelect\n\n const cloned = new ConditionalSelect(\n value.branches.map((branch) => ({\n ...branch,\n value: cloneSelectValueForPlacement(branch.value, context),\n })),\n value.defaultValue !== undefined\n ? cloneSelectValueForPlacement(value.defaultValue, context)\n : undefined,\n )\n clones.set(value, cloned)\n return cloned\n }\n\n if (value === null || typeof value !== `object` || Array.isArray(value)) {\n return value as SelectValueExpression\n }\n\n if (isRefProxy(value)) {\n return value as unknown as SelectValueExpression\n }\n\n return isExpressionLike(value)\n ? (value as SelectValueExpression)\n : cloneSelectForPlacement(value as Select, context)\n}\n"],"mappings":";;AAuCA,SAAgB,uBACd,OACA,mBACS;CACT,OAAO,WAAW,OAAO;EACvB,wBAAQ,IAAI,QAAQ;EACpB,mBAAmB;CACrB,CAAC;AACH;AAEA,SAAS,WAAW,OAAgB,SAAgC;CAClE,MAAM,EAAE,WAAW;CACnB,MAAM,WAAW,OAAO,IAAI,KAAK;CACjC,IAAI,UAAU,OAAO;CAErB,MAAM,SAAkB,EACtB,GAAG,MACL;CACA,OAAO,IAAI,OAAO,MAAM;CAExB,OAAO,OAAO,sBAAsB,MAAM,MAAM,OAAO;CACvD,OAAO,OAAO,MAAM,MAAM,KAAK,UAAU;EACvC,GAAG;EACH,MAAM,wBAAwB,KAAK,MAAM,OAAO;CAClD,EAAE;CACF,OAAO,SACL,MAAM,WAAW,KAAA,KAAaA,WAAAA,iBAAiB,MAAM,MAAM,IACvD,MAAM,SACN,wBAAwB,MAAM,QAAQ,OAAO;CACnD,OAAO;AACT;AAEA,SAAS,sBAAsB,MAAY,SAA6B;CACtE,IAAI,KAAK,SAAS,aAChB,OAAO,IAAIC,WAAAA,UACT,KAAK,QAAQ,KAAK,WAAW,wBAAwB,QAAQ,OAAO,CAAC,CACvE;CAGF,IAAI,KAAK,SAAS,YAChB,OAAO,IAAIC,WAAAA,SAAS,KAAK,QAAQ,KAAK,UAAU,WAAW,OAAO,OAAO,CAAC,CAAC;CAG7E,OAAO,wBAAwB,MAAM,OAAO;AAC9C;AAEA,SAAS,wBACP,QACA,SACgC;CAChC,IAAI,OAAO,SAAS,iBAClB,OAAO,IAAIC,WAAAA,cAAc,OAAO,YAAY,OAAO,OAAO,OAAO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