@pgxsinkit/client 0.1.32 → 0.1.36

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.
Files changed (85) hide show
  1. package/README.md +11 -0
  2. package/dist/convergence.d.ts +76 -0
  3. package/dist/convergence.d.ts.map +1 -0
  4. package/dist/debug.d.ts +34 -0
  5. package/dist/debug.d.ts.map +1 -0
  6. package/dist/index.d.ts +421 -7
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +7214 -2234
  9. package/dist/index.js.map +74 -44
  10. package/dist/lazy-guard.d.ts +58 -0
  11. package/dist/lazy-guard.d.ts.map +1 -0
  12. package/dist/local-store.d.ts +56 -0
  13. package/dist/local-store.d.ts.map +1 -0
  14. package/dist/local-tables.d.ts +707 -0
  15. package/dist/local-tables.d.ts.map +1 -0
  16. package/dist/mutation-state.d.ts +62 -0
  17. package/dist/mutation-state.d.ts.map +1 -0
  18. package/dist/mutation.d.ts +117 -4
  19. package/dist/mutation.d.ts.map +1 -1
  20. package/dist/schema.d.ts +49 -0
  21. package/dist/schema.d.ts.map +1 -1
  22. package/dist/shape-sync.d.ts +180 -0
  23. package/dist/shape-sync.d.ts.map +1 -0
  24. package/dist/sync/apply.d.ts +51 -0
  25. package/dist/sync/apply.d.ts.map +1 -0
  26. package/dist/sync/copy.d.ts +43 -0
  27. package/dist/sync/copy.d.ts.map +1 -0
  28. package/dist/sync/drizzle-executor.d.ts +5 -0
  29. package/dist/sync/drizzle-executor.d.ts.map +1 -0
  30. package/dist/sync/index.d.ts +32 -0
  31. package/dist/sync/index.d.ts.map +1 -0
  32. package/dist/sync/metadata-tables.d.ts +131 -0
  33. package/dist/sync/metadata-tables.d.ts.map +1 -0
  34. package/dist/sync/nudge.d.ts +49 -0
  35. package/dist/sync/nudge.d.ts.map +1 -0
  36. package/dist/sync/shape-inbox.d.ts +215 -0
  37. package/dist/sync/shape-inbox.d.ts.map +1 -0
  38. package/dist/sync/subscription-state.d.ts +39 -0
  39. package/dist/sync/subscription-state.d.ts.map +1 -0
  40. package/dist/sync/tags.d.ts +130 -0
  41. package/dist/sync/tags.d.ts.map +1 -0
  42. package/dist/sync/types.d.ts +118 -0
  43. package/dist/sync/types.d.ts.map +1 -0
  44. package/dist/sync-auth.d.ts +53 -0
  45. package/dist/sync-auth.d.ts.map +1 -0
  46. package/dist/worker/attach-sync-client.d.ts +75 -0
  47. package/dist/worker/attach-sync-client.d.ts.map +1 -0
  48. package/dist/worker/define-sync-worker.d.ts +75 -0
  49. package/dist/worker/define-sync-worker.d.ts.map +1 -0
  50. package/dist/worker/live-diff.d.ts +44 -0
  51. package/dist/worker/live-diff.d.ts.map +1 -0
  52. package/dist/worker/protocol.d.ts +260 -0
  53. package/dist/worker/protocol.d.ts.map +1 -0
  54. package/dist/worker/token-cache.d.ts +26 -0
  55. package/dist/worker/token-cache.d.ts.map +1 -0
  56. package/oxlint-plugin.js +143 -0
  57. package/package.json +15 -7
  58. package/skills/core/SKILL.md +197 -0
  59. package/skills/operating/SKILL.md +243 -0
  60. package/src/convergence.ts +197 -0
  61. package/src/debug.ts +151 -0
  62. package/src/index.ts +1104 -44
  63. package/src/lazy-guard.ts +121 -0
  64. package/src/local-store.ts +193 -0
  65. package/src/local-tables.ts +546 -0
  66. package/src/mutation-state.ts +90 -0
  67. package/src/mutation.ts +1868 -562
  68. package/src/schema.ts +352 -27
  69. package/src/shape-sync.ts +477 -0
  70. package/src/sync/apply.ts +530 -0
  71. package/src/sync/copy.ts +206 -0
  72. package/src/sync/drizzle-executor.ts +48 -0
  73. package/src/sync/index.ts +1076 -0
  74. package/src/sync/metadata-tables.ts +67 -0
  75. package/src/sync/nudge.ts +110 -0
  76. package/src/sync/shape-inbox.ts +566 -0
  77. package/src/sync/subscription-state.ts +135 -0
  78. package/src/sync/tags.ts +365 -0
  79. package/src/sync/types.ts +132 -0
  80. package/src/sync-auth.ts +117 -0
  81. package/src/worker/attach-sync-client.ts +558 -0
  82. package/src/worker/define-sync-worker.ts +596 -0
  83. package/src/worker/live-diff.ts +140 -0
  84. package/src/worker/protocol.ts +325 -0
  85. package/src/worker/token-cache.ts +84 -0
