@rebasepro/server-postgres 0.9.1-canary.f2f61da → 0.9.1-canary.fd3754b

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.
Files changed (53) hide show
  1. package/dist/PostgresBackendDriver.d.ts +2 -25
  2. package/dist/PostgresBootstrapper.d.ts +0 -10
  3. package/dist/auth/services.d.ts +0 -16
  4. package/dist/chunk-DSJWtz9O.js +40 -0
  5. package/dist/connection.d.ts +0 -21
  6. package/dist/data-transformer.d.ts +2 -9
  7. package/dist/index.es.js +2598 -2020
  8. package/dist/index.es.js.map +1 -1
  9. package/dist/schema/auth-default-policies.d.ts +10 -0
  10. package/dist/schema/doctor.d.ts +1 -1
  11. package/dist/security/policy-drift.d.ts +5 -16
  12. package/dist/security/rls-enforcement.d.ts +2 -27
  13. package/dist/services/FetchService.d.ts +24 -4
  14. package/dist/services/PersistService.d.ts +1 -27
  15. package/dist/services/RelationService.d.ts +1 -34
  16. package/dist/services/collection-helpers.d.ts +14 -79
  17. package/dist/services/dataService.d.ts +1 -3
  18. package/dist/services/index.d.ts +1 -1
  19. package/dist/services/realtimeService.d.ts +0 -7
  20. package/dist/src-Eh-CZosp.js +595 -0
  21. package/dist/src-Eh-CZosp.js.map +1 -0
  22. package/package.json +17 -15
  23. package/src/PostgresBackendDriver.ts +13 -127
  24. package/src/PostgresBootstrapper.ts +26 -68
  25. package/src/auth/ensure-tables.ts +11 -73
  26. package/src/auth/services.ts +19 -49
  27. package/src/cli-helpers.ts +20 -2
  28. package/src/connection.ts +1 -61
  29. package/src/data-transformer.ts +9 -11
  30. package/src/databasePoolManager.ts +0 -2
  31. package/src/schema/auth-default-policies.ts +125 -0
  32. package/src/schema/doctor-cli.ts +1 -5
  33. package/src/schema/doctor.ts +20 -45
  34. package/src/schema/generate-drizzle-schema-logic.ts +29 -24
  35. package/src/schema/generate-postgres-ddl-logic.ts +28 -76
  36. package/src/schema/introspect-db.ts +2 -19
  37. package/src/security/policy-drift.test.ts +14 -48
  38. package/src/security/policy-drift.ts +10 -72
  39. package/src/security/rls-enforcement.ts +3 -64
  40. package/src/services/BranchService.ts +10 -42
  41. package/src/services/FetchService.ts +270 -65
  42. package/src/services/PersistService.ts +14 -130
  43. package/src/services/RelationService.ts +94 -153
  44. package/src/services/collection-helpers.ts +47 -164
  45. package/src/services/dataService.ts +2 -3
  46. package/src/services/index.ts +0 -1
  47. package/src/services/realtimeService.ts +19 -40
  48. package/src/utils/drizzle-conditions.ts +0 -13
  49. package/src/websocket.ts +1 -4
  50. package/dist/collections/buildRegistry.d.ts +0 -27
  51. package/dist/services/row-pipeline.d.ts +0 -63
  52. package/src/collections/buildRegistry.ts +0 -59
  53. package/src/services/row-pipeline.ts +0 -239
@@ -1,11 +1,29 @@
1
1
  import path from "path";
2
2
  import fs from "fs";
3
3
  import { execSync } from "child_process";
4
- import { pathToFileURL } from "url";
4
+ import { fileURLToPath, pathToFileURL } from "url";
5
5
  import chalk from "chalk";
6
6
  import { logger } from "@rebasepro/server";
7
7
  import type { CollectionConfig, Relation } from "@rebasepro/types";
