@pgxsinkit/contracts 0.1.29 → 0.1.31
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/common.d.ts +3 -0
- package/dist/common.d.ts.map +1 -0
- package/dist/config.d.ts +132 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +127 -99
- package/dist/index.js.map +5 -5
- package/dist/mutation.d.ts +85 -0
- package/dist/mutation.d.ts.map +1 -0
- package/dist/registry.d.ts +475 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/runtime.d.ts +19 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +20 -0
- package/dist/supabase-rls.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/config.ts +88 -12
- package/src/registry.ts +64 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgxsinkit/contracts",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.31",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"src",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
],
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "./dist/index.js",
|
|
11
|
-
"types": "./
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"types": "./
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
15
|
"default": "./dist/index.js"
|
|
16
16
|
}
|
|
17
17
|
},
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
1
3
|
export type TableMode = "readonly" | "writeonly" | "readwrite";
|
|
2
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Minimal verified-JWT claim shape the sync layer understands. Providers may
|
|
7
|
+
* attach arbitrary extra claims; those stay reachable through index access and
|
|
8
|
+
* ownership claim paths (e.g. "app_metadata.person_id"). Parse decoded JWT
|
|
9
|
+
* payloads with this schema at the auth boundary so the static type is honest.
|
|
10
|
+
*/
|
|
11
|
+
export const jwtClaimsSchema = z.looseObject({
|
|
12
|
+
sub: z.string().optional(),
|
|
13
|
+
app_metadata: z
|
|
14
|
+
.looseObject({
|
|
15
|
+
roles: z.array(z.string()).optional(),
|
|
16
|
+
})
|
|
17
|
+
.optional(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export type JwtClaims = z.infer<typeof jwtClaimsSchema>;
|
|
21
|
+
|
|
3
22
|
export interface PrimaryKeySpec {
|
|
4
23
|
columns: string[];
|
|
5
24
|
}
|
|
@@ -18,11 +37,36 @@ export type ShapeSpecInput = Omit<ShapeSpec, "tableName" | "shapeKey"> & {
|
|
|
18
37
|
shapeKey?: string;
|
|
19
38
|
};
|
|
20
39
|
|
|
40
|
+
/** Context available to a {@link RowTransform}: the verified claims and any extra runtime params. */
|
|
41
|
+
export interface RowTransformContext {
|
|
42
|
+
claims: JwtClaims | null;
|
|
43
|
+
params?: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Per-row rewrite applied in the proxy response path (after the row filter, before
|
|
48
|
+
* column omission). Receives a shape-log row's column map (keys are wire/column names)
|
|
49
|
+
* and returns a possibly-rewritten one — letting the server strip a *sub-document* of a
|
|
50
|
+
* jsonb column, or otherwise rewrite a value, *conditionally on row data*. This expresses
|
|
51
|
+
* what a static, whole-column `omitColumns` cannot.
|
|
52
|
+
*
|
|
53
|
+
* It runs only in the proxy's per-response path: it never alters the local PGlite schema,
|
|
54
|
+
* never changes the Electric shape URL, and so never pollutes Electric's shared shape
|
|
55
|
+
* cache. Return the same `row` reference to signal "no change".
|
|
56
|
+
*/
|
|
57
|
+
export type RowTransform = (row: Record<string, unknown>, context: RowTransformContext) => Record<string, unknown>;
|
|
58
|
+
|
|
21
59
|
export interface ClientProjectionSpec {
|
|
22
60
|
syncedTable?: string;
|
|
23
61
|
overlayTable?: string;
|
|
24
62
|
journalTable?: string;
|
|
25
|
-
omitColumns?: string[];
|
|
63
|
+
omitColumns?: readonly string[];
|
|
64
|
+
/**
|
|
65
|
+
* Optional per-row rewrite applied in the proxy response path. Runs before column
|
|
66
|
+
* omission, so it may read a column (e.g. a control flag) that `omitColumns` then
|
|
67
|
+
* removes from the client-visible row. See {@link RowTransform}.
|
|
68
|
+
*/
|
|
69
|
+
rowTransform?: RowTransform;
|
|
26
70
|
localPrimaryKey?: PrimaryKeySpec;
|
|
27
71
|
}
|
|
28
72
|
|
|
@@ -54,10 +98,10 @@ export interface TableSpecInput {
|
|
|
54
98
|
governance?: TableGovernanceSpec;
|
|
55
99
|
}
|
|
56
100
|
|
|
57
|
-
export interface SyncConfigInput {
|
|
101
|
+
export interface SyncConfigInput<TTables extends Record<string, TableSpecInput> = Record<string, TableSpecInput>> {
|
|
58
102
|
electricUrl: string;
|
|
59
103
|
localSchema?: string;
|
|
60
|
-
tables:
|
|
104
|
+
tables: TTables;
|
|
61
105
|
}
|
|
62
106
|
|
|
63
107
|
export function getLocalSyncPrimaryKey(source: {
|
|
@@ -75,9 +119,15 @@ export function getLocalSyncPrimaryKeyColumns(source: {
|
|
|
75
119
|
}
|
|
76
120
|
|
|
77
121
|
export interface RowFilterSpec {
|
|
78
|
-
/** WHERE "column" =
|
|
122
|
+
/** WHERE "column" = '<owner claim>' — ownership-based row filtering. */
|
|
79
123
|
ownership?: {
|
|
80
124
|
column: string;
|
|
125
|
+
/**
|
|
126
|
+
* Dot-path into the JWT claims used as the owner value (e.g.
|
|
127
|
+
* "app_metadata.person_id"). Defaults to "sub". A missing, empty, or
|
|
128
|
+
* non-primitive claim denies all rows, matching the missing-sub behavior.
|
|
129
|
+
*/
|
|
130
|
+
claim?: string;
|
|
81
131
|
};
|
|
82
132
|
/** OR clause for shared content: adds rows owned by sharedUserId. */
|
|
83
133
|
shared?: {
|
|
@@ -92,7 +142,7 @@ export interface RowFilterSpec {
|
|
|
92
142
|
* Escape hatch: returns a raw SQL fragment ANDed with other filters.
|
|
93
143
|
* Return `null` to bypass all filters (e.g. admin access).
|
|
94
144
|
*/
|
|
95
|
-
customWhere?: (claims:
|
|
145
|
+
customWhere?: (claims: JwtClaims, params?: Record<string, unknown>) => string | null;
|
|
96
146
|
/** Column projection for the shape URL (e.g. ["id", "source_text"]). */
|
|
97
147
|
columns?: string[];
|
|
98
148
|
}
|
|
@@ -101,6 +151,29 @@ function escapeSqlLiteral(value: string): string {
|
|
|
101
151
|
return value.replace(/'/g, "''");
|
|
102
152
|
}
|
|
103
153
|
|
|
154
|
+
function readOwnerClaim(claims: JwtClaims | null, claimPath: string): string | null {
|
|
155
|
+
let current: unknown = claims;
|
|
156
|
+
|
|
157
|
+
for (const segment of claimPath.split(".")) {
|
|
158
|
+
if (typeof current !== "object" || current === null || Array.isArray(current)) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
current = (current as Record<string, unknown>)[segment];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
switch (typeof current) {
|
|
165
|
+
case "string":
|
|
166
|
+
return current.length > 0 ? current : null;
|
|
167
|
+
case "number":
|
|
168
|
+
case "bigint":
|
|
169
|
+
case "boolean":
|
|
170
|
+
// Mirrors the historical truthiness check on claims.sub.
|
|
171
|
+
return current ? String(current) : null;
|
|
172
|
+
default:
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
104
177
|
/**
|
|
105
178
|
* Composes ownership, shared, and customWhere filters into a single
|
|
106
179
|
* SQL WHERE clause suitable for Electric shape requests.
|
|
@@ -109,19 +182,22 @@ function escapeSqlLiteral(value: string): string {
|
|
|
109
182
|
*/
|
|
110
183
|
export function buildRowFilterWhere(
|
|
111
184
|
filter: RowFilterSpec,
|
|
112
|
-
claims:
|
|
185
|
+
claims: JwtClaims | null,
|
|
113
186
|
params?: Record<string, unknown>,
|
|
114
187
|
): string | null {
|
|
115
188
|
const parts: string[] = [];
|
|
116
189
|
const { ownership, shared, customWhere } = filter;
|
|
117
190
|
|
|
118
191
|
// Ownership
|
|
119
|
-
if (ownership
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
192
|
+
if (ownership) {
|
|
193
|
+
const ownerValue = readOwnerClaim(claims, ownership.claim ?? "sub");
|
|
194
|
+
|
|
195
|
+
if (ownerValue === null) {
|
|
196
|
+
// No authenticated owner claim — block all rows
|
|
197
|
+
return "1 = 0";
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
parts.push(`"${ownership.column}" = '${escapeSqlLiteral(ownerValue)}'`);
|
|
125
201
|
}
|
|
126
202
|
|
|
127
203
|
// Shared content
|
package/src/registry.ts
CHANGED
|
@@ -5,10 +5,16 @@ import {
|
|
|
5
5
|
pgTable,
|
|
6
6
|
pgView,
|
|
7
7
|
type AnyPgTable,
|
|
8
|
+
type PgBigInt64Builder,
|
|
9
|
+
type PgBuildColumns,
|
|
8
10
|
type PgBuildExtraConfigColumns,
|
|
9
11
|
type PgPolicy,
|
|
10
12
|
type PgTableExtraConfigValue,
|
|
13
|
+
type PgTableWithColumns,
|
|
14
|
+
type PgVarcharBuilder,
|
|
11
15
|
type PgView,
|
|
16
|
+
type PgViewWithSelection,
|
|
17
|
+
type SetNotNull,
|
|
12
18
|
} from "drizzle-orm/pg-core";
|
|
13
19
|
import type { pgSchema } from "drizzle-orm/pg-core";
|
|
14
20
|
import { varchar } from "drizzle-orm/pg-core";
|
|
@@ -30,9 +36,9 @@ import type {
|
|
|
30
36
|
|
|
31
37
|
type PgSchemaType = ReturnType<typeof pgSchema>;
|
|
32
38
|
|
|
33
|
-
// PgView
|
|
34
|
-
//
|
|
35
|
-
type AnyPgView = PgView
|
|
39
|
+
// PgView's parameters all default to their upper bounds, so the bare name
|
|
40
|
+
// covers any pg view instance.
|
|
41
|
+
type AnyPgView = PgView;
|
|
36
42
|
|
|
37
43
|
type TableColumnsShape<TTable extends AnyPgTable> = TTable extends {
|
|
38
44
|
_: {
|
|
@@ -59,7 +65,7 @@ export interface ResolvedManagedFieldSpecForTable<TTable extends AnyPgTable> {
|
|
|
59
65
|
}
|
|
60
66
|
|
|
61
67
|
export type ClientProjectionSpecForTable<TTable extends AnyPgTable> = Omit<ClientProjectionSpec, "omitColumns"> & {
|
|
62
|
-
omitColumns?:
|
|
68
|
+
omitColumns?: readonly TableColumnKey<TTable>[];
|
|
63
69
|
};
|
|
64
70
|
|
|
65
71
|
export interface ProjectedTableColumn<TTable extends AnyPgTable = AnyPgTable> {
|
|
@@ -106,6 +112,38 @@ type ProjectedColumnsShape<
|
|
|
106
112
|
TOmittedColumns extends readonly ColumnKeys<TColumns>[],
|
|
107
113
|
> = ColumnKeys<TColumns> extends TOmittedColumns[number] ? TColumns : Omit<TColumns, TOmittedColumns[number]>;
|
|
108
114
|
|
|
115
|
+
// Explicit shapes for the projected local table and the read-model view.
|
|
116
|
+
// Their drizzle-inferred types cannot survive declaration emit: the printer
|
|
117
|
+
// reproduces pgTable/pgView internals as `PgBuildColumn<TTableName, ...>` with
|
|
118
|
+
// drizzle's own out-of-scope generic, breaking the published .d.ts. The
|
|
119
|
+
// constructed values are cast to these equivalent, printable types instead.
|
|
120
|
+
type ProjectedLocalTable<
|
|
121
|
+
TName extends string,
|
|
122
|
+
TColumns extends Record<string, ColumnBuilderBase>,
|
|
123
|
+
TOmittedColumns extends readonly ColumnKeys<TColumns>[],
|
|
124
|
+
> = PgTableWithColumns<{
|
|
125
|
+
name: TName;
|
|
126
|
+
schema: string | undefined;
|
|
127
|
+
columns: PgBuildColumns<TName, ProjectedColumnsShape<TColumns, TOmittedColumns>>;
|
|
128
|
+
dialect: "pg";
|
|
129
|
+
}>;
|
|
130
|
+
|
|
131
|
+
type ReadModelView<
|
|
132
|
+
TName extends string,
|
|
133
|
+
TColumns extends Record<string, ColumnBuilderBase>,
|
|
134
|
+
TOmittedColumns extends readonly ColumnKeys<TColumns>[],
|
|
135
|
+
> = PgViewWithSelection<
|
|
136
|
+
`${TName}_read_model`,
|
|
137
|
+
true,
|
|
138
|
+
PgBuildColumns<
|
|
139
|
+
`${TName}_read_model`,
|
|
140
|
+
ProjectedColumnsShape<TColumns, TOmittedColumns> & {
|
|
141
|
+
overlay_kind: SetNotNull<PgVarcharBuilder<[string, ...string[]]>>;
|
|
142
|
+
local_updated_at_us: SetNotNull<PgBigInt64Builder>;
|
|
143
|
+
}
|
|
144
|
+
>
|
|
145
|
+
>;
|
|
146
|
+
|
|
109
147
|
/** Governance spec for `defineSyncTable` — columns are typed from `makeColumns`. */
|
|
110
148
|
export type SyncTableInputGovernance<TColumns extends Record<string, ColumnBuilderBase>> = Omit<
|
|
111
149
|
TableGovernanceSpecBase,
|
|
@@ -206,17 +244,19 @@ type ManagedFieldColumnKeysForOperation<TEntry, TOperation extends ManagedFieldA
|
|
|
206
244
|
: never;
|
|
207
245
|
|
|
208
246
|
export type SyncTableCreateInput<TRegistry extends SyncTableRegistry, TKey extends keyof TRegistry> =
|
|
209
|
-
TRegistry[TKey] extends SyncTableEntry<
|
|
247
|
+
TRegistry[TKey] extends SyncTableEntry<AnyPgTable, infer TLocalTable extends AnyPgTable>
|
|
210
248
|
? Omit<InferInsertModel<TLocalTable>, ManagedFieldColumnKeysForOperation<TRegistry[TKey], "create">>
|
|
211
249
|
: never;
|
|
212
250
|
|
|
213
251
|
export type SyncTableUpdateInput<TRegistry extends SyncTableRegistry, TKey extends keyof TRegistry> =
|
|
214
|
-
TRegistry[TKey] extends SyncTableEntry<
|
|
252
|
+
TRegistry[TKey] extends SyncTableEntry<AnyPgTable, infer TLocalTable extends AnyPgTable>
|
|
215
253
|
? Partial<Omit<InferInsertModel<TLocalTable>, ManagedFieldColumnKeysForOperation<TRegistry[TKey], "update">>>
|
|
216
254
|
: never;
|
|
217
255
|
|
|
218
256
|
export type SyncTableRecord<TRegistry extends SyncTableRegistry, TKey extends keyof TRegistry> =
|
|
219
|
-
TRegistry[TKey] extends SyncTableEntry<infer TTable extends AnyPgTable,
|
|
257
|
+
TRegistry[TKey] extends SyncTableEntry<infer TTable extends AnyPgTable, AnyPgTable>
|
|
258
|
+
? InferSelectModel<TTable>
|
|
259
|
+
: never;
|
|
220
260
|
|
|
221
261
|
/**
|
|
222
262
|
* Filters a columns object, omitting keys in `omitSet`, while preserving TypeScript column types.
|
|
@@ -290,7 +330,6 @@ export function defineSyncTable<
|
|
|
290
330
|
}
|
|
291
331
|
: undefined;
|
|
292
332
|
|
|
293
|
-
// biome-ignore lint: intentional any for policy/extras passthrough
|
|
294
333
|
const extrasFn =
|
|
295
334
|
policies || extras
|
|
296
335
|
? (self: PgBuildExtraConfigColumns<ReturnType<typeof makeColumns>>) => [
|
|
@@ -298,22 +337,22 @@ export function defineSyncTable<
|
|
|
298
337
|
...(extras ? extras(self) : []),
|
|
299
338
|
]
|
|
300
339
|
: undefined;
|
|
301
|
-
const table = schema
|
|
302
|
-
? schema.table(tableName, makeColumns(), extrasFn as any)
|
|
303
|
-
: pgTable(tableName, makeColumns(), extrasFn as any);
|
|
340
|
+
const table = schema ? schema.table(tableName, makeColumns(), extrasFn) : pgTable(tableName, makeColumns(), extrasFn);
|
|
304
341
|
|
|
305
342
|
const omittedColumns = (clientProjection?.omitColumns ?? []) as TOmittedColumns;
|
|
306
343
|
const projectedCols = viewColumnsForProjection(makeColumns(), omittedColumns);
|
|
307
|
-
const localTable =
|
|
344
|
+
const localTable = (
|
|
345
|
+
schema ? schema.table(tableName, projectedCols) : pgTable(tableName, projectedCols)
|
|
346
|
+
) as ProjectedLocalTable<TName, TColumns, TOmittedColumns>;
|
|
308
347
|
|
|
309
348
|
const viewColumns = viewColumnsForProjection(makeColumns(), omittedColumns);
|
|
310
349
|
const view =
|
|
311
350
|
resolvedMode === "readwrite"
|
|
312
|
-
? pgView(`${tableName}_read_model`, {
|
|
351
|
+
? (pgView(`${tableName}_read_model`, {
|
|
313
352
|
...viewColumns,
|
|
314
353
|
overlay_kind: varchar("overlay_kind", { length: 24 }).notNull(),
|
|
315
354
|
local_updated_at_us: bigint("local_updated_at_us", { mode: "bigint" }).notNull(),
|
|
316
|
-
}).existing()
|
|
355
|
+
}).existing() as ReadModelView<TName, TColumns, TOmittedColumns>)
|
|
317
356
|
: undefined;
|
|
318
357
|
|
|
319
358
|
const entry = {
|
|
@@ -321,8 +360,14 @@ export function defineSyncTable<
|
|
|
321
360
|
mode: resolvedMode,
|
|
322
361
|
primaryKey: resolvedPrimaryKey,
|
|
323
362
|
...(resolvedShape != null ? { shape: resolvedShape } : {}),
|
|
324
|
-
|
|
325
|
-
|
|
363
|
+
// The input specs are typed over the builder columns map (keys of
|
|
364
|
+
// TColumns); the entry fields demand specs typed over the built table.
|
|
365
|
+
// The key sets are equal by construction (BuildColumns preserves keys),
|
|
366
|
+
// but that equality is unprovable while TColumns is an open generic.
|
|
367
|
+
...(governance != null ? { governance: governance as TableGovernanceSpecForTable<typeof table> } : {}),
|
|
368
|
+
...(resolvedClientProjection != null
|
|
369
|
+
? { clientProjection: resolvedClientProjection as unknown as ClientProjectionSpecForTable<typeof table> }
|
|
370
|
+
: {}),
|
|
326
371
|
table,
|
|
327
372
|
localTable,
|
|
328
373
|
...(view != null ? { view } : {}),
|
|
@@ -331,13 +376,13 @@ export function defineSyncTable<
|
|
|
331
376
|
return entry as typeof entry & SyncTableInputGovernanceMarker<TGovernance>;
|
|
332
377
|
}
|
|
333
378
|
|
|
334
|
-
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry
|
|
379
|
+
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry }>(
|
|
335
380
|
registry: TRegistry,
|
|
336
381
|
): TRegistry;
|
|
337
|
-
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry
|
|
382
|
+
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry }>(
|
|
338
383
|
definition: SyncRegistryDefinition<TRegistry>,
|
|
339
384
|
): TRegistry;
|
|
340
|
-
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry
|
|
385
|
+
export function defineSyncRegistry<const TRegistry extends { [TKey in keyof TRegistry]: SyncTableEntry }>(
|
|
341
386
|
input: TRegistry | SyncRegistryDefinition<TRegistry>,
|
|
342
387
|
) {
|
|
343
388
|
if (isSyncRegistryDefinition(input)) {
|