How it works
Getting started
Ask the AI to build a realtime app. It handles the full setup — database, sync layer, template, wiring.AI tools used behind the scenes:
create_database { name: "my-db" }— provisions Neon Postgresdatabase_sync_enable { databaseId: "..." }— deploys PowerSync on Fly.iocreate_app { name: "my-app", template: "realtime" }— scaffolds the appconnect_app_to_database { appId: "...", databaseId: "..." }— wires credentials
realtime template includes a PowerSync client, sync handler, and migration runner out of the box.
Template structure
UI (src/)
src/lib/powersync.ts— PowerSync client and connectorsrc/lib/schema.ts— table/column schema (mirrors Postgres)src/main.tsx— wraps the app inPowerSyncContext.Provider
server/)
GET /api/powersync/token— signs Ed25519 JWT for authPOST /api/sync— handles CRUD uploads to Postgres- Migration runner and sync rule updater
Key patterns
Write locally first. Never wait for a server response before updating what the user sees. Write to local SQLite and let the upload queue handle sync.writeTransaction to group related changes. Subscribers see the final state, not partial updates.
Conflict resolution. Last-write-wins by default. The sync handler in POST /api/sync controls how uploads merge with server state — customize it for more complex strategies.
Offline resilience. PowerSync queues writes when offline and syncs them on reconnect. The app doesn’t need to know or care whether it’s online.
Schema updates
- Write a migration SQL file
- Commit — migrations run automatically
- Update
src/lib/schema.tsto match the new columns - PowerSync picks up the change and syncs
Auth flow
PowerSync client callsfetchCredentials() → the process component signs an Ed25519 JWT → PowerSync verifies it against inline JWKS. Include the user ID in the JWT for per-user sync rules.
Environment variables
Set automatically when you connect a database to the app:| Variable | Component | Purpose |
|---|---|---|
VITE_POWERSYNC_URL | UI | PowerSync instance URL |
VITE_POWERSYNC_TOKEN_ENDPOINT | UI | Token endpoint |
VITE_DATABASE_URL | UI | Pooled Postgres URI |
DIRECT_DATABASE_URL | Process | Direct Postgres URI |
POWERSYNC_SIGNING_KEY | Process | Ed25519 private key |
POWERSYNC_API_TOKEN | Process | Admin API auth |
POWERSYNC_URL | Process | Admin API URL |
When to use this
| Good fit | Overkill |
|---|---|
| Task managers, note apps | Static marketing sites |
| Collaborative tools | One-time form submissions |
| Field apps with spotty connectivity | Read-only dashboards |
| Anything where instant feel matters | Simple CRUD with no offline need |