feat: add expression helpers for parsing LoadSubsetOptions in queryFn #786
MergedKyleAMathews(GitHub profile, opens in a new tab)merged 14 commits into
query-driven-sync fromrefs/pull/786/headmerged by samwillis(GitHub profile, opens in a new tab) samwillis(GitHub profile, opens in a new tab) requested changes
packages/db/src/query/expression-helpers.ts lines 67 to 71@@ -0,0 +49,21 @@49+/**50+ * Options for customizing how WHERE expressions are parsed51+ */52+export interface ParseWhereOptions<T = any> {53+ /**54+ * Handler functions for different operators.55+ * Each handler receives the parsed field path(s) and value(s) and returns your custom format.56+ *57+ * Common operators:58+ * - eq: equality (=)59+ * - gt: greater than (>)60+ * - gte: greater than or equal (>=)61+ * - lt: less than (<)62+ * - lte: less than or equal (<=)63+ * - and: logical AND64+ * - or: logical OR65+ * - in: IN clause66+ */67+ handlers: {68+ [operator: string]: (...args: Array<any>) => T69+ }packages/query-db-collection/src/expression-helpers.ts line 38Outdated@@ -0,0 +18,21 @@18+ * lt: (field, value) => ({ [`${field}_lt`]: value }),19+ * and: (filters) => Object.assign({}, ...filters)20+ * })21+ *22+ * // Extract sort information23+ * const sort = parseOrderByExpression(orderBy)24+ *25+ * return api.getProducts({ ...filters, sort, limit })26+ * }27+ * ```28+ */29+30+import type { IR } from "@tanstack/db"31+32+type BasicExpression<T = any> = IR.BasicExpression<T>33+type OrderBy = IR.OrderBy34+35+/**36+ * Represents a simple field path extracted from an expression37+ */38+export type FieldPath = Array<string>packages/db/src/query/expression-helpers.ts line 323@@ -0,0 +280,21 @@280+281+/**282+ * Extracts all simple comparisons from a WHERE expression.283+ * This is useful for simple APIs that only support basic filters.284+ *285+ * Note: This only works for simple AND-ed conditions. Complex OR/nested conditions286+ * will require using parseWhereExpression with custom handlers.287+ *288+ * @param expr - The WHERE expression to parse289+ * @returns Array of simple comparisons290+ *291+ * @example292+ * ```typescript293+ * const comparisons = extractSimpleComparisons(where)294+ * // Returns: [295+ * // { field: ['category'], operator: 'eq', value: 'electronics' },296+ * // { field: ['price'], operator: 'lt', value: 100 }297+ * // ]298+ * ```299+ */300+export function extractSimpleComparisons(packages/query-db-collection/src/expression-helpers.ts lines 273 to 277Outdated@@ -0,0 +257,21 @@257+export function parseOrderByExpression(258+ orderBy: OrderBy | undefined | null259+): Array<ParsedOrderBy> {260+ if (!orderBy || orderBy.length === 0) {261+ return []262+ }263+264+ return orderBy.map((clause: IR.OrderByClause) => {265+ const field = extractFieldPath(clause.expression)266+267+ if (!field) {268+ throw new Error(269+ `ORDER BY expression must be a field reference, got: ${clause.expression.type}`270+ )271+ }272+273+ return {274+ field,275+ direction: clause.compareOptions.direction,276+ nulls: clause.compareOptions.nulls,277+ }samwillis(GitHub profile, opens in a new tab) approved these changes
samwillis(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: d0377c67e2e519218aaaad9418d43585ce26d7a0
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR