Skip to content

feat(plugin): DataReplicationPlugin with Adapter Pattern for external database replication (#72) - #314

Open
Saik2789 wants to merge 1 commit into
outerbase:mainfrom
Saik2789:feat/data-replication-plugin-72
Open

Saik2789 wants to merge 1 commit into
outerbase:mainfrom
Saik2789:feat/data-replication-plugin-72

Conversation

@Saik2789

Copy link
Copy Markdown

Resolves #72.
@algora-pbc /claim #72

Summary

Implements DataReplicationPlugin to pull data from external data sources (Postgres, MySQL, Turso, D1, StarbaseDB) into StarbaseDB's internal Durable Object SQLite database, turning the instance into a close-to-edge replica.

This implementation directly addresses CTO @Brayden's architectural feedback from PR #75:

  1. Adapter Pattern: Abstract base class SyncAdapter with dialect-specific adapters (PostgresSyncAdapter, MySQLSyncAdapter, GenericSyncAdapter) isolating schema introspection and query syntax.
  2. Configurable Cursor-Based Incremental Sync: Supports configurable cursor columns (id, created_at, or custom columns) and cursor types (integer, timestamp, string) so only newer rows are fetched on subsequent pulls.
  3. Automatic Schema Creation: Automatically introspects external source tables on first sync and creates corresponding SQLite tables with correct column affinities and PRIMARY KEY on the cursor column for idempotent INSERT OR REPLACE upserts.
  4. Schema-Qualified Table Resolution: Handles PostgreSQL schemas (e.g. public.users -> public_users), with an automated beforeQuery hook that seamlessly resolves public.users queries to internal SQLite tables.
  5. Admin REST API: Admin-only REST endpoints (admin token required):
    • GET /replication/status — State and checkpoint status of all tables
    • GET /replication/status/:table — State of a single table
    • POST /replication/sync — Trigger sync for all tables (202 Accepted)
    • POST /replication/sync/:table — Trigger sync for a specific table (202 Accepted)
    • DELETE /replication/state/:table — Reset checkpoint to re-sync from scratch (200 OK)
    • GET /replication/history — Audit log of the last 50 replication runs
    • GET /replication/history/:table — Audit log for a specific table
  6. Isolated Table Recovery & DO Yielding: Table failures are recorded in tmp_replication_state and tmp_replication_history without aborting other tables. Sync yields the DO event loop every 100 rows to prevent starvation of concurrent requests.
  7. CronPlugin Integration: Integrates directly with CronPlugin for scheduled synchronization.

Test Plan

  • pnpm vitest run plugins/replication passes with 34 tests covering:
    • Postgres, MySQL, and Generic schema introspection and type mapping (jsonb -> TEXT, bytea -> BLOB, integer, real).
    • Initial sync vs incremental cursor queries.
    • Auto table creation with primary key.
    • Conflict strategy (replace vs ignore).
    • Table name resolution (public.users -> public_users) in adapter and beforeQuery hook.
    • Admin endpoint authorization (401 for client role, 200/202 for admin).
    • Audit logging in tmp_replication_history and checkpoint reset.
    • Isolated error handling across multi-table configurations.
  • pnpm prettier --check passes with zero formatting issues.

Wallet (USDC on Base network): 0x20A0525b47f087f1Cc498cda257A0feB78D2320B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replicate data from external source to internal source with a Plugin

1 participant