@rebasepro/server-postgres 0.9.1-canary.a57c262 → 0.9.1-canary.ad25bc0

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.
@@ -40,22 +40,52 @@ export declare class FetchService {
40
40
  * the target relation so actual row data is returned.
41
41
  */
42
42
  private buildWithConfig;
43
+ /**
44
+ * Detect if a many-to-many relation uses a junction table in the Drizzle schema.
45
+ */
46
+ private isJunctionRelation;
43
47
  /**
44
48
  * Get the Drizzle relation name on the junction table that points to the actual target row.
45
49
  * For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
46
50
  */
47
51
  private getJunctionTargetRelationName;
52
+ /**
53
+ * Convert a db.query result row (with nested relation objects) to a flat row.
54
+ * Handles:
55
+ * - Type normalization (dates, numbers, NaN) via normalizeDbValues
56
+ * - Converting nested relation objects to { id, path, __type: "relation" } for CMS
57
+ * - Flattening junction-table many-to-many results
58
+ *
59
+ * The row's own address is not among them: it is derived by the consumer
60
+ * from the collection's primary keys.
61
+ */
62
+ private drizzleResultToRow;
48
63
  /**
49
64
  * Post-fetch joinPath relations for a single flat row.
50
65
  * joinPath relations cannot be expressed via Drizzle's `with` config,
51
66
  * so they must be loaded separately after the primary query.
52
67
  */
53
68
  private resolveJoinPathRelations;
69
+ /**
70
+ * Post-fetch joinPath relations for a batch of flat rows.
71
+ * Uses batch fetching to avoid N+1 queries for list views.
72
+ */
73
+ private resolveJoinPathRelationsBatch;
54
74
  /**
55
75
  * Resolves joinPath relations for raw REST rows and directly injects them.
56
76
  * Uses RelationService to query the database and maps results back to the flattened objects.
57
77
  */
58
78
  private resolveJoinPathRelationsBatchRest;
79
+ /**
80
+ * Convert a db.query result row to a flat REST-style row with populated relations.
81
+ *
82
+ * Every column is copied through under its own name, with the value Postgres
83
+ * returned. This used to open with a synthesized `id` and then skip the key
84
+ * column, which renamed it (a `sku` primary key was served as `id`, and `sku`
85
+ * did not appear at all) and restringified it (`42` → `"42"`). Consumers that
86
+ * need an address derive it from the collection's primary keys.
87
+ */
88
+ private drizzleResultToRestRow;
59
89
  /**
60
90
  * Build db.query-compatible options from standard fetch options.
61
91
  * Handles filter, search, orderBy, limit, and cursor-based pagination.
@@ -86,10 +116,8 @@ export declare class FetchService {
86
116
  }): Promise<Record<string, unknown>[]>;
87
117
  /**
88
118
  * Fallback path used when db.query is unavailable.
89
- *
90
- * The primary path runs the results through `toCmsRow`, which maps
91
- * relations from what drizzle already nested — no query per row. This one
92
- * has no nesting to read, so it resolves relations itself, in batches.
119
+ * The primary path uses drizzleResultToRow which handles relation
120
+ * mapping without N+1 queries.
93
121
  *
94
122
  * Process raw database results into flat rows with relations.
95
123
  */
@@ -20,40 +20,6 @@ export declare class RelationService {
20
20
  private db;
21
21
  private registry;
22
22
  constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
23
- /**
24
- * One target row, as the {@link RelatedRow} everything here returns.
25
- *
26
- * Eight sites built this by hand, which is how the address came to be the
27
- * target's first key column in all eight — one edit, eight places to miss.
28
- *
29
- * `resolveNested` is the one thing they did not agree on, and the
30
- * disagreement was invisible: the single-parent fetches pass `db` and
31
- * `registry` to `parseDataFromServer`, so the target's *own* relations get
32
- * resolved too, while the batch paths deliberately do not — a query per
33
- * target row is the N+1 the batching exists to avoid. Naming the parameter
34
- * makes that a decision rather than a difference between two call sites
35
- * nobody was comparing.
36
- */
37
- private toRelatedRow;
38
- /**
39
- * A WHERE matching any of `parentIds`, by the whole key.
40
- *
41
- * A single key is an `IN (…)`. A composite one cannot be: matching
42
- * `tenant_id IN (1, 1)` collects every row of tenant 1, so two parents that
43
- * share their first column each receive the other's relations. It becomes
44
- * an OR of ANDs — one exact address per parent — which Postgres indexes the
45
- * same way it would a multi-column key lookup.
46
- */
47
- private parentKeyCondition;
48
- /**
49
- * Reject a relation that cannot express a composite-keyed parent.
50
- *
51
- * `localKey` and `foreignKeyOnTarget` are single column names: one column
52
- * cannot reference a two-column key, so such a relation has no correct
53
- * reading. Left alone it would silently match on the first key column and
54
- * hand a tenant's rows to its neighbour — say so instead.
55
- */
56
- private assertSingleKeyAddressable;
57
23
  /**
58
24
  * Fetch rows related to a parent row through a specific relation
59
25
  */
@@ -106,6 +72,7 @@ export declare class RelationService {
106
72
  relationKey: string;
107
73
  relation: Relation;
108
74
  newValue: unknown;
75
+ currentId?: string | number;
109
76
  }>): Promise<void>;
110
77
  /**
111
78
  * Handle inverse relations with joinPath
@@ -24,80 +24,4 @@ export interface DrizzleColumnMeta {
24
24
  export declare function getColumnMeta(col: AnyPgColumn): DrizzleColumnMeta;
25
25
  export declare function getCollectionByPath(collectionPath: string, registry: PostgresCollectionRegistry): CollectionConfig;
26
26
  export declare function getTableForCollection(collection: CollectionConfig, registry: PostgresCollectionRegistry): PgTable<any>;
27
- /**
28
- * The key columns a collection's rows are addressed by.
29
- *
30
- * Three tiers, in order: properties marked `isId`, the primary keys of the
31
- * drizzle schema, and finally a column literally named `id`. Only the first is
32
- * visible to the browser, which is why a key known only to drizzle is reported
33
- * at boot — see {@link warnOnKeysTheAdminCannotResolve}.
34
- *
35
- * Returns `[]` when nothing resolves, rather than throwing. It used to open by
36
- * resolving the table, which throws when there is none — so the `isId` tier,
37
- * which needs no table at all, was unreachable for exactly the collections
38
- * most likely to have no table registered. Every caller that wanted "no keys"
39
- * to mean "no keys" had to spell that out in a try/catch.
40
- *
41
- * Callers that cannot proceed without a key must say so themselves, naming the
42
- * collection: an empty array here means "this collection has no address", which
43
- * is a different answer in a notification (broadcast a wildcard) than in a save
44
- * (fail).
45
- */
46
27
  export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
47
- /**
48
- * The key columns, for callers that cannot do their job without one.
49
- *
50
- * {@link getPrimaryKeys} answers "what keys, if any" and returns `[]` for a
51
- * collection with no address. Most of this driver, though, is building a WHERE
52
- * clause and has no meaning without a key — for those, an empty array is not an
53
- * answer, and indexing `[0]` into it produces `Cannot read properties of
54
- * undefined` three frames from where the real problem is. This says what is
55
- * wrong and which collection it is wrong about.
56
- */
57
- export declare function requirePrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
58
- /**
59
- * Collections whose key the *browser* cannot resolve, and what it will do
60
- * instead.
61
- *
62
- * The two sides resolve keys from different evidence. This driver reads, in
63
- * order: properties marked `isId`, the primary keys of the Drizzle schema, then
64
- * a column literally named `id`. The admin shares the `CollectionConfig` — it
65
- * compiles the same collection files into its bundle — but never the Drizzle
66
- * schema, so the middle tier is invisible to it.
67
- *
68
- * Nothing can normalize this at runtime: the server does not serve the admin
69
- * its collections, so a key resolved here cannot be handed over there. The
70
- * config files are the only thing both sides read, so the fix is an edit to
71
- * them, and the most this can do is say exactly which edit.
72
- *
73
- * Two shapes, and the second is the dangerous one:
74
- *
75
- * - No `isId`, no `id` property → the admin resolves no address, warns in the
76
- * console, and rows cannot be opened or linked.
77
- * - No `isId`, but an `id` property that is *not* the key → the admin addresses
78
- * rows by `id` while this driver reads the address as the real key. Nothing
79
- * errors: the addresses look right and route wrong.
80
- */
81
- export declare function findUnresolvableKeyCollections(collections: CollectionConfig[], registry: PostgresCollectionRegistry): {
82
- collection: CollectionConfig;
83
- keys: PrimaryKeyInfo[];
84
- shadowedByIdProperty: boolean;
85
- }[];
86
- /**
87
- * Report the collections from {@link findUnresolvableKeyCollections} at boot,
88
- * with the edit that fixes each one.
89
- *
90
- * Grouped by failure, not by collection: the shadowed case is a routing bug and
91
- * the silent case is a missing feature, and they deserve different urgency.
92
- */
93
- export declare function warnOnKeysTheAdminCannotResolve(collections: CollectionConfig[], registry: PostgresCollectionRegistry): void;
94
- /**
95
- * The address of a row: derived from the collection's primary keys, because a
96
- * row does not carry one — it is exactly its columns.
97
- *
98
- * Falls back to a literal `id` column, for a row that reached us from somewhere
99
- * other than this driver. Returns `""` when there is no key and no `id` —
100
- * callers decide what that means, since "unaddressable" is a different answer
101
- * in a notification (broadcast a wildcard) than in a save (fail).
102
- */
103
- export declare function deriveRowAddress(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): string;
@@ -1,4 +1,4 @@
1
1
  export { FetchService } from "./FetchService";
2
2
  export { PersistService } from "./PersistService";
3
3
  export { RelationService } from "./RelationService";
4
- export { getCollectionByPath, getTableForCollection, getPrimaryKeys, deriveRowAddress, parseIdValues, buildCompositeId } from "./collection-helpers";
4
+ export { getCollectionByPath, getTableForCollection, getPrimaryKeys, parseIdValues, buildCompositeId } from "./collection-helpers";
@@ -177,15 +177,8 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
177
177
  /**
178
178
  * Send a lightweight row-level patch to a collection subscriber.
179
179
  * The client can merge this into its cached data for instant feedback.
180
- *
181
- * The key columns ride along: the patch names a row by address, and the
182
- * client has to find that row among the ones it cached — which carry
183
- * columns and no address. The SDK holds no collection config to derive one
184
- * from, so this is the only place the mapping can come from.
185
180
  */
186
181
  private sendCollectionPatch;
187
- /** The key columns of the collection at `path`, if they can be resolved. */
188
- private primaryKeysForPath;
189
182
  private sendError;
190
183
  private sendMessage;
191
184
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgres",
3
3
  "type": "module",
4
- "version": "0.9.1-canary.a57c262",
4
+ "version": "0.9.1-canary.ad25bc0",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -71,20 +71,18 @@
71
71
  "execa": "^9.6.1",
72
72
  "pg": "^8.21.0",
73
73
  "ws": "^8.21.0",
74
- "@rebasepro/codegen": "0.9.1-canary.a57c262",
75
- "@rebasepro/common": "0.9.1-canary.a57c262",
76
- "@rebasepro/utils": "0.9.1-canary.a57c262",
77
- "@rebasepro/server": "0.9.1-canary.a57c262",
78
- "@rebasepro/types": "0.9.1-canary.a57c262"
74
+ "@rebasepro/codegen": "0.9.1-canary.ad25bc0",
75
+ "@rebasepro/types": "0.9.1-canary.ad25bc0",
76
+ "@rebasepro/utils": "0.9.1-canary.ad25bc0",
77
+ "@rebasepro/server": "0.9.1-canary.ad25bc0",
78
+ "@rebasepro/common": "0.9.1-canary.ad25bc0"
79
79
  },
