@pgxsinkit/server 0.1.51 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/electric-proxy.d.ts +2 -1
- package/dist/electric-proxy.d.ts.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -14
- package/dist/index.js.map +6 -6
- package/dist/mutations/plpgsql-apply.d.ts.map +1 -1
- package/dist/mutations/route.d.ts +2 -2
- package/dist/mutations/route.d.ts.map +1 -1
- package/package.json +2 -2
- package/skills/deploying/SKILL.md +6 -6
- package/src/electric-proxy.ts +2 -1
- package/src/index.ts +12 -10
- package/src/mutations/plpgsql-apply.ts +6 -13
- package/src/mutations/route.ts +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plpgsql-apply.d.ts","sourceRoot":"","sources":["../../src/mutations/plpgsql-apply.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnH,OAAO,EAOL,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EAEtB,KAAK,iBAAiB,EACvB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"plpgsql-apply.d.ts","sourceRoot":"","sources":["../../src/mutations/plpgsql-apply.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,KAAK,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnH,OAAO,EAOL,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EAEtB,KAAK,iBAAiB,EACvB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AA2fjD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,GAAE;IACP,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,GACL,MAAM,CAER;AAED,wBAAgB,4BAA4B,CAC1C,QAAQ,EAAE,iBAAiB,EAC3B,OAAO,GAAE;IACP,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,GACL,MAAM,CAUR;AAED,wBAAsB,2BAA2B,CAAC,SAAS,SAAS,iBAAiB,EACnF,EAAE,EAAE,eAAe,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC,EACnE,QAAQ,EAAE,SAAS,EACnB,OAAO,GAAE;IACP,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,GACL,OAAO,CAAC,IAAI,CAAC,CAGf;AAMD,wBAAsB,oBAAoB,CAAC,SAAS,SAAS,iBAAiB,EAC5E,EAAE,EAAE,eAAe,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,SAAS,CAAC,CAAC,GAClE,OAAO,CAAC,IAAI,CAAC,CAef;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,oBAAoB,EAAE,MAAM,GAAG,IAAI,CAAC;CACrC;AAQD,wBAAsB,mBAAmB,CACvC,EAAE,EAAE,iBAAiB,EACrB,KAAK,EAAE,oBAAoB,EAC3B,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,OAAO,EACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAKnC,mBAAmB,EAAE,MAAM,EAC3B,OAAO,GAAE;IACP,cAAc,CAAC,EAAE,MAAM,CAAC;CACpB,GACL,OAAO,CAAC,gBAAgB,EAAE,CAAC,CA0B7B"}
|
|
@@ -11,9 +11,9 @@ import type { FetchHandler } from "../router";
|
|
|
11
11
|
* worker sends ZERO queries before the mutation transaction itself.
|
|
12
12
|
*/
|
|
13
13
|
export type StartupVerificationMode = "in-process" | "deploy-time";
|
|
14
|
-
export declare const batchMutationPaths: readonly ["/api/mutations"
|
|
14
|
+
export declare const batchMutationPaths: readonly ["/api/mutations"];
|
|
15
15
|
/** The authoritative (pessimistic) write endpoint (ADR-0022 §3): one atomic write-unit per POST. */
|
|
16
|
-
export declare const authoritativeMutationPaths: readonly ["/api/mutations/unit"
|
|
16
|
+
export declare const authoritativeMutationPaths: readonly ["/api/mutations/unit"];
|
|
17
17
|
export declare function createMutationHandler<TRegistry extends SyncTableRegistry>(db: PgAsyncDatabase<PgQueryResultHKT, RegistryRelations<TRegistry>>, registry: TRegistry, operationsLogConfig: OperationsLogConfig, operationsLogReady: Promise<void>, resolveAuthClaims?: (request: Request) => Promise<JwtClaims | null> | JwtClaims | null, startupVerification?: StartupVerificationMode, logTimings?: boolean): {
|
|
18
18
|
batch: FetchHandler;
|
|
19
19
|
authoritative: FetchHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/mutations/route.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI7E,OAAO,KAAK,EAEV,oBAAoB,EACpB,SAAS,EAET,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EAClB,MAAM,sBAAsB,CAAC;AAS9B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAEnE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAU9C;;;;;;;GAOG;AACH,MAAM,MAAM,uBAAuB,GAAG,YAAY,GAAG,aAAa,CAAC;AAQnE,eAAO,MAAM,kBAAkB,YAAI,gBAAgB,
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/mutations/route.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI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G;AACH,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,cAAc;;GAYhE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgxsinkit/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"tanstack-intent"
|
|
6
6
|
],
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"build": "bun --cwd ../.. run build:public-packages"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@pgxsinkit/contracts": "0.
|
|
34
|
+
"@pgxsinkit/contracts": "0.2.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"drizzle-orm": "1.0.0-rc.4"
|
|
@@ -11,7 +11,7 @@ description: >-
|
|
|
11
11
|
metadata:
|
|
12
12
|
type: task
|
|
13
13
|
library: "@pgxsinkit/server"
|
|
14
|
-
library_version: "0.
|
|
14
|
+
library_version: "0.2.0"
|
|
15
15
|
source: https://pgxsinkit.github.io/start/deploying-the-server/
|
|
16
16
|
---
|
|
17
17
|
|
|
@@ -34,14 +34,14 @@ any framework you chose) inlines.
|
|
|
34
34
|
|
|
35
35
|
## 2. Strip the function-name path prefix before `server.fetch`
|
|
36
36
|
|
|
37
|
-
Edge Functions route by the first path segment, so a POST to `/functions/v1/write`
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
Edge Functions route by the first path segment, so a POST to `/functions/v1/write/api/mutations`
|
|
38
|
+
arrives as `/write/api/mutations`. Strip only the function-name prefix so the server receives the
|
|
39
|
+
canonical `/api/mutations` path:
|
|
40
40
|
|
|
41
41
|
```ts
|
|
42
42
|
Deno.serve((request) => {
|
|
43
43
|
const url = new URL(request.url);
|
|
44
|
-
url.pathname = "
|
|
44
|
+
url.pathname = url.pathname.replace(/^\/write(?=\/|$)/, "") || "/";
|
|
45
45
|
return server.fetch(new Request(url, request));
|
|
46
46
|
});
|
|
47
47
|
```
|
|
@@ -154,7 +154,7 @@ detail live in the `operating` skill.
|
|
|
154
154
|
function and column DEFAULTs call `public.pgxsinkit_clock_us()`, so migrate fails with an
|
|
155
155
|
undefined-function error. Generate it first (`--utilities`).
|
|
156
156
|
- Shipping toolkit source to Deno without bundling, or leaving builtins un-prefixed (not `node:*`).
|
|
157
|
-
- Forgetting the
|
|
157
|
+
- Forgetting to strip the function-name prefix, so `/write/api/mutations` 404s.
|
|
158
158
|
- Verifying the JWT only at the gateway instead of in `resolveAuthClaims` (non-portable).
|
|
159
159
|
- A sync function timeout below Electric's ~25s long-poll (constant read-path reconnects).
|
|
160
160
|
- Omitting `cache-control: no-store` on a same-origin shape proxy.
|
package/src/electric-proxy.ts
CHANGED
|
@@ -33,7 +33,8 @@ export interface ElectricProxyOptions {
|
|
|
33
33
|
/**
|
|
34
34
|
* Opt-in per-request timing log (default off). When on, each forwarded shape request emits one compact
|
|
35
35
|
* `[pgxsinkit-timing]` line (route `"shape"`) with the request's table/live/offset and the upstream
|
|
36
|
-
* Electric fetch duration + status, for attributing read-path latency.
|
|
36
|
+
* Electric fetch duration + status, for attributing read-path latency. Off by default — a pure
|
|
37
|
+
* diagnostic surface that adds no standing query or latency when unset.
|
|
37
38
|
*/
|
|
38
39
|
logTimings?: boolean;
|
|
39
40
|
/**
|
package/src/index.ts
CHANGED
|
@@ -37,8 +37,9 @@ interface BunNamespace {
|
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* How the operations-log table's presence is resolved at startup (ADR-0030 decision 3):
|
|
40
|
-
* - `"probe"` (default):
|
|
41
|
-
* disabled at runtime (with a warning) if it is absent
|
|
40
|
+
* - `"probe"` (default): the safe degradation posture (ADR-0030) — one query ensures/confirms the table,
|
|
41
|
+
* and logging is disabled at runtime (with a warning) if it is absent, so a missing table degrades
|
|
42
|
+
* gracefully instead of failing writes.
|
|
42
43
|
* - `"enabled"`: assume the table exists — NO query. If it is actually absent, writes then fail loudly.
|
|
43
44
|
* - `"disabled"`: turn logging off with NO query.
|
|
44
45
|
*
|
|
@@ -49,8 +50,9 @@ export type OperationsLogStartupMode = "probe" | "enabled" | "disabled";
|
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
52
|
* The `deployment` profile owns the server's startup query posture (ADR-0030 decision 3). Its defaults
|
|
52
|
-
*
|
|
53
|
-
*
|
|
53
|
+
* are the safe degradation posture — probe-and-verify at startup — so a long-lived host that never sets
|
|
54
|
+
* it still verifies and degrades gracefully; serverless / per-request workers set
|
|
55
|
+
* `{ startupVerification: "deploy-time", operationsLog: "enabled" | "disabled" }` for a
|
|
54
56
|
* zero-startup-query first write.
|
|
55
57
|
*/
|
|
56
58
|
export interface DeploymentProfile {
|
|
@@ -94,13 +96,14 @@ export interface CreateSyncServerOptions<
|
|
|
94
96
|
* The startup query posture (ADR-0030). The apply function now verifies its own ADR-0018 fingerprint
|
|
95
97
|
* in-body on every call (SQLSTATE `PXS01` on drift), so there is no startup drift check to configure;
|
|
96
98
|
* this governs only the RLS auth-helper verify and the operations-log presence resolution. Defaults
|
|
97
|
-
*
|
|
99
|
+
* are the safe degradation posture (ADR-0030). See {@link DeploymentProfile}.
|
|
98
100
|
*/
|
|
99
101
|
deployment?: DeploymentProfile;
|
|
100
102
|
/**
|
|
101
103
|
* Opt-in per-request timing log (default false). When on, each mutation and shape-proxy request emits
|
|
102
104
|
* one compact `[pgxsinkit-timing]` line with an ISO-8601(ms, UTC) timestamp and phase durations, for
|
|
103
|
-
* attributing wall-clock latency against the client's `syncDebug` lines.
|
|
105
|
+
* attributing wall-clock latency against the client's `syncDebug` lines. Off by default — a pure
|
|
106
|
+
* diagnostic surface that adds no standing query or latency when unset.
|
|
104
107
|
*/
|
|
105
108
|
logTimings?: boolean;
|
|
106
109
|
}
|
|
@@ -170,9 +173,8 @@ export function createSyncServer<
|
|
|
170
173
|
|
|
171
174
|
const shapeProxyPath = options.shapeProxyPath ?? "/api/shape";
|
|
172
175
|
|
|
173
|
-
// CORS
|
|
174
|
-
|
|
175
|
-
const corsScopes: CorsScope[] = [{ prefix: "/api/" }, { exact: "/mutations" }];
|
|
176
|
+
// CORS covers the canonical /api/* routes and the shape proxy path when it is relocated outside /api/.
|
|
177
|
+
const corsScopes: CorsScope[] = [{ prefix: "/api/" }];
|
|
176
178
|
if (options.electricUrl) {
|
|
177
179
|
corsScopes.push({ exact: shapeProxyPath });
|
|
178
180
|
}
|
|
@@ -204,7 +206,7 @@ export function createSyncServer<
|
|
|
204
206
|
router.get(healthCheckPath, () => Response.json({ ok: true }));
|
|
205
207
|
}
|
|
206
208
|
|
|
207
|
-
// The single mutation ingress point — all writes go through POST /api/mutations
|
|
209
|
+
// The single mutation ingress point — all writes go through POST /api/mutations.
|
|
208
210
|
const mutationHandlers = createMutationHandler(
|
|
209
211
|
db,
|
|
210
212
|
options.registry,
|
|
@@ -310,11 +310,10 @@ function buildTableBranch(entry: SyncTableEntry): string {
|
|
|
310
310
|
const APPLY_FUNCTION_NAME = "pgxsinkit_apply_mutations";
|
|
311
311
|
// ADR-0030: the trailing `text` is `p_expected_fingerprint` — the running server passes the fingerprint
|
|
312
312
|
// it expects for its registry + codegen, and the function verifies itself against its stamped comment
|
|
313
|
-
// before touching any table.
|
|
313
|
+
// before touching any table.
|
|
314
314
|
const APPLY_FUNCTION_ARG_TYPES = "jsonb, text, boolean, boolean, jsonb, text";
|
|
315
|
-
|
|
316
|
-
//
|
|
317
|
-
// treats an unknown prefix as "can't compare" (warn) rather than a mismatch (error).
|
|
315
|
+
// ADR-0018: the prefix namespaces the strict fingerprint format. Any absent or different prefix is
|
|
316
|
+
// an identity mismatch and the apply function refuses the write.
|
|
318
317
|
const APPLY_FINGERPRINT_PREFIX = "pgxsinkit:fp1:";
|
|
319
318
|
|
|
320
319
|
function buildApplyFunctionBody(
|
|
@@ -334,9 +333,8 @@ function buildApplyFunctionBody(
|
|
|
334
333
|
const functionName = qualifyIdent(options.functionSchema, APPLY_FUNCTION_NAME);
|
|
335
334
|
|
|
336
335
|
return `
|
|
337
|
-
--
|
|
338
|
-
-- return
|
|
339
|
-
-- first. Idempotent: re-running installs the same definition.
|
|
336
|
+
-- Install idempotently by dropping the current signature first. PostgreSQL cannot change a function's
|
|
337
|
+
-- return type through CREATE OR REPLACE, while a generated artifact must install its complete definition.
|
|
340
338
|
--
|
|
341
339
|
-- TWO-TIER MUTATION-ID INVARIANT. \`mutation_id\` is TEXT here (RETURNS TABLE + operations_log), not
|
|
342
340
|
-- uuid, so this apply function can serve a DIRECT server-side caller that derives child envelopes with
|
|
@@ -346,11 +344,6 @@ function buildApplyFunctionBody(
|
|
|
346
344
|
-- builds every ack from that request id, never from the TEXT conflict rows this returns. A non-UUID id
|
|
347
345
|
-- therefore never crosses into the UUID-typed surface.
|
|
348
346
|
--
|
|
349
|
-
-- ADR-0030: the signature gained a trailing p_expected_fingerprint text, so the prior (pre-ADR-0030,
|
|
350
|
-
-- 5-arg) function is a DIFFERENT overload — drop it too, else a CREATE of the 6-arg form would leave a
|
|
351
|
-
-- stale 5-arg beside it. The runtime only ever calls the 6-arg form; dropping the legacy one keeps an
|
|
352
|
-
-- in-place upgrade clean.
|
|
353
|
-
DROP FUNCTION IF EXISTS ${functionName}(${APPLY_FUNCTION_ARG_TYPES_LEGACY});
|
|
354
347
|
DROP FUNCTION IF EXISTS ${functionName}(${APPLY_FUNCTION_ARG_TYPES});
|
|
355
348
|
|
|
356
349
|
CREATE OR REPLACE FUNCTION ${functionName}(
|
|
@@ -386,7 +379,7 @@ BEGIN
|
|
|
386
379
|
-- ADR-0030: the apply function verifies ITSELF against its ADR-0018 fingerprint comment before it
|
|
387
380
|
-- touches any table. p_expected_fingerprint is what the running server expects for its registry +
|
|
388
381
|
-- codegen; obj_description(...) is the fingerprint this installed function was stamped with, atomically,
|
|
389
|
-
-- by the same migration. A mismatch — INCLUDING an absent comment (
|
|
382
|
+
-- by the same migration. A mismatch — INCLUDING an absent comment (an unstamped hand-installed
|
|
390
383
|
-- function ⇒ NULL, refused per ADR-0030) — means the installed function is stale relative to the caller,
|
|
391
384
|
-- so raise SQLSTATE 'PXS01' and apply nothing. The check IS the call (no read-then-call TOCTOU) and
|
|
392
385
|
-- costs no extra round trip (the argument rides the existing invocation; the comparison is one text
|
package/src/mutations/route.ts
CHANGED
|
@@ -49,9 +49,9 @@ const { createInsertSchema: createMutationInsertSchema } = createSchemaFactory({
|
|
|
49
49
|
|
|
50
50
|
type RouteReadQueryClient = Pick<PgAsyncDatabase<PgQueryResultHKT, AnyRelations>, "select">;
|
|
51
51
|
|
|
52
|
-
export const batchMutationPaths = ["/api/mutations"
|
|
52
|
+
export const batchMutationPaths = ["/api/mutations"] as const;
|
|
53
53
|
/** The authoritative (pessimistic) write endpoint (ADR-0022 §3): one atomic write-unit per POST. */
|
|
54
|
-
export const authoritativeMutationPaths = ["/api/mutations/unit"
|
|
54
|
+
export const authoritativeMutationPaths = ["/api/mutations/unit"] as const;
|
|
55
55
|
|
|
56
56
|
export function createMutationHandler<TRegistry extends SyncTableRegistry>(
|
|
57
57
|
db: PgAsyncDatabase<PgQueryResultHKT, RegistryRelations<TRegistry>>,
|