synclayer_supabase 1.0.0
synclayer_supabase: ^1.0.0 copied to clipboard
Supabase adapter for SyncLayer - enables offline-first sync with Supabase PostgreSQL backend.
synclayer_supabase #
Supabase adapter for SyncLayer - enables offline-first synchronization with Supabase PostgreSQL backend.
Features #
✅ Direct Supabase PostgreSQL integration
✅ Automatic conflict resolution
✅ Delta sync support (partial updates)
✅ Sync filters for multi-tenant apps
✅ Real-time synchronization
✅ Offline-first architecture
✅ Row Level Security (RLS) support
Installation #
Add to your pubspec.yaml:
dependencies:
synclayer: ^1.4.1
synclayer_supabase: ^1.0.0
supabase_flutter: ^2.12.0
Usage #
1. Initialize Supabase #
import 'package:supabase_flutter/supabase_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Supabase.initialize(
url: 'https://your-project.supabase.co',
anonKey: 'your-anon-key',
);
runApp(MyApp());
}
2. Initialize SyncLayer with Supabase #
import 'package:synclayer/synclayer.dart';
import 'package:synclayer_supabase/synclayer_supabase.dart';
await SyncLayer.init(
SyncConfig(
customBackendAdapter: SupabaseAdapter(
client: Supabase.instance.client,
),
collections: ['todos', 'users'],
syncInterval: Duration(minutes: 5),
),
);
3. Use SyncLayer #
// Save (works offline)
await SyncLayer.collection('todos').save({
'text': 'Buy groceries',
'done': false,
});
// Query
final todos = await SyncLayer.collection('todos')
.where('done', isEqualTo: false)
.get();
// Watch for changes
SyncLayer.collection('todos').watch().listen((todos) {
print('Todos updated: ${todos.length}');
});
Database Schema #
Create tables in Supabase with this structure:
CREATE TABLE todos (
record_id VARCHAR(255) PRIMARY KEY,
data JSONB NOT NULL,
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
version INTEGER NOT NULL DEFAULT 1
);
CREATE INDEX idx_todos_updated_at ON todos(updated_at);
Example row:
{
"record_id": "uuid-123",
"data": {
"text": "Buy groceries",
"done": false,
"userId": "user-456"
},
"updated_at": "2024-01-01T12:00:00Z",
"version": 1
}
Multi-Tenant Support #
Use sync filters to isolate user data:
await SyncLayer.init(
SyncConfig(
customBackendAdapter: SupabaseAdapter(
client: Supabase.instance.client,
),
collections: ['todos'],
syncFilters: {
'todos': SyncFilter(
where: {'userId': currentUserId},
),
},
),
);
Row Level Security (RLS) #
Enable RLS in Supabase:
-- Enable RLS
ALTER TABLE todos ENABLE ROW LEVEL SECURITY;
-- Policy: Users can only access their own data
CREATE POLICY "Users can access own data" ON todos
FOR ALL
USING (auth.uid() = (data->>'userId')::uuid);
Real-Time Subscriptions #
Supabase supports real-time updates:
// SyncLayer automatically handles sync
// But you can also use Supabase real-time directly
Supabase.instance.client
.from('todos')
.stream(primaryKey: ['record_id'])
.listen((data) {
print('Real-time update: $data');
});
Learn More #
License #
MIT License - see LICENSE file.