80
80
  "devDependencies": {
81
- "@hono/node-server": "^2.0.9",
82
81
  "@types/jest": "^30.0.0",
83
82
  "@types/node": "^25.9.3",
84
83
  "@types/pg": "^8.20.0",
85
84
  "@types/ws": "^8.18.1",
86
85
  "@vitejs/plugin-react": "^6.0.2",
87
- "hono": "^4.12.25",
88
86
  "jest": "^30.4.2",
89
87
  "ts-jest": "^29.4.11",
90
88
  "typescript": "^6.0.3",
@@ -105,7 +103,6 @@
105
103
  "test:lint": "eslint \"src/**\" --quiet",
106
104
  "test": "jest --passWithNoTests",
107
105
  "test:e2e": "vitest run --config vitest.e2e.config.ts",
108
- "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f",
109
- "smoke:baas": "tsx scripts/smoke-baas.ts"
106
+ "clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
110
107
  }
111
108
  }
@@ -29,7 +29,6 @@ import {
29
29
  import { sql as drizzleSql } from "drizzle-orm";
30
30
  import { buildPropertyCallbacks, buildSdkData, resolveCollectionRelations, updateDateAutoValues } from "@rebasepro/common";
31
31
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
32
- import { deriveRowAddress } from "./services/collection-helpers";
33
32
  import { HistoryService } from "./history/HistoryService";
34
33
  import { mergeDeep } from "@rebasepro/utils";
35
34
  import { logger } from "@rebasepro/server";
@@ -653,19 +652,8 @@ export class PostgresBackendDriver implements DataDriver {
653
652
  }
654
653
  }
