@rebasepro/server-postgres 0.12.1-canary.g181d0fe → 0.12.1-canary.g35be8cb

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.es.js CHANGED
@@ -11364,11 +11364,27 @@ function buildProperties(meta, enumMap) {
11364
11364
  return properties;
11365
11365
  }
11366
11366
  /**
11367
- * Owning relations, derived from this table's foreign keys. Mirrors the shape
11368
- * `generateCollectionFile` emits, except `target` uses the slug string form
11369
- * rather than a thunk to a module import.
11370
- */
11371
- function buildRelations(meta, slugByTable) {
11367
+ * Owning relations, derived from this table's foreign keys — the same shape
11368
+ * `generateCollectionFile` writes into a collection file: a `relation` property
11369
+ * whose nested descriptor carries `kind`, a `target` thunk and the `localKey`.
11370
+ *
11371
+ * The shape is load-bearing, not cosmetic. `resolveCollectionRelations` reads
11372
+ * relations from `property.relation` and `resolveRelation` requires `target` to
11373
+ * be a thunk; this used to emit `target`/`cardinality`/`localKey` flat on the
11374
+ * property with the slug as a bare string, which satisfies neither. Nothing
11375
+ * threw — the resolver simply skipped every such property and reported that the
11376
+ * collection had no relations. So an introspected BaaS collection had its FK
11377
+ * columns removed from `properties` (they "surface as relations") and then no
11378
+ * resolvable relation to surface as, which is why writing the FK column
11379
+ * directly came back as `has no field 'product_id'`: `assertKnownWriteFields`
11380
+ * learns that column from the resolved relation's `localKey`.
11381
+ *
11382
+ * The thunk closes over the collections being built in this same pass rather
11383
+ * than importing a module, which is what a runtime introspection has instead of
11384
+ * generated files. It is called lazily, after the map is fully populated, so a
11385
+ * table may reference one introspected later.
11386
+ */
11387
+ function buildRelations(meta, slugByTable, collectionBySlug) {
11372
11388
  const relations = {};
11373
11389
  for (const fk of meta.fks) {
11374
11390
  const targetSlug = slugByTable.get(fk.foreign_table_name);
@@ -11378,10 +11394,11 @@ function buildRelations(meta, slugByTable) {
11378
11394
  relations[key] = {
11379
11395
  name: humanize(key),
11380
11396
  type: "relation",
11381
- target: targetSlug,
11382
- cardinality: "one",
11383
- direction: "owning",
11384
- localKey: fk.column_name
11397
+ relation: {
11398
+ kind: "belongsTo",
11399
+ target: () => collectionBySlug.get(targetSlug),
11400
+ localKey: fk.column_name
11401
+ }
11385
11402
  };
11386
11403
  }
11387
11404
  return relations;
@@ -11396,6 +11413,7 @@ function buildCollectionsFromSchema({ tablesMap, enumMap, joinTables }, pgSchema
11396
11413
  const slugByTable = /* @__PURE__ */ new Map();
11397
11414
  for (const tableName of tablesMap.keys()) if (!joinTables.has(tableName)) slugByTable.set(tableName, tableName);
11398
11415
  const collections = [];
11416
+ const collectionBySlug = /* @__PURE__ */ new Map();
11399
11417
  for (const [tableName, meta] of tablesMap) {
11400
11418
  if (joinTables.has(tableName)) continue;
11401
11419
  const collectionName = humanize(tableName);
@@ -11408,10 +11426,11 @@ function buildCollectionsFromSchema({ tablesMap, enumMap, joinTables }, pgSchema
11408
11426
  icon: getIconForTable(tableName),
11409
11427
  properties: {
11410
11428
  ...buildProperties(meta, enumMap),
11411
- ...buildRelations(meta, slugByTable)
11429
+ ...buildRelations(meta, slugByTable, collectionBySlug)
11412
11430
  }
11413
11431
  };
11414
11432
  collections.push(collection);
11433
+ collectionBySlug.set(tableName, collection);
11415
11434
  }
11416
11435
  return collections;
11417
11436
  }
@@ -11912,11 +11931,13 @@ function createPostgresBootstrapper(pgConfig) {
11912
11931
  */
11913
11932
  async ensureCollectionSchema(collections, driverResult, log) {
11914
11933
  const internals = driverResult.internals;
11915
- const { ensureCollectionTables } = await import("./ensure-collection-tables-Vu-GRELM.js");
11916
- return { applied: (await ensureCollectionTables({ async query(text) {
11934
+ const { ensureCollectionTables } = await import("./ensure-collection-tables-DsDsNl6o.js");
11935
+ const plan = await ensureCollectionTables({ async query(text) {
11917
11936
  const result = await internals.db.execute(sql.raw(text));
11918
11937
  return { rows: result.rows ?? (Array.isArray(result) ? result : []) };
11919
- } }, collections, log)).actions.length };
11938
+ } }, collections, log);
11939
+ for (const failure of plan.failures) logger.warn(`🔗 [schema] Could not add foreign key "${failure.target}" — the column exists and the collection still serves, but rows are not policed by this constraint: ${failure.error}`);
11940
+ return { applied: plan.actions.length - plan.failures.length };
11920
11941
  },
11921
11942
  /**
11922
11943
  * Apply the collections' RLS policies — ENABLE ROW LEVEL SECURITY and the
@@ -11935,7 +11956,7 @@ function createPostgresBootstrapper(pgConfig) {
11935
11956
  */
11936
11957
  async ensureCollectionPolicies(collections, driverResult, log) {
11937
11958
  const internals = driverResult.internals;
11938
- const { ensureCollectionPolicies } = await import("./ensure-collection-policies-CT-zIUWA.js");
11959
+ const { ensureCollectionPolicies } = await import("./ensure-collection-policies-Vl3Cv1Q9.js");
11939
11960
  const outcome = await ensureCollectionPolicies({ async query(text) {
11940
11961
  const result = await internals.db.execute(sql.raw(text));
11941
11962
  return { rows: result.rows ?? (Array.isArray(result) ? result : []) };