@rebasepro/server-postgres 0.12.1-canary.g389e9b2 → 0.12.1-canary.g4e7bcbf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/PostgresBackendDriver.d.ts +1 -1
- package/dist/{backup-service-vAKWJkYL.js → backup-service-DLb2drIH.js} +7 -1
- package/dist/backup-service-DLb2drIH.js.map +1 -0
- package/dist/cli-helpers.d.ts +15 -0
- package/dist/{ensure-collection-policies-BjSwj0FM.js → ensure-collection-policies-Dv21KDMJ.js} +2 -2
- package/dist/{ensure-collection-policies-BjSwj0FM.js.map → ensure-collection-policies-Dv21KDMJ.js.map} +1 -1
- package/dist/{ensure-collection-tables-D6-XhqnQ.js → ensure-collection-tables-CT6xHB1d.js} +72 -12
- package/dist/ensure-collection-tables-CT6xHB1d.js.map +1 -0
- package/dist/index.es.js +74 -21
- package/dist/index.es.js.map +1 -1
- package/dist/schema/ensure-collection-tables.d.ts +21 -1
- package/dist/schema/generate-postgres-ddl-logic.d.ts +19 -0
- package/dist/services/FetchService.d.ts +4 -1
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/row-pipeline.d.ts +1 -1
- package/dist/{src-C_NHNVW2.js → src-BkdpiQdw.js} +137 -18
- package/dist/src-BkdpiQdw.js.map +1 -0
- package/dist/{websocket-DB7TbFPT.js → websocket-B2LsrINK.js} +4 -3
- package/dist/websocket-B2LsrINK.js.map +1 -0
- package/package.json +6 -6
- package/src/PostgresAdapter.ts +7 -2
- package/src/PostgresBackendDriver.ts +4 -0
- package/src/cli-helpers.ts +26 -0
- package/src/cli.ts +2 -19
- package/src/schema/ensure-collection-tables.ts +81 -10
- package/src/schema/generate-postgres-ddl-logic.ts +50 -6
- package/src/schema/introspect-db-logic.ts +21 -3
- package/src/security/policy-drift.test.ts +11 -3
- package/src/services/FetchService.ts +76 -14
- package/src/services/dataService.ts +3 -1
- package/src/services/row-pipeline.ts +1 -1
- package/src/websocket.ts +15 -6
- package/dist/backup-service-vAKWJkYL.js.map +0 -1
- package/dist/ensure-collection-tables-D6-XhqnQ.js.map +0 -1
- package/dist/src-C_NHNVW2.js.map +0 -1
- package/dist/websocket-DB7TbFPT.js.map +0 -1
|
@@ -4,7 +4,7 @@ __createRequire(import.meta.url);
|
|
|
4
4
|
import { c as __exportAll } from "./connection-BuZ97wsr.js";
|
|
5
5
|
import "./src-DoU9yPqq.js";
|
|
6
6
|
import { t as ANONYMOUS_USER_ID } from "./policy-CeA1JcxP.js";
|
|
7
|
-
import { extractUserFromToken, logger, safeCompare } from "@rebasepro/server";
|
|
7
|
+
import { extractUserFromToken, logger, resolveRequireAuth, safeCompare } from "@rebasepro/server";
|
|
8
8
|
import { WebSocketServer } from "ws";
|
|
9
9
|
import { inspect } from "util";
|
|
10
10
|
//#region ../types/src/types/backend.ts
|
|
@@ -74,7 +74,8 @@ function createPostgresWebSocket(server, realtimeService, driver, authConfig, au
|
|
|
74
74
|
if (err.code === "EADDRINUSE") return;
|
|
75
75
|
logger.error("❌ [WebSocket Server] Error", { error: err });
|
|
76
76
|
});
|
|
77
|
-
const requireAuth = authAdapter
|
|
77
|
+
const requireAuth = !!authAdapter || resolveRequireAuth(authConfig);
|
|
78
|
+
if (requireAuth && !authAdapter && !authConfig?.jwtSecret && !authConfig?.serviceKey) logger.warn("🔐 [WebSocket Server] Authentication is required but no adapter, jwtSecret or serviceKey is configured — no client can complete AUTH, so every realtime message will be refused with UNAUTHORIZED.");
|
|
78
79
|
wss.on("connection", (ws) => {
|
|
79
80
|
const clientId = `client_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
80
81
|
wsDebug(`WebSocket client connected: ${clientId}`);
|
|
@@ -526,4 +527,4 @@ function createPostgresWebSocket(server, realtimeService, driver, authConfig, au
|
|
|
526
527
|
//#endregion
|
|
527
528
|
export { websocket_exports as n, createPostgresWebSocket as t };
|
|
528
529
|
|
|
529
|
-
//# sourceMappingURL=websocket-
|
|
530
|
+
//# sourceMappingURL=websocket-B2LsrINK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket-B2LsrINK.js","names":[],"sources":["../../types/src/types/backend.ts","../src/websocket.ts"],"sourcesContent":["import type { CollectionConfig, FilterValues, WhereFilterOp } from \"./collections\";\nimport type { LogicalCondition } from \"../controllers/data\";\nimport type { AuthAdapter } from \"./auth_adapter\";\nimport type { HistoryConfig } from \"../controllers/client\";\nimport type { ChannelBusSetting } from \"./channel_bus\";\n\n// =============================================================================\n// DATABASE CONNECTION INTERFACES\n// =============================================================================\n\n/**\n * Abstract database connection interface.\n * Represents a connection to any database system.\n */\nexport interface DatabaseConnection {\n /**\n * Type identifier for this database (e.g., 'postgres', 'mongodb', 'mysql')\n */\n readonly type: string;\n\n /**\n * Whether the connection is currently active\n */\n readonly isConnected?: boolean;\n\n /**\n * Close the database connection and release resources.\n */\n close?(): Promise<void>;\n}\n\n// =============================================================================\n// QUERY BUILDING INTERFACES\n// =============================================================================\n\n/**\n * A single filter condition for database queries\n */\nexport interface QueryFilter {\n field: string;\n operator: WhereFilterOp;\n value: unknown;\n}\n\n/**\n * Options for fetching a collection of entities\n */\nexport interface FetchCollectionOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n offset?: number;\n startAfter?: unknown;\n searchString?: string;\n databaseId?: string;\n collection?: CollectionConfig;\n}\n\n/**\n * Options for searching entities\n */\nexport interface SearchOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n databaseId?: string;\n collection?: CollectionConfig;\n}\n\n/**\n * Options for counting entities\n */\nexport interface CountOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n /**\n * An `or(...)`/`and(...)` group, alongside `filter`.\n *\n * Counted as well as fetched, or `total` describes a different set of rows\n * from the one that was served — the same reason `filter` is here.\n */\n logical?: LogicalCondition;\n searchString?: string;\n databaseId?: string;\n}\n\n/**\n * Abstract condition builder interface.\n * Implementations translate Rebase filter conditions to database-specific queries.\n *\n * Note: This interface can be implemented as instance methods or as a class with static methods.\n * For static implementations (like DrizzleConditionBuilder), use the ConditionBuilderStatic type.\n *\n * @template T The type of condition returned by the builder (e.g., SQL for PostgreSQL, Filter<Document> for MongoDB)\n */\nexport interface ConditionBuilder<T = unknown> {\n /**\n * Build filter conditions from Rebase FilterValues\n */\n buildFilterConditions<M extends Record<string, unknown>>(\n filter: FilterValues<Extract<keyof M, string>>,\n collectionPath: string,\n ...args: unknown[]\n ): T[];\n\n /**\n * Build search conditions for text search\n */\n buildSearchConditions(\n searchString: string,\n properties: Record<string, unknown>,\n ...args: unknown[]\n ): T[];\n\n /**\n * Combine multiple conditions with AND operator\n */\n combineConditionsWithAnd(conditions: T[]): T | undefined;\n\n /**\n * Combine multiple conditions with OR operator\n */\n combineConditionsWithOr(conditions: T[]): T | undefined;\n}\n\n/**\n * Static condition builder type for implementations using static methods.\n * Use this type when the class provides static methods rather than instance methods.\n *\n * @example\n * // DrizzleConditionBuilder satisfies this type\n * const builder: ConditionBuilderStatic<SQL> = DrizzleConditionBuilder;\n */\nexport type ConditionBuilderStatic<T = unknown> = {\n buildFilterConditions<M extends Record<string, unknown>>(\n filter: FilterValues<Extract<keyof M, string>>,\n ...args: unknown[]\n ): T[];\n buildSearchConditions(\n searchString: string,\n properties: Record<string, unknown>,\n ...args: unknown[]\n ): T[];\n combineConditionsWithAnd(conditions: T[]): T | undefined;\n combineConditionsWithOr(conditions: T[]): T | undefined;\n};\n\n// =============================================================================\n// ENTITY REPOSITORY INTERFACES\n// =============================================================================\n\n/**\n * Abstract entity repository interface.\n * Handles all CRUD operations for entities in the database.\n *\n * Implementations should handle:\n * - Entity serialization/deserialization\n * - Relation resolution\n * - ID generation and conversion\n */\nexport interface DataRepository {\n /**\n * Fetch a single entity by ID\n */\n fetchOne<M extends Record<string, unknown>>(\n collectionPath: string,\n id: string | number,\n databaseId?: string\n ): Promise<Record<string, unknown> | undefined>;\n\n /**\n * Fetch a collection of entities with optional filtering, ordering, and pagination\n */\n fetchCollection<M extends Record<string, unknown>>(\n collectionPath: string,\n options?: FetchCollectionOptions<M>\n ): Promise<Record<string, unknown>[]>;\n\n /**\n * Search entities by text\n */\n searchRows<M extends Record<string, unknown>>(\n collectionPath: string,\n searchString: string,\n options?: SearchOptions<M>\n ): Promise<Record<string, unknown>[]>;\n\n /**\n * Count entities in a collection\n */\n count<M extends Record<string, unknown>>(\n collectionPath: string,\n options?: CountOptions<M>\n ): Promise<number>;\n\n /**\n * Save a entity (create or update)\n */\n save<M extends Record<string, unknown>>(\n collectionPath: string,\n values: Partial<M>,\n id?: string | number,\n databaseId?: string\n ): Promise<Record<string, unknown>>;\n\n /**\n * Delete a entity by ID\n */\n delete(\n collectionPath: string,\n id: string | number,\n databaseId?: string\n ): Promise<void>;\n\n /**\n * Check if a field value is unique in a collection\n */\n checkUniqueField(\n collectionPath: string,\n fieldName: string,\n value: unknown,\n excludeEntityId?: string,\n databaseId?: string\n ): Promise<boolean>;\n\n}\n\n// =============================================================================\n// REALTIME INTERFACES\n// =============================================================================\n\n/**\n * Configuration for subscribing to a collection\n */\nexport interface CollectionSubscriptionConfig {\n clientId: string;\n path: string;\n filter?: unknown;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n startAfter?: unknown;\n databaseId?: string;\n searchString?: string;\n}\n\n/**\n * Configuration for subscribing to a single entity\n */\nexport interface SingleSubscriptionConfig {\n clientId: string;\n path: string;\n id: string | number;\n}\n\n/**\n * Opt-in retention for one set of broadcast channels.\n *\n * Retention is configured on the server and nowhere else. A channel is created\n * by whoever names it, so letting a client ask for its own history depth would\n * let any visitor commit the backend to unbounded storage; and presence-only or\n * notification-only channels — the overwhelming majority — must not pay for a\n * feature they never use. With no rules configured nothing is written, no table\n * is created, and broadcast behaves exactly as it did before history existed.\n */\nexport interface ChannelRetentionRule {\n /**\n * Channel name to match. Either exact (`\"doc:42\"`) or a trailing-`*` prefix\n * (`\"doc:*\"`). Deliberately not a full glob or RegExp: this decides what\n * gets written to disk, and a rule whose blast radius is not obvious at a\n * glance is the wrong shape for that.\n */\n match: string;\n /** Keep at most this many of the most recent messages per channel. */\n limit?: number;\n /**\n * Keep messages for at most this long. Accepts a millisecond count or a\n * short duration string (`\"30s\"`, `\"15m\"`, `\"24h\"`, `\"7d\"`).\n */\n ttl?: number | string;\n}\n\n/**\n * Server-side realtime options.\n *\n * The channel bus contract and its config live in `./channel_bus` so that a\n * transport shipped as its own package depends on the contract alone.\n */\nexport interface RealtimeChannelsConfig {\n /**\n * Retention rules, most specific first — the first match wins. Omitted or\n * empty means no channel retains anything.\n */\n channels?: ChannelRetentionRule[];\n /**\n * How channel broadcast and presence reach other backend instances.\n * Defaults to `{ type: \"memory\" }` — i.e. they don't.\n */\n bus?: ChannelBusSetting;\n}\n\n/**\n * Abstract realtime provider interface.\n * Handles real-time subscriptions and notifications for entity changes.\n */\nexport interface RealtimeProvider {\n /**\n * Subscribe to collection changes\n */\n subscribeToCollection(\n subscriptionId: string,\n config: CollectionSubscriptionConfig,\n callback?: (rows: Record<string, unknown>[]) => void\n ): void;\n\n /**\n * Subscribe to single entity changes\n */\n subscribeToOne(\n subscriptionId: string,\n config: SingleSubscriptionConfig,\n callback?: (row: Record<string, unknown> | null) => void\n ): void;\n\n /**\n * Unsubscribe from a subscription\n */\n unsubscribe(subscriptionId: string): void;\n\n /**\n * Notify all relevant subscribers of a entity update\n */\n notifyUpdate(\n path: string,\n id: string,\n row: Record<string, unknown> | null,\n databaseId?: string\n ): Promise<void>;\n\n /**\n * Called when the HTTP server is ready and listening.\n * Useful for providers that need the server address for callbacks.\n */\n onServerReady?(serverInfo: { port: number; hostname?: string }): void;\n\n /**\n * Gracefully shut down the realtime provider.\n * Called during server shutdown to clean up resources.\n */\n destroy?(): Promise<void>;\n\n /**\n * Stop the internal LISTEN client (e.g., PostgreSQL LISTEN/NOTIFY).\n * Called during graceful shutdown before closing database connections.\n */\n stopListening?(): Promise<void>;\n}\n\n// =============================================================================\n// COLLECTION REGISTRY INTERFACES\n// =============================================================================\n\n/**\n * Abstract collection registry interface.\n * Manages registration and lookup of entity collections.\n */\nexport interface CollectionRegistryInterface {\n /**\n * Register a collection\n */\n register(collection: CollectionConfig): void;\n\n /**\n * Get a collection by its path\n */\n getCollectionByPath(path: string): CollectionConfig | undefined;\n\n /**\n * Get all registered collections\n */\n getCollections(): CollectionConfig[];\n\n /**\n * Get the currently registered global callbacks, if any.\n */\n getGlobalCallbacks(): any | undefined;\n}\n\n// =============================================================================\n// DATA TRANSFORMER INTERFACES\n// =============================================================================\n\n/**\n * Abstract data transformer interface.\n * Handles serialization/deserialization between frontend and database formats.\n */\nexport interface DataTransformer {\n /**\n * Transform entity data for storage in the database\n */\n serializeToDatabase<M extends Record<string, unknown>>(\n entity: M,\n collection: CollectionConfig\n ): Record<string, unknown>;\n\n /**\n * Transform database data back to entity format\n */\n deserializeFromDatabase<M extends Record<string, unknown>>(\n data: Record<string, unknown>,\n collection: CollectionConfig\n ): Promise<M>;\n}\n\n// =============================================================================\n// DATABASE ADMIN — CAPABILITY-SPECIFIC INTERFACES (1.3)\n// =============================================================================\n\n/**\n * Administrative operations for SQL-based databases (PostgreSQL, MySQL, etc.).\n * Used by the SQL Editor, RLS Editor, and schema browser.\n *\n * @group Admin\n */\nexport interface SQLAdmin {\n /**\n * Execute raw SQL against the database.\n */\n executeSql(sql: string, options?: { database?: string; role?: string; params?: unknown[] }): Promise<Record<string, unknown>[]>;\n\n /**\n * Fetch the available databases on the server.\n */\n fetchAvailableDatabases?(): Promise<string[]>;\n\n /**\n * Fetch the available *native PostgreSQL* database roles (from `pg_roles`).\n *\n * These are connection-level roles — what the SQL editor can `SET ROLE` to,\n * and what `SecurityRule.pgRoles` targets. They are NOT application roles;\n * for those use {@link fetchApplicationRoles}.\n */\n fetchAvailableRoles?(): Promise<string[]>;\n\n /**\n * Fetch the *application-level* roles in use in this project.\n *\n * These are the strings stored on the users table's `roles` column and\n * exposed to policies as `auth.roles()` — what `SecurityRule.roles`\n * matches against. Distinct from {@link fetchAvailableRoles}; the two are\n * not interchangeable.\n */\n fetchApplicationRoles?(): Promise<string[]>;\n\n /**\n * Fetch the current database name.\n */\n fetchCurrentDatabase?(): Promise<string | undefined>;\n}\n\n/**\n * Administrative operations for document-based databases (MongoDB, Firestore, etc.).\n * Used by future document administration tools.\n *\n * @group Admin\n */\nexport interface DocumentAdmin {\n /**\n * Execute an aggregation pipeline or equivalent query.\n */\n executeAggregate?(pipeline: Record<string, unknown>[]): Promise<Record<string, unknown>[]>;\n\n /**\n * Fetch statistics for a collection (document count, size, etc.).\n */\n fetchCollectionStats?(collectionName: string): Promise<{ count: number; sizeBytes?: number }>;\n}\n\n/**\n * Administrative operations for schema management.\n * Shared across SQL and document databases.\n *\n * @group Admin\n */\nexport interface SchemaAdmin {\n /**\n * Fetch database tables/collections not yet mapped to a Rebase collection.\n */\n fetchUnmappedTables?(mappedPaths?: string[]): Promise<string[]>;\n\n /**\n * Fetch column/field metadata for a single table/collection.\n * The return type is generic — SQL backends return TableMetadata,\n * document backends may return a different shape.\n */\n fetchTableMetadata?(tableName: string): Promise<unknown>;\n}\n\n/**\n * Metadata for a database branch.\n * @group Admin\n */\nexport interface BranchInfo {\n /** Branch name (without prefix). */\n name: string;\n /** The database this branch was created from. */\n parentDatabase: string;\n /** When the branch was created. */\n createdAt: Date;\n /** Size in bytes, if available from the server. */\n sizeBytes?: number;\n}\n\n/**\n * Administrative operations for database branching.\n * Allows creating isolated database copies for development/preview workflows.\n *\n * @group Admin\n */\nexport interface BranchAdmin {\n /** Create a new branch (database copy) from the current or specified source database. */\n createBranch(name: string, options?: { source?: string }): Promise<BranchInfo>;\n\n /** Delete a branch database. Cannot delete the main/default database. */\n deleteBranch(name: string): Promise<void>;\n\n /** List all branches (databases that were created via branching). */\n listBranches(): Promise<BranchInfo[]>;\n\n /** Get info about a specific branch. */\n getBranchInfo(name: string): Promise<BranchInfo | undefined>;\n}\n\n/**\n * Union type for all admin capabilities.\n * A backend may implement any combination of these interfaces.\n *\n * Use type guards (`isSQLAdmin`, `isDocumentAdmin`, `isSchemaAdmin`, `isBranchAdmin`)\n * to safely narrow the type before calling methods.\n *\n * @group Admin\n */\nexport type DatabaseAdmin = Partial<SQLAdmin> & Partial<DocumentAdmin> & Partial<SchemaAdmin> & Partial<BranchAdmin>;\n\n/**\n * Type guard: does this admin support SQL operations?\n * @group Admin\n */\nexport function isSQLAdmin(admin: DatabaseAdmin | undefined): admin is SQLAdmin {\n return !!admin && typeof (admin as SQLAdmin).executeSql === \"function\";\n}\n\n/**\n * Type guard: does this admin support document operations?\n * @group Admin\n */\nexport function isDocumentAdmin(admin: DatabaseAdmin | undefined): admin is DocumentAdmin {\n return !!admin && (\n typeof (admin as DocumentAdmin).executeAggregate === \"function\" ||\n typeof (admin as DocumentAdmin).fetchCollectionStats === \"function\"\n );\n}\n\n/**\n * Type guard: does this admin support schema management?\n * @group Admin\n */\nexport function isSchemaAdmin(admin: DatabaseAdmin | undefined): admin is SchemaAdmin {\n return !!admin && (\n typeof (admin as SchemaAdmin).fetchUnmappedTables === \"function\" ||\n typeof (admin as SchemaAdmin).fetchTableMetadata === \"function\"\n );\n}\n\n/**\n * Type guard: does this admin support database branching?\n * @group Admin\n */\nexport function isBranchAdmin(admin: DatabaseAdmin | undefined): admin is BranchAdmin {\n return !!admin && typeof (admin as BranchAdmin).createBranch === \"function\";\n}\n\n// =============================================================================\n// LIFECYCLE INTERFACES (1.4)\n// =============================================================================\n\n/**\n * Health check result returned by `healthCheck()`.\n * @group Lifecycle\n */\nexport interface HealthCheckResult {\n /** Whether the backend is healthy and able to serve requests. */\n healthy: boolean;\n /** Round-trip latency to the database in milliseconds. */\n latencyMs: number;\n /** Optional details (e.g., pool stats, replication lag). */\n details?: Record<string, unknown>;\n}\n\n/**\n * Lifecycle contract for backend components that hold resources\n * (database connections, WebSocket pools, timers, etc.).\n *\n * All methods are optional — simple backends (e.g., in-memory) can skip them.\n * @group Lifecycle\n */\nexport interface BackendLifecycle {\n /**\n * Initialize the backend: open connections, run migrations, seed data.\n * Called once during startup. Idempotent.\n */\n initialize?(): Promise<void>;\n\n /**\n * Check whether the backend is healthy and reachable.\n * Should be fast (< 1 s) and safe to call frequently.\n */\n healthCheck?(): Promise<HealthCheckResult>;\n\n /**\n * Gracefully shut down: close connections, flush buffers, cancel timers.\n * After calling `destroy()`, no other methods should be called.\n */\n destroy?(): Promise<void>;\n}\n\n// =============================================================================\n// BACKEND FACTORY INTERFACES\n// =============================================================================\n\n/**\n * Configuration for creating a database backend\n */\nexport interface BackendConfig {\n /**\n * Type of database backend\n */\n type: string;\n\n /**\n * Database connection (implementation-specific)\n */\n connection: unknown;\n\n /**\n * Schema definition (implementation-specific, e.g., Drizzle schema for PostgreSQL)\n */\n schema?: unknown;\n}\n\n/**\n * A complete backend instance with all required services.\n *\n * Now includes optional lifecycle management and admin capabilities.\n */\nexport interface BackendInstance extends BackendLifecycle {\n /**\n * Entity repository for CRUD operations\n */\n entityRepository: DataRepository;\n\n /**\n * Realtime provider for subscriptions\n */\n realtimeProvider: RealtimeProvider;\n\n /**\n * Collection registry\n */\n collectionRegistry: CollectionRegistryInterface;\n\n /**\n * The underlying database connection\n */\n connection: DatabaseConnection;\n\n /**\n * Administrative operations (SQL, schema, documents).\n * What's available depends on the backend type — use type guards\n * (`isSQLAdmin`, `isSchemaAdmin`, etc.) to narrow.\n */\n admin?: DatabaseAdmin;\n}\n\n/**\n * Factory function type for creating backend instances\n */\nexport type BackendFactory<TConfig extends BackendConfig = BackendConfig> =\n (config: TConfig) => BackendInstance;\n\n// =============================================================================\n// BACKEND BOOTSTRAPPER (1.2)\n// =============================================================================\n\n/**\n * A `BackendBootstrapper` encapsulates all driver-specific initialization logic.\n *\n * Instead of hard-coding Postgres setup into `initializeRebaseBackend()`,\n * each database backend provides its own bootstrapper that knows how to:\n * - Create the DataDriver from a config object\n * - Optionally initialize auth tables\n * - Optionally create a realtime service\n * - Mount driver-specific API routes\n *\n * The main `initializeRebaseBackend()` becomes a **coordinator** that iterates\n * registered bootstrappers, calls their hooks, and wires the results together.\n *\n * @group Backend\n *\n * @example\n * ```typescript\n * // Third-party MySQL bootstrapper\n * const mysqlBootstrapper: BackendBootstrapper = {\n * type: \"mysql\",\n * initializeDriver: async (config) => new MySQLDataDriver(config.connection),\n * initializeRealtime: async (config) => new MySQLChangeStreamRealtime(config.connection),\n * };\n *\n * initializeRebaseBackend({\n * ...config,\n * bootstrappers: [postgresBootstrapper, mysqlBootstrapper]\n * });\n * ```\n */\nexport interface BackendBootstrapper {\n /**\n * Which driver type this bootstrapper handles.\n * Must match the `type` field on the driver config object\n * (e.g., `\"postgres\"`, `\"mongodb\"`, `\"mysql\"`).\n */\n type: string;\n\n /**\n * Unique identifier for this bootstrapper instance.\n * Used to register the driver in the driver registry.\n * Defaults to `type` if not set.\n */\n id?: string;\n\n /**\n * Whether this bootstrapper provides the default driver.\n * When true, the coordinator uses this driver as the primary one.\n */\n isDefault?: boolean;\n\n /**\n * Run database migrations for this driver.\n * Called by the coordinator after all drivers are initialized.\n */\n runMigrations?(config: unknown, driverResult: InitializedDriver): Promise<void>;\n\n /**\n * Create a DataDriver from the given config.\n * This is the only **required** method.\n */\n initializeDriver(config: unknown): Promise<InitializedDriver>;\n\n /**\n * Initialize auth tables / services if this driver supports them.\n * Return undefined if auth is not supported by this backend.\n */\n initializeAuth?(config: unknown, driverResult: InitializedDriver): Promise<BootstrappedAuth | undefined>;\n\n /**\n * Initialize history tables / services if this driver supports them.\n * Return undefined if history is not supported by this backend.\n */\n initializeHistory?(config: HistoryConfig, driverResult: InitializedDriver): Promise<{ historyService: unknown } | undefined>;\n\n /**\n * Create a realtime provider for this driver.\n * Return undefined if the driver does not support realtime.\n */\n initializeRealtime?(config: unknown, driverResult: InitializedDriver): Promise<RealtimeProvider | undefined>;\n\n /**\n * Mount any driver-specific HTTP routes (e.g., custom admin endpoints).\n * Called after all drivers are initialized.\n */\n mountRoutes?(app: unknown, basePath: string, driverResult: InitializedDriver): void;\n\n /**\n * Return admin capabilities for this driver.\n */\n getAdmin?(driverResult: InitializedDriver): DatabaseAdmin | undefined;\n\n /**\n * Bring the database's collection tables up to date, additively.\n *\n * Optional because it is only meaningful for schema-ful drivers. A managed\n * runtime boots a compiled project against a database it has never seen; auth\n * tables are ensured on boot but collection tables were created by nothing,\n * so every data request answered 500 on a missing relation. The CLI's `db\n * push` cannot fill the gap — it needs Atlas, and the runtime image ships no\n * CLI.\n *\n * Implementations MUST be additive-only: create missing tables, columns and\n * enum types, and never drop, narrow or rewrite anything. This runs\n * unattended against live customer data with nobody reading a diff, so the\n * destructive half stays a deliberate migration.\n */\n ensureCollectionSchema?(\n collections: unknown[],\n driverResult: InitializedDriver,\n log?: (message: string) => void\n ): Promise<{ applied: number }>;\n\n /**\n * Apply the collections' row-level-security policies, additively and\n * idempotently — the companion to {@link ensureCollectionSchema}.\n *\n * That method creates the tables; a table with RLS disabled and no policies\n * is not servable, because authenticated requests run as a restricted role:\n * a read with no `SELECT` policy returns nothing (a public collection\n * answers 401) and a write with no `INSERT`/`UPDATE` policy is denied. The\n * `db push` CLI applies these from the same collections, but it cannot reach\n * a managed tenant's in-cluster database — the runtime, already connected,\n * is the only thing that can.\n *\n * MUST be idempotent (re-run on every boot) and MUST NOT be destructive.\n * Runs after auth initialization, because the generated policies call the\n * `auth.*` helper functions and `CREATE POLICY` validates they exist.\n */\n ensureCollectionPolicies?(\n collections: unknown[],\n driverResult: InitializedDriver,\n log?: (message: string) => void\n ): Promise<{ applied: number }>;\n\n /**\n * Initialize WebSocket server for realtime operations.\n */\n initializeWebsockets?(server: unknown, realtimeService: RealtimeProvider, driver: import(\"../controllers/data_driver\").DataDriver, config?: unknown, authAdapter?: AuthAdapter): Promise<void> | void;\n}\n\n/**\n * Result of `BackendBootstrapper.initializeDriver()`.\n * @group Backend\n */\nexport interface InitializedDriver {\n /** The DataDriver instance, ready for use. */\n driver: import(\"../controllers/data_driver\").DataDriver;\n\n /** The realtime service, if the driver created one during init. */\n realtimeProvider?: RealtimeProvider;\n\n /** A collection registry to register schema / tables into. */\n collectionRegistry?: CollectionRegistryInterface;\n\n /**\n * Collections the driver derived from the live database schema.\n *\n * Set by drivers that introspect in `baas` mode; the server serves these\n * instead of collections loaded from config files.\n */\n collections?: import(\"./collections\").CollectionConfig[];\n\n /** The underlying database connection (for lifecycle management). */\n connection?: DatabaseConnection;\n\n /**\n * Opaque handle that the bootstrapper can use in subsequent hooks\n * (e.g., `initializeAuth`, `mountRoutes`) to access driver internals.\n * Not used by the coordinator.\n */\n internals?: unknown;\n}\n\n/**\n * Result of `BackendBootstrapper.initializeAuth()`.\n * @group Backend\n */\nexport interface BootstrappedAuth {\n /** User management service. */\n userService: unknown;\n /** Role management service (optional, roles are now simple strings). */\n roleService?: unknown;\n /** Email service (optional). */\n emailService?: unknown;\n /** Combined Auth Repository for unified token and user management. */\n authRepository?: unknown;\n /**\n * Whether the auth schema in the database is one this runtime can serve.\n *\n * Folded into `healthCheck()` so a schema mismatch shows up as a degraded\n * health response. Without it, a server whose auth is entirely broken still\n * reports healthy — the database connection it probes is fine, and the\n * mismatch is only discovered one failed login at a time.\n */\n schemaHealthCheck?(): Promise<AuthSchemaHealth>;\n}\n\n/**\n * Result of {@link BootstrappedAuth.schemaHealthCheck}.\n * @group Lifecycle\n */\nexport interface AuthSchemaHealth {\n /** False when this runtime cannot be trusted to serve auth against this database. */\n healthy: boolean;\n /** Human-readable descriptions of each mismatch found. Empty when healthy. */\n problems: string[];\n /** Auth schema version recorded in the database, when it records one. */\n databaseVersion?: number | null;\n /** Auth schema version this runtime expects. */\n runtimeVersion?: number;\n}\n","import { RealtimeService } from \"./services/realtimeService\";\nimport { PostgresBackendDriver } from \"./PostgresBackendDriver\";\nimport type { DataDriver, DeleteProps, FetchCollectionProps, FetchOneProps, SaveProps, TableMetadata, BranchInfo, AuthAdapter } from \"@rebasepro/types\";\nimport { ANONYMOUS_USER_ID, isSQLAdmin, isSchemaAdmin } from \"@rebasepro/types\";\nimport type { User } from \"@rebasepro/types\";\n\nimport { WebSocketServer, WebSocket } from \"ws\";\nimport { Server } from \"http\";\nimport { inspect } from \"util\";\nimport { extractUserFromToken, AccessTokenPayload, safeCompare, resolveRequireAuth } from \"@rebasepro/server\";\nimport { logger } from \"@rebasepro/server\";\n\n/** Minimal subset of RebaseAuthConfig used by the WebSocket layer. */\ninterface WsAuthConfig {\n requireAuth?: boolean;\n jwtSecret?: string;\n /**\n * Same static server-to-server secret the HTTP middleware accepts. Without\n * it here, a service key authenticates over HTTP but not over the socket —\n * so any SDK client using one (scripts, cron, server-to-server) connects,\n * fails realtime auth with \"jwt malformed\", and silently gets no events.\n */\n serviceKey?: string;\n}\n\n/**\n * Normalized user identity for WebSocket sessions.\n */\ninterface WsUserIdentity {\n uid: string;\n roles: string[];\n isAdmin: boolean;\n}\n\ninterface ClientSession {\n ws: WebSocket;\n user?: WsUserIdentity;\n authenticated: boolean;\n /** Sliding window message counter for rate limiting */\n messageCount: number;\n messageWindowStart: number;\n}\n\n\n/** Maximum messages per client per window */\nconst WS_RATE_LIMIT = 2000;\n/** Rate limit window in milliseconds (60 seconds) */\nconst WS_RATE_WINDOW_MS = 60_000;\n\n/** Admin-only WebSocket message types */\nconst ADMIN_ONLY_TYPES = new Set([\n \"EXECUTE_SQL\",\n \"FETCH_DATABASES\",\n \"FETCH_ROLES\",\n \"FETCH_UNMAPPED_TABLES\",\n \"FETCH_TABLE_METADATA\",\n \"FETCH_CURRENT_DATABASE\",\n \"CREATE_BRANCH\",\n \"DELETE_BRANCH\",\n \"LIST_BRANCHES\"\n]);\n\n/**\n * Recursively extract the deepest error message from an error's cause chain (e.g., Drizzle wrapping a PG error).\n */\nfunction extractErrorMessage(error: unknown): string {\n if (!error) return \"Unknown error\";\n if (error instanceof Error) {\n if (\"cause\" in error && error.cause) {\n return extractErrorMessage(error.cause);\n }\n return error.message;\n }\n if (typeof error === \"object\" && \"message\" in error && typeof (error as { message: unknown }).message === \"string\") {\n return (error as { message: string }).message;\n }\n return String(error);\n}\n\n/**\n * Check if the current session belongs to an admin user.\n */\nfunction isAdminSession(session: ClientSession | undefined): boolean {\n if (!session?.user) return false;\n // Fast path: new adapter-aware sessions set isAdmin directly\n if (session.user.isAdmin) return true;\n if (!session.user.roles) return false;\n return session.user.roles.some((r) => r === \"admin\");\n}\n\nexport function createPostgresWebSocket(\n server: Server,\n realtimeService: RealtimeService,\n driver: PostgresBackendDriver,\n authConfig?: WsAuthConfig,\n authAdapter?: AuthAdapter\n) {\n // Session map scoped to this factory invocation — prevents stale sessions\n // leaking across hot reloads or multiple factory calls.\n const clientSessions = new Map<string, ClientSession>();\n\n const isProduction = process.env.NODE_ENV === \"production\";\n /** Debug logger that is suppressed in production to prevent PII/data leaks */\n const wsDebug = (...args: unknown[]) => { if (!isProduction) console.debug(...args); };\n const wss = new WebSocketServer({ server });\n\n // Handle errors on the WSS so that EADDRINUSE from the underlying HTTP\n // server doesn't surface as an unhandled 'error' event and crash the\n // process. The dev-mode `listenWithPortRetry` utility handles retry\n // logic on the HTTP server side — we just need the WSS not to throw.\n wss.on(\"error\", (err: NodeJS.ErrnoException) => {\n if (err.code === \"EADDRINUSE\") {\n // Silently absorbed — listenWithPortRetry will retry the next port\n return;\n }\n logger.error(\"❌ [WebSocket Server] Error\", { error: err });\n });\n\n // The same predicate the HTTP data routes use, from the same function —\n // this socket is the other enforcement point for one product decision, and\n // while it computed the answer itself it computed a different one. See\n // `resolveRequireAuth` for what its local copy got wrong and why a `false`\n // here grants access rather than skipping a check.\n const requireAuth = !!authAdapter || resolveRequireAuth(authConfig as never);\n\n if (requireAuth && !authAdapter && !authConfig?.jwtSecret && !authConfig?.serviceKey) {\n logger.warn(\n \"🔐 [WebSocket Server] Authentication is required but no adapter, jwtSecret or \" +\n \"serviceKey is configured — no client can complete AUTH, so every realtime \" +\n \"message will be refused with UNAUTHORIZED.\"\n );\n }\n\n wss.on(\"connection\", (ws) => {\n const clientId = `client_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;\n wsDebug(`WebSocket client connected: ${clientId}`);\n\n // Initialize client session\n clientSessions.set(clientId, { ws,\nauthenticated: !requireAuth,\nmessageCount: 0,\nmessageWindowStart: Date.now() });\n realtimeService.addClient(clientId, ws);\n\n ws.on(\"close\", () => {\n wsDebug(`WebSocket client disconnected: ${clientId}`);\n clientSessions.delete(clientId);\n });\n\n // Route all messages through RealtimeService for unified handling\n ws.on(\"message\", async (message) => {\n let requestId: string | undefined;\n try {\n const {\n type,\n payload,\n requestId: reqId\n } = JSON.parse(message.toString());\n requestId = reqId; // Capture requestId for use in catch block\n\n wsDebug(`[WS] ${clientId} → ${type}`, requestId ? `(${requestId})` : \"\");\n\n // Handle authentication first\n // Helper: send a canonical error frame\n const sendError = (errType: \"ERROR\" | \"AUTH_ERROR\", code: string, msg: string) => {\n ws.send(JSON.stringify({\n type: errType,\n requestId,\n payload: { error: { message: msg,\ncode } }\n }));\n };\n\n if (type === \"AUTHENTICATE\") {\n const { token } = payload || {};\n if (!token) {\n sendError(\"AUTH_ERROR\", \"INVALID_INPUT\", \"Token is required\");\n return;\n }\n\n // Use the auth adapter when available (custom auth, Clerk, etc.)\n // Fall back to JWT extraction otherwise.\n let verifiedUser: WsUserIdentity | null = null;\n\n if (authAdapter) {\n try {\n const adapterUser = authAdapter.verifyToken\n ? await authAdapter.verifyToken(token)\n : await authAdapter.verifyRequest(new Request(\"http://localhost/_ws_auth\", {\n headers: { Authorization: `Bearer ${token}` }\n }));\n\n if (adapterUser) {\n verifiedUser = {\n uid: adapterUser.uid,\n roles: adapterUser.roles,\n isAdmin: adapterUser.isAdmin\n };\n }\n } catch {\n // Adapter threw — treat as invalid token\n }\n } else if (authConfig?.serviceKey && safeCompare(token, authConfig.serviceKey)) {\n // Service key: a static secret, not a JWT. Checked\n // before verification, mirroring the HTTP middleware —\n // verifying it as a JWT can only ever fail.\n verifiedUser = { uid: \"service\", roles: [\"admin\"], isAdmin: true };\n } else {\n // Standard JWT path\n const jwtPayload = extractUserFromToken(token);\n if (jwtPayload) {\n verifiedUser = {\n uid: jwtPayload.uid,\n roles: jwtPayload.roles ?? [],\n isAdmin: (jwtPayload.roles ?? []).some((r: string) => r === \"admin\")\n };\n }\n }\n\n if (verifiedUser) {\n const session = clientSessions.get(clientId);\n if (session) {\n session.user = verifiedUser;\n session.authenticated = true;\n }\n wsDebug(`[WS] replying AUTH_SUCCESS for requestId ${requestId}`);\n ws.send(JSON.stringify({\n type: \"AUTH_SUCCESS\",\n requestId,\n payload: { uid: verifiedUser.uid,\nroles: verifiedUser.roles }\n }));\n wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.uid}`);\n } else {\n wsDebug(`[WS] replying AUTH_ERROR for requestId ${requestId} (invalid token)`);\n sendError(\"AUTH_ERROR\", \"INVALID_TOKEN\", \"Invalid or expired token\");\n }\n return;\n }\n\n // Check authentication for protected operations\n if (requireAuth) {\n const session = clientSessions.get(clientId);\n if (!session?.authenticated) {\n sendError(\"ERROR\", \"UNAUTHORIZED\", \"Authentication required\");\n return;\n }\n }\n\n // Rate limiting: reject if client exceeds message limit\n {\n const session = clientSessions.get(clientId);\n if (session) {\n const now = Date.now();\n if (now - session.messageWindowStart > WS_RATE_WINDOW_MS) {\n session.messageCount = 0;\n session.messageWindowStart = now;\n }\n session.messageCount++;\n if (session.messageCount > WS_RATE_LIMIT) {\n sendError(\"ERROR\", \"RATE_LIMITED\", \"Too many requests. Please slow down.\");\n return;\n }\n }\n }\n\n // Admin-only operations require admin role\n if (ADMIN_ONLY_TYPES.has(type)) {\n const session = clientSessions.get(clientId);\n if (!isAdminSession(session)) {\n sendError(\"ERROR\", \"FORBIDDEN\", \"Admin access required for this operation\");\n return;\n }\n }\n\n // Helper to get correctly scoped delegate for the current request\n const getScopedDelegate = async (): Promise<DataDriver> => {\n const session = clientSessions.get(clientId);\n // Check if the driver supports RLS-scoped delegates\n if (typeof driver.withAuth === \"function\") {\n try {\n const userForAuth: User = session?.user\n ? {\n uid: session.user.uid,\n displayName: null,\n email: null,\n photoURL: null,\n providerId: \"websocket\",\n isAnonymous: false,\n roles: session.user.roles ?? []\n }\n : {\n uid: ANONYMOUS_USER_ID,\n displayName: null,\n email: null,\n photoURL: null,\n providerId: \"websocket\",\n isAnonymous: true,\n roles: [\"anon\"]\n };\n return await driver.withAuth(userForAuth);\n } catch (e) {\n logger.error(\"Failed to create RLS scoped delegate for WS request\", { error: e });\n throw new Error(\"Internal authentication error\");\n }\n }\n return driver;\n };\n\n switch (type) {\n case \"FETCH_COLLECTION\": {\n wsDebug(\"📋 [WebSocket Server] Processing FETCH_COLLECTION request\");\n const request: FetchCollectionProps = payload;\n const delegate = await getScopedDelegate();\n const rows = await delegate.fetchCollection(request);\n wsDebug(\"📋 [WebSocket Server] FETCH_COLLECTION result - rows count:\", rows.length);\n const response = {\n type: \"FETCH_COLLECTION_SUCCESS\",\n payload: { rows },\n requestId\n };\n wsDebug(\"📋 [WebSocket Server] Sending FETCH_COLLECTION_SUCCESS response\");\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_ONE\": {\n wsDebug(\"📄 [WebSocket Server] Processing FETCH_ENTITY request\");\n const request: FetchOneProps = payload;\n const delegate = await getScopedDelegate();\n const row = await delegate.fetchOne(request);\n wsDebug(\"📄 [WebSocket Server] FETCH_ENTITY result:\", row);\n const response = {\n type: \"FETCH_ONE_SUCCESS\",\n payload: { row: row ?? null },\n requestId\n };\n wsDebug(\"📄 [WebSocket Server] Sending FETCH_ENTITY_SUCCESS response\");\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"SAVE\": {\n wsDebug(\"💾 [WebSocket Server] Processing SAVE_ENTITY request\");\n const request: SaveProps = payload;\n wsDebug(\"💾 [WebSocket Server] Saving row with request:\", inspect(request, { depth: null,\ncolors: true }));\n const delegate = await getScopedDelegate();\n const row = await delegate.save(request);\n wsDebug(\"💾 [WebSocket Server] SAVE_ENTITY result:\", inspect(row, { depth: null,\ncolors: true }));\n const response = {\n type: \"SAVE_SUCCESS\",\n payload: { row },\n requestId\n };\n wsDebug(\"💾 [WebSocket Server] Sending SAVE_ENTITY_SUCCESS response\");\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"DELETE\": {\n wsDebug(\"🗑️ [WebSocket Server] Processing DELETE_ENTITY request\");\n const request: DeleteProps = payload;\n wsDebug(\"🗑️ [WebSocket Server] Deleting row:\", request.row);\n const delegate = await getScopedDelegate();\n await delegate.delete(request);\n wsDebug(\"🗑️ [WebSocket Server] DELETE_ENTITY completed successfully\");\n const response = {\n type: \"DELETE_SUCCESS\",\n payload: { success: true },\n requestId\n };\n wsDebug(\"🗑️ [WebSocket Server] Sending DELETE_ENTITY_SUCCESS response\");\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"CHECK_UNIQUE_FIELD\": {\n wsDebug(\"🔍 [WebSocket Server] Processing CHECK_UNIQUE_FIELD request\");\n const {\n path,\n name,\n value,\n id,\n collection\n } = payload;\n const delegate = await getScopedDelegate();\n const isUnique = await delegate.checkUniqueField(path, name, value, id, collection);\n wsDebug(\"🔍 [WebSocket Server] CHECK_UNIQUE_FIELD result:\", isUnique);\n const response = {\n type: \"CHECK_UNIQUE_FIELD_SUCCESS\",\n payload: { isUnique },\n requestId\n };\n wsDebug(\"🔍 [WebSocket Server] Sending CHECK_UNIQUE_FIELD_SUCCESS response\");\n ws.send(JSON.stringify(response));\n }\n break;\n\n\n case \"COUNT\": {\n const request: FetchCollectionProps = payload;\n const delegate = await getScopedDelegate();\n const count = await delegate.count!(request);\n const response = {\n type: \"COUNT_SUCCESS\",\n payload: { count },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"EXECUTE_SQL\": {\n const { sql, options } = payload;\n try {\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n if (!isSQLAdmin(admin)) {\n sendError(\"ERROR\", \"NOT_SUPPORTED\", \"SQL execution is not available for this driver.\");\n break;\n }\n const result = await admin.executeSql(sql, options);\n if (process.env.NODE_ENV !== \"production\") {\n wsDebug(`⚡ [WebSocket Server] SQL executed. Returned ${Array.isArray(result) ? result.length : \"non-array\"} rows.`);\n }\n const auditSession = clientSessions.get(clientId);\n console.log(\"[SQL Audit] WebSocket SQL execution\", JSON.stringify({\n sql: typeof sql === \"string\" ? sql.substring(0, 500) : sql,\n options,\n resultRows: Array.isArray(result) ? result.length : \"unknown\",\n uid: auditSession?.user?.uid ?? \"unknown\",\n roles: auditSession?.user?.roles ?? [],\n isAdmin: auditSession?.user?.isAdmin ?? false,\n }));\n const response = {\n type: \"EXECUTE_SQL_SUCCESS\",\n payload: { result },\n requestId\n };\n ws.send(JSON.stringify(response));\n } catch (sqlError: unknown) {\n // This is a query execution error (e.g., syntax error, permission denied).\n // We return it cleanly to the client without logging a server stack trace.\n const errMsg = extractErrorMessage(sqlError);\n sendError(\"ERROR\", \"SQL_ERROR\", errMsg);\n }\n }\n break;\n\n case \"FETCH_DATABASES\": {\n wsDebug(\"📚 [WebSocket Server] Processing FETCH_DATABASES request\");\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let databases: string[] = [];\n if (isSQLAdmin(admin) && admin.fetchAvailableDatabases) {\n databases = await admin.fetchAvailableDatabases();\n }\n wsDebug(`📚 [WebSocket Server] Fetched ${databases.length} databases.`);\n const response = {\n type: \"FETCH_DATABASES_SUCCESS\",\n payload: { databases },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_ROLES\": {\n wsDebug(\"👤 [WebSocket Server] Processing FETCH_ROLES request\");\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let roles: string[] = [];\n if (isSQLAdmin(admin) && admin.fetchAvailableRoles) {\n roles = await admin.fetchAvailableRoles();\n }\n wsDebug(`👤 [WebSocket Server] Fetched ${roles.length} roles.`);\n const response = {\n type: \"FETCH_ROLES_SUCCESS\",\n payload: { roles },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_APPLICATION_ROLES\": {\n wsDebug(\"👤 [WebSocket Server] Processing FETCH_APPLICATION_ROLES request\");\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let roles: string[] = [];\n if (isSQLAdmin(admin) && admin.fetchApplicationRoles) {\n roles = await admin.fetchApplicationRoles();\n }\n wsDebug(`👤 [WebSocket Server] Fetched ${roles.length} application roles.`);\n const response = {\n type: \"FETCH_APPLICATION_ROLES_SUCCESS\",\n payload: { roles },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_CURRENT_DATABASE\": {\n wsDebug(\"📚 [WebSocket Server] Processing FETCH_CURRENT_DATABASE request\");\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let database: string | undefined = undefined;\n if (isSQLAdmin(admin) && admin.fetchCurrentDatabase) {\n database = await admin.fetchCurrentDatabase();\n }\n const response = {\n type: \"FETCH_CURRENT_DATABASE_SUCCESS\",\n payload: { database },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_UNMAPPED_TABLES\": {\n wsDebug(\"📋 [WebSocket Server] Processing FETCH_UNMAPPED_TABLES request\");\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let tables: string[] = [];\n if (isSchemaAdmin(admin) && admin.fetchUnmappedTables) {\n tables = await admin.fetchUnmappedTables(payload?.mappedPaths);\n }\n wsDebug(`📋 [WebSocket Server] Fetched ${tables.length} unmapped tables.`);\n const response = {\n type: \"FETCH_UNMAPPED_TABLES_SUCCESS\",\n payload: { tables },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"FETCH_TABLE_METADATA\": {\n wsDebug(\"📋 [WebSocket Server] Processing FETCH_TABLE_METADATA request\");\n const { tableName } = payload;\n const delegate = await getScopedDelegate();\n const admin = delegate.admin;\n let metadata: TableMetadata | undefined;\n if (isSchemaAdmin(admin) && admin.fetchTableMetadata) {\n metadata = await admin.fetchTableMetadata(tableName) as TableMetadata;\n }\n wsDebug(`📋 [WebSocket Server] Fetched metadata for table '${tableName}'. (${metadata?.columns?.length ?? 0} columns)`);\n const response = {\n type: \"FETCH_TABLE_METADATA_SUCCESS\",\n payload: { metadata },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"CREATE_BRANCH\": {\n wsDebug(\"🌿 [WebSocket Server] Processing CREATE_BRANCH request\");\n const { name, options } = payload;\n const delegate = await getScopedDelegate();\n if (!delegate.admin?.createBranch) {\n sendError(\"ERROR\", \"NOT_SUPPORTED\", \"Database branching is not available. Configure adminConnectionString.\");\n break;\n }\n const branch: BranchInfo = await delegate.admin.createBranch(name, options);\n wsDebug(`🌿 [WebSocket Server] Branch created: ${branch.name}`);\n const response = {\n type: \"CREATE_BRANCH_SUCCESS\",\n payload: { branch },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"DELETE_BRANCH\": {\n wsDebug(\"🗑️ [WebSocket Server] Processing DELETE_BRANCH request\");\n const { name: branchName } = payload;\n const delegate = await getScopedDelegate();\n if (!delegate.admin?.deleteBranch) {\n sendError(\"ERROR\", \"NOT_SUPPORTED\", \"Database branching is not available.\");\n break;\n }\n await delegate.admin.deleteBranch(branchName);\n wsDebug(`🗑️ [WebSocket Server] Branch deleted: ${branchName}`);\n const response = {\n type: \"DELETE_BRANCH_SUCCESS\",\n payload: { success: true },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n case \"LIST_BRANCHES\": {\n wsDebug(\"🌿 [WebSocket Server] Processing LIST_BRANCHES request\");\n const delegate = await getScopedDelegate();\n let branches: BranchInfo[] = [];\n if (delegate.admin?.listBranches) {\n branches = await delegate.admin.listBranches();\n }\n wsDebug(`🌿 [WebSocket Server] Listed ${branches.length} branches.`);\n const response = {\n type: \"LIST_BRANCHES_SUCCESS\",\n payload: { branches },\n requestId\n };\n ws.send(JSON.stringify(response));\n }\n break;\n\n // Route subscription messages, broadcast channels, and presence to RealtimeService\n case \"subscribe_collection\":\n case \"subscribe_one\":\n case \"unsubscribe\":\n case \"join_channel\":\n case \"leave_channel\":\n case \"broadcast\":\n case \"presence_track\":\n case \"presence_untrack\":\n case \"presence_state\":\n case \"channel_history\": {\n wsDebug(\"🔄 [WebSocket Server] Routing realtime message to RealtimeService:\", type);\n // Attach auth context from the WS session so RLS-aware refetches work\n const session = clientSessions.get(clientId);\n const authContext = session?.user\n ? { uid: session.user.uid,\nroles: session.user.roles ?? [] }\n : { uid: ANONYMOUS_USER_ID,\nroles: [\"anon\"] };\n // Let RealtimeService handle these messages\n await realtimeService.handleClientMessage(clientId, {\n type,\n payload,\n subscriptionId: payload?.subscriptionId\n }, authContext);\n break;\n }\n\n default:\n logger.error(\"❌ [WebSocket Server] Unknown message type\", { detail: type });\n }\n } catch (error: unknown) {\n logger.error(\"💥 [WebSocket Server] Error handling message\", { error: error });\n if (error instanceof Error) {\n logger.error(\"Stack trace\", { detail: error.stack });\n }\n // Unwrap the cause chain: a Drizzle failure reports itself as\n // \"Failed query: <sql> params:\", which tells the user nothing and\n // echoes the statement back at them. The reason is in the cause.\n const errorMessage = process.env.NODE_ENV === \"production\"\n ? \"An unexpected error occurred\"\n : extractErrorMessage(error);\n const errorResponse = {\n type: \"ERROR\",\n requestId,\n payload: {\n error: {\n message: errorMessage,\n code: \"INTERNAL_ERROR\"\n }\n }\n };\n ws.send(JSON.stringify(errorResponse));\n }\n });\n });\n}\n"],"mappings":";;;;;;;;;;;;;;AAqiBA,SAAgB,WAAW,OAAqD;CAC5E,OAAO,CAAC,CAAC,SAAS,OAAQ,MAAmB,eAAe;AAChE;;;;;AAiBA,SAAgB,cAAc,OAAwD;CAClF,OAAO,CAAC,CAAC,UACL,OAAQ,MAAsB,wBAAwB,cACtD,OAAQ,MAAsB,uBAAuB;AAE7D;;;;;AChhBA,IAAM,gBAAgB;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C,SAAS,OAAO,WAAW;OACnE,MAAM,WAAW;QACb,MAAM;QACN,SAAS,EAAE,SAAS;QACpB;OACJ;OACA,GAAG,KAAK,KAAK,UAAU,QAAQ,CAAC;MACpC;MACI;KAGJ,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK;KACL,KAAK,mBAAmB;MACpB,QAAQ,sEAAsE,IAAI;MAElF,MAAM,UAAU,eAAe,IAAI,QAAQ;MAC3C,MAAM,cAAc,SAAS,OACvB;OAAE,KAAK,QAAQ,KAAK;OAClD,OAAO,QAAQ,KAAK,SAAS,CAAC;MAAE,IACF;OAAE,KAAK;OACrC,OAAO,CAAC,MAAM;MAAE;MAEQ,MAAM,gBAAgB,oBAAoB,UAAU;OAChD;OACA;OACA,gBAAgB,SAAS;MAC7B,GAAG,WAAW;MACd;KACJ;KAEA,SACI,OAAO,MAAM,6CAA6C,EAAE,QAAQ,KAAK,CAAC;IAClF;GACJ,SAAS,OAAgB;IACrB,OAAO,MAAM,gDAAgD,EAAS,MAAM,CAAC;IAC7E,IAAI,iBAAiB,OACjB,OAAO,MAAM,eAAe,EAAE,QAAQ,MAAM,MAAM,CAAC;IAKvD,MAAM,eAAA,QAAA,IAAA,aAAwC,eACxC,iCACA,oBAAoB,KAAK;IAC/B,MAAM,gBAAgB;KAClB,MAAM;KACN;KACA,SAAS,EACL,OAAO;MACH,SAAS;MACT,MAAM;KACV,EACJ;IACJ;IACA,GAAG,KAAK,KAAK,UAAU,aAAa,CAAC;GACzC;EACJ,CAAC;CACL,CAAC;AACL"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebasepro/server-postgres",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.12.1-canary.
|
|
4
|
+
"version": "0.12.1-canary.g4e7bcbf",
|
|
5
5
|
"description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
|
|
6
6
|
"funding": {
|
|
7
7
|
"url": "https://github.com/sponsors/rebaseco"
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"execa": "^9.6.1",
|
|
48
48
|
"pg": "^8.22.0",
|
|
49
49
|
"ws": "^8.21.1",
|
|
50
|
-
"@rebasepro/codegen": "0.12.1-canary.
|
|
51
|
-
"@rebasepro/
|
|
52
|
-
"@rebasepro/
|
|
53
|
-
"@rebasepro/utils": "0.12.1-canary.
|
|
54
|
-
"@rebasepro/
|
|
50
|
+
"@rebasepro/codegen": "0.12.1-canary.g4e7bcbf",
|
|
51
|
+
"@rebasepro/common": "0.12.1-canary.g4e7bcbf",
|
|
52
|
+
"@rebasepro/types": "0.12.1-canary.g4e7bcbf",
|
|
53
|
+
"@rebasepro/utils": "0.12.1-canary.g4e7bcbf",
|
|
54
|
+
"@rebasepro/server": "0.12.1-canary.g4e7bcbf"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@hono/node-server": "^2.0.12",
|
package/src/PostgresAdapter.ts
CHANGED
|
@@ -36,9 +36,14 @@ export function createPostgresAdapter(pgConfig: PostgresDriverConfig): DatabaseA
|
|
|
36
36
|
return undefined;
|
|
37
37
|
},
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
// `adapter` is forwarded for the same reason the schema hooks below are:
|
|
40
|
+
// dropping an argument here is invisible at the call site and silent at
|
|
41
|
+
// runtime. This one decided whether the realtime socket authenticates at
|
|
42
|
+
// all — without it, a server whose auth comes from an AuthAdapter fell
|
|
43
|
+
// back to "is a jwtSecret set?", answered no, and admitted every client.
|
|
44
|
+
initializeWebsockets(server, realtimeService, driver, config, adapter) {
|
|
40
45
|
if (bootstrapper.initializeWebsockets) {
|
|
41
|
-
return bootstrapper.initializeWebsockets(server, realtimeService, driver, config);
|
|
46
|
+
return bootstrapper.initializeWebsockets(server, realtimeService, driver, config, adapter);
|
|
42
47
|
}
|
|
43
48
|
},
|
|
44
49
|
|
|
@@ -1025,12 +1025,16 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
1025
1025
|
path,
|
|
1026
1026
|
collection,
|
|
1027
1027
|
filter,
|
|
1028
|
+
logical,
|
|
1028
1029
|
searchString
|
|
1029
1030
|
}: FetchCollectionProps<M>): Promise<number> {
|
|
1030
1031
|
return this.dataService.count(
|
|
1031
1032
|
path,
|
|
1032
1033
|
{
|
|
1033
1034
|
filter,
|
|
1035
|
+
// Counted as well as filtered, or `meta.total` describes a
|
|
1036
|
+
// different set of rows from the `data` beside it.
|
|
1037
|
+
logical,
|
|
1034
1038
|
searchString
|
|
1035
1039
|
}
|
|
1036
1040
|
);
|
package/src/cli-helpers.ts
CHANGED
|
@@ -3,6 +3,7 @@ import path from "path";
|
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import { execSync } from "child_process";
|
|
5
5
|
import { createRequire } from "module";
|
|
6
|
+
import readline from "readline";
|
|
6
7
|
import { pathToFileURL } from "url";
|
|
7
8
|
import chalk from "chalk";
|
|
8
9
|
import { logger } from "@rebasepro/server";
|
|
@@ -276,3 +277,28 @@ export async function getTableExcludes(
|
|
|
276
277
|
return excludes;
|
|
277
278
|
}
|
|
278
279
|
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Ask a yes/no question on an interactive terminal.
|
|
283
|
+
*
|
|
284
|
+
* The `isTTY` guard is the contract, not an optimisation: non-interactive
|
|
285
|
+
* shells (CI, pipes, agents) can never answer, and `readline` on a
|
|
286
|
+
* non-TTY stdin resolves with whatever the pipe happens to contain — or never
|
|
287
|
+
* resolves at all. Returning false there is what makes an unattended
|
|
288
|
+
* `db push` abort instead of auto-confirming a destructive change. Callers
|
|
289
|
+
* should already have gone through {@link decidePushSafety}, which decides
|
|
290
|
+
* whether interactive confirmation is even possible; this is the backstop.
|
|
291
|
+
*
|
|
292
|
+
* Lives here rather than in cli.ts so it is reachable from a test — cli.ts uses
|
|
293
|
+
* `import.meta` and cannot be imported by the jest runner.
|
|
294
|
+
*/
|
|
295
|
+
export async function promptConfirm(question: string): Promise<boolean> {
|
|
296
|
+
if (!process.stdin.isTTY) return false;
|
|
297
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
298
|
+
try {
|
|
299
|
+
const answer: string = await new Promise((resolve) => rl.question(question, resolve));
|
|
300
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
301
|
+
} finally {
|
|
302
|
+
rl.close();
|
|
303
|
+
}
|
|
304
|
+
}
|
package/src/cli.ts
CHANGED
|
@@ -12,12 +12,12 @@ import {
|
|
|
12
12
|
getDevDatabaseUrl,
|
|
13
13
|
ensureDevDatabaseExists,
|
|
14
14
|
getTableExcludes,
|
|
15
|
-
ExcludeIntrospectionError
|
|
15
|
+
ExcludeIntrospectionError,
|
|
16
|
+
promptConfirm
|
|
16
17
|
} from "./cli-helpers";
|
|
17
18
|
import { checkDatabaseConnectivity, diagnoseDbError } from "./cli-errors";
|
|
18
19
|
import { AUTH_BOOTSTRAP_SQL } from "./schema/auth-bootstrap-sql";
|
|
19
20
|
import { detectDestructiveStatements, decidePushSafety } from "./schema/destructive-sql";
|
|
20
|
-
import readline from "readline";
|
|
21
21
|
|
|
22
22
|
const __cliDirname = path.dirname(fileURLToPath(import.meta.url));
|
|
23
23
|
|
|
@@ -611,23 +611,6 @@ function timeAgo(date: Date): string {
|
|
|
611
611
|
|
|
612
612
|
|
|
613
613
|
|
|
614
|
-
/**
|
|
615
|
-
* Ask a yes/no question on an interactive terminal. Non-interactive shells
|
|
616
|
-
* (CI, pipes, agents) can't answer, so this must only be reached after
|
|
617
|
-
* {@link decidePushSafety} has already ruled that interactive confirmation is
|
|
618
|
-
* possible.
|
|
619
|
-
*/
|
|
620
|
-
async function promptConfirm(question: string): Promise<boolean> {
|
|
621
|
-
if (!process.stdin.isTTY) return false;
|
|
622
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
623
|
-
try {
|
|
624
|
-
const answer: string = await new Promise((resolve) => rl.question(question, resolve));
|
|
625
|
-
return /^y(es)?$/i.test(answer.trim());
|
|
626
|
-
} finally {
|
|
627
|
-
rl.close();
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
614
|
async function runAtlas(
|
|
632
615
|
domain: "schema" | "migrate",
|
|
633
616
|
args: string[],
|
|
@@ -27,12 +27,14 @@
|
|
|
27
27
|
*/
|
|
28
28
|
import { type CollectionConfig, type Property, isPostgresCollectionConfig } from "@rebasepro/types";
|
|
29
29
|
import { getTableName } from "@rebasepro/common";
|
|
30
|
+
import { logger } from "@rebasepro/server";
|
|
30
31
|
import {
|
|
31
32
|
getSqlColumnType,
|
|
32
33
|
resolveColumnName,
|
|
33
34
|
isIdProperty,
|
|
34
35
|
planRelationalColumns,
|
|
35
|
-
planJunctionTables
|
|
36
|
+
planJunctionTables,
|
|
37
|
+
quoteSqlLiteral
|
|
36
38
|
} from "./generate-postgres-ddl-logic";
|
|
37
39
|
|
|
38
40
|
/**
|
|
@@ -75,7 +77,7 @@ export interface ExistingSchema {
|
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
export interface EnsureAction {
|
|
78
|
-
kind: "create-enum" | "create-table" | "add-column" | "add-constraint";
|
|
80
|
+
kind: "create-enum" | "create-table" | "add-column" | "add-constraint" | "rename-column";
|
|
79
81
|
/** Qualified target, for logging: `public.posts` or `public.posts.title`. */
|
|
80
82
|
target: string;
|
|
81
83
|
sql: string;
|
|
@@ -85,6 +87,27 @@ export interface EnsurePlan {
|
|
|
85
87
|
actions: EnsureAction[];
|
|
86
88
|
/** Every statement, in dependency order. Empty when the schema is current. */
|
|
87
89
|
statements: string[];
|
|
90
|
+
/**
|
|
91
|
+
* Relation columns this plan is about to create where the table already
|
|
92
|
+
* carries the same column under its pre-singularization name.
|
|
93
|
+
*
|
|
94
|
+
* The reason this is reported rather than silently handled: the ensure is
|
|
95
|
+
* additive, so it would add `category_id` beside a populated
|
|
96
|
+
* `categorie_id` and the relation would then read the new, empty one. No
|
|
97
|
+
* statement fails, no table is missing, and the only symptom is relations
|
|
98
|
+
* resolving to nothing — which is indistinguishable from having no data.
|
|
99
|
+
*/
|
|
100
|
+
legacyForeignKeys: LegacyForeignKey[];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A relation column whose old and new spellings both plausibly apply. */
|
|
104
|
+
export interface LegacyForeignKey {
|
|
105
|
+
/** `schema.table`. */
|
|
106
|
+
table: string;
|
|
107
|
+
/** The name the current rule derives, and what this plan would create. */
|
|
108
|
+
expected: string;
|
|
109
|
+
/** The name the old rule derived, which the table already has. */
|
|
110
|
+
legacy: string;
|
|
88
111
|
}
|
|
89
112
|
|
|
90
113
|
export interface EnsureOutcome extends EnsurePlan {
|
|
@@ -136,11 +159,6 @@ function requiredEnums(collection: CollectionConfig): { name: string; values: st
|
|
|
136
159
|
return out;
|
|
137
160
|
}
|
|
138
161
|
|
|
139
|
-
/** Single-quote escaping for an enum label. */
|
|
140
|
-
function quoteLiteral(value: string): string {
|
|
141
|
-
return `'${value.replace(/'/g, "''")}'`;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
162
|
/**
|
|
145
163
|
* Decide what to add. Pure — the caller supplies what exists and runs the result.
|
|
146
164
|
*
|
|
@@ -165,7 +183,7 @@ export function planCollectionSchemaEnsure(
|
|
|
165
183
|
actions.push({
|
|
166
184
|
kind: "create-enum",
|
|
167
185
|
target: name,
|
|
168
|
-
sql: `CREATE TYPE "${schema}"."${typeName}" AS ENUM (${values.map(
|
|
186
|
+
sql: `CREATE TYPE "${schema}"."${typeName}" AS ENUM (${values.map(quoteSqlLiteral).join(", ")});`
|
|
169
187
|
});
|
|
170
188
|
}
|
|
171
189
|
}
|
|
@@ -218,6 +236,41 @@ export function planCollectionSchemaEnsure(
|
|
|
218
236
|
}
|
|
219
237
|
|
|
220
238
|
// 3. Missing columns, on both brand-new and pre-existing tables.
|
|
239
|
+
const legacyForeignKeys: LegacyForeignKey[] = [];
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Move a relation column that is only missing because it was renamed.
|
|
243
|
+
*
|
|
244
|
+
* Returns true when it handled the column, so the caller skips the ordinary
|
|
245
|
+
* ADD. Adding here would be the wrong move and a quiet one: the data is in
|
|
246
|
+
* the old column, `ADD COLUMN` creates the new one empty beside it, every
|
|
247
|
+
* statement succeeds, and the relation reads the empty one. A rename is
|
|
248
|
+
* metadata-only in Postgres, keeps the values, and carries the column's
|
|
249
|
+
* indexes and constraints with it.
|
|
250
|
+
*
|
|
251
|
+
* Only ever reached when the new name is absent and the old name is
|
|
252
|
+
* present, so there is nothing to overwrite and nothing to choose between.
|
|
253
|
+
*/
|
|
254
|
+
const renameLegacyColumn = (
|
|
255
|
+
key: string,
|
|
256
|
+
schema: string,
|
|
257
|
+
table: string,
|
|
258
|
+
column: string,
|
|
259
|
+
legacyName: string | undefined
|
|
260
|
+
): boolean => {
|
|
261
|
+
const present = existing.tables.get(key);
|
|
262
|
+
if (!legacyName || !present) return false;
|
|
263
|
+
if (present.has(column) || !present.has(legacyName)) return false;
|
|
264
|
+
|
|
265
|
+
legacyForeignKeys.push({ table: key, expected: column, legacy: legacyName });
|
|
266
|
+
actions.push({
|
|
267
|
+
kind: "rename-column",
|
|
268
|
+
target: `${key}.${column}`,
|
|
269
|
+
sql: `ALTER TABLE "${schema}"."${table}" RENAME COLUMN "${legacyName}" TO "${column}";`
|
|
270
|
+
});
|
|
271
|
+
return true;
|
|
272
|
+
};
|
|
273
|
+
|
|
221
274
|
const addColumn = (
|
|
222
275
|
key: string,
|
|
223
276
|
schema: string,
|
|
@@ -261,14 +314,17 @@ export function planCollectionSchemaEnsure(
|
|
|
261
314
|
const key = `${junction.schema}.${junction.table}`;
|
|
262
315
|
if (created.has(key)) continue;
|
|
263
316
|
for (const column of junction.columns) {
|
|
317
|
+
if (renameLegacyColumn(key, junction.schema, junction.table, column.name, column.legacyName)) continue;
|
|
264
318
|
addColumn(key, junction.schema, junction.table, column.name, column.type);
|
|
265
319
|
}
|
|
266
320
|
}
|
|
267
321
|
|
|
268
322
|
// 3c. The columns relation and reference properties own.
|
|
269
323
|
for (const relational of planRelationalColumns(collections)) {
|
|
324
|
+
const relKey = `${relational.schema}.${relational.table}`;
|
|
325
|
+
if (renameLegacyColumn(relKey, relational.schema, relational.table, relational.column, relational.legacyColumn)) continue;
|
|
270
326
|
addColumn(
|
|
271
|
-
|
|
327
|
+
relKey,
|
|
272
328
|
relational.schema,
|
|
273
329
|
relational.table,
|
|
274
330
|
relational.column,
|
|
@@ -297,7 +353,7 @@ export function planCollectionSchemaEnsure(
|
|
|
297
353
|
});
|
|
298
354
|
}
|
|
299
355
|
|
|
300
|
-
return { actions, statements: actions.map(a => a.sql) };
|
|
356
|
+
return { actions, statements: actions.map(a => a.sql), legacyForeignKeys };
|
|
301
357
|
}
|
|
302
358
|
|
|
303
359
|
/** Read what the database has, for the schemas the collections live in. */
|
|
@@ -386,6 +442,21 @@ export async function ensureCollectionTables(
|
|
|
386
442
|
const plan = planCollectionSchemaEnsure(collections, existing);
|
|
387
443
|
const failures: { target: string; error: string }[] = [];
|
|
388
444
|
|
|
445
|
+
// Reported, not warned: this is a rename the ensure is about to perform, and
|
|
446
|
+
// the operator should be able to see in the log why a column changed name.
|
|
447
|
+
// The interesting case is the one that no longer happens — before this,
|
|
448
|
+
// ensure added the new column empty beside the populated old one, every
|
|
449
|
+
// statement succeeded, and the relation read the empty one.
|
|
450
|
+
for (const legacy of plan.legacyForeignKeys) {
|
|
451
|
+
const message =
|
|
452
|
+
`Renaming "${legacy.table}"."${legacy.legacy}" to "${legacy.expected}". The old name is ` +
|
|
453
|
+
"the one Rebase derived for this relation before it singularized properly; the column " +
|
|
454
|
+
"keeps its data, indexes and constraints. To keep the old name instead, set " +
|
|
455
|
+
`\`localKey: "${legacy.legacy}"\` on the relation and this will stop.`;
|
|
456
|
+
logger.info(`[schema] ${message}`);
|
|
457
|
+
log?.(message);
|
|
458
|
+
}
|
|
459
|
+
|
|
389
460
|
if (plan.actions.length === 0) {
|
|
390
461
|
log?.("Schema is up to date; nothing to create.");
|
|
391
462
|
return { ...plan, failures };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionConfig, NumberProperty, Property, ResolvedRelation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty, isManyToMany, type ResolvedManyToMany, type ResolvedBelongsTo } from "@rebasepro/types";
|
|
2
2
|
import { getEnumVarName, getTableName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, getInjectedSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig, resolveStringColumnLength } from "@rebasepro/common";
|
|
3
|
-
import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
|
|
3
|
+
import { toSnakeCase, getPolicyNamesForRule, generateForeignKeyName, legacyForeignKeyName } from "@rebasepro/utils";
|
|
4
4
|
|
|
5
5
|
// --- Helper Functions ---
|
|
6
6
|
|
|
@@ -116,6 +116,16 @@ const generateSinglePolicyStatements = (collection: CollectionConfig, rule: Secu
|
|
|
116
116
|
return [drop, create];
|
|
117
117
|
};
|
|
118
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Single-quote escaping for a SQL string literal (PostgreSQL doubles the
|
|
121
|
+
* quote). Enum labels come straight from user-authored collection config, so a
|
|
122
|
+
* label like `it's` closes the literal early and the whole generated file stops
|
|
123
|
+
* parsing at the `CREATE TYPE`. Lives here rather than next to its other caller
|
|
124
|
+
* because ensure-collection-tables already imports from this module — the
|
|
125
|
+
* reverse would be a cycle.
|
|
126
|
+
*/
|
|
127
|
+
export const quoteSqlLiteral = (value: string): string => `'${value.replace(/'/g, "''")}'`;
|
|
128
|
+
|
|
119
129
|
export const getSqlColumnType = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string => {
|
|
120
130
|
switch (prop.type) {
|
|
121
131
|
case "string": {
|
|
@@ -242,6 +252,13 @@ export const generatePostgresDdl = async (
|
|
|
242
252
|
if (uniqueSchemas.length > 0) ddl += "\n";
|
|
243
253
|
|
|
244
254
|
// 2. Generate Enums
|
|
255
|
+
//
|
|
256
|
+
// The enum type name is derived from table + column, so two collections
|
|
257
|
+
// mapped onto the same table — or two properties whose `columnName`
|
|
258
|
+
// resolves to the same column — land on the same name. `CREATE TYPE` has no
|
|
259
|
+
// IF NOT EXISTS, so emitting it twice aborts the whole file on the second
|
|
260
|
+
// statement. Dedupe by name, matching planCollectionSchemaEnsure.
|
|
261
|
+
const emittedEnums = new Set<string>();
|
|
245
262
|
collections.forEach(collection => {
|
|
246
263
|
const collectionTable = getTableName(collection);
|
|
247
264
|
const schema = isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
@@ -253,8 +270,9 @@ export const generatePostgresDdl = async (
|
|
|
253
270
|
String(typeof v === "object" && v !== null && "id" in v ? v.id : v)
|
|
254
271
|
)
|
|
255
272
|
: Object.keys(prop.enum);
|
|
256
|
-
if (values.length > 0) {
|
|
257
|
-
|
|
273
|
+
if (values.length > 0 && !emittedEnums.has(`${schema}.${enumDbName}`)) {
|
|
274
|
+
emittedEnums.add(`${schema}.${enumDbName}`);
|
|
275
|
+
ddl += `CREATE TYPE "${schema}"."${enumDbName}" AS ENUM (${values.map(quoteSqlLiteral).join(", ")});\n`;
|
|
258
276
|
}
|
|
259
277
|
}
|
|
260
278
|
});
|
|
@@ -530,6 +548,15 @@ export interface RelationalColumnPlan {
|
|
|
530
548
|
type: string;
|
|
531
549
|
/** Absent when the target collection is not part of this bundle. */
|
|
532
550
|
foreignKey?: ForeignKeyPlan;
|
|
551
|
+
/**
|
|
552
|
+
* What this column would have been called before `generateForeignKeyName`
|
|
553
|
+
* learned to singularize — set only when the two differ and the column name
|
|
554
|
+
* is the derived default rather than one the author wrote.
|
|
555
|
+
*
|
|
556
|
+
* Carried so the boot-time ensure can notice a database provisioned under
|
|
557
|
+
* the old rule. It is never used to name anything.
|
|
558
|
+
*/
|
|
559
|
+
legacyColumn?: string;
|
|
533
560
|
}
|
|
534
561
|
|
|
535
562
|
/** The table behind a many-to-many `through` relation. */
|
|
@@ -537,7 +564,7 @@ export interface JunctionTablePlan {
|
|
|
537
564
|
schema: string;
|
|
538
565
|
/** Bare table name, no schema prefix. */
|
|
539
566
|
table: string;
|
|
540
|
-
columns: { name: string; type: string }[];
|
|
567
|
+
columns: { name: string; type: string; legacyName?: string }[];
|
|
541
568
|
/** Both endpoint columns plus the composite primary key. */
|
|
542
569
|
createTable: string;
|
|
543
570
|
foreignKeys: ForeignKeyPlan[];
|
|
@@ -613,10 +640,16 @@ export const planRelationalColumns = (collections: CollectionConfig[]): Relation
|
|
|
613
640
|
if (!targetCollection) continue;
|
|
614
641
|
|
|
615
642
|
const required = prop.validation?.required;
|
|
643
|
+
// Only a *derived* column name can be affected by the change to
|
|
644
|
+
// `generateForeignKeyName`; one the author wrote is theirs.
|
|
645
|
+
const relationName = refProp.relation?.relationName ?? propName;
|
|
646
|
+
const legacyKey = legacyForeignKeyName(relationName);
|
|
647
|
+
const derived = relInfo.localKey === generateForeignKeyName(relationName);
|
|
616
648
|
plans.push({
|
|
617
649
|
schema,
|
|
618
650
|
table,
|
|
619
651
|
column: relInfo.localKey,
|
|
652
|
+
legacyColumn: derived && legacyKey !== relInfo.localKey ? legacyKey : undefined,
|
|
620
653
|
type: getSqlColumnType(propName, prop, collection, collections),
|
|
621
654
|
foreignKey: foreignKeyPlan({
|
|
622
655
|
schema,
|
|
@@ -675,9 +708,20 @@ export const planJunctionTables = (collections: CollectionConfig[]): JunctionTab
|
|
|
675
708
|
|
|
676
709
|
for (const spec of resolveJunctionSpecs(collections).values()) {
|
|
677
710
|
const [source, target] = spec.endpoints;
|
|
711
|
+
// A junction column's default name is derived from the endpoint
|
|
712
|
+
// collection's slug — which is normally plural, so this is where the
|
|
713
|
+
// singularization change actually lands: a `categories` endpoint used
|
|
714
|
+
// to give `categorie_id` and now gives `category_id`. Recorded, not
|
|
715
|
+
// used, so boot-ensure can recognise a table built under the old rule.
|
|
716
|
+
const legacyFor = (endpoint: typeof source): string | undefined => {
|
|
717
|
+
const slug = toSnakeCase(endpoint.collection.slug ?? endpoint.collection.name ?? "");
|
|
718
|
+
const legacy = legacyForeignKeyName(slug);
|
|
719
|
+
const derived = endpoint.junctionColumn === generateForeignKeyName(slug);
|
|
720
|
+
return derived && legacy !== endpoint.junctionColumn ? legacy : undefined;
|
|
721
|
+
};
|
|
678
722
|
const columns = [
|
|
679
|
-
{ name: source.junctionColumn, type: junctionKeyType(source.collection) },
|
|
680
|
-
{ name: target.junctionColumn, type: junctionKeyType(target.collection) }
|
|
723
|
+
{ name: source.junctionColumn, type: junctionKeyType(source.collection), legacyName: legacyFor(source) },
|
|
724
|
+
{ name: target.junctionColumn, type: junctionKeyType(target.collection), legacyName: legacyFor(target) }
|
|
681
725
|
];
|
|
682
726
|
// Every declaring side agrees on the edge's lifetime; the first one wins,
|
|
683
727
|
// as it does in the generator's walk.
|