655
654
 
656
- // The row is exactly its columns, so its address is derived, not read
657
- // off it: `savedRow.id` is undefined for every table whose key is not
658
- // literally named `id`, and is ordinary data for a table that has such
659
- // a column without it being the key.
660
- const savedId = deriveRowAddress(
661
- savedRow,
662
- (resolvedCollection ?? collection) as CollectionConfig,
663
- this.registry
664
- );
665
- // `values` are the row's columns — all of them. For an `id`-keyed table
666
- // that includes `id`, which used to be stripped here because it was the
667
- // synthesized address rather than the column it now is.
668
- const savedValues = savedRow;
655
+ const savedId = savedRow.id as string | number;
656
+ const { id: _savedId, ...savedValues } = savedRow;
669
657
 
670
658
  if (globalCallbacks?.afterSave || callbacks?.afterSave || propertyCallbacks?.afterSave) {
671
659
  // 1. Global callbacks first
@@ -710,7 +698,7 @@ export class PostgresBackendDriver implements DataDriver {
710
698
  if (this.historyService && resolvedCollection?.history) {
711
699
  this.historyService.recordHistory({
712
700
  tableName: path,
713
- id: savedId,
701
+ id: savedId.toString(),
714
702
  action: status === "new" ? "create" : "update",
715
703
  values: savedValues as Record<string, unknown>,
716
704
  previousValues: previousValuesForHistory as Record<string, unknown> | undefined,
@@ -722,14 +710,14 @@ export class PostgresBackendDriver implements DataDriver {
722
710
  if (this._deferNotifications) {
723
711
  this._pendingNotifications.push({
724
712
  path,
725
- id: savedId,
713
+ id: savedId.toString(),
726
714
  row: savedRow,
727
715
  databaseId: resolvedCollection?.databaseId
728
716
  });
729
717
  } else {
730
718
  await this.realtimeService.notifyUpdate(
731
719
  path,
732
- savedId,
720
+ savedId.toString(),
733
721
  savedRow,
734
722
  resolvedCollection?.databaseId
735
723
  );
@@ -855,10 +843,7 @@ export class PostgresBackendDriver implements DataDriver {
855
843
  }: DeleteProps<M>): Promise<void> {
856
844
 
857
845
  const targetPath = row.path;
858
- // The callbacks' `row` is the row: its columns, nothing else. The address
859
- // travels beside it as `id`, so merging it in here only ever invented an
860
- // `id` field for tables that have no such column.
861
- const targetRow: Record<string, unknown> = { ...(row.values ?? {}) };
846
+ const targetRow: Record<string, unknown> = { id: row.id, ...(row.values ?? {}) };
862
847
 
863
848
  // Resolve from backend registry to restore callbacks lost during WebSocket serialization
864
849
  const {
@@ -4,7 +4,7 @@
4
4
  * Implements the `BackendBootstrapper` interface for PostgreSQL.
5
5
  */
6
6
 
7
- import { Relations, sql } from "drizzle-orm";
7
+ import { getTableName, isTable, Relations, sql } from "drizzle-orm";
8
8
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
9
9
  import { PgEnum, PgTable } from "drizzle-orm/pg-core";
10
10
  import type { RebasePgTable } from "./types";
@@ -21,7 +21,6 @@ import {
21
21
  } from "@rebasepro/types";
22
22
  import { PostgresBackendDriver } from "./PostgresBackendDriver";
23
23
  import { RealtimeService } from "./services/realtimeService";
24
- import { buildCollectionRegistry } from "./collections/buildRegistry";
25
24
  import { DatabasePoolManager } from "./databasePoolManager";
26
25
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
27
26
  import { createEmailService, type EmailConfig, type EmailService, logger } from "@rebasepro/server";
@@ -162,17 +161,30 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
162
161
  }
163
162
 
164
163
  const activeCollections = introspectedCollections ?? collections;
164
+
165
+ // Create a fresh registry for this driver
166
+ const registry = new PostgresCollectionRegistry();
167
+ if (activeCollections) {
168
+ registry.registerMultiple(activeCollections);
169
+ logger.info(`📋 [PostgresRegistry] Registered ${registry.getCollections().length} collections: [${registry.getCollections().map(c => c.slug).join(", ")}]`);
170
+ }
171
+
165
172
  const schemaTables = introspectedTables ?? pgConfig.schema?.tables;
166
- const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
167
173
 
168
- // Create a fresh registry for this driver. Registration order is
169
- // load-bearing, so it lives in one place — see `buildCollectionRegistry`.
170
- const registry = buildCollectionRegistry({
171
- collections: activeCollections,
172
- tables: schemaTables,
173
- enums: pgConfig.schema?.enums as Record<string, PgEnum<[string, ...string[]]>> | undefined,
174
- relations: schemaRelations
175
- });
174
+ // Register tables
175
+ if (schemaTables) {
176
+ Object.values(schemaTables).forEach((table) => {
177
+ if (isTable(table)) {
178
+ const tableName = getTableName(table);
179
+ registry.registerTable(table as PgTable, tableName);
180
+ }
181
+ });
182
+ }
183
+
184
+ if (pgConfig.schema?.enums) registry.registerEnums(pgConfig.schema.enums as Record<string, PgEnum<[string, ...string[]]>>);
185
+
186
+ const schemaRelations = introspectedRelations ?? (pgConfig.schema?.relations as Record<string, Relations> | undefined);
187
+ if (schemaRelations) registry.registerRelations(schemaRelations);
176
188
 
177
189
  // Patch Drizzle's PgArray columns to handle NULL values safely.
178
190
  // Drizzle's mapFromDriverValue crashes with "value.map is not a function"
@@ -20,19 +20,12 @@ import { logger } from "@rebasepro/server";
20
20
  export interface SerializedEntityData {
21
21
  /** Scalar column values ready for INSERT/UPDATE. */
22
22
  scalarData: Record<string, unknown>;
23
- /**
24
- * Inverse relation updates that must be applied to target tables.
25
- *
26
- * No address here: the row being written does not know its own. These are
27
- * applied by `PersistService`, which addresses them with the id it holds —
28
- * the one it was given for an update, or the one the INSERT returned — and
29
- * that is the authority. This item used to carry a `currentId` derived from
30
- * the *input* values, which nothing ever read.
31
- */
23
+ /** Inverse relation updates that must be applied to target tables. */
32
24
  inverseRelationUpdates: Array<{
33
25
  relationKey: string;
34
26
  relation: Relation;
35
27
  newValue: unknown;
28
+ currentId?: string | number;
36
29
  }>;
37
30
  /** JoinPath relation updates that require multi-hop writes. */
38
31
  joinPathRelationUpdates: Array<{
@@ -112,6 +105,7 @@ joinPathRelationUpdates: [] };
112
105
  relationKey: string;
113
106
  relation: Relation;
114
107
  newValue: unknown;
108
+ currentId?: string | number;
115
109
  }> = [];
116
110
  const joinPathRelationUpdates: Array<{
117
111
  relationKey: string;
@@ -152,10 +146,12 @@ joinPathRelationUpdates: [] };
152
146
  } else if (relation.direction === "inverse" && relation.foreignKeyOnTarget) {
153
147
  // Inverse relation: Need to update the target table's FK
154
148
  const serializedValue = serializePropertyToServer(effectiveValue, property);
149
+ const pks = getPrimaryKeys(collection, registry!);
155
150
  inverseRelationUpdates.push({
156
151
  relationKey: key,
157
152
  relation,
158
- newValue: serializedValue
153
+ newValue: serializedValue,
154
+ currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
159
155
  });
160
156
  // Don't add the original relation property to the result
161
157
  continue;
@@ -174,10 +170,12 @@ joinPathRelationUpdates: [] };
174
170
  });
175
171
  } else {
176
172
  // Many inverse joinPath: capture as inverse relation update
173
+ const pks = getPrimaryKeys(collection, registry!);
177
174
  inverseRelationUpdates.push({
178
175
  relationKey: key,
179
176
  relation,
180
- newValue: serializedValue
177
+ newValue: serializedValue,
178
+ currentId: (row.id as string | number | undefined) || buildCompositeId(row, pks)
181
179
  });
182
180
  }
183
181
  // Don't add the original relation property to the result
@@ -1,6 +1,6 @@
1
1
  import { CollectionConfig, NumberProperty, Property, Relation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty } from "@rebasepro/types";
2
2
  import { getPrimaryKeys } from "../services/collection-helpers";
3
- import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig } from "@rebasepro/common";
3
+ import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules } from "@rebasepro/common";
4
4
  import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
5
5
  import { logger } from "@rebasepro/server";
6
6
  // --- Helper Functions ---
@@ -545,10 +545,6 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
545
545
  });
