@pgxsinkit/contracts 0.1.30 → 0.1.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgxsinkit/contracts",
3
- "version": "0.1.30",
3
+ "version": "0.1.32",
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": "./src/index.ts",
11
+ "types": "./dist/index.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
- "types": "./src/index.ts",
14
+ "types": "./dist/index.d.ts",
15
15
  "default": "./dist/index.js"
16
16
  }
17
17
  },
@@ -24,5 +24,9 @@
24
24
  "peerDependencies": {
25
25
  "drizzle-orm": "1.0.0-rc.2",
26
26
  "zod": "^4.4.3"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/pgxsinkit/pgxsinkit.git"
27
31
  }
28
32
  }
package/src/config.ts CHANGED
@@ -37,11 +37,36 @@ export type ShapeSpecInput = Omit<ShapeSpec, "tableName" | "shapeKey"> & {
37
37
  shapeKey?: string;
38
38
  };
39
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
+
40
59
  export interface ClientProjectionSpec {
41
60
  syncedTable?: string;
42
61
  overlayTable?: string;
43
62
  journalTable?: string;
44
- 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;
45
70
  localPrimaryKey?: PrimaryKeySpec;
46
71
  }
47
72
 
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 is generic; this alias covers any pg view instance.
34
- // biome-ignore lint: intentional any
35
- type AnyPgView = PgView<any, any, any>;
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?: Array<TableColumnKey<TTable>>;
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<any, infer TLocalTable extends AnyPgTable>
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<any, infer TLocalTable extends AnyPgTable>
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, any> ? InferSelectModel<TTable> : never;
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 = schema ? schema.table(tableName, projectedCols) : pgTable(tableName, projectedCols);
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
- ...(governance != null ? { governance: governance as any } : {}),
325
- ...(resolvedClientProjection != null ? { clientProjection: resolvedClientProjection as any } : {}),
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<any> }>(
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<any> }>(
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<any> }>(
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)) {