@rebasepro/server-postgres 0.16.0 → 0.16.1-canary.g0d7af95

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 (99) hide show
  1. package/dist/PostgresAdapter.d.ts +1 -1
  2. package/dist/PostgresBackendDriver.d.ts +75 -12
  3. package/dist/PostgresBootstrapper.d.ts +6 -6
  4. package/dist/auth/services.d.ts +1 -1
  5. package/dist/backup/backup-cron.d.ts +1 -1
  6. package/dist/backup/backup-service.d.ts +2 -2
  7. package/dist/backup/index.d.ts +4 -4
  8. package/dist/{backup-service-BZoixhVl.js → backup-service-BtgHxfFm.js} +5 -4
  9. package/dist/{backup-service-BZoixhVl.js.map → backup-service-BtgHxfFm.js.map} +1 -1
  10. package/dist/cli-helpers.d.ts +41 -0
  11. package/dist/{auth-users-columns-CgyPWQ18.js → collection-index-DxJBvVTH.js} +486 -503
  12. package/dist/collection-index-DxJBvVTH.js.map +1 -0
  13. package/dist/collections/PostgresCollectionRegistry.d.ts +1 -1
  14. package/dist/collections/buildRegistry.d.ts +1 -1
  15. package/dist/collections/validate-relations.d.ts +1 -1
  16. package/dist/{connection-BuZ97wsr.js → connection-GOKU3Hu5.js} +34 -7
  17. package/dist/connection-GOKU3Hu5.js.map +1 -0
  18. package/dist/connection.d.ts +16 -0
  19. package/dist/data-transformer.d.ts +1 -1
  20. package/dist/{ensure-collection-policies-BVFb2olB.js → ensure-collection-policies-DFpOl8SM.js} +4 -4
  21. package/dist/{ensure-collection-policies-BVFb2olB.js.map → ensure-collection-policies-DFpOl8SM.js.map} +1 -1
  22. package/dist/ensure-collection-tables-DMjOkeRy.js +1952 -0
  23. package/dist/ensure-collection-tables-DMjOkeRy.js.map +1 -0
  24. package/dist/index.d.ts +16 -16
  25. package/dist/index.es.js +19 -7128
  26. package/dist/index.es.js.map +1 -1
  27. package/dist/{rls-bootstrap-sql-B5Sajku6.js → rls-bootstrap-sql-DNzaWd4C.js} +3 -3
  28. package/dist/{rls-bootstrap-sql-B5Sajku6.js.map → rls-bootstrap-sql-DNzaWd4C.js.map} +1 -1
  29. package/dist/{rls-enforcement-Ch0T6OwW.js → rls-enforcement-CInuYj1-.js} +14 -4
  30. package/dist/rls-enforcement-CInuYj1-.js.map +1 -0
  31. package/dist/schema/classify-change.d.ts +82 -0
  32. package/dist/schema/collection-index.d.ts +182 -0
  33. package/dist/schema/dynamic-tables.d.ts +1 -1
  34. package/dist/schema/ensure-collection-policies.d.ts +1 -1
  35. package/dist/schema/ensure-collection-tables.d.ts +93 -2
  36. package/dist/schema/generate-schema-commit.d.ts +136 -0
  37. package/dist/schema/generated-schema-staleness.d.ts +19 -0
  38. package/dist/schema/introspect-db-constraints.d.ts +1 -1
  39. package/dist/schema/introspect-db-logic.d.ts +3 -3
  40. package/dist/schema/introspect-db-project.d.ts +1 -1
  41. package/dist/schema/introspect-db-queries.d.ts +1 -1
  42. package/dist/schema/introspect-db-structure.d.ts +2 -2
  43. package/dist/schema/introspect-runtime.d.ts +1 -1
  44. package/dist/schema/vector-index.d.ts +88 -0
  45. package/dist/services/BranchService.d.ts +2 -2
  46. package/dist/services/FetchService.d.ts +4 -4
  47. package/dist/services/PersistService.d.ts +5 -5
  48. package/dist/services/RelationService.d.ts +3 -3
  49. package/dist/services/RelationWriteService.d.ts +3 -3
  50. package/dist/services/cdc/junction-tables.d.ts +1 -1
  51. package/dist/services/cdc/trigger-cdc.d.ts +1 -1
  52. package/dist/services/channel-bus/PostgresChannelBus.d.ts +1 -1
  53. package/dist/services/channel-bus/index.d.ts +2 -2
  54. package/dist/services/collection-helpers.d.ts +1 -1
  55. package/dist/services/dataService.d.ts +10 -10
  56. package/dist/services/index.d.ts +4 -4
  57. package/dist/services/junction-writes.d.ts +2 -2
  58. package/dist/services/nested-path.d.ts +1 -1
  59. package/dist/services/realtimeService.d.ts +3 -3
  60. package/dist/services/row-pipeline.d.ts +1 -1
  61. package/dist/services/write-denial.d.ts +1 -1
  62. package/dist/{src-BBFsDaeA.js → src-DiDgtX8P.js} +97 -1
  63. package/dist/src-DiDgtX8P.js.map +1 -0
  64. package/dist/utils/drizzle-conditions.d.ts +2 -2
  65. package/dist/websocket-CUnpSe8v.js +8188 -0
  66. package/dist/websocket-CUnpSe8v.js.map +1 -0
  67. package/dist/websocket.d.ts +29 -2
  68. package/package.json +7 -7
  69. package/src/PostgresBackendDriver.ts +190 -59
  70. package/src/backup/backup-service.ts +1 -1
  71. package/src/cli-helpers.ts +117 -2
  72. package/src/cli.ts +22 -0
  73. package/src/connection.ts +37 -3
  74. package/src/databasePoolManager.ts +5 -2
  75. package/src/schema/classify-change.ts +436 -0
  76. package/src/schema/collection-index.ts +427 -0
  77. package/src/schema/ensure-collection-tables.test.ts +168 -1
  78. package/src/schema/ensure-collection-tables.ts +365 -14
  79. package/src/schema/generate-drizzle-schema-logic.ts +23 -11
  80. package/src/schema/generate-drizzle-schema.ts +13 -2
  81. package/src/schema/generate-postgres-ddl-logic.ts +33 -6
  82. package/src/schema/generate-postgres-ddl.ts +13 -2
  83. package/src/schema/generate-schema-commit.ts +242 -0
  84. package/src/schema/generated-schema-staleness.ts +114 -1
  85. package/src/schema/vector-index.ts +278 -0
  86. package/src/services/collection-helpers.ts +3 -2
  87. package/src/websocket.ts +47 -3
  88. package/dist/auth-users-columns-CgyPWQ18.js.map +0 -1
  89. package/dist/connection-BuZ97wsr.js.map +0 -1
  90. package/dist/data_driver-ULAyJEi9.js +0 -193
  91. package/dist/data_driver-ULAyJEi9.js.map +0 -1
  92. package/dist/ensure-collection-tables-BY1pHRD_.js +0 -840
  93. package/dist/ensure-collection-tables-BY1pHRD_.js.map +0 -1
  94. package/dist/rls-enforcement-Ch0T6OwW.js.map +0 -1
  95. package/dist/src-BBFsDaeA.js.map +0 -1
  96. package/dist/utils/table-classification.d.ts +0 -8
  97. package/dist/websocket-BVgDVO-V.js +0 -614
  98. package/dist/websocket-BVgDVO-V.js.map +0 -1
  99. package/src/utils/table-classification.ts +0 -16
