What if your app worked offline and synced when connected? Electric SQL makes local-first development practical.
What is Electric SQL?
Electric SQL syncs data between PostgreSQL in the cloud and SQLite on the client. Your app reads and writes locally (instant), and Electric handles the sync.
Why Local-First?
- Instant responses — reads and writes hit local SQLite, not a network round-trip
- Works offline — app functions without internet
- Multi-device sync — changes propagate to all devices
- Conflict resolution — automatic, based on CRDTs
Quick Start
bun add @electric-sql/client
Syncing Data
import { ShapeStream, Shape } from '@electric-sql/client';
// Define a shape (what data to sync)
const stream = new ShapeStream({
url: 'http://localhost:3000/v1/shape',
params: {
table: 'tasks',
where: 'user_id = 123',
},
});
const shape = new Shape(stream);
// Subscribe to changes
shape.subscribe((data) => {
console.log('Tasks updated:', data);
// data is always the latest synced state
});
// Get current value
const currentTasks = shape.value;
With React
import { useShape } from '@electric-sql/react';
function TaskList({ userId }) {
const { data: tasks, isLoading } = useShape({
url: 'http://localhost:3000/v1/shape',
params: {
table: 'tasks',
where: `user_id = '${userId}'`,
},
});
if (isLoading) return <p>Loading...</p>;
return (
<ul>
{tasks.map(task => (
<li key={task.id}>
{task.completed ? 'Done' : 'Todo'}: {task.text}
</li>
))}
</ul>
);
}
How Sync Works
Client (SQLite) <----> Electric <----> PostgreSQL
1. Client writes to local SQLite (instant)
2. Electric detects the change
3. Electric syncs to PostgreSQL
4. PostgreSQL change is pushed to all other clients
5. Conflicts resolved automatically via CRDTs
Use Cases
- Note-taking apps — write notes offline, sync later
- Project management — update tasks on the go
- Field data collection — collect data without internet
- Collaborative editing — multiple users, no conflicts
- Mobile apps — seamless online/offline experience
Electric SQL vs Alternatives
| Feature | Electric SQL | Firebase | Convex | CRDTs (Yjs) |
|---|---|---|---|---|
| Local-first | Yes | No | No | Yes |
| Offline | Full | Partial | No | Yes |
| Database | PostgreSQL + SQLite | Firestore | Custom | Custom |
| Conflict Resolution | CRDT | Last-write-wins | ACID | CRDT |
| Sync | Automatic | Automatic | Automatic | Manual |
| Schema | PostgreSQL DDL | Schemaless | TypeScript | Custom |
Need offline-ready data sources? Check out my Apify actors — extract data once, use it offline forever. For custom solutions, email spinov001@gmail.com.
Top comments (0)