8
- import { moduleDir as __helpersDirname } from "./module-dir";
8
+
9
+ const getHelpersDirname = () => {
10
+ try {
11
+ return eval("__dirname");
12
+ } catch {
13
+ const err = new Error();
14
+ const stackLine = err.stack?.split("\n")[1] || "";
15
+ const match = stackLine.match(/\((.*?):\d+:\d+\)/) || stackLine.match(/at (.*?):\d+:\d+/);
16
+ if (match && match[1]) {
17
+ let cleanPath = match[1];
18
+ if (cleanPath.startsWith("file://")) {
19
+ cleanPath = fileURLToPath(cleanPath);
20
+ }
21
+ return path.dirname(cleanPath);
22
+ }
23
+ return process.cwd();
24
+ }
25
+ };
26
+ const __helpersDirname = getHelpersDirname();
9
27
 
10
28
 
11
29
 
package/src/connection.ts CHANGED
@@ -27,68 +27,11 @@ const DEFAULT_POOL: Required<PostgresPoolConfig> = {
27
27
  max: 20,
28
28
  idleTimeoutMillis: 30_000,
29
29
  connectionTimeoutMillis: 10_000,
30
- // The client-side read timeout MUST be comfortably above the server-side
31
- // statement_timeout. When the client timer fires first, node-postgres
32
- // abandons the in-flight statement but keeps the connection — inside a
33
- // transaction that leaves the tx open (and any pending ROLLBACK is
34
- // spliced out of the client queue before it ever reaches the wire), so
35
- // the pooled connection is returned still in-transaction with its RLS
36
- // GUCs set. The server abort (SQLSTATE 57014) is the clean path; the
37
- // client timeout is only a backstop for a dead network.
38
- queryTimeout: 60_000,
30
+ queryTimeout: 30_000,
39
31
  statementTimeout: 30_000,
40
32
  keepAlive: true
41
33
  };
42
34
 
