TanStackT
TanStack4w ago
2 replies
sad-indigo

How to make distinct/groupby server side ?

Hello folks, is there a way to make distinct/groupby queries server side ?
Deno.test({
  name: "distinct - verify it's a client-side operation (sync collection)",
  sanitizeOps: false,
  sanitizeResources: false,
  fn: async () => {
    const testData = [
      { key: "1", data: { status: "active" } },
      { key: "2", data: { status: "active" } },
      { key: "3", data: { status: "inactive" } },
      { key: "4", data: { status: "active" } },
      { key: "5", data: { status: "pending" } },
    ];
    const collection = createCollection(
      queryCollectionOptions<{ key: string; data: { status: string } }>({
        id: "test-client-distinct",
        queryKey: ["test-client-distinct"],
        queryClient: queryClient,
        startSync: true,
        getKey: (row) => row.key,
        syncMode: "on-demand",
        queryFn: async (ctx) => {
          const options = parseLoadSubsetOptions(ctx.meta?.loadSubsetOptions);
          console.log(options); // I want to have distinct server side
          return testData;
        },
      }),
    );
    const { result } = renderHook(() =>
      useLiveSuspenseQuery(
        (q) =>
          q
            .from({ row: collection })
            .select(({ row }) => ({
              status: row?.data?.status,
            }))
            .distinct(),
      )
    );
    await waitFor(() => {
      assertEquals(result.current.data.length, 3);
    });
  },
});
Was this page helpful?