--- title: Infinite Query Composable description: 'Vue Composable for infinite lists, fetching data from Supabase.' preview: |- --- ## Installation ### Set up the Supabase client This block requires a Supabase client for Vue. If your project already has one, reuse it and update the block's imports to match your client. Otherwise, install the client: Configure the environment variables described in the [Vue client guide](/library/docs/vue/client#getting-started) before using the block. ### Install the block ## Introduction The Infinite Query Composable provides a single Vue Composable which will make it easier to load data progressively from your Supabase database. It handles data fetching and pagination state, It is meant to be used with infinite lists or tables. The Composable is fully typed, provided you have generated and setup your database types. ## Adding types Before using this composable, we **highly** recommend you setup database types in your project. This will make the composable fully-typesafe. More info about generating TypeScript types from database schema [here](https://supabase.com/docs/guides/api/rest/generating-types). ## Props | Prop | Type | Description | | --------------- | --------------------------------------------------------- | ---------------------------------------------------------------- | | `tableName` | `string` | **Required.** The name of the Supabase table to fetch data from. | | `columns` | `string` | Columns to select from the table. Defaults to `'*'`. | | `pageSize` | `number` | Number of items to fetch per page. Defaults to `20`. | | `trailingQuery` | `(query: SupabaseSelectBuilder) => SupabaseSelectBuilder` | Function to apply filters or sorting to the Supabase query. | ## Return type data, count, isSuccess, isLoading, isFetching, error, hasMore, fetchNextPage | Prop | Type | Description | | --------------- | ------------- | ----------------------------------------------------------------------------------- | | `data` | `TableData[]` | An array of fetched items. | | `count` | `number` | Number of total items in the database. It takes `trailingQuery` into consideration. | | `isSuccess` | `boolean` | It's true if the last API call succeeded. | | `isLoading` | `boolean` | It's true only for the initial fetch. | | `isFetching` | `boolean` | It's true for the initial and all incremental fetches. | | `error` | `any` | The error from the last fetch. | | `hasMore` | `boolean` | Whether the query has finished fetching all items from the database | | `fetchNextPage` | `() => void` | Sends a new request for the next items | ## Type safety The hook will use the typed defined on your Supabase client if they're setup ([more info](https://supabase.com/docs/reference/javascript/typescript-support)). The hook also supports an custom defined result type by using `useInfiniteQuery`. For example, if you have a custom type for `Product`, you can use it like this `useInfiniteQuery`. ## Usage ### With sorting ```vue ``` ### With filtering on search params This example will filter based on a search param like `example.com/?q=hello`. ```vue ``` ## Reusable components ### Infinite list (fetches as you scroll) The following component abstracts the composable into a component. It includes few utility components for no results and end of the list. ```vue ``` Use the `InfiniteList` component with the [Todo List](https://supabase.com/dashboard/project/_/sql/quickstarts) quickstart. Add `` to a page to see it in action. Ensure the [Checkbox](https://ui.shadcn.com/docs/components/checkbox) component from shadcn/ui is installed, and [regenerate/download](https://supabase.com/docs/guides/api/rest/generating-types) types after running the quickstart. ```vue ``` The Todo List table has Row Level Security (RLS) enabled by default. Feel free disable it temporarily while testing. With RLS enabled, you will get an [empty array](https://supabase.com/kb/troubleshooting/why-is-my-select-returning-an-empty-data-array-and-i-have-data-in-the-table-xvOPgx) of results by default. [Read more](https://supabase.com/docs/guides/database/postgres/row-level-security) about RLS. ## Further reading - [Generating TypeScript types from the database](https://supabase.com/docs/reference/javascript/typescript-support) - [Supabase Database API](https://supabase.com/docs/reference/javascript/select) - [Supabase Pagination](https://supabase.com/docs/reference/javascript/select#pagination) - [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)