43
- /** ReadyForQuery status byte: `I` idle, `T` in transaction, `E` failed transaction. */
44
- const TX_IDLE = "I";
45
-
46
- /**
47
- * Destroy pool clients that are released while still inside a transaction.
48
- *
49
- * pg-pool returns a client to the idle list whenever `release()` is called
50
- * without an error — even if the connection is still mid-transaction (status
51
- * `T`/`E`). That happens in practice: drizzle's pool transaction releases in
52
- * a `finally` after attempting ROLLBACK, and if the ROLLBACK itself fails
53
- * (e.g. it was queued behind a statement that hit the client-side
54
- * query_timeout), the client goes back dirty. The next checkout then runs
55
- * its statements inside the zombie transaction — with the previous request's
56
- * `app.*` RLS GUCs still applied, which turns unrelated queries into
57
- * RLS-scoped ones (observed in production as registration failing with
58
- * SQLSTATE 42501 under a leaked anonymous context).
59
- *
60
- * pg-pool emits `release` before it consults its private `_expired` set, so
61
- * marking the client expired here makes `_release()` destroy it instead of
62
- * pooling it. Both `client._txStatus` (pg ≥ 8.16) and `pool._expired` are
63
- * private APIs — feature-detect and fall back to loud logging so an upstream
64
- * change degrades to observability, never to silent corruption.
65
- */
66
- export function guardPoolAgainstDirtyRelease(pool: Pool, label: string): void {
67
- pool.on("release", (err: Error | undefined, client: unknown) => {
68
- if (err) return; // errored clients are already destroyed by pg-pool
69
- const txStatus = (client as { _txStatus?: string | null })?._txStatus;
70
- if (typeof txStatus !== "string" || txStatus === TX_IDLE) return;
71
-
72
- // pg-pool keeps expired clients in a WeakSet (a plain object works too
73
- // if upstream ever changes it — duck-type on add/has).
74
- const expired = (pool as unknown as { _expired?: { add(c: object): unknown; has(c: object): boolean } })._expired;
75
- if (expired && typeof expired.add === "function" && typeof expired.has === "function" && client && typeof client === "object") {
76
- expired.add(client);
77
- logger.error(
78
- `[${label}] Client released back to the pool while still in a transaction ` +
79
- `(status '${txStatus}') — destroying it so the open transaction and its ` +
80
- `session state (RLS GUCs) cannot leak into the next request.`
81
- );
82
- } else {
83
- logger.error(
84
- `[${label}] Client released mid-transaction (status '${txStatus}') but the ` +
85
- `pool's internal expiry set is unavailable (pg-pool internals changed?). ` +
86
- `The connection may leak its open transaction into subsequent requests.`
87
- );
88
- }
89
- });
90
- }
91
-
92
35
  /**
93
36
  * Create a Drizzle-backed Postgres connection with a production-grade
94
37
  * connection pool.
@@ -132,7 +75,6 @@ export function createPostgresDatabaseConnection(
132
75
  logger.warn("[pg-pool] Connection timeout detected — pool will auto-retry");
133
76
  }
134
77
  });
135
- guardPoolAgainstDirtyRelease(pool, "pg-pool");
136
78
 
137
79
  // Create drizzle instance — pass schema when available to enable db.query relational API
138
80
  const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
@@ -176,7 +118,6 @@ export function createDirectDatabaseConnection(
176
118
  pool.on("error", (err) => {
177
119
  logger.error("[pg-direct-pool] Unexpected pool error", { detail: err.message });
178
120
  });
179
- guardPoolAgainstDirtyRelease(pool, "pg-direct-pool");
180
121
 
181
122
  const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
182
123
 
@@ -216,7 +157,6 @@ export function createReadReplicaConnection(
216
157
  pool.on("error", (err) => {
217
158
  logger.error("[pg-replica-pool] Unexpected pool error", { detail: err.message });
218
159
  });
219
- guardPoolAgainstDirtyRelease(pool, "pg-replica-pool");
220
160
 
221
161
  const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
222
162
 
@@ -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
@@ -2,7 +2,6 @@ import { Pool } from "pg";
2
2
  import { drizzle } from "drizzle-orm/node-postgres";
3
3
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
4
4
  import { logger } from "@rebasepro/server";
5
- import { guardPoolAgainstDirtyRelease } from "./connection";
6
5
 
7
6
  export class DatabasePoolManager {
8
7
  private pools: Map<string, Pool> = new Map();
@@ -51,7 +50,6 @@ export class DatabasePoolManager {
51
50
  pool.on("error", (err) => {
52
51
  logger.error(`[DatabasePoolManager] Unexpected error on idle client for db ${databaseName}`, { error: err });
53
52
  });
54
- guardPoolAgainstDirtyRelease(pool, `pg-pool:${databaseName}`);
55
53
 
56
54
  this.pools.set(databaseName, pool);
57
55
  return pool;
@@ -0,0 +1,125 @@
1
+ import { CollectionConfig, SecurityRule, SecurityOperation, AuthCollectionConfig, PolicyExpression, isPostgresCollectionConfig, policy } from "@rebasepro/types";
2
+ import { getTableName } from "@rebasepro/common";
3
+
4
+ /**
5
+ * Default RLS policies injected by the schema generator.
6
+ *
7
+ * Rebase's enforcement model is unified: authenticated (user-context) requests
8
+ * run under the restricted `rebase_user` role, so Postgres RLS binds *every*
9
+ * statement — reads and writes. A collection's `securityRules` are the whole
10
+ * authorization model. The server context (auth flows, migrations,
11
+ * `dataAsAdmin`) runs as the owner and bypasses RLS.
12
+ *
13
+ * Because RLS default-denies, every collection is **locked by default**: with
14
+ * no rules, only the server context and admins can touch it. The generator
15
+ * injects that safe baseline:
16
+ *
17
+ * **For every collection**
18
+ * 1. A permissive **server-or-admin SELECT** grant.
19
+ * 2. A permissive **server-or-admin write** grant (insert/update/delete).
20
+ *
21
+ * Author `securityRules` are permissive and OR together, so explicit rules only
22
+ * *broaden* access from this locked baseline (e.g. "users read/write their own
23
+ * rows").
24
+ *
25
+ * **For auth collections additionally**
26
+ * 3. A permissive **self SELECT** grant (`id = auth.uid()`), so users can read
27
+ * their own row (profile, session bootstrap) without every app re-declaring
28
+ * it.
29
+ * 4. A **restrictive** admin write gate. Restrictive policies are AND'd with
30
+ * every other policy, so a write is rejected unless the caller is an admin
31
+ * (or the server context) — even if the author also wrote a permissive rule
32
+ * such as "a user may edit their own row". Without this, a permissive owner
33
+ * rule would let a user change their own `roles`.
34
+ *
35
+ * The server context is recognised as `auth.uid() IS NULL` — the built-in flows
36
+ * that run without a user (signup, migrations) set no user GUC — which also
37
+ * lets the owner connection satisfy these policies even under FORCE RLS.
38
+ *
39
+ * Opt out with `disableDefaultPolicies: true` to take full responsibility for
40
+ * the collection's RLS.
41
+ */
42
+ // Expressed structurally (not as raw SQL) so the admin UI can evaluate it
43
+ // exactly — the framework's most security-critical policies must be reflected
44
+ // precisely, not left as un-evaluable raw clauses. Compiles to
45
+ // `auth.uid() IS NULL OR (string_to_array(auth.roles(), ',') && ARRAY['admin'])`.
46
+ const SERVER_OR_ADMIN_EXPR: PolicyExpression = policy.or(
47
+ policy.not(policy.authenticated()),
48
+ policy.rolesOverlap(["admin"])
49
+ );
50
+
51
+ /** Write operations that must be admin-gated by default on auth collections. */
52
+ const DEFAULT_GUARDED_OPS: SecurityOperation[] = ["insert", "update", "delete"];
53
+
54
+ /** Whether a collection is flagged as an authentication collection. */
55
+ function isAuthCollection(collection: CollectionConfig): boolean {
56
+ const auth = collection.auth;
57
+ return auth === true || (typeof auth === "object" && (auth as AuthCollectionConfig)?.enabled === true);
58
+ }
59
+
60
+ /** The property marked as the row id (falls back to `id`). */
61
+ function getIdPropertyName(collection: CollectionConfig): string {
62
+ for (const [name, prop] of Object.entries(collection.properties ?? {})) {
63
+ if (prop && typeof prop === "object" && "isId" in prop && (prop as { isId?: unknown }).isId) {
64
+ return name;
65
+ }
66
+ }
67
+ return "id";
68
+ }
69
+
70
+ /**
71
+ * Returns the security rules that should be applied to a collection: the
72
+ * author's explicit `securityRules` plus the framework defaults described in
73
+ * the module doc (baseline server/admin read for all collections; self-read
74
+ * and the admin write gate for auth collections).
75
+ *
76
+ * Collections that opt out via `disableDefaultPolicies` are returned unchanged.
77
+ */
78
+ export function getEffectiveSecurityRules(collection: CollectionConfig): SecurityRule[] {
79
+ const explicit = [...((isPostgresCollectionConfig(collection) ? collection.securityRules : undefined) ?? [])];
80
+
81
+ if (collection.disableDefaultPolicies) {
82
+ return explicit;
83
+ }
84
+
85
+ const tableName = getTableName(collection);
86
+ const injected: SecurityRule[] = [];
87
+
88
+ // Baseline read + write: the server context and admins can always operate.
89
+ // RLS default-denies under the user role, so without these a rule-less
90
+ // collection would be locked to everyone — including the admin studio.
91
+ // Author rules are permissive and broaden access from here.
92
+ injected.push({
93
+ name: `${tableName}_default_admin_read`,
94
+ operations: ["select"],
95
+ condition: SERVER_OR_ADMIN_EXPR
96
+ });
97
+ injected.push({
98
+ name: `${tableName}_default_admin_write`,
99
+ operations: [...DEFAULT_GUARDED_OPS],
100
+ condition: SERVER_OR_ADMIN_EXPR,
101
+ check: SERVER_OR_ADMIN_EXPR
102
+ });
103
+
104
+ if (isAuthCollection(collection)) {
105
+ // Self-read: a user can always read their own row.
106
+ injected.push({
107
+ name: `${tableName}_default_self_read`,
108
+ operations: ["select"],
109
+ condition: policy.compare(policy.field(getIdPropertyName(collection)), "eq", policy.authUid())
110
+ });
111
+
112
+ // Restrictive gate: AND'd with all other policies, so no permissive rule
113
+ // (e.g. an owner "edit your own row" rule) can let a non-admin change
114
+ // privileged columns like `roles`.
115
+ injected.push({
116
+ name: `${tableName}_require_admin_write`,
117
+ mode: "restrictive",
118
+ operations: [...DEFAULT_GUARDED_OPS],
119
+ condition: SERVER_OR_ADMIN_EXPR,
120
+ check: SERVER_OR_ADMIN_EXPR
121
+ });
122
+ }
123
+
124
+ return [...explicit, ...injected];
125
+ }
@@ -8,7 +8,7 @@ import chalk from "chalk";
8
8
  import fs from "fs";