546
546
  schemaContent += "\n";
547
547
 
548
- // Junction policy derivation needs every declaring side of each junction,
549
- // not just the first relation that reached it in the walk below.
550
- const junctionSpecs = resolveJunctionSpecs(collections);
551
-
552
548
  // 2. Identify all tables (collections and junction tables only)
553
549
  for (const collection of collections) {
554
550
  const tableName = getTableName(collection);
@@ -605,22 +601,9 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
605
601
  schemaContent += `export const ${tableVarName} = ${tableCreator}(\"${baseTableName}\", {\n`;
606
602
  schemaContent += ` ${sourceColumn}: ${sourceColType}(\"${sourceColumn}\").notNull().references(() => ${getTableVarName(getTableName(sourceCollection))}.${sourceId}, ${refOptions}),\n`;
607
603
  schemaContent += ` ${targetColumn}: ${targetColType}(\"${targetColumn}\").notNull().references(() => ${getTableVarName(getTableName(targetCollection))}.${targetId}, ${refOptions}),\n`;
608
- schemaContent += "}, (table) => ([\n";
609
- schemaContent += ` primaryKey({ columns: [table.${sourceColumn}, table.${targetColumn}] }),\n`;
610
-
611
- // Junctions are generated tables like any other: locked by default,
612
- // with derived policies (reads follow the endpoints, writes follow
613
- // the declaring side's update rules). RLS is enabled regardless of
614
- // policy stripping — a bare junction must default-deny, not fail open.
615
- const junctionSpec = junctionSpecs.get(baseTableName);
616
- if (!stripPolicies && junctionSpec) {
617
- const junctionCollection = getJunctionCollectionConfig(junctionSpec);
618
- const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
619
- getJunctionSecurityRules(junctionSpec).forEach((rule: SecurityRule, idx: number) => {
620
- schemaContent += generatePolicyCode(junctionCollection, rule, idx, resolveCollection);
621
- });
622
- }
623
- schemaContent += "])).enableRLS();\n\n";
604
+ schemaContent += "}, (table) => ({\n";
605
+ schemaContent += ` pk: primaryKey({ columns: [table.${sourceColumn}, table.${targetColumn}] })\n`;
606
+ schemaContent += "}));\n\n";
624
607
  } else if (!isJunction) {
625
608
  const schema = isPostgresCollectionConfig(collection) ? collection.schema : undefined;
626
609
  const tableCreator = schema ? `${schema}Schema.table` : "pgTable";
@@ -1,5 +1,5 @@
1
1
  import { CollectionConfig, NumberProperty, Property, Relation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty } from "@rebasepro/types";
2
- import { getEnumVarName, getTableName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, getInjectedSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig } from "@rebasepro/common";
2
+ import { getEnumVarName, getTableName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, getInjectedSecurityRules } from "@rebasepro/common";
3
3
  import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
4
4
 
5
5
  // --- Helper Functions ---
@@ -226,10 +226,6 @@ export const generatePostgresDdl = async (
226
226
  });
