@pgxsinkit/contracts 0.6.1 → 0.6.3

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/index.js CHANGED
@@ -80,7 +80,7 @@ var unixMicrosecondsSchema = z.string().regex(/^[0-9]+$/);
80
80
  import { z as z2 } from "zod";
81
81
 
82
82
  // packages/contracts/src/predicate.ts
83
- import { getColumnTable, getTableName } from "drizzle-orm";
83
+ import { getColumnTable, getTableName, getTableSchema } from "drizzle-orm";
84
84
  function isLeafPredicate(node) {
85
85
  return "col" in node && "op" in node;
86
86
  }
@@ -137,11 +137,19 @@ var p = {
137
137
  and: (...nodes) => ({ and: nodes }),
138
138
  or: (...nodes) => ({ or: nodes }),
139
139
  not: (node) => ({ not: node }),
140
- subquery: (projected, where) => ({
141
- table: getTableName(getColumnTable(projected)),
142
- project: projected.name,
143
- ...where !== undefined ? { where } : {}
144
- }),
140
+ subquery: (projected, where) => {
141
+ const table = getColumnTable(projected);
142
+ const name = getTableName(table);
143
+ const schema = getTableSchema(table);
144
+ if (name.includes(".") || schema?.includes(".")) {
145
+ throw new Error(`Circuits subquery for column "${projected.name}" has a dot in a Postgres schema or table identifier`);
146
+ }
147
+ return {
148
+ table: schema === undefined || schema === "public" ? name : `${schema}.${name}`,
149
+ project: projected.name,
150
+ ...where !== undefined ? { where } : {}
151
+ };
152
+ },
145
153
  in: (column, ref) => ({ col: column.name, in: ref }),
146
154
  notIn: (column, ref) => ({
147
155
  col: column.name,
@@ -301,9 +309,9 @@ import {
301
309
  pgTable,
302
310
  pgView,
303
311
  primaryKey as pgPrimaryKey,
304
- PrimaryKeyBuilder
312
+ PrimaryKeyBuilder,
313
+ varchar
305
314
  } from "drizzle-orm/pg-core";
306
- import { varchar } from "drizzle-orm/pg-core";
307
315
  import { getColumns } from "drizzle-orm/utils";
308
316
 
309
317
  // packages/contracts/src/event-stream.ts
@@ -569,7 +577,6 @@ function defineSyncRegistry(input) {
569
577
  validateSyncTableEntry(entry);
570
578
  }
571
579
  validateRegistryTableUniqueness(input.tables);
572
- validateRegistryShapeKeyUniqueness(input.tables);
573
580
  validateRegistryLocalIndexNames(input.tables);
574
581
  validateRegistryLifecycleGroups(input.tables);
575
582
  validateStorageDeclaration(input.storage);
@@ -612,15 +619,15 @@ function validateRegistryLocalIndexNames(registry) {
612
619
  }
613
620
  }
614
621
  }
615
- function validateRegistryShapeKeyUniqueness(registry) {
622
+ function validateRegistryShapeKeyUniqueness(registry, shapeFor = (entry) => entry.shape) {
616
623
  const declaredBy = new Map;
617
624
  for (const [key, entry] of Object.entries(registry)) {
618
- const shape = entry.shape;
625
+ const shape = shapeFor(entry);
619
626
  if (shape === undefined)
620
627
  continue;
621
628
  const firstKey = declaredBy.get(shape.shapeKey);
622
629
  if (firstKey !== undefined) {
623
- throw new Error(`shapeKey "${shape.shapeKey}" is declared by two registry entries ("${firstKey}" and "${key}"): ` + `a duplicate shapeKey makes the second entry unreachable \u2014 the control plane resolves a ` + `subscription by shapeKey and would serve the first entry's shape. Give each entry a unique ` + `shape.shapeKey, or use defineReadProjection (a distinct "as") for a second shape over one table.`);
630
+ throw new Error(`shapeKey "${shape.shapeKey}" is declared by two registry entries ("${firstKey}" and "${key}"): ` + `give each entry a unique shape.shapeKey, or use defineReadProjection (a distinct "as") ` + `for a second shape over one table.`);
624
631
  }
625
632
  declaredBy.set(shape.shapeKey, key);
626
633
  }
@@ -726,30 +733,41 @@ function resolveServerVersionColumnName(entry) {
726
733
  function attachSyncRegistrySchema(registry, schema) {
727
734
  const normalizedSchema = normalizeSchemaName(schema);
728
735
  if (!normalizedSchema) {
736
+ validateRegistryShapeKeyUniqueness(registry);
729
737
  return registry;
730
738
  }
739
+ const qualifiedShapes = new Map;
731
740
  for (const entry of Object.values(registry)) {
732
741
  const tableSchema = getTableConfig(entry.table).schema ?? "public";
733
742
  if (tableSchema !== normalizedSchema) {
734
743
  throw new Error(`Registry schema ${normalizedSchema} does not match table schema ${tableSchema} for ${getTableConfig(entry.table).name}`);
735
744
  }
736
- if (!entry.shape) {
745
+ const { shape } = entry;
746
+ if (!shape) {
737
747
  continue;
738
748
  }
739
749
  const qualify = (name) => name.includes(".") ? name : `${normalizedSchema}.${name}`;
740
- const qualifiedTableName = `${normalizedSchema}.${entry.shape.tableName}`;
741
- entry.shape = {
742
- ...entry.shape,
743
- physicalTable: entry.shape.physicalTable ? qualify(entry.shape.physicalTable) : qualifiedTableName,
744
- ...entry.shape.shapeKey === entry.shape.tableName ? { shapeKey: qualifiedTableName } : {}
745
- };
750
+ const separator = shape.tableName.indexOf(".");
751
+ if (separator !== -1 && shape.tableName.slice(0, separator) !== normalizedSchema) {
752
+ throw new Error(`Registry schema ${normalizedSchema} does not match shape table ${shape.tableName}`);
753
+ }
754
+ const qualifiedTableName = qualify(shape.tableName);
755
+ qualifiedShapes.set(entry, {
756
+ ...shape,
757
+ physicalTable: shape.physicalTable ? qualify(shape.physicalTable) : qualifiedTableName,
758
+ ...shape.shapeKey === shape.tableName ? { shapeKey: qualifiedTableName } : {}
759
+ });
746
760
  }
761
+ validateRegistryShapeKeyUniqueness(registry, (entry) => qualifiedShapes.get(entry));
747
762
  Object.defineProperty(registry, syncRegistrySchemaSymbol, {
748
763
  value: normalizedSchema,
749
764
  enumerable: false,
750
765
  configurable: false,
751
766
  writable: false
752
767
  });
768
+ for (const [entry, shape] of qualifiedShapes) {
769
+ entry.shape = shape;
770
+ }
753
771
  return registry;
754
772
  }
755
773
  function getSyncRegistrySchema(registry) {
@@ -2392,4 +2410,4 @@ export {
2392
2410
  withRetention
2393
2411
  };
2394
2412
 
2395
- //# debugId=68C3BDD5068DEA1A64756E2164756E21
2413
+ //# debugId=17A63423B5FD5DC464756E2164756E21