9
9
  import { runDoctor, loadCollections } from "./doctor";
10
10
  import { checkPolicyDrift, formatPolicyDrift, hasDrift } from "../security/policy-drift";
11
- import { validatePolicyPgRoles, warnOnAnonymousGrants } from "../security/rls-enforcement";
11
+ import { validatePolicyPgRoles } from "../security/rls-enforcement";
12
12
  import { logger } from "@rebasepro/server";
13
13
 
14
14
  /**
@@ -41,10 +41,6 @@ async function runPolicyChecks(collectionsPath: string, databaseUrl?: string): P
41
41
  logger.error(chalk.red(err instanceof Error ? err.message : String(err)));
42
42
  }
43
43
 
44
- // A rule that reads as a lockdown but is true for every caller compiles
45
- // to a grant. Report it without booting a server.
46
- warnOnAnonymousGrants(collections as never);
47
-
48
44
  const drift = await checkPolicyDrift(pool as never, collections);
49
45
  logger.info("");
50
46
  if (hasDrift(drift)) {
@@ -37,7 +37,7 @@ export type IssueSeverity = "error" | "warning" | "info";
37
37
 
38
38
  export interface DoctorIssue {
39
39
  severity: IssueSeverity;
40
- category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale" | "sdk_not_generated";
40
+ category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale";
41
41
  table?: string;
42
42
  column?: string;
43
43
  expected?: string;
@@ -61,29 +61,19 @@ export function getExpectedColumnType(prop: Property): string | null {
61
61
  const sp = prop as StringProperty;
62
62
  if (sp.enum) return "USER-DEFINED"; // pgEnum → USER-DEFINED in information_schema
63
63
  if ("isId" in sp && sp.isId === "uuid") return "uuid";
64
- if (sp.columnType === "uuid") return "uuid";
65
- // A markdown/multiline string compiles to `text`, not varchar — the
66
- // generator treats those UI hints as column-type signals, so the
67
- // expectation here must too or every such column reads as drift.
68
- if (sp.columnType === "text" || sp.ui?.markdown || sp.ui?.multiline) return "text";
64
+ if (sp.columnType === "text") return "text";
69
65
  if (sp.columnType === "char") return "character";
70
66
  return "character varying";
71
67
  }
72
68
  case "number": {
73
69
  const np = prop as NumberProperty;
74
- if (np.columnType) {
75
- // The generator passes any columnType straight through to drizzle,
76
- // so mirror that rather than enumerating a subset (which reported
77
- // drift for anything unlisted, e.g. smallint). Serial types are
78
- // integers with a sequence default; information_schema reports the
79
- // underlying width.
80
- const serialWidths: Record<string, string> = {
81
- serial: "integer",
82
- bigserial: "bigint",
83
- smallserial: "smallint"
84
- };
85
- return serialWidths[np.columnType] ?? np.columnType;
86
- }
70
+ if (np.columnType === "double precision") return "double precision";
71
+ if (np.columnType === "real") return "real";
72
+ if (np.columnType === "bigint") return "bigint";
73
+ if (np.columnType === "serial") return "integer"; // serial is integer under the hood
74
+ if (np.columnType === "bigserial") return "bigint";
75
+ if (np.columnType === "integer") return "integer";
76
+ if (np.columnType === "numeric") return "numeric";
87
77
  if (np.validation?.integer || ("isId" in np && np.isId)) return "integer";
88
78
  return "numeric";
89
79
  }
@@ -211,18 +201,15 @@ export async function checkCollectionsVsSdk(
211
201
  ): Promise<{ passed: boolean; issues: DoctorIssue[] }> {
212
202
  const issues: DoctorIssue[] = [];
213
203
 
214
- // The typed SDK is opt-in — nothing in a scaffolded project imports it until
215
- // you choose to. A project that never generated one isn't drifting, so report
216
- // it as information rather than a warning; otherwise `doctor` can never come
217
- // back clean on a fresh project and users learn to ignore its output.
204
+ // Check if SDK file exists
218
205
  if (!fs.existsSync(sdkFilePath)) {
219
206
  issues.push({
220
- severity: "info",
221
- category: "sdk_not_generated",
222
- message: "Typed SDK not generated (optional).",
223
- fix: "Run `rebase generate-sdk` if you want typed collection access"
207
+ severity: "warning",
208
+ category: "sdk_stale",
209
+ message: `Generated SDK typedefs file does not exist at "${sdkFilePath}".`,
210
+ fix: "Run `rebase generate-sdk`"
224
211
  });
225
- return { passed: true,
212
+ return { passed: false,
226
213
  issues };
227
214
  }
228
215
 
@@ -644,15 +631,11 @@ export function renderReport(report: DoctorReport): void {
644
631
  }
645
632
 
646
633
  function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): void {
647
- const errorCount = issues.filter((i) => i.severity === "error").length;
648
- const warnCount = issues.filter((i) => i.severity === "warning").length;
649
- const infoIssues = issues.filter((i) => i.severity === "info");
650
-
651
- // Informational notes don't make a phase unhealthy, so key the header off
652
- // real problems rather than `passed` alone.
653
- if (errorCount === 0 && warnCount === 0) {
634
+ if (passed) {
654
635
  logger.info(` ${chalk.green("✅")} ${label}: ${chalk.green("In sync")}`);
655
636
  } else {
637
+ const errorCount = issues.filter((i) => i.severity === "error").length;
638
+ const warnCount = issues.filter((i) => i.severity === "warning").length;
656
639
  const parts: string[] = [];
657
640
  if (errorCount > 0) parts.push(`${errorCount} error${errorCount > 1 ? "s" : ""}`);
658
641
  if (warnCount > 0) parts.push(`${warnCount} warning${warnCount > 1 ? "s" : ""}`);
@@ -660,14 +643,7 @@ function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): voi
660
643
  }
661
644
  logger.info("");
662
645
 
663
- // Notes render as a quiet one-liner, not a full drift box.
664
- for (const issue of infoIssues) {
665
- const fixPart = issue.fix ? chalk.gray(` — ${issue.fix}`) : "";
666
- logger.info(` ${chalk.gray("ℹ")} ${chalk.gray(issue.message)}${fixPart}`);
667
- logger.info("");
668
- }
669
-
670
- for (const issue of issues.filter((i) => i.severity !== "info")) {
646
+ for (const issue of issues) {
671
647
  const severityIcon = issue.severity === "error" ? chalk.red("✗") : chalk.yellow("⚠");
672
648
  const categoryLabel = formatCategory(issue.category);
673
649
  logger.info(` ${chalk.gray("┌─")} ${severityIcon} ${chalk.bold(categoryLabel)} ${chalk.gray("─".repeat(Math.max(0, 42 - categoryLabel.length)))}`);
@@ -698,8 +674,7 @@ function formatCategory(cat: DoctorIssue["category"]): string {
698
674
  missing_enum: "Missing Enum",
699
675
  enum_value_mismatch: "Enum Value Mismatch",
700
676
  missing_foreign_key: "Missing Foreign Key",
701
- sdk_stale: "Stale SDK Types",
702
- sdk_not_generated: "SDK Types Not Generated"
677
+ sdk_stale: "Stale SDK Types"
703
678
  };
704
679
  return labels[cat];
705
680
  }
@@ -1,8 +1,10 @@
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";
4
- import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
3
+ import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres } from "@rebasepro/common";
4
+ import { toSnakeCase } from "@rebasepro/utils";
5
+ import { createHash } from "crypto";
5
6
  import { logger } from "@rebasepro/server";
7
+ import { getEffectiveSecurityRules } from "./auth-default-policies";
6
8
  // --- Helper Functions ---
7
9
 
8
10
  /**
@@ -313,6 +315,22 @@ const wrapSql = (clause: string): string => `sql\`${clause}\``;
313
315
  /**
314
316
  * Generates a deterministic hash based on the rule configuration.
315
317
  */