227
227
  if (ddl.endsWith(";\n")) ddl += "\n";
228
228
 
229
- // Junction policy derivation needs every declaring side of each junction,
230
- // not just the first relation that reached it in the walk below.
231
- const junctionSpecs = resolveJunctionSpecs(collections);
232
-
233
229
  const allTablesToGenerate = new Map<string, {
234
230
  collection: CollectionConfig,
235
231
  isJunction?: boolean,
@@ -265,11 +261,6 @@ export const generatePostgresDdl = async (
265
261
 
266
262
  // 3. Generate tables
267
263
  const fkStatements: string[] = [];
268
- // Policies are emitted after every CREATE TABLE, like the FK constraints:
269
- // a policy may reference other tables (a junction's derived policies always
270
- // reference both endpoints; `policy.existsIn` references a join table), and
271
- // CREATE POLICY validates those relations at creation time.
272
- const policyStatements: string[] = [];
273
264
  for (const [tableName, {
274
265
  collection,
275
266
  isJunction,
@@ -302,25 +293,6 @@ export const generatePostgresDdl = async (
302
293
 
303
294
  fkStatements.push(`ALTER TABLE "${schema}"."${baseTableName}" ADD CONSTRAINT "${baseTableName}_${sourceColumn}_fkey" FOREIGN KEY ("${sourceColumn}") REFERENCES "${sourceSchema}"."${sourceTable}" ("${sourceId}") ON DELETE ${onDelete.toUpperCase()};`);
304
295
  fkStatements.push(`ALTER TABLE "${schema}"."${baseTableName}" ADD CONSTRAINT "${baseTableName}_${targetColumn}_fkey" FOREIGN KEY ("${targetColumn}") REFERENCES "${targetSchema}"."${targetTable}" ("${targetId}") ON DELETE ${onDelete.toUpperCase()};`);
305
-
306
- if (options.includePolicies) {
307
- // Junction tables are generated tables like any other: locked by
308
- // default, with derived policies — reads follow the endpoints'
309
- // visibility, writes follow the declaring side's update rules.
310
- // Without this they were the one kind of generated table with no
311
- // RLS at all, readable and writable by every signed-in user.
312
- ddl += `ALTER TABLE "${schema}"."${baseTableName}" ENABLE ROW LEVEL SECURITY;\n`;
313
- ddl += `\n`;
314
-
315
- const spec = junctionSpecs.get(baseTableName);
316
- if (spec) {
317
- const junctionCollection = getJunctionCollectionConfig(spec);
318
- const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
319
- getJunctionSecurityRules(spec).forEach((rule: SecurityRule) => {
320
- policyStatements.push(generatePolicyDdl(junctionCollection, rule, resolveCollection));
321
- });
322
- }
323
- }
324
296
  } else if (!isJunction) {
325
297
  ddl += `CREATE TABLE "${schema}"."${baseTableName}" (\n`;
326
298
  const columns: string[] = [];
@@ -442,8 +414,9 @@ export const generatePostgresDdl = async (
442
414
  if (securityRules.length > 0) {
443
415
  const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
444
416
  securityRules.forEach((rule: SecurityRule) => {
445
- policyStatements.push(generatePolicyDdl(collection, rule, resolveCollection));
417
+ ddl += generatePolicyDdl(collection, rule, resolveCollection);
446
418
  });
419
+ ddl += "\n";
447
420
  }
448
421
  }
449
422
  }
@@ -454,12 +427,6 @@ export const generatePostgresDdl = async (
454
427
  ddl += fkStatements.join("\n") + "\n\n";
455
428
  }
456
429
 
457
- if (policyStatements.length > 0) {
458
- ddl += "-- Row Level Security Policies\n";
459
- ddl += policyStatements.join("");
460
- ddl += "\n";
461
- }
462
-
463
430
  return ddl;
464
431
  };
465
432
 
@@ -506,33 +473,6 @@ export const generatePostgresPoliciesDdl = (collections: CollectionConfig[]): st
506
473
  }
507
474
  }
508
475
 
509
- // Junction tables are generated from `through` relations, not declared as
510
- // collections, so the walk above never sees them. They get the same
511
- // treatment as any generated table: locked by default, with derived
512
- // policies — reads follow the endpoints, writes follow the declaring
513
- // side's update rules.
514
- const junctionSpecs = resolveJunctionSpecs(collections);
515
- for (const spec of junctionSpecs.values()) {
516
- ddl += `ALTER TABLE "${spec.schema}"."${spec.table}" ENABLE ROW LEVEL SECURITY;\n`;
517
- ddl += `\n`;
518
-
519
- const junctionRules = getJunctionSecurityRules(spec);
520
- if (junctionRules.length === 0) continue;
521
-
522
- const junctionCollection = getJunctionCollectionConfig(spec);
523
- const resolveCollection: ResolveCollection = (slug) => collections.find(c => c.slug === slug || getTableName(c) === slug);
524
- const declaringSlugs = spec.declaringSides.map(s => s.collection.slug).join('", "');
525
-
526
- ddl += `-- Derived by Rebase for the junction "${spec.table}" (no collection declares it).\n`;
527
- ddl += `-- Reads require both endpoint rows to be visible; writes follow the update\n`;
528
- ddl += `-- rules of "${declaringSlugs}". Set \`disableDefaultPolicies: true\` on the\n`;
529
- ddl += `-- declaring collection(s) to drop these and police the junction yourself.\n`;
530
- junctionRules.forEach((rule: SecurityRule) => {
531
- ddl += generatePolicyDdl(junctionCollection, rule, resolveCollection);
532
- });
533
- ddl += "\n";
534
- }
535
-
536
476
  return ddl;
537
477
  };
538
478
 
@@ -30,7 +30,6 @@ async function main() {
30
30
  "--force": Boolean,
31
31
  "--schema": String,
32
32
  "--data-inference": Boolean,
33
- "--no-data-inference": Boolean,
34
33
  "-o": "--output",
35
34
  "-c": "--collections",
36
35
  "-f": "--force"
@@ -167,14 +166,8 @@ async function main() {
167
166
  logger.info(chalk.blue(`Found ${tablesMap.size} tables (including ${joinTables.size} detected join tables).`));
168
167
 
169
168
  let runDataInference = false;
170
- if (args["--no-data-inference"]) {
171
- runDataInference = false;
172
- } else if (args["--data-inference"] !== undefined) {
169
+ if (args["--data-inference"] !== undefined) {
173
170
  runDataInference = args["--data-inference"];
174
- } else if (!process.stdin.isTTY) {
175
- // No terminal to answer the question below (scaffolding scripts, CI,
176
- // `rebase init --introspect`) — asking would hang forever.
177
- logger.info(chalk.gray("Skipping data inference (non-interactive run; pass --data-inference to enable)."));
178
171
  } else {
179
172
  const rl = readline.createInterface({
180
173
  input: process.stdin,