Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const todos = createCollection(
| `realtime` | `boolean` | No | When `true`, subscribes to Postgres changes and reconciles inserts, updates, and deletes into the collection. Defaults to `false`. |
| `realtimeUseFilter` | `boolean` | No | **Experimental.** Only applies when `realtime` is `true`. When `true`, each active query's `WHERE` clause is pushed to the Realtime subscription as a `postgres_changes` filter, so the channel only receives changes those queries care about. Defaults to `false`, which subscribes to every change on the table and filters client-side — simpler, at the cost of more Realtime traffic. Queries whose `WHERE` cannot be expressed as a Realtime filter (e.g. `or(...)`) transparently fall back to the unfiltered subscription. |
| `queryClient` | `QueryClient` | No | TanStack Query client. If omitted, a shared global client is used. |
| `defaultStringCollation` | `StringCollationConfig` | No | Controls how string columns are compared when ordering, matching the database's collation settings. Defaults to `{ stringSort: 'lexical' }` (byte-order comparison). Pass `{ stringSort: 'locale', locale, localeOptions }` for locale-aware sorting. |

**Returns** a collection options object to pass to `createCollection`.

Expand Down
14 changes: 13 additions & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* biome-ignore-all lint/suspicious/noExplicitAny: collection items are typed by the caller's schema, not statically known here */
import type { StandardSchemaV1 } from "@standard-schema/spec"
import type { SupabaseClient } from "@supabase/supabase-js"
import { BasicIndex, type Collection } from "@tanstack/db"
import {
BasicIndex,
type Collection,
type StringCollationConfig,
} from "@tanstack/db"
import type { QueryClient } from "@tanstack/query-core"
import { queryCollectionOptions } from "@tanstack/query-db-collection"
import {
Expand All @@ -15,6 +19,12 @@ import { getQueryClient } from "./query-client"
import { syncTableSubscription, type TableEntry } from "./realtime"

interface SupabaseCollectionOptions<TSchema extends StandardSchemaV1> {
/**
* Controls how string columns are compared when ordering, matching the
* database's collation settings. Defaults to `{ stringSort: 'lexical' }`
* (byte-order comparison).
*/
defaultStringCollation?: StringCollationConfig
/**
* The columns that uniquely identify a row. Used to extract the key for
* storing the item in the collection and to build the where clause for
Expand Down Expand Up @@ -97,6 +107,7 @@ export const supabaseCollectionOptions = <TSchema extends StandardSchemaV1>({
supabase,
realtime,
realtimeUseFilter = false,
defaultStringCollation = { stringSort: `lexical` },
}: SupabaseCollectionOptions<TSchema>) => {
// if the query client is not provided, use the global query client
queryClient = queryClient ?? getQueryClient()
Expand Down Expand Up @@ -139,6 +150,7 @@ export const supabaseCollectionOptions = <TSchema extends StandardSchemaV1>({
onDelete: (ctx) => supabaseOnDelete(supabase, tableName, keyColumns, ctx),
autoIndex: "eager",
defaultIndexType: BasicIndex,
defaultStringCollation,
})

const originalSync = config.sync.sync
Expand Down
Loading