@tanstack/db
Version:
A reactive client store for building super fast apps on sync
329 lines (328 loc) • 11.6 kB
JavaScript
const require_runtime = require("../_virtual/_rolldown/runtime.cjs");
const require_error_message = require("../error-message.cjs");
const require_ref_proxy_identity = require("./builder/ref-proxy-identity.cjs");
var ir_exports = /* @__PURE__ */ require_runtime.__exportAll({
Aggregate: () => Aggregate,
CollectionRef: () => CollectionRef,
ConditionalSelect: () => ConditionalSelect,
DescriptorRef: () => DescriptorRef,
Func: () => Func,
INCLUDES_SCALAR_FIELD: () => INCLUDES_SCALAR_FIELD,
IncludesSubquery: () => IncludesSubquery,
PropRef: () => PropRef,
QueryRef: () => QueryRef,
UnionAll: () => UnionAll,
UnionFrom: () => UnionFrom,
Value: () => Value,
collectCollectionSources: () => collectCollectionSources,
collectPropRefs: () => collectPropRefs,
collectSourceRefs: () => collectSourceRefs,
createResidualWhere: () => createResidualWhere,
followRef: () => followRef,
getFromSources: () => getFromSources,
getHavingExpression: () => getHavingExpression,
getPropRefPropertyPath: () => getPropRefPropertyPath,
getPropRefSourceAlias: () => getPropRefSourceAlias,
getWhereExpression: () => getWhereExpression,
isBasicOrAggregateExpression: () => isBasicOrAggregateExpression,
isExpressionLike: () => isExpressionLike,
isResidualWhere: () => isResidualWhere,
requireCollectionSource: () => requireCollectionSource
});
var INCLUDES_SCALAR_FIELD = `__includes_scalar__`;
var nextCollectionSourceId = 0;
var nextLexicalBindingId = 0;
var BaseExpression = class {};
var CollectionRef = class extends BaseExpression {
#sourceId;
#bindingId;
constructor(collection, alias, bindingId) {
super();
this.type = `collectionRef`;
this.#sourceId = `source-${++nextCollectionSourceId}`;
this.collection = collection;
this.alias = alias;
this.#bindingId = bindingId ?? `binding-${++nextLexicalBindingId}`;
}
/** Opaque runtime identity; aliases are lexical names only. */
get sourceId() {
return this.#sourceId;
}
get bindingId() {
return this.#bindingId;
}
};
var DescriptorRef = class extends BaseExpression {
#sourceId;
#bindingId;
constructor(descriptor, alias, bindingId) {
super();
this.descriptor = descriptor;
this.alias = alias;
this.type = `descriptorRef`;
this.#sourceId = `source-${++nextCollectionSourceId}`;
this.#bindingId = bindingId ?? `binding-${++nextLexicalBindingId}`;
}
/** Opaque runtime identity for this unbound source placement. */
get sourceId() {
return this.#sourceId;
}
get bindingId() {
return this.#bindingId;
}
};
function requireCollectionSource(source) {
if (source.type === `descriptorRef`) throw new Error(/* @__PURE__ */ require_error_message.devBuild() && process.env.NODE_ENV !== `production` ? `Collection descriptor "${source.alias}" requires a DbClient when the query is consumed. Bind the query through a client-aware API or use a concrete Collection.` : require_error_message.codedMessage(113, { alias: source.alias }));
return source.collection;
}
var QueryRef = class extends BaseExpression {
#bindingId;
constructor(query, alias, bindingId) {
super();
this.query = query;
this.alias = alias;
this.type = `queryRef`;
this.#bindingId = bindingId ?? `binding-${++nextLexicalBindingId}`;
}
get bindingId() {
return this.#bindingId;
}
};
var UnionFrom = class extends BaseExpression {
constructor(sources) {
super();
this.sources = sources;
this.type = `unionFrom`;
}
get alias() {
return this.sources[0]?.alias ?? ``;
}
};
var UnionAll = class extends BaseExpression {
/**
* Result-level UNION ALL. Downstream query clauses see the union result row
* shape, not the branch source aliases. Optimizers may push safe operations
* into branches, but compiler phases should treat this as a derived relation
* unless they are explicitly handling branch lowering.
*/
constructor(queries) {
super();
this.queries = queries;
this.type = `unionAll`;
}
get alias() {
return ``;
}
};
var PropRef = class extends BaseExpression {
constructor(path, sourceAlias, bindingId) {
super();
this.path = path;
this.type = `ref`;
if (sourceAlias !== void 0) this.sourceAlias = sourceAlias;
if (bindingId !== void 0) Object.defineProperty(this, `bindingId`, {
value: bindingId,
enumerable: false
});
}
};
function getPropRefSourceAlias(ref) {
return ref.sourceAlias !== void 0 && ref.path[0] === ref.sourceAlias ? ref.sourceAlias : void 0;
}
function getPropRefPropertyPath(ref) {
return getPropRefSourceAlias(ref) === void 0 ? ref.path : ref.path.slice(1);
}
var Value = class extends BaseExpression {
constructor(value) {
super();
this.value = value;
this.type = `val`;
}
};
var Func = class extends BaseExpression {
constructor(name, args) {
super();
this.name = name;
this.args = args;
this.type = `func`;
}
};
var Aggregate = class extends BaseExpression {
constructor(name, args, grouping) {
super();
this.name = name;
this.args = args;
this.grouping = grouping;
this.type = `agg`;
}
};
function collectPropRefs(expression) {
if (expression.type === `ref`) return [expression];
return expression.type === `func` || expression.type === `agg` ? expression.args.flatMap(collectPropRefs) : [];
}
var IncludesSubquery = class extends BaseExpression {
constructor(query, correlationField, childCorrelationField, fieldName, runtime, parentFilters, parentProjection, materialization = `collection`, scalarField) {
super();
this.query = query;
this.correlationField = correlationField;
this.childCorrelationField = childCorrelationField;
this.fieldName = fieldName;
this.runtime = runtime;
this.parentFilters = parentFilters;
this.parentProjection = parentProjection;
this.materialization = materialization;
this.scalarField = scalarField;
this.type = `includesSubquery`;
}
};
var ConditionalSelect = class extends BaseExpression {
constructor(branches, defaultValue) {
super();
this.branches = branches;
this.defaultValue = defaultValue;
this.type = `conditionalSelect`;
}
};
function isBasicOrAggregateExpression(value) {
return value instanceof Aggregate || value instanceof Func || value instanceof PropRef || value instanceof Value;
}
function isExpressionLike(value) {
return isBasicOrAggregateExpression(value) || value instanceof ConditionalSelect || value instanceof IncludesSubquery;
}
function collectSourceRefs(query, includeSourceIds) {
const sources = [];
const seen = /* @__PURE__ */ new Set();
let includeDepth = 0;
const visitSource = (source) => {
if (source.type === `collectionRef` || source.type === `descriptorRef`) {
if (!seen.has(source.sourceId)) {
seen.add(source.sourceId);
sources.push(source);
if (includeDepth > 0) includeSourceIds?.add(source.sourceId);
}
} else if (source.type === `queryRef`) visitQuery(source.query);
else if (source.type === `unionFrom`) source.sources.forEach(visitSource);
else source.queries.forEach(visitQuery);
};
const visitSelectValue = (value) => {
if (value instanceof IncludesSubquery) {
includeDepth++;
visitQuery(value.query);
includeDepth--;
} else if (value instanceof ConditionalSelect) {
value.branches.forEach((branch) => visitSelectValue(branch.value));
if (value.defaultValue !== void 0) visitSelectValue(value.defaultValue);
} else if (value !== null && typeof value === `object` && !Array.isArray(value) && !isExpressionLike(value) && !require_ref_proxy_identity.isRefProxy(value)) Object.values(value).forEach(visitSelectValue);
};
const visitQuery = (current) => {
visitSource(current.from);
current.join?.forEach(({ from }) => visitSource(from));
if (current.select) Object.values(current.select).forEach(visitSelectValue);
};
visitQuery(query);
return sources;
}
function collectCollectionSources(query) {
return collectSourceRefs(query).map((source) => source.type === `descriptorRef` ? requireCollectionSource(source) : source);
}
function getWhereExpression(where) {
return typeof where === `object` && `expression` in where ? where.expression : where;
}
function getHavingExpression(having) {
return typeof having === `object` && `expression` in having ? having.expression : having;
}
function isResidualWhere(where) {
return typeof where === `object` && `expression` in where && where.residual === true;
}
function createResidualWhere(expression) {
return {
expression,
residual: true
};
}
function getFromSources(from) {
if (from.type === `unionFrom`) return from.sources;
if (from.type === `unionAll`) return [];
return [from];
}
function getRefFromAlias(query, alias, bindingId) {
for (const source of getFromSources(query.from)) if (source.alias === alias && (bindingId === void 0 || source.bindingId === bindingId)) return source;
for (const join of query.join || []) if (join.from.alias === alias && (bindingId === void 0 || join.from.bindingId === bindingId)) return join.from;
}
function followRef(query, ref, collection) {
const explicitAlias = getPropRefSourceAlias(ref);
if (explicitAlias !== void 0) {
const aliasRef = getRefFromAlias(query, explicitAlias, ref.bindingId);
if (!aliasRef) return;
const propertyPath = getPropRefPropertyPath(ref);
if (aliasRef.type === `queryRef`) return followRef(aliasRef.query, new PropRef(propertyPath), collection);
return {
collection: requireCollectionSource(aliasRef),
path: propertyPath,
alias: explicitAlias,
sourceId: aliasRef.sourceId
};
}
if (ref.path.length === 0) return;
if (ref.path.length === 1) {
const field = ref.path[0];
if (query.select) {
const selectedField = query.select[field];
if (selectedField) return selectedField.type === `ref` ? followRef(query, selectedField, collection) : void 0;
}
const from = getFromSources(query.from)[0];
if (from?.type === `queryRef`) return followRef(from.query, new PropRef([field]), collection);
if (from?.type !== `collectionRef`) return;
return {
collection,
path: [field],
alias: from.alias,
sourceId: from.sourceId
};
}
if (ref.path.length > 1) {
const [alias, ...rest] = ref.path;
const aliasRef = getRefFromAlias(query, alias, ref.bindingId);
if (!aliasRef) return;
if (aliasRef.type === `queryRef`) return followRef(aliasRef.query, new PropRef(rest), collection);
else return {
collection: requireCollectionSource(aliasRef),
path: rest,
alias,
sourceId: aliasRef.sourceId
};
}
}
exports.Aggregate = Aggregate;
exports.CollectionRef = CollectionRef;
exports.ConditionalSelect = ConditionalSelect;
exports.DescriptorRef = DescriptorRef;
exports.Func = Func;
exports.INCLUDES_SCALAR_FIELD = INCLUDES_SCALAR_FIELD;
exports.IncludesSubquery = IncludesSubquery;
exports.PropRef = PropRef;
exports.QueryRef = QueryRef;
exports.UnionAll = UnionAll;
exports.UnionFrom = UnionFrom;
exports.Value = Value;
exports.collectCollectionSources = collectCollectionSources;
exports.collectPropRefs = collectPropRefs;
exports.collectSourceRefs = collectSourceRefs;
exports.createResidualWhere = createResidualWhere;
exports.followRef = followRef;
exports.getFromSources = getFromSources;
exports.getHavingExpression = getHavingExpression;
exports.getPropRefPropertyPath = getPropRefPropertyPath;
exports.getPropRefSourceAlias = getPropRefSourceAlias;
exports.getWhereExpression = getWhereExpression;
Object.defineProperty(exports, "ir_exports", {
enumerable: true,
get: function() {
return ir_exports;
}
});
exports.isBasicOrAggregateExpression = isBasicOrAggregateExpression;
exports.isExpressionLike = isExpressionLike;
exports.isResidualWhere = isResidualWhere;
exports.requireCollectionSource = requireCollectionSource;
//# sourceMappingURL=ir.cjs.map