318
+ const getPolicyNameHash = (rule: SecurityRule): string => {
319
+ const data = JSON.stringify({
320
+ a: rule.access,
321
+ m: rule.mode,
322
+ op: rule.operation,
323
+ ops: rule.operations?.slice().sort(),
324
+ own: rule.ownerField,
325
+ rol: rule.roles?.slice().sort(),
326
+ pg: rule.pgRoles?.slice().sort(),
327
+ u: rule.using,
328
+ w: rule.withCheck,
329
+ c: rule.condition,
330
+ ch: rule.check
331
+ });
332
+ return createHash("sha1").update(data).digest("hex").substring(0, 7);
333
+ };
316
334
 
317
335
  /**
318
336
  * Generates Drizzle pgPolicy() calls from a declarative SecurityRule definition.
@@ -333,11 +351,15 @@ const generatePolicyCode = (collection: CollectionConfig, rule: SecurityRule, in
333
351
  ? rule.operations
334
352
  : [rule.operation ?? "all"];
335
353
 
336
- const policyNames = getPolicyNamesForRule(rule, tableName);
354
+ const ruleHash = getPolicyNameHash(rule);
337
355
 
338
356
  // Generate one pgPolicy per operation
339
357
  return ops.map((op, opIdx) => {
340
- return generateSinglePolicyCode(collection, rule, op, policyNames[opIdx], resolveCollection);
358
+ const policyName = rule.name
359
+ ? (ops.length > 1 ? `${rule.name}_${op}` : rule.name)
360
+ : `${tableName}_${op}_${ruleHash}${ops.length > 1 ? `_${opIdx}` : ""}`;
361
+
362
+ return generateSinglePolicyCode(collection, rule, op, policyName, resolveCollection);
341
363
  }).join("");
342
364
  };
343
365
 
@@ -545,10 +567,6 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
545
567
  });
546
568
  schemaContent += "\n";
547
569
 
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
570
  // 2. Identify all tables (collections and junction tables only)
553
571
  for (const collection of collections) {
554
572
  const tableName = getTableName(collection);
@@ -605,22 +623,9 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
605
623
  schemaContent += `export const ${tableVarName} = ${tableCreator}(\"${baseTableName}\", {\n`;
606
624
  schemaContent += ` ${sourceColumn}: ${sourceColType}(\"${sourceColumn}\").notNull().references(() => ${getTableVarName(getTableName(sourceCollection))}.${sourceId}, ${refOptions}),\n`;
607
625
  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";
626
+ schemaContent += "}, (table) => ({\n";
627
+ schemaContent += ` pk: primaryKey({ columns: [table.${sourceColumn}, table.${targetColumn}] })\n`;
628
+ schemaContent += "}));\n\n";
624
629
  } else if (!isJunction) {
625
630
  const schema = isPostgresCollectionConfig(collection) ? collection.schema : undefined;
626
631
  const tableCreator = schema ? `${schema}Schema.table` : "pgTable";