Skip to content
Source
Sign inSign up
samwillis/neon-realtime-electric
Open History to see the last commit

local-function.md

81 lines4.6 KB

1# Local Function development
2​
3Use Sam's staging Postgres/Realtime and keep the Function local. The project is
4linked in ignored `.neon`; credentials are in ignored `.env.local`.
5​
6```sh
7# Configure any custom hosts and CLI profile in ignored .env.local.
8pnpm install --frozen-lockfile
9# The linked branch must already have Realtime; no deploy/config apply is needed.
10pnpm dev:function
11```
12​
13The local example serves the configured `public.electric_items` table. Apply
14`apps/function/example.sql` to the dedicated staging test database to initialize
15it. This is an explicit local example allowlist, not an application authorization
16implementation. Real applications supply authentication and a trusted predicate
17in the handler's `authorize` callback, which runs on every read.
18​
19```sh
20pnpm test:local-baseline
21pnpm test:local-lifecycle
22# After changes are stable; leave neon dev running for the entire test:
23pnpm test:local-expiry
24```
25​
26`test:local-baseline` uses the copied Electric facade with the real Durable Streams
27reader against `http://localhost:4327/api/shape`. Override `SHAPE_URL` to test
28another existing proxy path. It checks snapshot readiness, unchanged path/origin,
29GET-only requests and saved-offset resume. The separate lifecycle script proves real no-reader writes/replay, live updates,
30filter exits/reentries, deletes and public transaction-ID conversion against SQL.
31Logical replication must be enabled; see `testing-environment.md`.
32​
33The expiry script runs a real 15-minute interval and checks the handle shortly
34before and after its absolute deadline, then requires a replacement query-string
35handle and no redirect. A cancelled run is not passing evidence.
36​
37Neon CLI 8.3.5 rejected `realtime` in `neon.ts`, so that service was provisioned
38through the staging API before the Function declaration was added. The checked-in
39config declares the local Function only. The local CLI injects database variables;
40it inherits server-only Realtime variables from the loaded environment. If needed,
41start it with Node's `--env-file=.env.local` and the installed Neon CLI entry point.
42Do not deploy merely to satisfy a local configuration mismatch.
43​
44In the observed single-source CLI workflow, changes inside workspace dependencies
45did not cause a rebundle. Restart `neon dev` after changing `packages/server` to
46ensure tests run the new bundle. Restarting kills owner state, so do it before
47starting an expiry/lifecycle acceptance run.
48​
49The handler's initial profile supports GET/optional HEAD/DELETE, JSON catch-up and
50long polling, configured primary-keyed tables and projections. WHERE text passes
51through to Realtime with values supplied separately as positional parameters;
52Realtime validates SQL. Root/projection identifiers still use the table allowlist,
53and the application authorizes predicates. SSE, both replica modes, changes-only
54and now offsets are supported. See [WHERE evidence](./where-queries.md) for tested
55subquery behavior; subsets remain a later gate.
56​
57For Object Storage mode, load `.env.storage.local` alongside `.env.local` and set
58`NEON_SHAPE_LOG_BUCKET=your-private-shape-log-bucket`. `NEON_SHAPE_SEAL_BYTES=1` forces small
59sealed objects for acceptance tests; omit it for the normal 256 KiB threshold.
60`NEON_SHAPE_DIAGNOSTICS=true` logs event names, owner closure reasons, 100 ms
61process memory/row/log samples and object operation sizes/durations without
62credentials, object keys or row values. These diagnostics are in-process only;
63there is no diagnostics HTTP endpoint. The local process sweeps expired prefixes at startup and every
64minute; this is not a platform background-execution guarantee.
65​
66To investigate full-duration failures, optionally set
67`NEON_SHAPE_DIAGNOSTICS_FILE` to an absolute ignored file path alongside
68`NEON_SHAPE_DIAGNOSTICS=true`. Each safe event record includes a process ID and
69module-instance ID; boot and owner-close records identify reloads or source loss.
70The expiry runner checks optional HEAD liveness every 30 seconds and saves both a
71latest report and a per-run report. These probes are test instrumentation; normal
72application/proxy flows still require GET only. Finish code/build changes before
73starting the acceptance run to avoid mixing hot reload with expiry behavior.
74​
75See [local performance](./local-performance.md) for the measured size/fan-out
76profile, limitations and the `test:local-performance` workflow with real storage.
77​
78Non-live saved-tail/now JSON reads use `200 []`; only long-poll timeouts use
79`204`. The `test:local-baseline` script checks the body as well as its status.
80See [native server conformance](./native-server-conformance.md) for the shared
81production read/store profile and its explicitly excluded scope.