@@ -1,5 +1,5 @@
1
1
  import { DatabaseAdapter } from "@rebasepro/types";
2
- import type { PostgresDriverConfig } from "./PostgresBootstrapper";
2
+ import type { PostgresDriverConfig } from "./PostgresBootstrapper.js";
3
3
  /**
4
4
  * Creates a Postgres database adapter for Rebase.
5
5
  */
@@ -1,11 +1,51 @@
1
- import { DataService } from "./services/dataService";
2
- import { BranchService } from "./services/BranchService";
3
- import { RealtimeService } from "./services/realtimeService";
4
- import { DatabasePoolManager } from "./databasePoolManager";
5
- import { DrizzleClient } from "./interfaces";
1
+ import { DataService } from "./services/dataService.js";
2
+ import { BranchService } from "./services/BranchService.js";
3
+ import { RealtimeService } from "./services/realtimeService.js";
4
+ import { DatabasePoolManager } from "./databasePoolManager.js";
5
+ import { DrizzleClient } from "./interfaces.js";
6
6
  import { DatabaseAdmin, DataDriver, DeleteProps, CollectionConfig, FetchCollectionProps, FetchOneProps, ListenCollectionProps, ListenOneProps, RebaseClient, RebaseSdkData, RestFetchService, SaveManyProps, SaveProps, UpdateManyProps, DeleteManyProps, TableMetadata, User } from "@rebasepro/types";
