@rebasepro/server-postgres 0.9.1-canary.a639738 → 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.
Files changed (39) hide show
  1. package/dist/PostgresBootstrapper.d.ts +0 -10
  2. package/dist/auth/services.d.ts +0 -16
  3. package/dist/connection.d.ts +0 -21
  4. package/dist/data-transformer.d.ts +2 -9
  5. package/dist/index.es.js +329 -841
  6. package/dist/index.es.js.map +1 -1
  7. package/dist/schema/doctor.d.ts +1 -1
  8. package/dist/security/policy-drift.d.ts +0 -30
  9. package/dist/services/FetchService.d.ts +32 -4
  10. package/dist/services/RelationService.d.ts +1 -34
  11. package/dist/services/collection-helpers.d.ts +0 -76
  12. package/dist/services/index.d.ts +1 -1
  13. package/dist/services/realtimeService.d.ts +0 -7
  14. package/package.json +7 -11
  15. package/src/PostgresBackendDriver.ts +11 -39
  16. package/src/PostgresBootstrapper.ts +25 -62
  17. package/src/auth/ensure-tables.ts +11 -73
  18. package/src/auth/services.ts +19 -49
  19. package/src/cli.ts +0 -60
  20. package/src/connection.ts +1 -61
  21. package/src/data-transformer.ts +9 -11
  22. package/src/databasePoolManager.ts +0 -2
  23. package/src/schema/doctor.ts +20 -45
  24. package/src/schema/generate-drizzle-schema-logic.ts +4 -21
  25. package/src/schema/generate-postgres-ddl-logic.ts +3 -63
  26. package/src/schema/introspect-db.ts +2 -19
  27. package/src/security/policy-drift.test.ts +1 -106
  28. package/src/security/policy-drift.ts +0 -56
  29. package/src/services/FetchService.ts +229 -50
  30. package/src/services/PersistService.ts +2 -9
  31. package/src/services/RelationService.ts +94 -153
  32. package/src/services/collection-helpers.ts +3 -166
  33. package/src/services/index.ts +0 -1
  34. package/src/services/realtimeService.ts +19 -40
  35. package/src/utils/drizzle-conditions.ts +0 -13
  36. package/dist/collections/buildRegistry.d.ts +0 -27
  37. package/dist/services/row-pipeline.d.ts +0 -63
  38. package/src/collections/buildRegistry.ts +0 -59
  39. package/src/services/row-pipeline.ts +0 -239
@@ -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,
@@ -243,17 +236,7 @@ async function main() {
243
236
  const merged = mergeIndexContent(existing, generatedFiles);
244
237
  fs.writeFileSync(indexPath, merged, "utf-8");
245
238
  } else {
246
- // --force replaces collections derived from the database, but the
247
- // directory can also hold hand-written ones with no table in the
248
- // introspected schema (the auth users collection lives in
249
- // "rebase"). The backend discovers the whole directory, so an
250
- // index listing only introspected tables would silently drop them
251
- // from the admin UI while the API still served them.
252
- const siblings = fs.readdirSync(outDir)
253
- .filter(f => f.endsWith(".ts") && f !== "index.ts")
254
- .map(f => f.replace(/\.ts$/, ""));
255
- const allFiles = [...new Set([...generatedFiles, ...siblings])];
256
- const indexContent = generateIndexContent(allFiles);
239
+ const indexContent = generateIndexContent(generatedFiles);
257
240
  fs.writeFileSync(indexPath, indexContent, "utf-8");
258
241
  }
259
242
  logger.info(chalk.green(` ✓ ${indexPath}`));
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from "@jest/globals";
2
2
  import type { CollectionConfig } from "@rebasepro/types";
3
3
 
4
- import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, dropOrphanedPolicies, isGeneratedPolicyName, type PolicyRef, type Queryable } from "./policy-drift";
4
+ import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, type PolicyRef, type Queryable } from "./policy-drift";
5
5
  import { generatePostgresPoliciesDdl } from "../schema/generate-postgres-ddl-logic";
6
6
 
