Skip to content

feat: add Electric-SQL replication plugin#8019

Draft
Copilot wants to merge 6 commits intomasterfrom
copilot/add-electric-sql-sync-plugin
Draft

feat: add Electric-SQL replication plugin#8019
Copilot wants to merge 6 commits intomasterfrom
copilot/add-electric-sql-sync-plugin

Conversation

Copy link
Contributor

Copilot AI commented Mar 11, 2026

Adds replication-electric-sql plugin that syncs PostgreSQL data to RxDB collections via Electric-SQL's shape streaming HTTP API.

Plugin (src/plugins/replication-electric-sql/)

  • Pull: Fetches from Electric's /v1/shape endpoint with offset-based checkpointing. Handles must-refetch control messages by retrying from scratch within the pull handler.
  • Live: Long-polls Electric with live=true after initial sync, delivering documents directly through pullStream$. Falls back with retry on error.
  • Push: Passthrough to user-provided handler — Electric only has a read path, so writes go to the user's own backend API.
  • Types, helpers, and RxElectricSQLReplicationState class follow the same patterns as replication-supabase and replication-firestore.

Integration

  • Export entry in package.json, generated plugin bridge files in plugins/replication-electric-sql/
  • Documentation page in docs-src/docs/replication-electric-sql.md with sidebar entry

Tests (test/replication-electric-sql.test.ts)

20 tests using mock fetch functions (no backend required):

  • Helper function unit tests: buildElectricUrl, electricMessageToRxDBDocData, hasMustRefetch
  • Pull replication: basic pull, checkpoint tracking, delete operations, must-refetch recovery, custom headers
  • Push replication: user-provided push handler passthrough
  • Live replication: real-time polling delivers new documents via pull stream
  • Error handling: HTTP errors emitted on error$ with recovery
  • Instance type: returns correct RxElectricSQLReplicationState

CI

  • Added test:replication-electric-sql npm script in package.json
  • Added to test-others job in .github/workflows/main.yml alongside other no-backend replication tests (google-drive, microsoft-onedrive)

Usage

import { replicateElectricSQL } from 'rxdb/plugins/replication-electric-sql';

const replication = replicateElectricSQL({
    collection: db.items,
    replicationIdentifier: 'items-electric',
    url: 'http://localhost:3000/v1/shape',
    params: {
        table: 'items',
        where: "status='active'"
    },
    live: true,
    pull: { batchSize: 100 },
    push: {
        async handler(rows) {
            // write to your own backend
            return conflicts;
        }
    }
});

No dependency on @electric-sql/client — communicates with Electric over plain HTTP/JSON using the standard fetch API (or a user-provided fetch function).


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits March 11, 2026 01:48
…in implementation

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
…ntry

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
…ic-SQL plugin

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
Copilot AI changed the title [WIP] Add Electric SQL sync plugin feat: add Electric-SQL replication plugin Mar 11, 2026
@pubkey
Copy link
Owner

pubkey commented Mar 11, 2026

@copilot add tests

Adds test/replication-electric-sql.test.ts with 20 tests covering:
- Helper functions: buildElectricUrl, electricMessageToRxDBDocData, hasMustRefetch
- Pull replication: basic pull, checkpoint tracking, delete operations,
  must-refetch handling, custom headers
- Push replication: user-provided push handler passthrough
- Live replication: real-time polling updates
- Error handling: HTTP error emission and recovery
- Instance type verification

Also fixes must-refetch handling in the pull handler to retry
internally instead of returning empty to the replication framework.

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 11, 2026

@copilot add tests

Added comprehensive tests in test/replication-electric-sql.test.ts (20 tests, all passing) in commit 407e123. Tests use mock fetch functions so no backend is needed.

Coverage includes:

  • Helper function unit tests (buildElectricUrl, electricMessageToRxDBDocData, hasMustRefetch)
  • Pull replication (basic pull, checkpoint tracking, deletes, must-refetch retry, custom headers)
  • Push handler passthrough
  • Live polling for real-time updates
  • Error handling and recovery
  • Instance type verification

Also fixed the must-refetch handling in the pull handler to properly retry from scratch within the handler rather than returning an empty result to the replication framework.

@pubkey
Copy link
Owner

pubkey commented Mar 11, 2026

@copilot add to CI

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 11, 2026

@copilot add to CI

Added in d1b4e63:

  • test:replication-electric-sql npm script in package.json
  • CI step in .github/workflows/main.yml under the test-others job, alongside replication-google-drive and replication-microsoft-onedrive (no backend required since tests use mock fetch)

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.

2 participants