TanStackT
TanStack2mo ago
2 replies
endless-jade

type signature for query builder instance?

I'm wondering what's the correct pattern here -- I'd like to have a function that applies a bunch of filters to any given query that selects a certain type of collection. for instance

const someFancyFilteringFunctionThatWillBeReused = <
  T extends QueryBuilder<unknown>
>(
  query: T
): T => {
  return query.where(({ item }) => eq(item.active, true));
};

const { data } = useLiveQuery((q) => {
  let query = q
    .from({ item: itemCollection })
    .where(({ item }) => and(eq(item.id, 1)));

  query = someFancyFilteringFunctionThatWillBeReused(query);

  return query.select(({ item }) => ({
    ...item,
  }));
}, []);

data.map((item) => item.active); // item with id 1 and active true


I'd like to know if there's an approach to type that object properly. I can alternatively see the approach of just having a hook that does return a pre-applied query, but I'd like to explore the other option first.

thanks!
Was this page helpful?