@pgxsinkit/contracts 0.1.51 → 0.1.53

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.
@@ -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;CAClB;AAiED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,GAAG,aAAa,CAwBvF"}
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/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
- return !column.isArray && COPY_SAFE_BASE_TYPES.has(normalizeBaseType(column.sqlType));
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)) {
@@ -136,7 +142,9 @@ import {
136
142
  bigint,
137
143
  getTableConfig,
138
144
  pgTable,
139
- pgView
145
+ pgView,
146
+ primaryKey as pgPrimaryKey,
147
+ PrimaryKeyBuilder
140
148
  } from "drizzle-orm/pg-core";
141
149
  import { varchar } from "drizzle-orm/pg-core";
142
150
  import { getColumns } from "drizzle-orm/utils";
@@ -157,6 +165,7 @@ function defineSyncTable(input) {
157
165
  schema,
158
166
  mode,
159
167
  primaryKey,
168
+ applyMode,
160
169
  governance,
161
170
  clientProjection,
162
171
  serverProjection,
@@ -164,7 +173,34 @@ function defineSyncTable(input) {
164
173
  ...otherRest
165
174
  } = input;
166
175
  const resolvedMode = mode ?? "readonly";
167
- const resolvedPrimaryKey = { columns: primaryKey ?? ["id"] };
176
+ const primaryKeyColumns = Array.isArray(primaryKey) ? primaryKey : primaryKey?.columns ?? ["id"];
177
+ const resolvedPrimaryKey = { columns: primaryKeyColumns };
178
+ const resolvedApplyMode = applyMode ?? "insert";
179
+ const customPrimaryKeyName = !Array.isArray(primaryKey) ? primaryKey?.name : undefined;
180
+ const primaryKeyConstraintName = customPrimaryKeyName ?? `${tableName}_pkey`;
181
+ const probeColumns = getColumns(pgTable(tableName, makeColumns()));
182
+ const columnLevelPkKeys = Object.entries(probeColumns).filter(([, col]) => col.primary === true).map(([key]) => key);
183
+ const specPropertyKeys = primaryKeyColumns.map((key) => {
184
+ if (probeColumns[key])
185
+ return key;
186
+ const byColumnName = Object.entries(probeColumns).find(([, col]) => col.name === key);
187
+ if (byColumnName)
188
+ return byColumnName[0];
189
+ throw new Error(`[pgxsinkit] ${tableName}: primaryKey column "${key}" not found among the table's columns`);
190
+ });
191
+ if (columnLevelPkKeys.length > 1) {
192
+ 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`);
193
+ }
194
+ if (columnLevelPkKeys.length === 1) {
195
+ const columnLevelPkKey = columnLevelPkKeys[0];
196
+ if (specPropertyKeys.length !== 1 || specPropertyKeys[0] !== columnLevelPkKey) {
197
+ 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`);
198
+ }
199
+ if (customPrimaryKeyName != null && customPrimaryKeyName !== `${tableName}_pkey`) {
200
+ 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`);
201
+ }
202
+ }
203
+ const emitPrimaryKey = columnLevelPkKeys.length === 0;
168
204
  const resolvedClientProjection = resolvedMode !== "writeonly" || clientProjection != null ? {
169
205
  ...clientProjection ?? {},
170
206
  syncedTable: clientProjection?.syncedTable ?? tableName,
@@ -173,10 +209,19 @@ function defineSyncTable(input) {
173
209
  journalTable: clientProjection?.journalTable ?? `${tableName}_mutations`
174
210
  } : {}
175
211
  } : undefined;
176
- const extrasFn = policies || extras ? (self) => [
177
- ...policies ?? [],
178
- ...extras ? extras(self) : []
179
- ] : undefined;
212
+ const extrasFn = policies || extras || emitPrimaryKey ? (self) => {
213
+ const consumerExtras = [...policies ?? [], ...extras ? extras(self) : []];
214
+ for (const extra of consumerExtras) {
215
+ if (extra instanceof PrimaryKeyBuilder) {
216
+ 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`);
217
+ }
218
+ }
219
+ if (!emitPrimaryKey)
220
+ return consumerExtras;
221
+ const selfCols = self;
222
+ const pkColumns = specPropertyKeys.map((k) => selfCols[k]);
223
+ return [...consumerExtras, pgPrimaryKey({ name: primaryKeyConstraintName, columns: pkColumns })];
224
+ } : undefined;
180
225
  const table = schema ? schema.table(tableName, makeColumns(), extrasFn) : pgTable(tableName, makeColumns(), extrasFn);
181
226
  const resolvedRowFilter = typeof shape?.rowFilter === "function" ? shape.rowFilter(getColumns(table)) : shape?.rowFilter ?? undefined;
182
227
  const resolvedShape = resolvedMode !== "writeonly" || shape != null ? {
@@ -197,6 +242,7 @@ function defineSyncTable(input) {
197
242
  ...otherRest,
198
243
  mode: resolvedMode,
199
244
  primaryKey: resolvedPrimaryKey,
245
+ applyMode: resolvedApplyMode,
200
246
  ...resolvedShape != null ? { shape: resolvedShape } : {},
201
247
  ...governance != null ? { governance } : {},
202
248
  ...resolvedClientProjection != null ? { clientProjection: resolvedClientProjection } : {},
@@ -379,7 +425,8 @@ function deriveSyncColumnTypes(entry) {
379
425
  return {
380
426
  name: columnName,
381
427
  sqlType,
382
- isArray: (typedColumn.dimensions ?? 0) > 0
428
+ isArray: (typedColumn.dimensions ?? 0) > 0,
429
+ isEnum: typedColumn.columnType?.startsWith("PgEnum") === true
383
430
  };
384
431
  });
385
432
  }
@@ -1028,6 +1075,7 @@ function asReadonly(entry) {
1028
1075
  localTable: entry.localTable,
1029
1076
  mode: "readonly",
1030
1077
  primaryKey: entry.primaryKey,
1078
+ applyMode: entry.applyMode,
1031
1079
  ...entry.shape != null ? { shape: entry.shape } : {},
1032
1080
  ...readonlyProjection != null ? { clientProjection: readonlyProjection } : {},
1033
1081
  ...entry.serverProjection != null ? { serverProjection: entry.serverProjection } : {},
@@ -1456,4 +1504,4 @@ export {
1456
1504
  CLOCK_US_CALL_SQL_TEXT
1457
1505
  };
1458
1506
 
1459
- //# debugId=D6E103DA9ACAE24B64756E2164756E21
1507
+ //# debugId=92A50F53676F654B64756E2164756E21