Creates a sync backend that uses ElectricSQL for real-time event synchronization.
ElectricSQL enables real-time sync by streaming PostgreSQL changes to clients.
This backend handles push (inserting events) and pull (streaming events via Electric's
shape-based sync protocol).
The endpoint should typically be part of your API layer to handle authentication,
rate limiting, and proxying requests to the Electric server.
Creates a sync backend that uses ElectricSQL for real-time event synchronization.
ElectricSQL enables real-time sync by streaming PostgreSQL changes to clients.
This backend handles push (inserting events) and pull (streaming events via Electric's
shape-based sync protocol).
The endpoint should typically be part of your API layer to handle authentication,
rate limiting, and proxying requests to the Electric server.
The endpoint to pull/push events. Pull is a GET request, push is a POST request.
Usually this endpoint is part of your API layer to proxy requests to the Electric server
e.g. to implement auth, rate limiting, etc.
The URL interface is used to parse, construct, normalize, and encode URLs. It works by providing properties which allow you to easily read and modify the components of a URL.
The searchParams read-only property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained in the URL.
This function should be called in a trusted environment (e.g. a proxy server) as it
requires access to senstive information (e.g. apiSecret / sourceSecret).
makeElectricUrl({
electricHost: string
electricHost,
searchParams: URLSearchParams
Needed to extract information from the search params which the @livestore/sync-electric
client implementation automatically adds:
handle: the ElectricSQL handle
storeId: the Livestore storeId
searchParams,
apiSecret?: string
For self-hosted ElectricSQL
apiSecret: 'your-electric-secret',
})
// Add your authentication logic here
// if (!isAuthenticated(request)) {
// return new Response('Unauthorized', { status: 401 })
Decodes an unknown input against a schema synchronously, returning the
decoded value or throwing a
SchemaError
for schema mismatches.
When to use
Use when you need to validate unknown data at a synchronous boundary and want
schema mismatches to throw SchemaError.
Details
For input already typed as the schema's Encoded type use decodeSync.
Only service-free schemas can be decoded synchronously. For alternatives that
do not throw on schema mismatches, see decodeUnknownOption,
decodeUnknownExit, or decodeUnknownEffect. Options may be provided either
when creating the decoder or when applying it; application options override
creation options.
Gotchas
Non-schema failures may throw a runtime failure instead of SchemaError.
The json() static method of the Response interface returns a Response that contains the provided JSON data as body, and a Content-Type header which is set to application/json. The response status, status message, and additional headers can also be set.
The initial version of the ElectricSQL sync provider will use the server-side
Postgres DB as a store for the mutation event history.
Events are stored in a table following the pattern
eventlog_${PERSISTENCE_FORMAT_VERSION}_${storeId} where
PERSISTENCE_FORMAT_VERSION is a number that is incremented whenever the
sync-electric internal storage format changes.
Unless the database is already modelled as a eventlog following the
@livestore/sync-electric storage format, you won’t be able to easily use your
existing database with this sync backend implementation.
We might support this use case in the future, you can follow the progress
here. Please share any
feedback you have on this use case there.
Why do I need an API proxy in front of the ElectricSQL server?