T
TanStack2w ago
metropolitan-bronze

On-Demand Sync Mode w/ Electric Collections

I have some Electric Collections using the API proxy setup:
import { ELECTRIC_PROTOCOL_QUERY_PARAMS } from "@electric-sql/client"
import { createFileRoute } from "@tanstack/react-router"

import { getElectricShapeUrl } from "~/web/lib/config"

const serve = async ({ request }: { request: Request }) => {

const url = new URL(request.url)
const originUrl = new URL(getElectricShapeUrl())

url.searchParams.forEach((value, key) => {
if (ELECTRIC_PROTOCOL_QUERY_PARAMS.includes(key)) {
originUrl.searchParams.set(key, value)
}
})

originUrl.searchParams.set("table", "users")
)

const response = await fetch(originUrl)
const headers = new Headers(response.headers)
headers.delete("content-encoding")
headers.delete("content-length")
headers.set("Vary", "Cookie")

return new Response(response.body, {
headers,
status: response.status,
statusText: response.statusText,
})
}
import { ELECTRIC_PROTOCOL_QUERY_PARAMS } from "@electric-sql/client"
import { createFileRoute } from "@tanstack/react-router"

import { getElectricShapeUrl } from "~/web/lib/config"

const serve = async ({ request }: { request: Request }) => {

const url = new URL(request.url)
const originUrl = new URL(getElectricShapeUrl())

url.searchParams.forEach((value, key) => {
if (ELECTRIC_PROTOCOL_QUERY_PARAMS.includes(key)) {
originUrl.searchParams.set(key, value)
}
})

originUrl.searchParams.set("table", "users")
)

const response = await fetch(originUrl)
const headers = new Headers(response.headers)
headers.delete("content-encoding")
headers.delete("content-length")
headers.set("Vary", "Cookie")

return new Response(response.body, {
headers,
status: response.status,
statusText: response.statusText,
})
}
if I add syncMode: "on-demand" to my collection config, I get an error like this:
11:20:04.453 Uncaught (in promise) FetchError: HTTP Error 400 at .../api/electric/admin/users?cursor=&handle=104219484-1763462903658924&log=changes_only&offset=124979816_1862&subset__limit=21&subset__order_by=%22created_at%22+DESC+NULLS+FIRST&subset__params%5B1%5D=true&subset__where=%22email_verified%22+%3D+%241: {"message":"Invalid request","errors":{"subset":{"where":["At location 19: parameter $1 was not provided"]}}}
_FetchError error.ts:15
fromResponse error.ts:44
11:20:04.453 Uncaught (in promise) FetchError: HTTP Error 400 at .../api/electric/admin/users?cursor=&handle=104219484-1763462903658924&log=changes_only&offset=124979816_1862&subset__limit=21&subset__order_by=%22created_at%22+DESC+NULLS+FIRST&subset__params%5B1%5D=true&subset__where=%22email_verified%22+%3D+%241: {"message":"Invalid request","errors":{"subset":{"where":["At location 19: parameter $1 was not provided"]}}}
_FetchError error.ts:15
fromResponse error.ts:44
I have to add smth like
url.searchParams.forEach((value, key) => {
// Pass through Electric protocol params and subset params for on-demand mode
if (
ELECTRIC_PROTOCOL_QUERY_PARAMS.includes(key)
// ||
// key.startsWith("subset__")
) {
originUrl.searchParams.set(key, value)
}
})
url.searchParams.forEach((value, key) => {
// Pass through Electric protocol params and subset params for on-demand mode
if (
ELECTRIC_PROTOCOL_QUERY_PARAMS.includes(key)
// ||
// key.startsWith("subset__")
) {
originUrl.searchParams.set(key, value)
}
})
to make it work
2 Replies
compatible-crimson
compatible-crimson2w ago
Yes sorry about that. I have a pr up to fix this https://github.com/electric-sql/electric/pull/3430
GitHub
Don't create dynamic query keys for subset_params by KyleAMathews ...
Users discovered that proxy configurations couldn't match subsetparams because it was using deepObject style (subsetparams[1], subset__params[2]) which creates dynamic parameter names. ...
metropolitan-bronze
metropolitan-bronzeOP2w ago
gotcha, thanks as usual!

Did you find this page helpful?