@pgxsinkit/contracts 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/apply-strategy.d.ts +9 -0
- package/dist/apply-strategy.d.ts.map +1 -1
- package/dist/config.d.ts +47 -11
- package/dist/config.d.ts.map +1 -1
- package/dist/index.js +116 -22
- package/dist/index.js.map +7 -7
- package/dist/mutation.d.ts.map +1 -1
- package/dist/projection.d.ts.map +1 -1
- package/dist/registry.d.ts +72 -14
- package/dist/registry.d.ts.map +1 -1
- package/dist/runtime.d.ts +1 -1
- package/package.json +1 -1
- package/skills/registry-authoring/SKILL.md +71 -6
- package/src/apply-strategy.ts +22 -3
- package/src/config.ts +58 -19
- package/src/mutation.ts +1 -2
- package/src/projection.ts +3 -0
- package/src/registry.ts +239 -44
- package/src/runtime.ts +1 -1
package/dist/apply-strategy.d.ts
CHANGED
|
@@ -22,6 +22,15 @@ export interface SyncColumnType {
|
|
|
22
22
|
sqlType: string;
|
|
23
23
|
/** True when the column is an array of any dimension. */
|
|
24
24
|
isArray: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* True when the column is a Drizzle `pgEnum` column. An enum's {@link sqlType} is its custom type NAME
|
|
27
|
+
* (not a base type), so it is not in {@link COPY_SAFE_BASE_TYPES} — but enum labels round-trip
|
|
28
|
+
* losslessly through both the COPY text format and a `json_to_recordset` cast (whose cast type is the
|
|
29
|
+
* enum type name), so an enum column is COPY-safe / JSON-safe. This flag carries that positively rather
|
|
30
|
+
* than trying to whitelist every enum type name. When true, {@link sqlType} is the enum type name,
|
|
31
|
+
* usable as a cast type once the applier identifier-quotes (and schema-qualifies) it.
|
|
32
|
+
*/
|
|
33
|
+
isEnum: boolean;
|
|
25
34
|
}
|
|
26
35
|
/**
|
|
27
36
|
* Chooses the bulk-insert strategy for a table from its column types (ADR-0009 decision 3):
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apply-strategy.d.ts","sourceRoot":"","sources":["../src/apply-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,wFAAwF;AACxF,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEvD,oGAAoG;AACpG,MAAM,WAAW,cAAc;IAC7B,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,OAAO,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"apply-strategy.d.ts","sourceRoot":"","sources":["../src/apply-strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,wFAAwF;AACxF,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEvD,oGAAoG;AACpG,MAAM,WAAW,cAAc;IAC7B,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,OAAO,EAAE,OAAO,CAAC;IACjB;;;;;;;OAOG;IACH,MAAM,EAAE,OAAO,CAAC;CACjB;AA2ED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,GAAG,aAAa,CAwBvF"}
|
package/dist/config.d.ts
CHANGED
|
@@ -7,14 +7,15 @@ export type TableMode = "readonly" | "writeonly" | "readwrite";
|
|
|
7
7
|
* interleaved). It is a **required** declaration on every writable table; there is no silent default
|
|
8
8
|
* (registry validation rejects an undeclared writable table — the third hard-require). v1:
|
|
9
9
|
*
|
|
10
|
-
* - `last-write-wins` — apply the stale write anyway.
|
|
11
|
-
*
|
|
10
|
+
* - `last-write-wins` — apply the stale write anyway. A required, named declaration: the toolkit never
|
|
11
|
+
* silently clobbers under an unspecified default, so choosing this is an explicit acceptance of the
|
|
12
|
+
* stale-overwrite semantics.
|
|
12
13
|
* - `reject-if-stale` — do not apply; surface the conflict so the user's edit is kept (the optimistic
|
|
13
14
|
* Overlay stays, marked conflicted) and resolved as a new write.
|
|
14
15
|
*
|
|
15
16
|
* `field-merge` (apply only the changed fields over the current row) and `custom-resolver` (a client
|
|
16
|
-
* re-resolution protocol) are reserved
|
|
17
|
-
*
|
|
17
|
+
* re-resolution protocol) are reserved values for future policies — declared here so the policy surface
|
|
18
|
+
* names its full intended range.
|
|
18
19
|
*/
|
|
19
20
|
export type ConflictPolicy = "last-write-wins" | "reject-if-stale";
|
|
20
21
|
/** The Conflict policy values accepted in v1 (ADR-0015). Source of truth for registry validation. */
|
|
@@ -74,6 +75,48 @@ export type WriteMode = "optimistic" | "pessimistic";
|
|
|
74
75
|
export declare const WRITE_MODES: readonly ["optimistic", "pessimistic"];
|
|
75
76
|
/** Type guard: is `value` a {@link WriteMode}? */
|
|
76
77
|
export declare function isWriteMode(value: unknown): value is WriteMode;
|
|
78
|
+
/**
|
|
79
|
+
* The registry-declared BROWSER storage backend (ADR-0049 decision 1). `opfs` (default) is the store's
|
|
80
|
+
* normal boot on every platform — the capability machinery selects the `opfs-repacked` VFS where a home
|
|
81
|
+
* can hold sync-access handles, and falls back to in-SharedWorker `idbfs` when no home can. `idbfs` is the
|
|
82
|
+
* one way to opt out of that machinery entirely: no probe, no election, the engine boots on idb.
|
|
83
|
+
*
|
|
84
|
+
* Scopes the BROWSER store only. Environment resolution is orthogonal and unchanged (ADR-0049 decision 14):
|
|
85
|
+
* a Node mint stays `file://` and the export clone stays memory regardless of this declaration.
|
|
86
|
+
*/
|
|
87
|
+
export type StorageBackend = "opfs" | "idbfs";
|
|
88
|
+
/** The {@link StorageBackend} values. Source of truth for {@link SyncStorageDeclaration} validation. */
|
|
89
|
+
export declare const STORAGE_BACKENDS: readonly ["opfs", "idbfs"];
|
|
90
|
+
/** Type guard: is `value` a {@link StorageBackend}? */
|
|
91
|
+
export declare function isStorageBackend(value: unknown): value is StorageBackend;
|
|
92
|
+
/**
|
|
93
|
+
* The registry-declared durability mode (ADR-0047; ADR-0049 decision 9). `relaxed` (default) returns a
|
|
94
|
+
* write before its durable flush and schedules the flush asynchronously — the local-first "instant write"
|
|
95
|
+
* the toolkit exists to deliver. `strict` reinstates the synchronous flush boundary (a ~50ms+ per-statement
|
|
96
|
+
* floor on idb; cheap on `opfs-repacked`). It binds EVERY open of EVERY store minted from the registry, so
|
|
97
|
+
* no minting/open site takes a durability option to contradict it, and an opfs→idbfs capability fallback
|
|
98
|
+
* keeps the declared mode.
|
|
99
|
+
*/
|
|
100
|
+
export type StorageDurability = "relaxed" | "strict";
|
|
101
|
+
/** The {@link StorageDurability} values. Source of truth for {@link SyncStorageDeclaration} validation. */
|
|
102
|
+
export declare const STORAGE_DURABILITIES: readonly ["relaxed", "strict"];
|
|
103
|
+
/** Type guard: is `value` a {@link StorageDurability}? */
|
|
104
|
+
export declare function isStorageDurability(value: unknown): value is StorageDurability;
|
|
105
|
+
/**
|
|
106
|
+
* The registry's storage contract (ADR-0049 decision 1, ADR-0047). Storage is PART OF THE DATA CONTRACT:
|
|
107
|
+
* whether losing the last not-yet-flushed action is acceptable, and whether OPFS may be used at all, is
|
|
108
|
+
* decided by what the data IS — so it is declared once on the registry, not at any minting/open site.
|
|
109
|
+
*
|
|
110
|
+
* - {@link StorageDurability durability} defaults to `"relaxed"` and binds every toolkit-minted open of
|
|
111
|
+
* every store the registry mints (its own boot AND the provision/spare path). No open-site option exists
|
|
112
|
+
* to contradict it; a capability fallback keeps the declared mode. A no-op on `memory` clones.
|
|
113
|
+
* - {@link StorageBackend backend} defaults to `"opfs"` and scopes the BROWSER store only; Node/`file` and
|
|
114
|
+
* `memory` clones are unaffected. `"idbfs"` opts the store out of the capability/election machinery.
|
|
115
|
+
*/
|
|
116
|
+
export interface SyncStorageDeclaration {
|
|
117
|
+
backend?: StorageBackend;
|
|
118
|
+
durability?: StorageDurability;
|
|
119
|
+
}
|
|
77
120
|
/**
|
|
78
121
|
* Minimal verified-JWT claim shape the sync layer understands. Providers may
|
|
79
122
|
* attach arbitrary extra claims; those stay reachable through index access and
|
|
@@ -266,13 +309,6 @@ export interface RowFilterSpec {
|
|
|
266
309
|
*/
|
|
267
310
|
revision?: string | number;
|
|
268
311
|
}
|
|
269
|
-
/**
|
|
270
|
-
* The non-parameterized inline `where` for a filter: the string a `customWhere` returns (the raw
|
|
271
|
-
* escape hatch), or `null` when there is no filter or `customWhere` returns a Drizzle `SQL` fragment
|
|
272
|
-
* (which is parameterized by {@link buildRowFilterShape}, not inlined here). Most callers want
|
|
273
|
-
* `buildRowFilterShape` — this is the inline-string view it composes from.
|
|
274
|
-
*/
|
|
275
|
-
export declare function buildRowFilterWhere(filter: RowFilterSpec, claims: JwtClaims | null, params?: Record<string, unknown>): string | null;
|
|
276
312
|
/**
|
|
277
313
|
* A **bare** (table-unqualified) quoted identifier for a Drizzle column — `"workspace_id"`, never
|
|
278
314
|
* `"work_items"."workspace_id"`. Electric's shape `where` grammar requires *plain* column references
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,SAAS,EAAE,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAC;AAE/D
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,SAAS,EAAE,KAAK,GAAG,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAC;AAE/D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,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AS,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,GAAG,CAExG;AAED,uGAAuG;AACvG,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,SAAS,GAAG,IAAI,EACxB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,cAAc,GAAG,IAAI,CAavB"}
|
package/dist/index.js
CHANGED
|
@@ -26,7 +26,13 @@ function normalizeBaseType(sqlType) {
|
|
|
26
26
|
return sqlType.replace(/\[\]/g, "").replace(/\(.*\)/g, "").replace(/\s+/g, " ").trim().toLowerCase();
|
|
27
27
|
}
|
|
28
28
|
function isCopySafe(column) {
|
|
29
|
-
|
|
29
|
+
if (column.isArray) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
if (column.isEnum) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return COPY_SAFE_BASE_TYPES.has(normalizeBaseType(column.sqlType));
|
|
30
36
|
}
|
|
31
37
|
function isJsonSafe(column) {
|
|
32
38
|
if (isCopySafe(column)) {
|
|
@@ -84,6 +90,14 @@ var WRITE_MODES = ["optimistic", "pessimistic"];
|
|
|
84
90
|
function isWriteMode(value) {
|
|
85
91
|
return typeof value === "string" && WRITE_MODES.includes(value);
|
|
86
92
|
}
|
|
93
|
+
var STORAGE_BACKENDS = ["opfs", "idbfs"];
|
|
94
|
+
function isStorageBackend(value) {
|
|
95
|
+
return typeof value === "string" && STORAGE_BACKENDS.includes(value);
|
|
96
|
+
}
|
|
97
|
+
var STORAGE_DURABILITIES = ["relaxed", "strict"];
|
|
98
|
+
function isStorageDurability(value) {
|
|
99
|
+
return typeof value === "string" && STORAGE_DURABILITIES.includes(value);
|
|
100
|
+
}
|
|
87
101
|
var jwtClaimsSchema = z2.looseObject({
|
|
88
102
|
sub: z2.string().optional(),
|
|
89
103
|
app_metadata: z2.looseObject({
|
|
@@ -96,10 +110,6 @@ function getLocalSyncPrimaryKey(source) {
|
|
|
96
110
|
function getLocalSyncPrimaryKeyColumns(source) {
|
|
97
111
|
return [...getLocalSyncPrimaryKey(source).columns];
|
|
98
112
|
}
|
|
99
|
-
function buildRowFilterWhere(filter, claims, params) {
|
|
100
|
-
const custom = filter.customWhere?.(claims ?? {}, params);
|
|
101
|
-
return typeof custom === "string" && custom ? custom : null;
|
|
102
|
-
}
|
|
103
113
|
function c(column) {
|
|
104
114
|
return sql`${sql.identifier(column.name)}`;
|
|
105
115
|
}
|
|
@@ -136,11 +146,14 @@ import {
|
|
|
136
146
|
bigint,
|
|
137
147
|
getTableConfig,
|
|
138
148
|
pgTable,
|
|
139
|
-
pgView
|
|
149
|
+
pgView,
|
|
150
|
+
primaryKey as pgPrimaryKey,
|
|
151
|
+
PrimaryKeyBuilder
|
|
140
152
|
} from "drizzle-orm/pg-core";
|
|
141
153
|
import { varchar } from "drizzle-orm/pg-core";
|
|
142
154
|
import { getColumns } from "drizzle-orm/utils";
|
|
143
155
|
var syncRegistrySchemaSymbol = Symbol.for("@pgxsinkit/contracts/syncRegistrySchema");
|
|
156
|
+
var syncRegistryStorageSymbol = Symbol.for("@pgxsinkit/contracts/syncRegistryStorage");
|
|
144
157
|
function viewColumnsForProjection(columns, omittedColumns) {
|
|
145
158
|
if (omittedColumns.length === 0) {
|
|
146
159
|
return columns;
|
|
@@ -157,6 +170,7 @@ function defineSyncTable(input) {
|
|
|
157
170
|
schema,
|
|
158
171
|
mode,
|
|
159
172
|
primaryKey,
|
|
173
|
+
applyMode,
|
|
160
174
|
governance,
|
|
161
175
|
clientProjection,
|
|
162
176
|
serverProjection,
|
|
@@ -164,7 +178,34 @@ function defineSyncTable(input) {
|
|
|
164
178
|
...otherRest
|
|
165
179
|
} = input;
|
|
166
180
|
const resolvedMode = mode ?? "readonly";
|
|
167
|
-
const
|
|
181
|
+
const primaryKeyColumns = Array.isArray(primaryKey) ? primaryKey : primaryKey?.columns ?? ["id"];
|
|
182
|
+
const resolvedPrimaryKey = { columns: primaryKeyColumns };
|
|
183
|
+
const resolvedApplyMode = applyMode ?? "insert";
|
|
184
|
+
const customPrimaryKeyName = !Array.isArray(primaryKey) ? primaryKey?.name : undefined;
|
|
185
|
+
const primaryKeyConstraintName = customPrimaryKeyName ?? `${tableName}_pkey`;
|
|
186
|
+
const probeColumns = getColumns(pgTable(tableName, makeColumns()));
|
|
187
|
+
const columnLevelPkKeys = Object.entries(probeColumns).filter(([, col]) => col.primary === true).map(([key]) => key);
|
|
188
|
+
const specPropertyKeys = primaryKeyColumns.map((key) => {
|
|
189
|
+
if (probeColumns[key])
|
|
190
|
+
return key;
|
|
191
|
+
const byColumnName = Object.entries(probeColumns).find(([, col]) => col.name === key);
|
|
192
|
+
if (byColumnName)
|
|
193
|
+
return byColumnName[0];
|
|
194
|
+
throw new Error(`[pgxsinkit] ${tableName}: primaryKey column "${key}" not found among the table's columns`);
|
|
195
|
+
});
|
|
196
|
+
if (columnLevelPkKeys.length > 1) {
|
|
197
|
+
throw new Error(`[pgxsinkit] ${tableName}: multiple columns declare .primaryKey() (${columnLevelPkKeys.join(", ")}) \u2014 declare a composite key via the primaryKey option ({ name?, columns }) and remove the column-level .primaryKey() flags; the spec is the single source of truth`);
|
|
198
|
+
}
|
|
199
|
+
if (columnLevelPkKeys.length === 1) {
|
|
200
|
+
const columnLevelPkKey = columnLevelPkKeys[0];
|
|
201
|
+
if (specPropertyKeys.length !== 1 || specPropertyKeys[0] !== columnLevelPkKey) {
|
|
202
|
+
throw new Error(`[pgxsinkit] ${tableName}: column "${columnLevelPkKey}" declares .primaryKey() but the primaryKey spec is [${resolvedPrimaryKey.columns.join(", ")}] \u2014 they must match; the spec is the single source of truth`);
|
|
203
|
+
}
|
|
204
|
+
if (customPrimaryKeyName != null && customPrimaryKeyName !== `${tableName}_pkey`) {
|
|
205
|
+
throw new Error(`[pgxsinkit] ${tableName}: a custom primary-key name ("${customPrimaryKeyName}") requires defineSyncTable to emit the constraint \u2014 remove .primaryKey() from column "${columnLevelPkKey}" so the named constraint can be emitted`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const emitPrimaryKey = columnLevelPkKeys.length === 0;
|
|
168
209
|
const resolvedClientProjection = resolvedMode !== "writeonly" || clientProjection != null ? {
|
|
169
210
|
...clientProjection ?? {},
|
|
170
211
|
syncedTable: clientProjection?.syncedTable ?? tableName,
|
|
@@ -173,12 +214,21 @@ function defineSyncTable(input) {
|
|
|
173
214
|
journalTable: clientProjection?.journalTable ?? `${tableName}_mutations`
|
|
174
215
|
} : {}
|
|
175
216
|
} : undefined;
|
|
176
|
-
const extrasFn = policies || extras ? (self) =>
|
|
177
|
-
...policies ?? [],
|
|
178
|
-
|
|
179
|
-
|
|
217
|
+
const extrasFn = policies || extras || emitPrimaryKey ? (self) => {
|
|
218
|
+
const consumerExtras = [...policies ?? [], ...extras ? extras(self) : []];
|
|
219
|
+
for (const extra of consumerExtras) {
|
|
220
|
+
if (extra instanceof PrimaryKeyBuilder) {
|
|
221
|
+
throw new Error(`[pgxsinkit] ${tableName}: declare the primary key via the primaryKey option (string[] or { name, columns }), not a primaryKey(...) extra \u2014 the spec is the single source of truth and defineSyncTable emits the constraint`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (!emitPrimaryKey)
|
|
225
|
+
return consumerExtras;
|
|
226
|
+
const selfCols = self;
|
|
227
|
+
const pkColumns = specPropertyKeys.map((k) => selfCols[k]);
|
|
228
|
+
return [...consumerExtras, pgPrimaryKey({ name: primaryKeyConstraintName, columns: pkColumns })];
|
|
229
|
+
} : undefined;
|
|
180
230
|
const table = schema ? schema.table(tableName, makeColumns(), extrasFn) : pgTable(tableName, makeColumns(), extrasFn);
|
|
181
|
-
const resolvedRowFilter =
|
|
231
|
+
const resolvedRowFilter = shape?.rowFilter?.(getColumns(table));
|
|
182
232
|
const resolvedShape = resolvedMode !== "writeonly" || shape != null ? {
|
|
183
233
|
tableName: shape?.tableName ?? tableName,
|
|
184
234
|
shapeKey: shape?.shapeKey ?? shape?.tableName ?? tableName,
|
|
@@ -197,6 +247,7 @@ function defineSyncTable(input) {
|
|
|
197
247
|
...otherRest,
|
|
198
248
|
mode: resolvedMode,
|
|
199
249
|
primaryKey: resolvedPrimaryKey,
|
|
250
|
+
applyMode: resolvedApplyMode,
|
|
200
251
|
...resolvedShape != null ? { shape: resolvedShape } : {},
|
|
201
252
|
...governance != null ? { governance } : {},
|
|
202
253
|
...resolvedClientProjection != null ? { clientProjection: resolvedClientProjection } : {},
|
|
@@ -262,7 +313,7 @@ function defineReadProjection(owner, opts) {
|
|
|
262
313
|
...opts.subscription != null ? { subscription: opts.subscription } : {},
|
|
263
314
|
...opts.retention != null ? { retention: opts.retention } : {}
|
|
264
315
|
});
|
|
265
|
-
const resolvedRowFilter =
|
|
316
|
+
const resolvedRowFilter = opts.rowFilter?.(getColumns(owner.table));
|
|
266
317
|
const ownerColumns = getColumns(owner.table);
|
|
267
318
|
const serverOnlyColumnNames = serverOnlyColumns.map((key) => ownerColumns[key].name);
|
|
268
319
|
const allowListColumns = omittedKeys.length > 0 ? [...getProjectedColumnNames(built), ...serverOnlyColumnNames] : undefined;
|
|
@@ -292,7 +343,8 @@ function defineSyncRegistry(input) {
|
|
|
292
343
|
}
|
|
293
344
|
validateRegistryTableUniqueness(input.tables);
|
|
294
345
|
validateRegistryLifecycleGroups(input.tables);
|
|
295
|
-
|
|
346
|
+
validateStorageDeclaration(input.storage);
|
|
347
|
+
return attachSyncRegistryStorage(attachSyncRegistrySchema(input.tables, input.schema), input.storage);
|
|
296
348
|
}
|
|
297
349
|
for (const entry of getRegistryEntries(input)) {
|
|
298
350
|
validateSyncTableEntry(entry);
|
|
@@ -379,7 +431,8 @@ function deriveSyncColumnTypes(entry) {
|
|
|
379
431
|
return {
|
|
380
432
|
name: columnName,
|
|
381
433
|
sqlType,
|
|
382
|
-
isArray: (typedColumn.dimensions ?? 0) > 0
|
|
434
|
+
isArray: (typedColumn.dimensions ?? 0) > 0,
|
|
435
|
+
isEnum: typedColumn.columnType?.startsWith("PgEnum") === true
|
|
383
436
|
};
|
|
384
437
|
});
|
|
385
438
|
}
|
|
@@ -410,11 +463,7 @@ function resolveServerVersionColumnName(entry) {
|
|
|
410
463
|
return;
|
|
411
464
|
}
|
|
412
465
|
const columns = getColumns(entry.table);
|
|
413
|
-
|
|
414
|
-
if (byProperty) {
|
|
415
|
-
return byProperty.name;
|
|
416
|
-
}
|
|
417
|
-
return Object.values(columns).find((column) => column.name === field.column)?.name;
|
|
466
|
+
return columns[field.column]?.name;
|
|
418
467
|
}
|
|
419
468
|
function attachSyncRegistrySchema(registry, schema) {
|
|
420
469
|
const normalizedSchema = normalizeSchemaName(schema);
|
|
@@ -449,6 +498,40 @@ function getSyncRegistrySchema(registry) {
|
|
|
449
498
|
const schema = Reflect.get(registry, syncRegistrySchemaSymbol);
|
|
450
499
|
return typeof schema === "string" && schema.length > 0 ? schema : "public";
|
|
451
500
|
}
|
|
501
|
+
function attachSyncRegistryStorage(registry, storage) {
|
|
502
|
+
if (storage == null) {
|
|
503
|
+
return registry;
|
|
504
|
+
}
|
|
505
|
+
const existing = getSyncRegistryStorage(registry);
|
|
506
|
+
if (existing != null) {
|
|
507
|
+
if (existing.backend !== storage.backend || existing.durability !== storage.durability) {
|
|
508
|
+
throw new Error(`conflicting storage declaration for registry: already ${JSON.stringify(existing)}, cannot re-declare ` + `${JSON.stringify(storage)}`);
|
|
509
|
+
}
|
|
510
|
+
return registry;
|
|
511
|
+
}
|
|
512
|
+
Object.defineProperty(registry, syncRegistryStorageSymbol, {
|
|
513
|
+
value: storage,
|
|
514
|
+
enumerable: false,
|
|
515
|
+
configurable: false,
|
|
516
|
+
writable: false
|
|
517
|
+
});
|
|
518
|
+
return registry;
|
|
519
|
+
}
|
|
520
|
+
function getSyncRegistryStorage(registry) {
|
|
521
|
+
const storage = Reflect.get(registry, syncRegistryStorageSymbol);
|
|
522
|
+
return storage != null && typeof storage === "object" ? storage : undefined;
|
|
523
|
+
}
|
|
524
|
+
function validateStorageDeclaration(storage) {
|
|
525
|
+
if (storage == null) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (storage.backend !== undefined && !isStorageBackend(storage.backend)) {
|
|
529
|
+
throw new Error(`invalid storage.backend "${String(storage.backend)}": must be one of ${STORAGE_BACKENDS.join(", ")}`);
|
|
530
|
+
}
|
|
531
|
+
if (storage.durability !== undefined && !isStorageDurability(storage.durability)) {
|
|
532
|
+
throw new Error(`invalid storage.durability "${String(storage.durability)}": must be one of ${STORAGE_DURABILITIES.join(", ")}`);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
452
535
|
function isSyncRegistryDefinition(input) {
|
|
453
536
|
return typeof input === "object" && input !== null && "tables" in input && typeof input.tables === "object" && input.tables !== null;
|
|
454
537
|
}
|
|
@@ -482,7 +565,11 @@ function validateSyncTableEntry(entry) {
|
|
|
482
565
|
if (entry.writeMode === "pessimistic" && entry.mode === "readonly") {
|
|
483
566
|
throw new Error(`readonly table ${tableName} cannot be pessimistic (ADR-0022): write-mode governs the write path, ` + `which a readonly table does not have. Declare writeMode only on a writable table.`);
|
|
484
567
|
}
|
|
568
|
+
const managedFieldPropertyKeys = new Set(Object.keys(getColumns(entry.table)));
|
|
485
569
|
for (const field of entry.governance?.managedFields ?? []) {
|
|
570
|
+
if (!managedFieldPropertyKeys.has(field.column)) {
|
|
571
|
+
throw new Error(`managed field ${tableName}.${String(field.column)} names an unknown column: field.column must be the ` + `drizzle PROPERTY KEY of the target column, not its SQL column name (ADR-0012)`);
|
|
572
|
+
}
|
|
486
573
|
if (field.strategy === "authClaim") {
|
|
487
574
|
if (!Array.isArray(field.claimPath) || field.claimPath.length === 0) {
|
|
488
575
|
throw new Error(`managed field ${tableName}.${String(field.column)} (strategy "authClaim") must declare a non-empty ` + `claimPath (e.g. ["sub"] or ["app_metadata","person_id"]) \u2014 ADR-0026`);
|
|
@@ -1028,6 +1115,7 @@ function asReadonly(entry) {
|
|
|
1028
1115
|
localTable: entry.localTable,
|
|
1029
1116
|
mode: "readonly",
|
|
1030
1117
|
primaryKey: entry.primaryKey,
|
|
1118
|
+
applyMode: entry.applyMode,
|
|
1031
1119
|
...entry.shape != null ? { shape: entry.shape } : {},
|
|
1032
1120
|
...readonlyProjection != null ? { clientProjection: readonlyProjection } : {},
|
|
1033
1121
|
...entry.serverProjection != null ? { serverProjection: entry.serverProjection } : {},
|
|
@@ -1377,6 +1465,7 @@ function buildGrantScopeShapeWhere(scopeColumn, ids) {
|
|
|
1377
1465
|
export {
|
|
1378
1466
|
withRetention,
|
|
1379
1467
|
unixMicrosecondsSchema,
|
|
1468
|
+
syncRegistryStorageSymbol,
|
|
1380
1469
|
syncRegistrySchemaSymbol,
|
|
1381
1470
|
supabaseOwnerOrAdminDefaults,
|
|
1382
1471
|
summarizeRegistryDiff,
|
|
@@ -1396,10 +1485,13 @@ export {
|
|
|
1396
1485
|
jwtClaimsSchema,
|
|
1397
1486
|
isWriteMode,
|
|
1398
1487
|
isSubscriptionTiming,
|
|
1488
|
+
isStorageDurability,
|
|
1489
|
+
isStorageBackend,
|
|
1399
1490
|
isRetention,
|
|
1400
1491
|
isConflictPolicy,
|
|
1401
1492
|
isClaimsDependentRowFilter,
|
|
1402
1493
|
hashString,
|
|
1494
|
+
getSyncRegistryStorage,
|
|
1403
1495
|
getSyncRegistrySchema,
|
|
1404
1496
|
getProjectedColumns,
|
|
1405
1497
|
getProjectedColumnNames,
|
|
@@ -1432,7 +1524,6 @@ export {
|
|
|
1432
1524
|
buildSupabaseOwnerOrAdminNativePolicies,
|
|
1433
1525
|
buildSupabaseMembershipNativePolicies,
|
|
1434
1526
|
buildSupabaseGrantScopeNativePolicies,
|
|
1435
|
-
buildRowFilterWhere,
|
|
1436
1527
|
buildRowFilterShape,
|
|
1437
1528
|
buildRegistryLock,
|
|
1438
1529
|
buildOwnershipShapeWhere,
|
|
@@ -1442,12 +1533,15 @@ export {
|
|
|
1442
1533
|
batchMutationErrorSchema,
|
|
1443
1534
|
batchMutationAckSchema,
|
|
1444
1535
|
authoritativeWriteRequestSchema,
|
|
1536
|
+
attachSyncRegistryStorage,
|
|
1445
1537
|
attachSyncRegistrySchema,
|
|
1446
1538
|
assertReadContractPreserved,
|
|
1447
1539
|
asReadonly,
|
|
1448
1540
|
asEphemeral,
|
|
1449
1541
|
WRITE_MODES,
|
|
1450
1542
|
SUBSCRIPTION_TIMINGS,
|
|
1543
|
+
STORAGE_DURABILITIES,
|
|
1544
|
+
STORAGE_BACKENDS,
|
|
1451
1545
|
RETENTIONS,
|
|
1452
1546
|
NOW_MICROSECONDS_SQL_TEXT,
|
|
1453
1547
|
DENY_ALL,
|
|
@@ -1456,4 +1550,4 @@ export {
|
|
|
1456
1550
|
CLOCK_US_CALL_SQL_TEXT
|
|
1457
1551
|
};
|
|
1458
1552
|
|
|
1459
|
-
//# debugId=
|
|
1553
|
+
//# debugId=F0D61A9CF519A39664756E2164756E21
|