7
- import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
8
- import { HistoryService } from "./history/HistoryService";
7
+ import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry.js";
8
+ import { HistoryService } from "./history/HistoryService.js";
9
+ /**
10
+ * Has an operator opted out of database role switching entirely?
11
+ *
12
+ * `DISABLE_DB_ROLE_SWITCHING=true` is documented (README, the configuration
13
+ * page, the backend skill) as "run Studio SQL Editor queries as the connection
14
+ * owner", for deployments whose application roles have no database role behind
15
+ * them. It is the only sanctioned way a statement that named a role runs
16
+ * without it — every other route now refuses.
17
+ *
18
+ * Exact `"true"` on purpose, matching the check this replaced. `=1` and `=yes`
19
+ * silently do nothing, which `docs/audits/80-config-and-env.md` already records
20
+ * as a finding across the env surface; fixing it here alone would make this one
21
+ * variable disagree with the rest.
22
+ */
23
+ export declare function isRoleSwitchingOptedOut(): boolean;
24
+ /**
25
+ * The role a statement will actually have run as, given the role it asked for.
26
+ *
27
+ * For the audit log, which recorded `options.role` — the *requested* role — and
28
+ * so restated the caller's request as though it were the outcome. The two part
29
+ * company exactly when {@link isRoleSwitchingOptedOut} holds, because every
30
+ * other divergence is now an error rather than a quiet substitution.
31
+ */
32
+ export declare function effectiveSqlRole(requestedRole?: string): string;
33
+ /** How {@link effectiveSqlRole} names "whatever role the connection holds". */
34
+ export declare const CONNECTION_OWNER = "<connection owner>";
35
+ /**
36
+ * A statement named a database role the connection cannot assume.
37
+ *
38
+ * Its own type because the tempting recovery — run it anyway, as the owner — is
39
+ * the one thing that must not happen. Callers that catch this should report it,
40
+ * not retry unscoped.
41
+ */
42
+ export declare class RoleSwitchUnavailableError extends Error {
43
+ readonly code = "ROLE_SWITCH_UNAVAILABLE";
44
+ readonly role: string;
45
+ /** The underlying Postgres error, when the refusal came from a live attempt. */
46
+ readonly pgError?: unknown;
47
+ constructor(role: string, pgError?: unknown);
48
+ }
9
49
  export declare class PostgresBackendDriver implements DataDriver {
10
50
  db: DrizzleClient;
11
51
  readonly registry: PostgresCollectionRegistry;
@@ -20,12 +60,18 @@ export declare class PostgresBackendDriver implements DataDriver {
20
60
  data: RebaseSdkData;
21
61
  client?: RebaseClient;
22
62
  /**
23
- * Auto-set to `true` when a SET LOCAL ROLE fails with insufficient
24
- * privileges, so subsequent queries skip the doomed attempt.
25
- * Mirrors the static `DISABLE_DB_ROLE_SWITCHING` env var but is
26
- * learned at runtime.
63
+ * Auto-set to `true` once a `SET LOCAL ROLE` has failed with insufficient
64
+ * privileges, so later statements refuse without spending the round trip
65
+ * to be refused again.
66
+ *
67
+ * Deliberately NOT a mirror of `DISABLE_DB_ROLE_SWITCHING`, which it used
68
+ * to be described as. The env var is an operator saying "run these as the
69
+ * connection owner"; this flag is the database saying "I cannot give you
70
+ * the role you asked for". The first is a decision and permits the
71
+ * fallback, the second is a failure and must not — see the SECURITY note
72
+ * in {@link executeSql}.
27
73
  */
28
- private _roleSwitchingDisabled;
74
+ private _roleSwitchingUnavailable;
29
75
  /**
30
76
  * Restricted role that authenticated (user-context) requests run as (via
31
77
  * `SET LOCAL ROLE`) so RLS binds every statement — reads *and* writes. Set
@@ -54,6 +100,15 @@ export declare class PostgresBackendDriver implements DataDriver {
54
100
  * allowing test spies applied after construction to take effect.
55
101
  */
56
102
  get admin(): DatabaseAdmin;
103
+ /**
104
+ * The catalogue-reading shim the schema planner wants.
105
+ *
106
+ * Text in, rows out — every statement it issues is a catalogue read keyed by
107
+ * schema name, and schema names are identifiers rather than bindable values,
108
+ * so there is nothing to parameterise. Runs on the driver's own handle, which
109
+ * is the connection whose privileges are already known to work.
110
+ */
111
+ private schemaFactsQueryable;
57
112
  /**
58
113
  * REST-optimised fetch service (include-aware eager-loading).
59
114
  * Delegates to the underlying FetchService (include-aware eager loading),
@@ -154,6 +209,14 @@ export declare class PostgresBackendDriver implements DataDriver {
154
209
  checkUniqueField(path: string, name: string, value: unknown, id?: string, collection?: CollectionConfig): Promise<boolean>;
155
210
  count<M extends Record<string, unknown>>({ path, collection, filter, logical, searchString, vectorSearch }: FetchCollectionProps<M>): Promise<number>;
156
211
  private getTargetDb;
212
+ /**
213
+ * Build one statement, binding `$n` placeholders as real parameters.
214
+ *
215
+ * Shared by the role-switched path (inside a transaction) and the
216
+ * unswitched one. They held byte-identical copies of this loop, which is
217
+ * exactly the shape where a fix lands in one copy and not the other.
218
+ */
219
+ private buildStatement;
157
220
  executeSql(sqlText: string, options?: {
158
221
  database?: string;
159
222
  role?: string;
@@ -5,12 +5,12 @@
5
5
  */
6
6
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
7
7
  import { BackendBootstrapper, CollectionConfig, type RealtimeChannelsConfig } from "@rebasepro/types";
8
- import { PostgresBackendDriver } from "./PostgresBackendDriver";
9
- import { RealtimeService } from "./services/realtimeService";
10
- import { DatabasePoolManager } from "./databasePoolManager";
11
- import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
12
- import { type CdcTableRef } from "./services/cdc/trigger-cdc";
13
- import { type UnknownFilterFieldsMode } from "./utils/drizzle-conditions";
8
+ import { PostgresBackendDriver } from "./PostgresBackendDriver.js";
9
+ import { RealtimeService } from "./services/realtimeService.js";
10
+ import { DatabasePoolManager } from "./databasePoolManager.js";
11
+ import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry.js";
12
+ import { type CdcTableRef } from "./services/cdc/trigger-cdc.js";
13
+ import { type UnknownFilterFieldsMode } from "./utils/drizzle-conditions.js";
14
14
  export interface PostgresDriverConfig {
15
15
  connectionString?: string;
16
16
  adminConnectionString?: string;
@@ -1,5 +1,5 @@
1
1
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
2
- import type { RebasePgTable } from "../types";
2
+ import type { RebasePgTable } from "../types.js";
3
3
  import { UserRepository, TokenRepository, MfaRepository, AuthRepository, UserData, CreateUserData, RoleData, CreateRoleData, RefreshTokenInfo, RefreshTokenSession, PasswordResetTokenInfo, MagicLinkTokenInfo, UserIdentityData, ListUsersOptions, PaginatedUsersResult, MfaFactor, MfaChallengeInfo, RoleData as Role } from "@rebasepro/server";
4
4
  export type { Role };
5
5
  export interface AuthSchemaTables {
@@ -1,6 +1,6 @@
1
1
  import type { CronJobDefinition } from "@rebasepro/types";
2
2
  import type { StorageController } from "@rebasepro/server";
3
- import { BackupDestination } from "./pg-tools";
3
+ import { BackupDestination } from "./pg-tools.js";
4
4
  export interface BackupCronConfig {
5
5
  /** Cron expression, e.g. `"0 3 * * *"` for 03:00 daily. */
6
6
  schedule: string;
@@ -1,6 +1,6 @@
1
1
  import type { StorageController } from "@rebasepro/server";
2
- import { BackupDestination, type RowSecurityIdentity, VersionCompatibility } from "./pg-tools";
3
- import { BackupObject, RetentionOptions } from "./retention";
2
+ import { BackupDestination, type RowSecurityIdentity, VersionCompatibility } from "./pg-tools.js";
3
+ import { BackupObject, RetentionOptions } from "./retention.js";
4
4
  /**
5
5
  * Remove a dump artifact abandoned by a failed tool run. See
6
6
  * {@link discardPartialDumpWith} for why this exists and why the logic lives
@@ -6,7 +6,7 @@
6
6
  * - `backup-service`— pg_dump/pg_restore + storage orchestration
7
7
  * - `backup-cron` — scheduled backups for the server cron system
8
8
  */
9
- export * from "./pg-tools";
10
- export * from "./retention";
11
- export * from "./backup-service";
12
- export * from "./backup-cron";
9
+ export * from "./pg-tools.js";
10
+ export * from "./retention.js";
11
+ export * from "./backup-service.js";
12
+ export * from "./backup-cron.js";
@@ -1,8 +1,9 @@
1
1
  import { createRequire as __createRequire } from "module";
2
2
  import process from "process";
3
3
  __createRequire(import.meta.url);
4
- import { c as __exportAll, l as __require, s as __commonJSMin, u as __toESM } from "./connection-BuZ97wsr.js";
5
- import "./src-BBFsDaeA.js";
4
+ import { d as __require, f as __toESM, l as __commonJSMin, u as __exportAll } from "./connection-GOKU3Hu5.js";
5
+ import "./collection-index-DxJBvVTH.js";
6
+ import "./src-DiDgtX8P.js";
6
7
  import fs from "fs";
7
8
  import path from "path";
8
9
  import os from "os";
@@ -8992,7 +8993,7 @@ async function uploadBackup(storage, localFile, dest) {
8992
8993
  key,
8993
8994
  bucket: dest.bucket,
8994
8995
  metadata: { "rebase-backup": "1" }
8995
- })).storageUrl ?? `${dest.kind}://${dest.bucket}/${key}`
8996
+ })).storageUrl
8996
8997
  };
8997
8998
  }
