MCPcopy Index your code
hub / github.com/TanStack/db / fn

Method fn

packages/db/src/query/builder/index.ts:850–935  ·  view source on GitHub ↗

* Functional variants of the query builder * These are imperative function that are called for ery row. * Warning: that these cannot be optimized by the query compiler, and may prevent * some type of optimizations being possible. * @example * ```ts * q.fn.select((row) => ({ *

()

Source from the content-addressed store, hash-verified

848 * ```
849 */
850 get fn() {
851 const builder = this
852 return {
853 /**
854 * Select fields using a function that operates on each row
855 * Warning: This cannot be optimized by the query compiler
856 *
857 * @param callback - A function that receives a row and returns the selected value
858 * @returns A QueryBuilder with functional selection applied
859 *
860 * @example
861 * ```ts
862 * // Functional select (not optimized)
863 * query
864 * .from({ users: usersCollection })
865 * .fn.select(row => ({
866 * name: row.users.name.toUpperCase(),
867 * age: row.users.age + 1,
868 * }))
869 * ```
870 */
871 select<TFuncSelectResult>(
872 callback: (row: TContext[`schema`]) => TFuncSelectResult,
873 ): QueryBuilder<WithResult<TContext, TFuncSelectResult>> {
874 return new BaseQueryBuilder({
875 ...builder.query,
876 select: undefined, // remove the select clause if it exists
877 fnSelect: callback,
878 }) as any
879 },
880 /**
881 * Filter rows using a function that operates on each row
882 * Warning: This cannot be optimized by the query compiler
883 *
884 * @param callback - A function that receives a row and returns a boolean
885 * @returns A QueryBuilder with functional filtering applied
886 *
887 * @example
888 * ```ts
889 * // Functional where (not optimized)
890 * query
891 * .from({ users: usersCollection })
892 * .fn.where(row => row.users.name.startsWith('A'))
893 * ```
894 */
895 where(
896 callback: (row: TContext[`schema`]) => any,
897 ): QueryBuilder<TContext> {
898 return new BaseQueryBuilder({
899 ...builder.query,
900 fnWhere: [
901 ...(builder.query.fnWhere || []),
902 callback as (row: NamespacedRow) => any,
903 ],
904 })
905 },
906 /**
907 * Filter grouped rows using a function that operates on each aggregated row

Calls

no outgoing calls