UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

385 lines (384 loc) • 19.8 kB
import { isRefProxy } from "./builder/ref-proxy-identity.js"; import { isPlainObject } from "../utils/type-guards.js"; import { UnhashableQueryIRError } from "../errors.js"; import { normalizeValue } from "../utils/comparison.js"; import { toExpression } from "./builder/ref-proxy.js"; import { getQueryIR } from "./builder/query-ir.js"; import { getRuntimeReferenceIdentity, getStringCollationIdentity } from "./runtime-reference-identity.js"; import { getPropRefPropertyPath, getPropRefSourceAlias } from "./ir.js"; function getStableQueryIRHash(query) { return getQueryIdentity(query); } function getStableQueryBuilderHash(query) { return getStableQueryIRHash(getQueryIR(query)); } function getStableValueHash(value, path = `value`) { return JSON.stringify(canonicalizeRuntimeValue(value, path, /* @__PURE__ */ new WeakSet())); } function getQueryIdentity(query) { return JSON.stringify(canonicalizeQueryIR(query)); } function getLoadSubsetDemandKey(options) { if (options.where === void 0 && !options.orderBy?.length && options.limit === void 0 && (options.offset === void 0 || options.offset === 0) && options.cursor === void 0) return; const seen = /* @__PURE__ */ new WeakSet(); const result = { type: `loadSubsetDemand`, query: canonicalizeLoadSubsetQuery(options, `loadSubset`, seen) }; if (options.limit !== void 0) result.limit = canonicalizeRuntimeValue(options.limit, `loadSubset.limit`, seen); if (options.offset !== void 0 && options.offset !== 0) result.offset = canonicalizeRuntimeValue(options.offset, `loadSubset.offset`, seen); if (options.cursor !== void 0) { const cursor = { whereFrom: canonicalizeExpression(options.cursor.whereFrom, `loadSubset.cursor.whereFrom`, seen, `exact-output`), whereCurrent: canonicalizeExpression(options.cursor.whereCurrent, `loadSubset.cursor.whereCurrent`, seen, `exact-output`) }; if (options.cursor.lastKey !== void 0) cursor.lastKey = canonicalizeRuntimeValue(options.cursor.lastKey, `loadSubset.cursor.lastKey`, seen); result.cursor = cursor; } return JSON.stringify(result); } function canonicalizeQueryIR(query) { return canonicalizeQuery(query, `query`, /* @__PURE__ */ new WeakSet()); } function createAliasScope(query, parent) { const bindings = /* @__PURE__ */ new Map(); const bindingIds = /* @__PURE__ */ new Map(); const bindSource = (source) => { if (source.type === `unionFrom`) { source.sources.forEach(bindSource); return; } if (source.type === `unionAll`) return; if (!bindings.has(source.alias)) bindings.set(source.alias, bindings.size); bindingIds.set(source.bindingId, bindings.get(source.alias)); }; bindSource(query.from); query.join?.forEach(({ from }) => bindSource(from)); return { bindings, bindingIds, hasUnqualifiedOutput: query.from.type === `unionAll`, parent }; } function resolveAliasBinding(scope, alias, bindingId) { let current = scope; let parentDistance = 0; while (current) { const binding = bindingId === void 0 ? current.bindings.get(alias) : current.bindingIds.get(bindingId); if (binding !== void 0) return [parentDistance, binding]; if (current.hasUnqualifiedOutput) return void 0; current = current.parent; parentDistance++; } } function canonicalizeQuery(query, path, seen, parentScope) { return canonicalizeQueryInScope(query, path, seen, createAliasScope(query, parentScope)); } function canonicalizeQueryInScope(query, path, seen, scope) { if (query.fnSelect) throw new UnhashableQueryIRError(`${path}.fnSelect`, `function select`); if (query.fnWhere?.length) throw new UnhashableQueryIRError(`${path}.fnWhere`, `function where`); if (query.fnHaving?.length) throw new UnhashableQueryIRError(`${path}.fnHaving`, `function having`); const result = { type: `query`, from: canonicalizeSource(query.from, `${path}.from`, seen, scope) }; if (query.select) result.select = canonicalizeSelect(query.select, `${path}.select`, seen, scope); if (!query.select && (query.from.type === `unionFrom` || query.join !== void 0 || query.groupBy !== void 0)) result.implicitOutput = { type: `namespaced`, aliases: Array.from(scope.bindings.keys()) }; if (query.join) result.join = query.join.map((join, index) => canonicalizeJoin(join, `${path}.join[${index}]`, seen, scope)); if (query.where) result.where = canonicalizeImplicitConjunction(query.where, `${path}.where`, seen, scope); if (query.grouping) result.grouping = true; if (query.groupBy) result.groupBy = query.groupBy.map((expression, index) => canonicalizeExpression(expression, `${path}.groupBy[${index}]`, seen, `exact-output`, scope)); if (query.having) result.having = canonicalizeImplicitConjunction(query.having, `${path}.having`, seen, scope); if (query.orderBy) result.orderBy = query.orderBy.map((orderBy, index) => canonicalizeOrderBy(orderBy, `${path}.orderBy[${index}]`, seen, `ordering-operand`, scope)); if (query.limit !== void 0) result.limit = canonicalizeRuntimeValue(query.limit, `${path}.limit`, seen); if (query.offset !== void 0 && query.offset !== 0) result.offset = canonicalizeRuntimeValue(query.offset, `${path}.offset`, seen); if (query.distinct) result.distinct = true; if (query.singleResult) result.singleResult = true; return result; } function canonicalizeImplicitConjunction(clauses, path, seen, scope) { const canonical = clauses.map((clause, index) => canonicalizeWhere(clause, `${path}[${index}]`, seen, scope)); canonical.sort(compareStableIdentityValues); return canonical.filter((clause, index) => index === 0 || compareStableIdentityValues(clause, canonical[index - 1]) !== 0); } function canonicalizeLoadSubsetQuery(options, path, seen) { const result = { type: `loadSubsetQuery` }; if (options.where !== void 0) result.where = canonicalizeExpression(options.where, `${path}.where`, seen, `exact-output`); if (options.orderBy?.length) result.orderBy = options.orderBy.map((orderBy, index) => canonicalizeOrderBy(orderBy, `${path}.orderBy[${index}]`, seen, `ordering-operand`)); return result; } function canonicalizeJoin(join, path, seen, scope) { return { type: join.type, from: canonicalizeSource(join.from, `${path}.from`, seen, scope), on: canonicalizeExpression(join.on, `${path}.on`, seen, `exact-output`, scope) }; } function canonicalizeSource(source, path, seen, scope) { if (source.type === `collectionRef` || source.type === `descriptorRef`) return { type: `collectionRef`, collectionId: canonicalizeRuntimeValue(source.type === `descriptorRef` ? source.descriptor.id : source.collection.id, `${path}.collection.id`, seen) }; if (source.type === `unionFrom`) return { type: `unionFrom`, sources: source.sources.map((unionSource, index) => canonicalizeSource(unionSource, `${path}.sources[${index}]`, seen, scope)) }; if (source.type === `unionAll`) return { type: `unionAll`, queries: source.queries.map((query, index) => canonicalizeQuery(query, `${path}.queries[${index}]`, seen, scope.parent)) }; return { type: `queryRef`, query: canonicalizeQuery(source.query, `${path}.query`, seen, scope) }; } function canonicalizeSelect(select, path, seen, scope) { return { type: `select`, fields: Object.keys(select).sort().map((key) => [key, canonicalizeSelectValue(select[key], `${path}.${key}`, seen, scope)]) }; } function canonicalizeSelectValue(value, path, seen, scope) { if (isRefProxy(value)) return canonicalizeExpression(toExpression(value), path, seen, `exact-output`, scope); if (isExpression(value)) return canonicalizeExpression(value, path, seen, `exact-output`, scope); if (isPlainObject(value)) return canonicalizeSelect(value, path, seen, scope); return canonicalizeExactOutputRuntimeValue(value, path, seen); } function canonicalizeWhere(where, path, seen, scope) { if (isWhereObject(where)) { const result = { type: `where`, expression: canonicalizeExpression(where.expression, `${path}.expression`, seen, `exact-output`, scope) }; if (where.residual === true) result.residual = true; return result; } return canonicalizeExpression(where, path, seen, `exact-output`, scope); } function canonicalizeOrderBy(orderBy, path, seen, valueContext = `exact-output`, scope) { return { expression: canonicalizeExpression(orderBy.expression, `${path}.expression`, seen, valueContext, scope), compareOptions: canonicalizeCompareOptions(orderBy.compareOptions, `${path}.compareOptions`, seen) }; } function canonicalizeCompareOptions(compareOptions, path, seen) { return canonicalizeRuntimeValue(getStringCollationIdentity(compareOptions), path, seen); } function canonicalizeExpression(expression, path, seen, valueContext = `exact-output`, scope) { if (expression.type === `ref`) { const explicitAlias = getPropRefSourceAlias(expression); if (explicitAlias !== void 0) { const binding2 = resolveAliasBinding(scope, explicitAlias, expression.bindingId); if (binding2 !== void 0) return { type: `ref`, path: [[`binding`, ...binding2], ...getPropRefPropertyPath(expression).map((segment, index) => canonicalizeRuntimeValue(segment, `${path}.path[${index + 1}]`, seen))] }; return { type: `ref`, path: expression.path.map((segment, index) => canonicalizeRuntimeValue(segment, `${path}.path[${index}]`, seen)), sourceAlias: canonicalizeRuntimeValue(explicitAlias, `${path}.sourceAlias`, seen) }; } const binding = resolveAliasBinding(scope, expression.path[0] ?? ``, expression.bindingId); return { type: `ref`, path: binding === void 0 ? expression.path.map((segment, index) => canonicalizeRuntimeValue(segment, `${path}.path[${index}]`, seen)) : [[`binding`, ...binding], ...expression.path.slice(1).map((segment, index) => canonicalizeRuntimeValue(segment, `${path}.path[${index + 1}]`, seen))] }; } if (expression.type === `val`) return { type: `val`, value: valueContext === `equality-operand` ? canonicalizeEqualityRuntimeValue(expression.value, `${path}.value`, seen, scope) : valueContext === `ordering-operand` ? canonicalizeOrderingRuntimeValue(expression.value, `${path}.value`, seen) : canonicalizeExactOutputRuntimeValue(expression.value, `${path}.value`, seen) }; if (expression.type === `func`) { if (expression.name === `in` && expression.args.length === 2 && expression.args[1]?.type === `val` && Array.isArray(expression.args[1].value)) { const candidates = expression.args[1].value.map((value, index) => canonicalizeEqualityRuntimeValue(value, `${path}.args[1].value[${index}]`, seen, scope)); return canonicalizeFunction(expression.name, [canonicalizeExpression(expression.args[0], `${path}.args[0]`, seen, `equality-operand`, scope), { type: `val`, value: [`set`, sortUniqueStableIdentityValues(candidates)] }]); } const operandContext = expression.name === `eq` ? `equality-operand` : expression.name === `gt` || expression.name === `gte` || expression.name === `lt` || expression.name === `lte` ? `ordering-operand` : `exact-output`; const args = expression.args.map((arg, index) => canonicalizeExpression(arg, `${path}.args[${index}]`, seen, operandContext, scope)); return canonicalizeFunction(expression.name, args); } if (expression.type === `agg`) return { type: `agg`, ...expression.grouping ? {} : { withoutOperator: true }, name: expression.name, args: expression.args.map((arg, index) => canonicalizeExpression(arg, `${path}.args[${index}]`, seen, `exact-output`, scope)) }; if (expression.type === `conditionalSelect`) { const result2 = { type: `conditionalSelect`, branches: expression.branches.map((branch, index) => ({ condition: canonicalizeExpression(branch.condition, `${path}.branches[${index}].condition`, seen, `exact-output`, scope), value: canonicalizeSelectValue(branch.value, `${path}.branches[${index}].value`, seen, scope) })) }; if (expression.defaultValue !== void 0) result2.defaultValue = canonicalizeSelectValue(expression.defaultValue, `${path}.defaultValue`, seen, scope); return result2; } const childScope = createAliasScope(expression.query, scope); const result = { type: `includesSubquery`, ...expression.runtime ? {} : { withoutOperator: true }, query: canonicalizeQueryInScope(expression.query, `${path}.query`, seen, childScope), correlationField: canonicalizeExpression(expression.correlationField, `${path}.correlationField`, seen, `equality-operand`, scope), childCorrelationField: canonicalizeExpression(expression.childCorrelationField, `${path}.childCorrelationField`, seen, `equality-operand`, childScope), fieldName: expression.fieldName, materialization: expression.materialization }; if (expression.parentFilters) result.parentFilters = expression.parentFilters.map((where, index) => canonicalizeWhere(where, `${path}.parentFilters[${index}]`, seen, scope)); if (expression.parentProjection) result.parentProjection = expression.parentProjection.map((projection, index) => canonicalizeExpression(projection, `${path}.parentProjection[${index}]`, seen, `exact-output`, scope)); if (expression.scalarField !== void 0) result.scalarField = expression.scalarField; return result; } function canonicalizeFunction(name, args) { if ((name === `and` || name === `or`) && args.length > 0) return { type: `func`, name, args: sortUniqueStableIdentityValues(args.flatMap((arg) => isCanonicalFunction(arg, name) ? arg.args : [arg])) }; if (name === `eq` && args.length === 2) { args.sort(compareStableIdentityValues); return { type: `func`, name, args }; } if ((name === `gt` || name === `gte` || name === `lt` || name === `lte`) && args.length === 2 && compareStableIdentityValues(args[0], args[1]) > 0) return { type: `func`, name: invertComparison(name), args: [args[1], args[0]] }; return { type: `func`, name, args }; } function sortUniqueStableIdentityValues(values) { const keyedValues = values.map((value) => ({ key: JSON.stringify(value), value })); keyedValues.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0); return keyedValues.filter((entry, index) => index === 0 || entry.key !== keyedValues[index - 1].key).map((entry) => entry.value); } function isCanonicalFunction(value, name) { return value !== null && typeof value === `object` && !Array.isArray(value) && value.type === `func` && value.name === name && Array.isArray(value.args); } function invertComparison(name) { switch (name) { case `gt`: return `lt`; case `gte`: return `lte`; case `lt`: return `gt`; case `lte`: return `gte`; } } function canonicalizeRuntimeValue(value, path, seen) { if (value === null) return [`null`]; if (typeof value === `string`) return [`string`, value]; if (typeof value === `boolean`) return [`boolean`, value]; if (typeof value === `number`) { if (Number.isNaN(value)) return [`number`, `NaN`]; if (value === Infinity) return [`number`, `Infinity`]; if (value === -Infinity) return [`number`, `-Infinity`]; if (Object.is(value, -0)) return [`number`, `-0`]; return [`number`, value]; } if (typeof value === `undefined`) return [`undefined`]; if (typeof value === `bigint`) return [`bigint`, value.toString()]; if (typeof value === `function`) throw new UnhashableQueryIRError(path, `function value`); if (typeof value === `symbol`) throw new UnhashableQueryIRError(path, `symbol value`); if (isRefProxy(value)) return canonicalizeExpression(toExpression(value), path, seen); if (Array.isArray(value)) return withCircularGuard(value, path, seen, () => [`array`, value.map((item, index) => canonicalizeRuntimeValue(item, `${path}[${index}]`, seen))]); if (value instanceof Date) { const timestamp = value.getTime(); if (Number.isNaN(timestamp)) throw new UnhashableQueryIRError(path, `invalid Date`); return [`Date`, value.toISOString()]; } if (value instanceof ArrayBuffer) return [ `binary`, `ArrayBuffer`, Array.from(new Uint8Array(value)) ]; if (ArrayBuffer.isView(value)) return [ `binary`, value.constructor.name, Array.from(new Uint8Array(value.buffer, value.byteOffset, value.byteLength)) ]; if (value instanceof Map) return withCircularGuard(value, path, seen, () => { const entries = Array.from(value.entries(), ([key, entryValue], index) => [canonicalizeRuntimeValue(key, `${path}.key[${index}]`, seen), canonicalizeRuntimeValue(entryValue, `${path}.value[${index}]`, seen)]); entries.sort(compareStableIdentityValues); return [`Map`, entries]; }); if (value instanceof Set) return withCircularGuard(value, path, seen, () => { const entries = Array.from(value, (entry, index) => canonicalizeRuntimeValue(entry, `${path}[${index}]`, seen)); entries.sort(compareStableIdentityValues); return [`Set`, entries]; }); if (isPlainObject(value)) return canonicalizeObject(value, path, seen); throw new UnhashableQueryIRError(path, `non-plain object value`); } function canonicalizeExactOutputRuntimeValue(value, path, seen) { if (typeof value === `object` && value !== null || typeof value === `function` || typeof value === `symbol`) return getRuntimeReferenceIdentity(value); return canonicalizeRuntimeValue(value, path, seen); } function canonicalizeEqualityRuntimeValue(value, path, seen, scope) { if (isRefProxy(value)) return canonicalizeExpression(toExpression(value), path, seen, `equality-operand`, scope); if (typeof value === `number` && Object.is(value, -0)) return canonicalizeRuntimeValue(0, path, seen); if (typeof Buffer !== `undefined` && value instanceof Buffer || value instanceof Uint8Array) return [ `binary`, `Uint8Array`, Array.from(value) ]; const normalized = normalizeValue(value); if (normalized !== value) return canonicalizeRuntimeValue(normalized, path, seen); if (typeof value === `object` && value !== null || typeof value === `function` || typeof value === `symbol`) return getRuntimeReferenceIdentity(value); return canonicalizeRuntimeValue(value, path, seen); } function canonicalizeOrderingRuntimeValue(value, path, seen) { if (typeof value === `number` && Object.is(value, -0)) return canonicalizeRuntimeValue(0, path, seen); if (value instanceof Date && Number.isNaN(value.getTime())) return canonicalizeRuntimeValue(NaN, path, seen); const normalized = normalizeValue(value); if (normalized !== value && !(value instanceof Uint8Array)) return canonicalizeRuntimeValue(normalized, path, seen); try { return canonicalizeRuntimeValue(value, path, seen); } catch (error) { if (error instanceof UnhashableQueryIRError && typeof value === `object` && value !== null) return getRuntimeReferenceIdentity(value); throw error; } } function compareStableIdentityValues(left, right) { const serializedLeft = JSON.stringify(left); const serializedRight = JSON.stringify(right); return serializedLeft < serializedRight ? -1 : serializedLeft > serializedRight ? 1 : 0; } function canonicalizeObject(value, path, seen) { return withCircularGuard(value, path, seen, () => [`object`, Object.keys(value).sort().map((key) => [key, canonicalizeRuntimeValue(value[key], `${path}.${key}`, seen)])]); } function withCircularGuard(value, path, seen, callback) { if (seen.has(value)) throw new UnhashableQueryIRError(path, `circular value`); seen.add(value); try { return callback(); } finally { seen.delete(value); } } function isWhereObject(where) { return `expression` in where; } function isExpression(value) { if (value === null || typeof value !== `object`) return false; const expressionType = value.type; return expressionType === `agg` || expressionType === `conditionalSelect` || expressionType === `func` || expressionType === `ref` || expressionType === `val` || expressionType === `includesSubquery`; } export { UnhashableQueryIRError, canonicalizeQueryIR, getLoadSubsetDemandKey, getQueryIdentity, getStableQueryBuilderHash, getStableQueryIRHash, getStableValueHash }; //# sourceMappingURL=ir-stable-identity.js.map