7
7
  function collection(slug: string): CollectionConfig {
@@ -154,108 +154,3 @@ describe("checkPolicyDrift", () => {
154
154
  expect(drift.missing.length).toBeGreaterThan(0);
155
155
  });
156
156
  });
157
-
158
- describe("isGeneratedPolicyName", () => {
159
- it("recognises the generator's own shape", () => {
160
- expect(isGeneratedPolicyName("documents_insert_a1b2c3d", "documents")).toBe(true);
161
- // One rule spanning several operations appends the operation index.
162
- expect(isGeneratedPolicyName("documents_update_a1b2c3d_1", "documents")).toBe(true);
163
- });
164
-
165
- it("does not claim names a human could have written", () => {
166
- expect(isGeneratedPolicyName("owner_access", "documents")).toBe(false);
167
- expect(isGeneratedPolicyName("documents_default_admin_read", "documents")).toBe(false);
168
- // Right shape, wrong table — belongs to something else.
169
- expect(isGeneratedPolicyName("teams_insert_a1b2c3d", "documents")).toBe(false);
170
- // A digest is 7 lowercase hex characters, nothing else.
171
- expect(isGeneratedPolicyName("documents_insert_notahex", "documents")).toBe(false);
172
- expect(isGeneratedPolicyName("documents_grant_a1b2c3d", "documents")).toBe(false);
173
- });
174
- });
175
-
176
- describe("dropOrphanedPolicies", () => {
177
- /** Records the DDL issued so the test can assert on what was dropped. */
178
- function recordingDb(rows: Record<string, unknown>[]) {
179
- const executed: string[] = [];
180
- const db: Queryable = {
181
- query: async (text: string) => {
182
- if (!/^SELECT/i.test(text)) executed.push(text);
183
- return { rows: rows as never[] };
184
- }
185
- };
186
- return { db, executed };
187
- }
188
-
189
- it("drops the policy a rule edit superseded", async () => {
190
- // The reported failure: tightening a rule renames its policy, and the
191
- // permissive original stays live and keeps ORing itself back in.
192
- const cols = [collection("documents")];
193
- const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
194
- const stale = {
195
- schemaname: "public", tablename: "documents", policyname: "documents_insert_dead1ee",
196
- roles: ["public"], cmd: "INSERT", qual: null, with_check: "true"
197
- };
198
- const live = [...expected.map((p) => liveRow(p)), stale];
199
-
200
- const drift = await checkPolicyDrift(dbWith(live), cols);
201
- const { db, executed } = recordingDb(live);
202
- const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
203
-
204
- expect(dropped).toHaveLength(1);
205
- expect(dropped[0].name).toBe("documents_insert_dead1ee");
206
- expect(kept).toHaveLength(0);
207
- expect(executed).toEqual([
208
- 'DROP POLICY IF EXISTS "documents_insert_dead1ee" ON "public"."documents"'
209
- ]);
210
- });
211
-
212
- it("leaves a hand-written policy alone and reports it instead", async () => {
213
- const cols = [collection("documents")];
214
- const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
215
- const live = [
216
- ...expected.map((p) => liveRow(p)),
217
- { schemaname: "public", tablename: "documents", policyname: "ops_break_glass", roles: ["public"], cmd: "ALL", qual: "true", with_check: null }
218
- ];
219
-
220
- const drift = await checkPolicyDrift(dbWith(live), cols);
221
- const { db, executed } = recordingDb(live);
222
- const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
223
-
224
- expect(dropped).toHaveLength(0);
225
- expect(kept.map((p) => p.name)).toEqual(["ops_break_glass"]);
226
- expect(executed).toEqual([]);
227
- });
228
-
229
- it("never touches a table the collections do not describe", async () => {
230
- // Another application sharing the schema owns this table; a
231
- // generator-shaped name there is coincidence, not our leftover.
232
- const cols = [collection("documents")];
233
- const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
234
- const live = [
235
- ...expected.map((p) => liveRow(p)),
236
- { schemaname: "public", tablename: "legacy", policyname: "legacy_select_a1b2c3d", roles: ["public"], cmd: "SELECT", qual: "true", with_check: null }
237
- ];
238
-
239
- const drift = await checkPolicyDrift(dbWith(live), cols);
240
- const { db, executed } = recordingDb(live);
241
- const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
242
-
243
- expect(dropped).toHaveLength(0);
244
- expect(kept.map((p) => p.name)).toEqual(["legacy_select_a1b2c3d"]);
245
- expect(executed).toEqual([]);
246
- });
247
-
248
- it("does nothing when the database already matches", async () => {
249
- const cols = [collection("documents")];
250
- const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
251
- const live = expected.map((p) => liveRow(p));
252
-
253
- const drift = await checkPolicyDrift(dbWith(live), cols);
254
- const { db, executed } = recordingDb(live);
255
- const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
256
-
257
- expect(dropped).toHaveLength(0);
258
- expect(kept).toHaveLength(0);
259
- expect(executed).toEqual([]);
260
- });
261
- });
@@ -192,62 +192,6 @@ export async function checkPolicyDrift(
192
192
  return drift;
193
193
  }
