feat: implement data replication plugin for external database sync
Closes #72 /claim #72
Purpose
Implement a comprehensive data replication plugin for StarbaseDB that enables users to pull data from external databases (PostgreSQL, MySQL, etc.) into the internal SQLite database with configurable sync intervals and incremental updates.
This addresses the request for creating a "pull mechanism" to replicate data from external sources like Supabase PostgreSQL into StarbaseDB's edge-close SQLite replica.
https://github.com/user-attachments/assets/b44e78c6-0e76-4cde-a489-e4ab87e23a96
Tasks
- [x] Create DataReplicationPlugin following StarbaseDB plugin architecture
- [x] Implement external database connection support (PostgreSQL, MySQL, Hyperdrive)
- [x] Add incremental sync mechanism using tracking columns (id, created_at, updated_at)
- [x] Create configurable sync intervals per replication configuration
- [x] Build comprehensive REST API for replication management
- [x] Add real-time logging and monitoring system
- [x] Implement event callback system for external integrations
- [x] Create internal database tables for configuration and logs storage
- [x] Add proper error handling and graceful degradation
- [x] Write unit tests and comprehensive documentation
- [x] Register plugin in main application with event handling
- [x] Deploy and test on live Cloudflare Workers environment
Verify
๐ Code Review Checklist
- Plugin Architecture: Verify adherence to StarbaseDB plugin patterns in
plugins/data-replication/index.ts - External Database Security: Review credential handling and SQL injection prevention
- API Endpoints: Test all 7 REST endpoints with proper authentication
- TypeScript Types: Validate type safety and interface compliance
- Error Handling: Ensure graceful degradation and comprehensive logging
๐งช Testing Steps
- Build & Deploy:
npm run build && npx wrangler deploy - Configure Replication:
curl -X POST https://starbasedb.mavrickrishi.workers.dev/data-replication/configure \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ABC123" \ -d '{"name":"test","sourceConfig":{"dialect":"postgresql","host":"demo.supabase.co","port":5432,"user":"postgres","password":"demo-pass","database":"postgres"},"targetTable":"users","sourceTable":"users","syncIntervalMinutes":30,"trackingColumn":"updated_at"}' - Check Status:
curl -X GET https://starbasedb.mavrickrishi.workers.dev/data-replication/status -H "Authorization: Bearer ABC123" - Monitor Logs: View Cloudflare Workers dashboard for real-time sync events
๐ Performance Verification
- Memory: Plugin reuses existing connection infrastructure (minimal overhead)
- Scalability: Supports multiple concurrent replication configurations
- Efficiency: Incremental sync with WHERE clause filtering
Before
GitHub Issue #72 Request:
- No data replication functionality
- No external database sync capability
- Manual data management required
- No edge-close replica creation
StarbaseDB Plugin Ecosystem:
plugins/
โโโ cdc/ # Change Data Capture
โโโ cron/ # Scheduled tasks
โโโ query-log/ # Query logging
โโโ stats/ # System statistics
โโโ [missing data replication]
After
โ Complete Data Replication System:
๐ New Plugin Added
plugins/data-replication/
โโโ index.ts # Main plugin (475+ lines)
โโโ index.test.ts # Unit tests (3 passing)
โโโ meta.json # Plugin metadata
โโโ README.md # Comprehensive docs
๐ Live Deployment
- URL: https://starbasedb.mavrickrishi.workers.dev
- Status: โ Successfully deployed and tested
- Plugin: โ Registered and running
๐ก 7 New API Endpoints
POST /data-replication/configure- Set up replicationPOST /data-replication/start/:name- Start specific replicationPOST /data-replication/stop/:name- Stop specific replicationPOST /data-replication/sync/:name- Manual sync triggerGET /data-replication/status- View all configurationsGET /data-replication/logs- Sync operation logsDELETE /data-replication/configure/:name- Remove config
๐พ Database Schema
Two new internal tables created:
tmp_replication_configs- Store replication settingstmp_replication_logs- Track sync operations and performance
๐ Working Demo
# โ
TESTED: Configuration successful
{"result":{"success":true,"message":"Replication configured successfully"}}
# โ
TESTED: Status shows active replication
{"result":{"configs":[{"name":"supabase-users","is_running":true,"sync_interval_minutes":30}]}}
๐ฏ Issue Requirements Met
โ
Pull mechanism for external databases
โ
Configurable sync intervals
โ
Table-specific replication
โ
Incremental updates via tracking columns
โ
Supabase PostgreSQL support
โ
Edge-close SQLite replica creation
โ
wrangler.toml configuration support
cc @Brayden