|
| 1 | +# PowerSync + Supabase: Time-Based Sync |
| 2 | + |
| 3 | +This demo shows how to use [PowerSync Sync Streams](https://docs.powersync.com/sync/sync-streams) to dynamically control which data is synced to the client based on a date. The backend contains a set of issues with `created_at` / `updated_at` as **`TIMESTAMPTZ`** in Postgres. Each selected date creates its own Sync Stream subscription with a `date` parameter. Toggling dates on or off adds or removes stream subscriptions and PowerSync syncs the matching issues. TTL is set to 0 so data is removed when dates are deselected. |
| 4 | + |
| 5 | +This lets you model patterns like “sync the last N days of data” or “sync only the time ranges the user cares about” without re-deploying Sync Streams. |
| 6 | + |
| 7 | +The stream definition lives in `powersync/sync-config.yaml`: |
| 8 | + |
| 9 | +```yaml |
| 10 | +streams: |
| 11 | + issues_by_date: |
| 12 | + query: | |
| 13 | + SELECT * FROM issues |
| 14 | + WHERE substring(updated_at, 1, 10) = subscription.parameter('date') |
| 15 | +``` |
| 16 | +
|
| 17 | +Postgres `TIMESTAMPTZ` values are handled like text for the first 10 characters (the `YYYY-MM-DD` prefix) in both the stream query and on the client replica. |
| 18 | + |
| 19 | +The client implementation is in `src/app/views/issues/page.tsx`. It builds an array of stream options from the selected dates and passes them directly to `useQuery` via the `streams` option: |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { useQuery } from '@powersync/react'; |
| 23 | +
|
| 24 | +const streams = selectedDates.map((date) => ({ |
| 25 | + name: 'issues_by_date', |
| 26 | + parameters: { date }, |
| 27 | + ttl: 0 |
| 28 | +})); |
| 29 | +
|
| 30 | +const { data: issues } = useQuery( |
| 31 | + 'SELECT * FROM issues ORDER BY updated_at DESC', |
| 32 | + [], |
| 33 | + { streams } |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +`useQuery` manages the stream subscriptions internally — subscribing to new streams and unsubscribing from removed ones as the array changes. |
| 38 | + |
| 39 | +The demo runs against local Supabase (`supabase start`) and self-hosted PowerSync (via the PowerSync CLI). It uses anonymous Supabase auth — there is no login or registration flow. |
| 40 | + |
| 41 | +## Prerequisites |
| 42 | + |
| 43 | +- [Docker](https://docs.docker.com/get-docker/) (running) |
| 44 | +- [Supabase CLI](https://supabase.com/docs/guides/local-development/cli/getting-started) |
| 45 | +- [PowerSync CLI](https://docs.powersync.com/tools/cli) |
| 46 | + |
| 47 | +## Local development (recommended) |
| 48 | + |
| 49 | +1. Switch into this demo: |
| 50 | + |
| 51 | + ```bash |
| 52 | + cd demos/react-supabase-time-based-sync |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Install dependencies: |
| 56 | + |
| 57 | + ```bash |
| 58 | + pnpm install |
| 59 | + ``` |
| 60 | + |
| 61 | +3. Create env file: |
| 62 | + |
| 63 | + ```bash |
| 64 | + cp .env.local.template .env.local |
| 65 | + ``` |
| 66 | + |
| 67 | + The template already contains the well-known local Supabase anon key, so no manual changes are needed. |
| 68 | + |
| 69 | +4. Start local Supabase + local PowerSync: |
| 70 | + |
| 71 | + > Ensure the [PowerSync CLI](https://docs.powersync.com/tools/cli) is installed before running the following command. |
| 72 | + |
| 73 | + ```bash |
| 74 | + pnpm local:up |
| 75 | + ``` |
| 76 | + |
| 77 | + This does three things: |
| 78 | + - starts Supabase Docker services |
| 79 | + - starts PowerSync using the checked-in `powersync/service.yaml` |
| 80 | + - loads sync streams from `powersync/sync-config.yaml` |
| 81 | + |
| 82 | +5. Start the app: |
| 83 | + |
| 84 | + ```bash |
| 85 | + pnpm dev |
| 86 | + ``` |
| 87 | + |
| 88 | +Open [http://localhost:5173](http://localhost:5173). |
| 89 | + |
| 90 | +## Database setup and seed data |
| 91 | + |
| 92 | +The schema and seed data are in `supabase/migrations/20260312000000_init_issues.sql`. |
| 93 | + |
| 94 | +When Supabase starts for the first time, the migration creates: |
| 95 | + |
| 96 | +- the `issues` table (`created_at` / `updated_at` are `TIMESTAMPTZ`) |
| 97 | +- RLS policies for authenticated users (including anonymous sessions) |
| 98 | +- realtime publication for `issues` |
| 99 | +- sample issues used by the time-based sync filters |
| 100 | + |
| 101 | +Run `supabase db reset` to re-apply migrations from scratch (required if you previously applied this migration when `created_at` / `updated_at` were `TEXT`). |
| 102 | + |
| 103 | +```bash |
| 104 | +supabase db reset |
| 105 | +``` |
| 106 | + |
| 107 | +## Notes |
| 108 | + |
| 109 | +- The app signs in with `signInAnonymously()` automatically in the connector. |
| 110 | +- No login/register routes are used in this demo. |
| 111 | +- To stop local services: |
| 112 | + |
| 113 | + ```bash |
| 114 | + pnpm local:down |
| 115 | + ``` |
| 116 | + |
| 117 | +## Learn More |
| 118 | + |
| 119 | +- [PowerSync CLI docs](https://docs.powersync.com/tools/cli) |
| 120 | +- [PowerSync Sync Streams](https://docs.powersync.com/sync/sync-streams) |
| 121 | +- [Supabase anonymous sign-ins](https://supabase.com/docs/guides/auth/auth-anonymous) |
0 commit comments