194
194
 
195
- /**
196
- * Does this name look like one the generator produced for this table?
197
- *
198
- * Unnamed rules compile to `<table>_<op>_<sha1[0:7]>` (plus `_<idx>` when one
199
- * rule spans several operations), and the hash covers the rule's semantics — so
200
- * *editing* a rule renames its policy. The policy under the old name is left
201
- * behind by `db push`, which only DROPs the names it is about to CREATE, and
202
- * Postgres ORs PERMISSIVE policies together: a superseded `USING (true)` keeps
203
- * granting everything no matter how tight its replacement is.
204
- *
205
- * Matching the shape is what makes dropping them safe. A hand-written policy
206
- * would have to collide with a 7-hex digest to be mistaken for generated one;
207
- * a policy named anything else is left alone and merely reported, because a
208
- * custom name is indistinguishable from one someone wrote in SQL on purpose.
209
- */
210
- export function isGeneratedPolicyName(name: string, table: string): boolean {
211
- return new RegExp(`^${table.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}_(select|insert|update|delete|all)_[0-9a-f]{7}(_\\d+)?$`)
212
- .test(name);
213
- }
214
-
215
- export interface OrphanCleanup {
216
- /** Superseded generated policies that were dropped. */
217
- dropped: PolicyRef[];
218
- /** Orphans left in place because their names are not generator-shaped. */
219
- kept: PolicyRef[];
220
- }
221
-
222
- /**
223
- * Drop the policies an earlier push superseded but never removed.
224
- *
225
- * Only touches tables the collections describe — a table with no expected
226
- * policy is not ours to reconcile, and scanning by schema alone would sweep up
227
- * policies belonging to something else sharing the database.
228
- */
229
- export async function dropOrphanedPolicies(
230
- client: Queryable,
231
- drift: PolicyDrift,
232
- collections: CollectionConfig[]
233
- ): Promise<OrphanCleanup> {
234
- const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(collections));
235
- const managed = new Set(expected.map((p) => `${p.schema}.${p.table}`));
236
-
237
- const cleanup: OrphanCleanup = { dropped: [], kept: [] };
238
- for (const p of drift.orphaned) {
239
- if (!managed.has(`${p.schema}.${p.table}`) || !isGeneratedPolicyName(p.name, p.table)) {
240
- cleanup.kept.push(p);
241
- continue;
242
- }
243
- // Identifiers are quoted, and the name came from pg_policies rather than
244
- // from user input, so it is already a valid identifier.
245
- await client.query(`DROP POLICY IF EXISTS "${p.name}" ON "${p.schema}"."${p.table}"`);
246
- cleanup.dropped.push(p);
247
- }
248
- return cleanup;
249
- }
250
-
251
195
  export const hasDrift = (d: PolicyDrift): boolean =>
252
196
  d.missing.length > 0 || d.orphaned.length > 0 || d.diverged.length > 0;
253
197