8998
8999
  /**
@@ -9040,4 +9041,4 @@ async function pruneBackups(dest, options, storage) {
9040
9041
  //#endregion
9041
9042
  export { parseDbNameFromUrl as A, buildRowSecurityPgOptions as C, joinStorageKey as D, globalsFileForDump as E, withDatabaseName as F, resolveConnectionString as M, serverVersionNumToMajor as N, parseBackupDestination as O, splitGlobalsStatements as P, buildPgRestoreListArgs as S, diagnoseRowSecurityDumpFailure as T, selectBackupsToPrune as _, detectToolMajor as a, buildPgDumpallGlobalsArgs as b, getServerVersionMajor as c, pruneBackups as d, resolvePgBinary as f, require_source as g, validateDump as h, createDump as i, parsePgToolMajor as j, parseBackupTimestamp as k, listBackups as l, uploadBackup as m, applyGlobals as n, discardPartialDump as o, restoreDump as p, backup_service_exports as r, ensureDatabaseExists as s, BackupToolError as t, preflight as u, buildBackupFilename as v, checkToolServerCompatibility as w, buildPgRestoreArgs as x, buildPgDumpArgs as y };
9042
9043
 
9043
- //# sourceMappingURL=backup-service-BZoixhVl.js.map
9044
+ //# sourceMappingURL=backup-service-BtgHxfFm.js.map