@@ -0,0 +1,260 @@
1
+ import type { SyncRuntimeStatus } from "@pgxsinkit/contracts";
2
+ export declare const BRIDGE_CHANNEL: "pgxsinkit-bridge";
3
+ export declare const BRIDGE_PROTOCOL_VERSION: 1;
4
+ /**
5
+ * A structured-clone transferable (an `ArrayBuffer`, a `MessagePort`, …). The library carries no DOM lib
6
+ * dependency (like the convergence triggers), so this is the toolkit's own name for the type a future
7
+ * columnar codec would hand to `postMessage`'s transfer list. v1's identity codec never produces one.
8
+ */
9
+ export type BridgeTransferable = unknown;
10
+ /**
11
+ * The minimal MessagePort-shaped transport the bridge needs — the intersection of a real `MessagePort`,
12
+ * a `SharedWorker` port, and a dedicated `Worker`/`self`. Injected everywhere so the protocol layer is
13
+ * exercised with a plain `MessageChannel` (no Worker) in tests. `start()` is optional (MessagePort needs
14
+ * it, `Worker`/`self` do not); `close()` is optional (a dedicated `self` cannot be closed by the tab).
15
+ */
16
+ export interface BridgePort {
17
+ postMessage: (message: unknown, transfer?: BridgeTransferable[]) => void;
18
+ addEventListener: (type: "message", listener: (event: {
19
+ data: unknown;
20
+ }) => void) => void;
21
+ removeEventListener: (type: "message", listener: (event: {
22
+ data: unknown;
23
+ }) => void) => void;
24
+ start?: () => void;
25
+ close?: () => void;
26
+ }
27
+ /**
28
+ * The wire envelope. `payload` is the codec-encoded body — opaque to the router, which only routes on
29
+ * `type` and correlates on `id`. Keeping the body opaque is what lets a future codec return a columnar
30
+ * buffer (a transferable) in `payload` without any change to routing.
31
+ */
32
+ export interface BridgeEnvelope {
33
+ ch: typeof BRIDGE_CHANNEL;
34
+ v: typeof BRIDGE_PROTOCOL_VERSION;
35
+ type: BridgeMessageType;
36
+ /** Correlation id for request/response pairs (rpc, subscribe, token pull). Absent for fire-and-forget fanout. */
37
+ id?: string;
38
+ /** Codec-encoded body — opaque to the router; decode with the same {@link BridgeCodec}. */
39
+ payload: unknown;
40
+ }
41
+ export type BridgeMessageType = "provision" | "attach" | "detach" | "token-push" | "token-response" | "rpc" | "subscribe" | "unsubscribe" | "wake" | "set-online" | "provision-ack" | "attach-ack" | "token-request" | "rpc-result" | "live-initial" | "live-diff" | "event";
42
+ /** A single auth token snapshot the tab owns and pushes to the worker (ADR-0032 decision 3). */
43
+ export interface AuthTokenSnapshot {
44
+ accessToken: string;
45
+ /** Absolute expiry in epoch milliseconds (`Date.now()`-comparable), so the worker can apply an expiry margin. */
46
+ expiresAt: number;
47
+ }
48
+ /**
49
+ * tab → worker: pre-spawn the store WITHOUT booting the engine (ADR-0032 decision 5). Sent at the
50
+ * login screen against the freshly-named spare worker: the worker only runs PGlite `create`/initdb
51
+ * (off every thread that matters) and holds the raw store idle — no schema, no shape streams, no token
52
+ * — until the real {@link AttachPayload} arrives (with config + token) and adopts the provisioned store.
53
+ * Role-agnostic on purpose: the spare is minted before the user (and role) is known, so provisioning
54
+ * carries no registry — the schema is applied at attach with the role-resolved registry.
55
+ */
56
+ export interface ProvisionPayload {
57
+ /** The bound store id (informational; naming is done tab-side via the SharedWorker name). */
58
+ storeId?: string;
59
+ /** The PGlite data dir to `create` ahead of attach (`idb://…` in a browser, `memory://…` in tests). */
60
+ dataDir?: string;
61
+ }
62
+ /** worker → tab: the provision completed (or failed). Lets the login screen confirm the spare is warm. */
63
+ export interface ProvisionAckPayload {
64
+ ok: boolean;
65
+ storeId?: string;
66
+ /** Present when `!ok` — the initdb/create failed; the worker falls back to a fresh create at attach. */
67
+ error?: {
68
+ message: string;
69
+ };
70
+ }
71
+ /** tab → worker: the attach handshake. The first attach boots the engine; later attaches join it. */
72
+ export interface AttachPayload {
73
+ /** The bound store id (SharedWorker naming resolves it tab-side before attach — ADR-0032 decision 5). */
74
+ storeId?: string;
75
+ /** Explicit PGlite data dir if the worker should create its own store; `memory://…` in tests. */
76
+ dataDir?: string;
77
+ /** The tab's current auth token at attach time, or null when unauthenticated. */
78
+ token: AuthTokenSnapshot | null;
79
+ /**
80
+ * Serializable per-attach config overrides (kept minimal; the urls are baked into the worker). `role`
81
+ * selects which baked registry the worker boots (ADR-0032 S3): the spare is minted role-agnostic, so
82
+ * the role is only known at claim/attach — a single worker file bakes BOTH registries and picks here.
83
+ * `freshStore` is the fresh-store prefetch-overlap hint (ADR-0032 S4): the tab's claim path knows a
84
+ * claimed spare is schemaless (fresh) while a mapped/returning store never is, and forwards that here so
85
+ * the worker's `createSyncClient` can overlap the shape catch-up with the local boot phases.
86
+ */
87
+ config?: {
88
+ syncEnabled?: boolean;
89
+ role?: string;
90
+ freshStore?: boolean;
91
+ };
92
+ }
93
+ /**
94
+ * tab → worker: gate the worker's outbound convergence (the board's Offline toggle, ADR-0032 S3). In
95
+ * in-process mode the toggle gates the local `autoSync` trigger; the worker owns convergence instead, so
96
+ * the tab forwards the flag and the worker suppresses/resumes its flush passes. Going back online fires
97
+ * one immediate pass so queued writes flush without waiting for the next interval tick.
98
+ */
99
+ export interface SetOnlinePayload {
100
+ online: boolean;
101
+ }
102
+ /** worker → tab: attach acknowledged. `alreadyBooted` is false for the attach that booted the engine. */
103
+ export interface AttachAckPayload {
104
+ alreadyBooted: boolean;
105
+ /**
106
+ * True when the engine's initial sync had ALREADY fired by the time this attach was acked (a late
107
+ * attach, ADR-0032 FIX 3). `ready` is monotonic in-process — once it resolves it stays resolved even if
108
+ * the phase later degrades — so a tab attaching after that moment must resolve `ready` immediately from
109
+ * the ack, rather than wait for a phase-"ready" status that may never come again.
110
+ */
111
+ engineReady?: boolean;
112
+ /**
113
+ * Present when the engine boot REJECTED for this attach: the tab rejects `attachSyncClient` with this
114
+ * message instead of hanging forever on the ack (ADR-0032 FIX 1). A later attach retries the boot.
115
+ */
116
+ error?: {
117
+ message: string;
118
+ detail?: unknown;
119
+ };
120
+ }
121
+ /** worker → tab: a broadcast pull-request — any attached tab may answer, first response wins (ADR-0032 decision 3). */
122
+ export interface TokenRequestPayload {
123
+ requestId: string;
124
+ }
125
+ /** tab → worker: the answer to a {@link TokenRequestPayload}. `token: null` = the tab has none. */
126
+ export interface TokenResponsePayload {
127
+ requestId: string;
128
+ token: AuthTokenSnapshot | null;
129
+ }
130
+ /**
131
+ * The RPC ops the attach facade proxies to the worker's booted client — the write API (mirrors
132
+ * `client.mutate`/flush), mutation-state reads, and the one-shot raw inspection reads (`rawQuery`/`rawExec`).
133
+ */
134
+ export type RpcOp = "create" | "update" | "delete" | "batch" | "transaction" | "flush" | "reconcile" | "retryFailed" | "recoverSending" | "discardConflict" | "discardQuarantined" | "readMutationDetails" | "diagnostics" | "rawQuery" | "rawExec";
135
+ /** tab → worker: a write/read-of-mutation-state RPC. `id` correlates the matching {@link RpcResultPayload}. */
136
+ export interface RpcPayload {
137
+ op: RpcOp;
138
+ args: unknown[];
139
+ }
140
+ /** worker → tab: an RPC outcome, mirroring today's in-process resolve/reject semantics. */
141
+ export interface RpcResultPayload {
142
+ ok: boolean;
143
+ /** The resolved value (present when `ok`). */
144
+ value?: unknown;
145
+ /** The rejection message + optional structured detail (present when `!ok`). */
146
+ error?: {
147
+ message: string;
148
+ detail?: unknown;
149
+ };
150
+ }
151
+ /** tab → worker: register a live query. `id` correlates the initial snapshot; `queryId` keys subsequent diffs. */
152
+ export interface SubscribePayload {
153
+ queryId: string;
154
+ sql: string;
155
+ params: unknown[];
156
+ /**
157
+ * The result's primary-key columns, driving diff keying (§4). One column → the incremental path
158
+ * (`live.incrementalQuery`); many → worker-side diff by composite PK; empty/absent → value-identity
159
+ * fallback for a keyless query.
160
+ */
161
+ pkColumns?: string[];
162
+ /** Lazy relations to activate before the query runs (ADR-0021), forwarded to the worker's `prepareQuery`. */
163
+ use?: string[];
164
+ }
165
+ /** worker → tab: the initial ordered snapshot for a subscription. `id` correlates the `subscribe` request. */
166
+ export interface LiveInitialPayload {
167
+ queryId: string;
168
+ rows: Record<string, unknown>[];
169
+ }
170
+ /**
171
+ * worker → tab: a DIFF update for a subscription (§4). Never a full result-set resend after the initial
172
+ * snapshot. `order` is the full ordered list of row keys (cheap — keys only, no row bodies); `added`/
173
+ * `changed` carry only the delta row bodies; `removed` carries only the dropped keys. The tab materializer
174
+ * rebuilds the ordered array from `order`, reusing cached row objects for keys not in `added`/`changed` so
175
+ * an unchanged row keeps its object identity (React memo bails on `===`).
176
+ */
177
+ export interface LiveDiffPayload {
178
+ queryId: string;
179
+ /** Every result key in ORDER BY order (the query's delivered order). */
180
+ order: string[];
181
+ added: {
182
+ key: string;
183
+ row: Record<string, unknown>;
184
+ }[];
185
+ changed: {
186
+ key: string;
187
+ row: Record<string, unknown>;
188
+ }[];
189
+ removed: string[];
190
+ }
191
+ /** worker → tab: the single broadcast event stream (ADR-0032 decision 7), re-exposed as today's callbacks. */
192
+ export type BridgeEvent = {
193
+ kind: "status";
194
+ status: SyncRuntimeStatus;
195
+ } | {
196
+ kind: "groupReady";
197
+ groupKey: string;
198
+ } | {
199
+ kind: "conflict";
200
+ details: unknown;
201
+ } | {
202
+ kind: "quarantine";
203
+ details: unknown;
204
+ } | {
205
+ kind: "reject";
206
+ details: unknown;
207
+ } | {
208
+ kind: "schema-change";
209
+ event: unknown;
210
+ } | {
211
+ kind: "sync-error";
212
+ message: string;
213
+ } | {
214
+ kind: "timing";
215
+ label: string;
216
+ ms: number;
217
+ data?: Record<string, unknown>;
218
+ } | {
219
+ kind: "debug";
220
+ stamp: number;
221
+ line: string;
222
+ data?: Record<string, unknown>;
223
+ };
224
+ /** tab → worker: an app-driven wake (online/visibilitychange), treated as a convergence pass request. */
225
+ export interface WakePayload {
226
+ reason: "online" | "visibility" | "manual";
227
+ }
228
+ /**
229
+ * The codec every bridge payload crosses through (owner-mandated seam, ADR-0032 S2 §1). v1 ships exactly
230
+ * ONE codec — {@link identityCodec}, which relies on the transport's own structured clone. The seam exists
231
+ * so a columnar/transferable codec (e.g. one that packs live-diff rows into a shared `ArrayBuffer` and
232
+ * returns it as a transferable) can be swapped in later WITHOUT any protocol change: the router already
233
+ * treats `BridgeEnvelope.payload` as opaque, and `encode` may return `{ payload, transfer }` so the sender
234
+ * hands transferables to `postMessage`. Keep encode/decode inverse and side-effect free.
235
+ */
236
+ export interface BridgeCodec {
237
+ /** Encode a raw payload into the envelope body. May declare transferables for a future zero-copy codec. */
238
+ encode: (payload: unknown) => {
239
+ payload: unknown;
240
+ transfer?: BridgeTransferable[];
241
+ };
242
+ /** Decode an envelope body back into the raw payload. Inverse of {@link encode}. */
243
+ decode: (payload: unknown) => unknown;
244
+ }
245
+ /**
246
+ * The v1 identity codec: the body IS the payload, and the transport's structured clone does the real
247
+ * copying. No transferables. The only codec shipped in S2 — swapping in a columnar codec later touches
248
+ * only this object, never the message types or the router.
249
+ */
250
+ export declare const identityCodec: BridgeCodec;
251
+ /** Build a wire envelope, running the payload through the codec. Returns the envelope + any transferables. */
252
+ export declare function encodeEnvelope(codec: BridgeCodec, type: BridgeMessageType, payload: unknown, id?: string): {
253
+ envelope: BridgeEnvelope;
254
+ transfer?: BridgeTransferable[];
255
+ };
256
+ /** Type-guard a received message as a bridge envelope of the current protocol version (ignore foreign traffic). */
257
+ export declare function isBridgeEnvelope(data: unknown): data is BridgeEnvelope;
258
+ /** Send a typed message over a port: encode → post (with any transferables). The one write choke point. */
259
+ export declare function postBridgeMessage(port: BridgePort, codec: BridgeCodec, type: BridgeMessageType, payload: unknown, id?: string): void;
260
+ //# sourceMappingURL=protocol.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/worker/protocol.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,eAAO,MAAM,cAAc,EAAG,kBAA2B,CAAC;AAC1D,eAAO,MAAM,uBAAuB,EAAG,CAAU,CAAC;AAElD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,WAAW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MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;CACvC;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,WAG3B,CAAC;AAIF,8GAA8G;AAC9G,wBAAgB,cAAc,CAC5B,KAAK,EAAE,WAAW,EAClB,IAAI,EAAE,iBAAiB,EACvB,OAAO,EAAE,OAAO,EAChB,EAAE,CAAC,EAAE,MAAM,GACV;IAAE,QAAQ,EAAE,cAAc,CAAC;IAAC,QAAQ,CAAC,EAAE,kBAAkB,EAAE,CAAA;CAAE,CAU/D;AAED,mHAAmH;AACnH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,cAAc,CAOtE;AAED,2GAA2G;AAC3G,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,WAAW,EAClB,IAAI,EAAE,iBAAiB,EACvB,OAAO,EAAE,OAAO,EAChB,EAAE,CAAC,EAAE,MAAM,GACV,IAAI,CAON"}
@@ -0,0 +1,26 @@
1
+ import type { AuthTokenSnapshot } from "./protocol";
2
+ export interface WorkerTokenCache {
3
+ /**
4
+ * Resolve the access token for a shape/flush request. Returns the cached token when it is comfortably
5
+ * ahead of expiry; otherwise broadcasts ONE pull (deduped across concurrent callers) and resolves when a
6
+ * tab answers. Falls back to the (stale) cached token if a pull yields nothing.
7
+ */
8
+ getToken: () => Promise<string | undefined>;
9
+ /** The tab pushed a fresh token (auth state change or the initial attach) — cache it and satisfy any pull. */
10
+ push: (token: AuthTokenSnapshot | null) => void;
11
+ /**
12
+ * Seed the cache from a LATER attach's token, but only if it is strictly fresher than what is cached
13
+ * (ADR-0032 FIX 5). Attach seeding must never CLOBBER a fresher cached token with an older/null one —
14
+ * only the explicit `push` path (the tab as auth owner saying "this is current") may overwrite
15
+ * unconditionally, including null on logout.
16
+ */
17
+ pushIfFresher: (token: AuthTokenSnapshot | null) => void;
18
+ /** A tab answered a pull-request. First matching answer wins; later duplicates are ignored. */
19
+ respond: (requestId: string, token: AuthTokenSnapshot | null) => void;
20
+ }
21
+ export declare function createWorkerTokenCache(options: {
22
+ marginMs: number;
23
+ broadcastRequest: (requestId: string) => void;
24
+ now?: () => number;
25
+ }): WorkerTokenCache;
26
+ //# sourceMappingURL=token-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-cache.d.ts","sourceRoot":"","sources":["../../src/worker/token-cache.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,QAAQ,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC5C,8GAA8G;IAC9G,IAAI,EAAE,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,KAAK,IAAI,CAAC;IAChD;;;;;OAKG;IACH,aAAa,EAAE,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,KAAK,IAAI,CAAC;IACzD,+FAA+F;IAC/F,OAAO,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB,GAAG,IAAI,KAAK,IAAI,CAAC;CACvE;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE;IAC9C,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9C,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB,GAAG,gBAAgB,CAmDnB"}
@@ -0,0 +1,143 @@
1
+ // Custom oxlint plugin for pgxsinkit's guarded-query surface (ADR-0021).
2
+ //
3
+ // The type split makes `use` structurally absent on the pure `client.query` / `queryRow` path (they take
4
+ // the builder callback directly). This rule catches the two provenance facts the type system cannot see:
5
+ // A) a raw `sql` fragment smuggled into the pure path — the compiled-SQL scan can miss a lazy relation
6
+ // named as a bare/unquoted identifier there, silently losing the guard; and
7
+ // B) a `use` on the raw path (`queryRaw` / `useLiveQueryRaw`) whose builder is actually pure Drizzle — the
8
+ // scan already detects every relation, so the `use` is redundant (autofixed away).
9
+ //
10
+ // Shipped with @pgxsinkit/client via the `./oxlint` subpath export, so consumers can enable it in their
11
+ // own oxlint config: `"jsPlugins": ["@pgxsinkit/client/oxlint"]` + `"pgxsinkit/guarded-query-purity": "error"`.
12
+ // Plain JS (not TS, and outside src/) on purpose: it is a tooling artifact loaded by oxlint at lint time,
13
+ // not part of the library's typed surface, so it is neither built nor typechecked nor self-linted.
14
+ //
15
+ // NOTE: this plugin has NO version logic — it runs under whatever oxlint loads it (1.72, 1.73, …), not
16
+ // only the version it was written against. But oxlint's jsPlugins feature is alpha and NOT semver-covered:
17
+ // the plugin API it relies on (create/context/report, ESTree node `.range`, `fixer.removeRange`) can
18
+ // change on a *minor* oxlint bump without that being flagged as breaking. That is why oxlint is pinned to
19
+ // an EXACT version in package.json (no `^`) — so upgrades are deliberate. On every oxlint bump, re-run
20
+ // lint and confirm this rule still loads and fires; adjust it here if the alpha API drifted.
21
+
22
+ const PURE_METHODS = new Set(["query", "queryRow"]);
23
+ const RAW_METHODS = new Set(["queryRaw", "queryRawRow", "useLiveQueryRaw", "useLiveQueryRawRow"]);
24
+
25
+ /** Depth-first visit of every descendant AST node. */
26
+ function walk(node, visit) {
27
+ if (!node || typeof node !== "object") return;
28
+ if (typeof node.type === "string") visit(node);
29
+ for (const key in node) {
30
+ if (key === "parent") continue;
31
+ const child = node[key];
32
+ if (Array.isArray(child)) {
33
+ for (const item of child) walk(item, visit);
34
+ } else if (child && typeof child === "object") {
35
+ walk(child, visit);
36
+ }
37
+ }
38
+ }
39
+
40
+ /** True if this build function embeds a raw SQL fragment: a ``sql`…` `` tagged template or `sql.raw(...)`. */
41
+ function usesRawSql(fn) {
42
+ let found = false;
43
+ walk(fn.body, (n) => {
44
+ if (found) return;
45
+ if (n.type === "TaggedTemplateExpression") {
46
+ const tag = n.tag;
47
+ if (tag && tag.type === "Identifier" && tag.name === "sql") found = true;
48
+ else if (tag && tag.type === "MemberExpression" && tag.property && tag.property.name === "sql") found = true;
49
+ } else if (n.type === "CallExpression") {
50
+ const c = n.callee;
51
+ if (
52
+ c &&
53
+ c.type === "MemberExpression" &&
54
+ c.object &&
55
+ c.object.name === "sql" &&
56
+ c.property &&
57
+ c.property.name === "raw"
58
+ ) {
59
+ found = true;
60
+ }
61
+ }
62
+ });
63
+ return found;
64
+ }
65
+
66
+ function isFunction(node) {
67
+ return !!node && (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression");
68
+ }
69
+
70
+ const guardedQueryPurity = {
71
+ meta: {
72
+ type: "problem",
73
+ fixable: "code",
74
+ docs: { description: "Guard the pure/raw split of the pgxsinkit client query surface (ADR-0021)." },
75
+ },
76
+ create(context) {
77
+ const sourceCode = context.sourceCode ?? context.getSourceCode();
78
+ return {
79
+ CallExpression(node) {
80
+ const callee = node.callee;
81
+ const method =
82
+ callee && callee.type === "MemberExpression"
83
+ ? callee.property && callee.property.name
84
+ : callee && callee.type === "Identifier"
85
+ ? callee.name
86
+ : undefined;
87
+ if (!method || (!PURE_METHODS.has(method) && !RAW_METHODS.has(method))) return;
88
+
89
+ const arg0 = node.arguments && node.arguments[0];
90
+ let buildFn;
91
+ let useProp;
92
+ if (isFunction(arg0)) {
93
+ buildFn = arg0; // pure callback form: query(fn) / queryRow(fn)
94
+ } else if (arg0 && arg0.type === "ObjectExpression") {
95
+ for (const p of arg0.properties) {
96
+ if (p.type !== "Property" || !p.key) continue;
97
+ if (p.key.name === "build") buildFn = p.value;
98
+ else if (p.key.name === "use") useProp = p;
99
+ }
100
+ }
101
+ if (!isFunction(buildFn)) return;
102
+
103
+ const raw = usesRawSql(buildFn);
104
+
105
+ // A) raw sql on the PURE path — the scan can miss a bare identifier → a real safety hole.
106
+ if (PURE_METHODS.has(method) && raw) {
107
+ const rawEquivalent = method === "queryRow" ? "queryRawRow" : "queryRaw";
108
+ context.report({
109
+ node: buildFn,
110
+ message:
111
+ `${method}() is the pure-Drizzle path but its build() embeds a raw \`sql\` fragment, which the ` +
112
+ `lazy-relation scan can miss (a bare identifier). Use client.${rawEquivalent}({ use, build }) and ` +
113
+ `declare the lazy relations in \`use\` (ADR-0021).`,
114
+ });
115
+ return;
116
+ }
117
+
118
+ // B) `use` on the raw path with a pure build — redundant. Autofix: drop the `use` property.
119
+ if (RAW_METHODS.has(method) && useProp && !raw) {
120
+ context.report({
121
+ node: useProp,
122
+ message:
123
+ "`use` is redundant here: build() is pure Drizzle, so the compiled-SQL scan detects every " +
124
+ "relation it reads. Drop `use` and prefer the pure query method (ADR-0021).",
125
+ fix(fixer) {
126
+ const text = sourceCode.getText();
127
+ const start = useProp.range[0];
128
+ let end = useProp.range[1];
129
+ while (end < text.length && /\s/.test(text[end])) end++;
130
+ if (text[end] === ",") end++;
131
+ return fixer.removeRange([start, end]);
132
+ },
133
+ });
134
+ }
135
+ },
136
+ };
137
+ },
138
+ };
139
+
140
+ export default {
141
+ meta: { name: "pgxsinkit" },
142
+ rules: { "guarded-query-purity": guardedQueryPurity },
143
+ };
package/package.json CHANGED
@@ -1,10 +1,16 @@
1
1
  {
2
2
  "name": "@pgxsinkit/client",
3
- "version": "0.1.32",
3
+ "version": "0.1.36",
4
+ "keywords": [
5
+ "tanstack-intent"
6
+ ],
7
+ "license": "MIT",
4
8
  "files": [
5
9
  "dist",
6
10
  "src",
7
- "README.md"
11
+ "README.md",
12
+ "skills",
13
+ "oxlint-plugin.js"
8
14
  ],
9
15
  "type": "module",
10
16
  "main": "./dist/index.js",
@@ -13,7 +19,8 @@
13
19
  ".": {
14
20
  "types": "./dist/index.d.ts",
15
21
  "default": "./dist/index.js"
16
- }
22
+ },
23
+ "./oxlint": "./oxlint-plugin.js"
17
24
  },
18
25
  "publishConfig": {
19
26
  "access": "public"
@@ -22,12 +29,13 @@
22
29
  "build": "bun --cwd ../.. run build:public-packages"
23
30
  },
24
31
  "dependencies": {
25
- "@pgxsinkit/contracts": "0.1.32",
26
- "@pgxsinkit/sync-engine": "0.1.32"
32
+ "@electric-sql/client": "^1.5.23",
33
+ "@electric-sql/experimental": "^6.0.23",
34
+ "@pgxsinkit/contracts": "0.1.36"
27
35
  },
28
36
  "peerDependencies": {
29
- "@electric-sql/pglite": "0.5.2",
30
- "drizzle-orm": "1.0.0-rc.2",
37
+ "@electric-sql/pglite": "0.5.4",
38
+ "drizzle-orm": "1.0.0-rc.4",
31
39
  "zod": "^4.4.3"
32
40
  },
33
41
  "repository": {