@rebasepro/server-postgresql 0.1.2 → 0.2.1

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 (68) hide show
  1. package/LICENSE +22 -6
  2. package/dist/common/src/util/entities.d.ts +2 -2
  3. package/dist/common/src/util/relations.d.ts +1 -1
  4. package/dist/index.es.js +1160 -612
  5. package/dist/index.es.js.map +1 -1
  6. package/dist/index.umd.js +1158 -610
  7. package/dist/index.umd.js.map +1 -1
  8. package/dist/server-postgresql/src/PostgresAdapter.d.ts +6 -0
  9. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +2 -1
  10. package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +0 -5
  11. package/dist/server-postgresql/src/auth/ensure-tables.d.ts +2 -1
  12. package/dist/server-postgresql/src/auth/services.d.ts +37 -15
  13. package/dist/server-postgresql/src/index.d.ts +1 -0
  14. package/dist/server-postgresql/src/schema/auth-schema.d.ts +43 -856
  15. package/dist/server-postgresql/src/schema/default-collections.d.ts +2 -0
  16. package/dist/server-postgresql/src/schema/doctor.d.ts +10 -1
  17. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +1 -0
  18. package/dist/server-postgresql/src/services/entity-helpers.d.ts +1 -1
  19. package/dist/server-postgresql/src/services/realtimeService.d.ts +12 -0
  20. package/dist/server-postgresql/src/websocket.d.ts +2 -1
  21. package/dist/types/src/controllers/auth.d.ts +9 -8
  22. package/dist/types/src/controllers/client.d.ts +3 -0
  23. package/dist/types/src/types/auth_adapter.d.ts +356 -0
  24. package/dist/types/src/types/collections.d.ts +67 -2
  25. package/dist/types/src/types/database_adapter.d.ts +94 -0
  26. package/dist/types/src/types/entity_actions.d.ts +7 -1
  27. package/dist/types/src/types/entity_callbacks.d.ts +1 -1
  28. package/dist/types/src/types/entity_views.d.ts +36 -1
  29. package/dist/types/src/types/index.d.ts +2 -0
  30. package/dist/types/src/types/plugins.d.ts +1 -1
  31. package/dist/types/src/types/properties.d.ts +24 -5
  32. package/dist/types/src/types/property_config.d.ts +6 -2
  33. package/dist/types/src/types/relations.d.ts +1 -1
  34. package/dist/types/src/types/translations.d.ts +8 -0
  35. package/dist/types/src/users/user.d.ts +5 -0
  36. package/package.json +21 -15
  37. package/src/PostgresAdapter.ts +59 -0
  38. package/src/PostgresBackendDriver.ts +57 -8
  39. package/src/PostgresBootstrapper.ts +35 -15
  40. package/src/auth/ensure-tables.ts +82 -189
  41. package/src/auth/services.ts +421 -170
  42. package/src/cli.ts +44 -13
  43. package/src/data-transformer.ts +78 -8
  44. package/src/history/HistoryService.ts +25 -2
  45. package/src/index.ts +1 -0
  46. package/src/schema/auth-schema.ts +130 -98
  47. package/src/schema/default-collections.ts +68 -0
  48. package/src/schema/doctor-cli.ts +5 -1
  49. package/src/schema/doctor.ts +85 -8
  50. package/src/schema/generate-drizzle-schema-logic.ts +74 -27
  51. package/src/schema/generate-drizzle-schema.ts +13 -3
  52. package/src/schema/introspect-db-inference.ts +5 -5
  53. package/src/schema/introspect-db-logic.ts +9 -2
  54. package/src/schema/introspect-db.ts +14 -3
  55. package/src/services/EntityFetchService.ts +5 -5
  56. package/src/services/RelationService.ts +2 -2
  57. package/src/services/entity-helpers.ts +1 -1
  58. package/src/services/realtimeService.ts +145 -136
  59. package/src/utils/drizzle-conditions.ts +16 -2
  60. package/src/websocket.ts +113 -37
  61. package/test/auth-services.test.ts +163 -74
  62. package/test/data-transformer-hardening.test.ts +57 -0
  63. package/test/data-transformer.test.ts +43 -0
  64. package/test/generate-drizzle-schema.test.ts +7 -5
  65. package/test/introspect-db-utils.test.ts +4 -1
  66. package/test/postgresDataDriver.test.ts +17 -0
  67. package/test/realtimeService.test.ts +7 -7
  68. package/test/websocket.test.ts +139 -0
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @module DatabaseAdapter
3
+ *
4
+ * Pluggable database abstraction for Rebase.
5
+ *
6
+ *
7
+ * A `DatabaseAdapter` focuses purely on data persistence and related concerns (realtime, history).
8
+ * It does NOT handle authentication — auth is managed separately by an `AuthAdapter`.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { createPostgresAdapter } from "@rebasepro/server-postgresql";
13
+ *
14
+ * initializeRebaseBackend({
15
+ * database: createPostgresAdapter({ connection: db, schema }),
16
+ * auth: { jwtSecret: "..." },
17
+ * });
18
+ * ```
19
+ *
20
+ * @group Backend
21
+ */
22
+ import type { DataDriver } from "../controllers/data_driver";
23
+ import type { EntityCollection } from "./collections";
24
+ import type { CollectionRegistryInterface, DatabaseAdmin, InitializedDriver, RealtimeProvider, BootstrappedAuth } from "./backend";
25
+ /**
26
+ * A `DatabaseAdapter` provides data persistence for Rebase.
27
+ *
28
+ * @group Backend
29
+ */
30
+ export interface DatabaseAdapter {
31
+ /**
32
+ * Which database engine this adapter handles.
33
+ *
34
+ * @example "postgres", "mysql", "mongodb", "sqlite"
35
+ */
36
+ readonly type: string;
37
+ /**
38
+ * Create the DataDriver for CRUD operations.
39
+ *
40
+ * This is the only **required** method.
41
+ *
42
+ * @param config - Coordinator-provided config containing registered
43
+ * collections and the collection registry.
44
+ */
45
+ initializeDriver(config: DatabaseAdapterInitConfig): Promise<InitializedDriver>;
46
+ /**
47
+ * Create a realtime provider for this database.
48
+ *
49
+ * Return `undefined` if the database does not support realtime
50
+ * change notifications.
51
+ */
52
+ initializeRealtime?(driverResult: InitializedDriver): Promise<RealtimeProvider | undefined>;
53
+ /**
54
+ * Initialize auth tables / services if this driver supports them.
55
+ */
56
+ initializeAuth?(config: unknown, driverResult: InitializedDriver): Promise<BootstrappedAuth | undefined>;
57
+ /**
58
+ * Initialize entity history tracking.
59
+ *
60
+ * Return `undefined` if the database does not support history.
61
+ */
62
+ initializeHistory?(config: unknown, driverResult: InitializedDriver): Promise<{
63
+ historyService: unknown;
64
+ } | undefined>;
65
+ /**
66
+ * Initialize WebSocket server for realtime operations.
67
+ */
68
+ initializeWebsockets?(server: unknown, realtimeService: RealtimeProvider, driver: DataDriver, config?: unknown): Promise<void> | void;
69
+ /**
70
+ * Return admin capabilities for this database (SQL editor, schema browser, branching).
71
+ */
72
+ getAdmin?(driverResult: InitializedDriver): DatabaseAdmin | undefined;
73
+ /**
74
+ * Mount any database-specific HTTP routes (e.g., custom admin endpoints).
75
+ *
76
+ * Called after all adapters are initialized.
77
+ */
78
+ mountRoutes?(app: unknown, basePath: string, driverResult: InitializedDriver): void;
79
+ /**
80
+ * Graceful shutdown: close connections, release resources.
81
+ */
82
+ destroy?(): Promise<void>;
83
+ }
84
+ /**
85
+ * Configuration passed by the coordinator to `DatabaseAdapter.initializeDriver()`.
86
+ *
87
+ * @group Backend
88
+ */
89
+ export interface DatabaseAdapterInitConfig {
90
+ /** Registered collection definitions. */
91
+ collections: EntityCollection[];
92
+ /** The shared collection registry to register into. */
93
+ collectionRegistry: CollectionRegistryInterface;
94
+ }
@@ -40,6 +40,12 @@ export interface EntityAction<M extends Record<string, unknown> = Record<string,
40
40
  * @param props
41
41
  */
42
42
  isEnabled?(props: EntityActionClickProps<M, USER>): boolean;
43
+ /**
44
+ * When true, this action is rendered inline on each row in the list view.
45
+ * By default, entity actions only appear in the table view and entity form.
46
+ * Use this for actions that should be easily accessible regardless of view mode.
47
+ */
48
+ showActionsInListView?: boolean;
43
49
  /**
44
50
  * Show this action collapsed in the menu of the collection view.
45
51
  * Defaults to true
@@ -72,7 +78,7 @@ export type EntityActionClickProps<M extends Record<string, unknown>, USER exten
72
78
  /**
73
79
  * If the action is rendered in the form, is it open in a side panel or full screen?
74
80
  */
75
- openEntityMode: "side_panel" | "full_screen" | "split";
81
+ openEntityMode: "side_panel" | "full_screen" | "split" | "dialog";
76
82
  /**
77
83
  * Optional selection controller, present if the action is being called from a collection view
78
84
  */
@@ -39,7 +39,7 @@ export type EntityCallbacks<M extends Record<string, unknown> = Record<string, u
39
39
  *
40
40
  * @param props
41
41
  */
42
- beforeDelete?(props: EntityBeforeDeleteProps<M, USER>): void;
42
+ beforeDelete?(props: EntityBeforeDeleteProps<M, USER>): Promise<boolean | void> | boolean | void;
43
43
  /**
44
44
  * Callback used after the entity is deleted.
45
45
  *
@@ -35,12 +35,17 @@ export interface FormContext<M extends Record<string, unknown> = Record<string,
35
35
  status: "new" | "existing" | "copy";
36
36
  entity?: Entity<M>;
37
37
  savingError?: Error;
38
- openEntityMode: "side_panel" | "full_screen" | "split";
38
+ openEntityMode: "side_panel" | "full_screen" | "split" | "dialog";
39
39
  /**
40
40
  * The underlying formex controller that powers the form.
41
41
  */
42
42
  formex: FormexController<M>;
43
43
  disabled: boolean;
44
+ /**
45
+ * Whether the form context is in read-only detail view mode.
46
+ * Custom entity views can use this to adjust their rendering.
47
+ */
48
+ readOnly?: boolean;
44
49
  }
45
50
  export type EntityCustomView<M extends Record<string, unknown> = Record<string, unknown>> = {
46
51
  key: string;
@@ -58,3 +63,33 @@ export interface EntityCustomViewParams<M extends Record<string, unknown> = Reco
58
63
  parentCollectionSlugs?: string[];
59
64
  parentEntityIds?: string[];
60
65
  }
66
+ /**
67
+ * Configuration for customizing the read-only detail view of an entity.
68
+ * Only used when `defaultEntityAction` is set to `"view"` on the collection.
69
+ * @group Models
70
+ */
71
+ export type EntityDetailViewConfig<M extends Record<string, unknown> = Record<string, unknown>> = {
72
+ /**
73
+ * Custom component rendered above the property display in the detail view.
74
+ */
75
+ Header?: ComponentRef<EntityDetailViewParams<M>>;
76
+ /**
77
+ * Custom component rendered below the property display in the detail view.
78
+ */
79
+ Footer?: ComponentRef<EntityDetailViewParams<M>>;
80
+ /**
81
+ * Completely replace the default detail view with a custom component.
82
+ * When set, Header and Footer are ignored.
83
+ */
84
+ Builder?: ComponentRef<EntityDetailViewParams<M>>;
85
+ };
86
+ /**
87
+ * Props passed to detail view customization components (Header, Footer, Builder).
88
+ * @group Models
89
+ */
90
+ export interface EntityDetailViewParams<M extends Record<string, unknown> = Record<string, unknown>> {
91
+ collection: EntityCollection<M>;
92
+ entity: Entity<M>;
93
+ path: string;
94
+ onEditClick: () => void;
95
+ }
@@ -24,3 +24,5 @@ export * from "./data_source";
24
24
  export * from "./cron";
25
25
  export * from "./backend_hooks";
26
26
  export * from "./component_ref";
27
+ export * from "./auth_adapter";
28
+ export * from "./database_adapter";
@@ -250,7 +250,7 @@ export interface PluginFormActionProps<USER extends User = User, EC extends Enti
250
250
  disabled: boolean;
251
251
  formContext?: FormContext;
252
252
  context: RebaseContext<USER>;
253
- openEntityMode: "side_panel" | "full_screen" | "split";
253
+ openEntityMode: "side_panel" | "full_screen" | "split" | "dialog";
254
254
  }
255
255
  /**
256
256
  * Parameters passed to the field builder wrap function.
@@ -1,5 +1,5 @@
1
1
  import type { ComponentRef } from "./component_ref";
2
- import type { EntityReference, EntityRelation, EntityValues, GeoPoint, Entity } from "./entities";
2
+ import type { EntityReference, EntityRelation, EntityValues, GeoPoint, Entity, Vector } from "./entities";
3
3
  import type { Relation, JoinStep, OnAction } from "./relations";
4
4
  import type { EntityCollection, FilterValues } from "./collections";
5
5
  import type { ColorKey, ColorScheme } from "./chips";
@@ -31,8 +31,8 @@ export type PropertyCallbacks<T = unknown, M extends Record<string, unknown> = R
31
31
  /**
32
32
  * @group Entity properties
33
33
  */
34
- export type DataType = "string" | "number" | "boolean" | "date" | "geopoint" | "reference" | "relation" | "array" | "map";
35
- export type Property = StringProperty | NumberProperty | BooleanProperty | DateProperty | GeopointProperty | ReferenceProperty | RelationProperty | ArrayProperty | MapProperty;
34
+ export type DataType = "string" | "number" | "boolean" | "date" | "geopoint" | "reference" | "relation" | "array" | "map" | "vector" | "binary";
35
+ export type Property = StringProperty | NumberProperty | BooleanProperty | DateProperty | GeopointProperty | ReferenceProperty | RelationProperty | ArrayProperty | MapProperty | VectorProperty | BinaryProperty;
36
36
  export type Properties = {
37
37
  [key: string]: Property;
38
38
  };
@@ -48,7 +48,7 @@ export type FirebaseProperties = {
48
48
  * A helper type to infer the underlying data type from a Property definition.
49
49
  * This is the core of the type inference system.
50
50
  */
51
- export type InferPropertyType<P extends Property> = P extends StringProperty ? string : P extends NumberProperty ? number : P extends BooleanProperty ? boolean : P extends DateProperty ? Date : P extends GeopointProperty ? GeoPoint : P extends ReferenceProperty ? EntityReference : P extends RelationProperty ? EntityRelation | EntityRelation[] : P extends ArrayProperty ? (P["of"] extends Property ? InferPropertyType<P["of"]>[] : unknown[]) : P extends MapProperty ? (P["properties"] extends Properties ? InferEntityType<P["properties"]> : Record<string, unknown>) : never;
51
+ export type InferPropertyType<P extends Property> = P extends StringProperty ? string : P extends NumberProperty ? number : P extends BooleanProperty ? boolean : P extends DateProperty ? Date : P extends GeopointProperty ? GeoPoint : P extends ReferenceProperty ? EntityReference : P extends RelationProperty ? EntityRelation | EntityRelation[] : P extends ArrayProperty ? (P["of"] extends Property ? InferPropertyType<P["of"]>[] : unknown[]) : P extends MapProperty ? (P["properties"] extends Properties ? InferEntityType<P["properties"]> : Record<string, unknown>) : P extends VectorProperty ? Vector : P extends BinaryProperty ? string : never;
52
52
  /**
53
53
  * Helper type that determines whether a property is required.
54
54
  * Uses direct structural matching against `{ validation: { required: true } }`
@@ -309,6 +309,25 @@ export interface BooleanProperty extends BaseProperty {
309
309
  */
310
310
  validation?: PropertyValidationSchema;
311
311
  }
312
+ /**
313
+ * @group Entity properties
314
+ */
315
+ export interface VectorUIConfig extends BaseUIConfig {
316
+ clearable?: boolean;
317
+ }
318
+ export interface VectorProperty extends BaseProperty {
319
+ ui?: VectorUIConfig;
320
+ type: "vector";
321
+ dimensions: number;
322
+ validation?: PropertyValidationSchema;
323
+ }
324
+ /**
325
+ * @group Entity properties
326
+ */
327
+ export interface BinaryProperty extends BaseProperty {
328
+ type: "binary";
329
+ validation?: PropertyValidationSchema;
330
+ }
312
331
  /**
313
332
  * @group Entity properties
314
333
  */
@@ -421,7 +440,7 @@ export interface RelationProperty extends BaseProperty {
421
440
  * When set, the framework treats this property as a self-contained relation
422
441
  * definition and no separate `relations[]` entry is needed.
423
442
  */
424
- target?: () => EntityCollection;
443
+ target?: string | (() => EntityCollection | string);
425
444
  /**
426
445
  * Whether this property references one or many records.
427
446
  * Defaults to `"one"`.
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { ArrayProperty, MapProperty, StringProperty, NumberProperty, BooleanProperty, DateProperty, GeopointProperty, ReferenceProperty, RelationProperty } from "./properties";
2
+ import { ArrayProperty, MapProperty, StringProperty, NumberProperty, BooleanProperty, DateProperty, GeopointProperty, ReferenceProperty, RelationProperty, VectorProperty, BinaryProperty } from "./properties";
3
3
  import { BaseProperty } from "./properties";
4
4
  type CMSBasePropertyNoName = Omit<BaseProperty, "name">;
5
5
  export type ConfigProperty = (Omit<StringProperty, "name"> & {
@@ -28,6 +28,10 @@ export type ConfigProperty = (Omit<StringProperty, "name"> & {
28
28
  } & CMSBasePropertyNoName) | (Omit<MapProperty, "name" | "properties"> & {
29
29
  name?: string;
30
30
  properties?: Record<string, ConfigProperty>;
31
+ } & CMSBasePropertyNoName) | (Omit<VectorProperty, "name"> & {
32
+ name?: string;
33
+ } & CMSBasePropertyNoName) | (Omit<BinaryProperty, "name"> & {
34
+ name?: string;
31
35
  } & CMSBasePropertyNoName);
32
36
  /**
33
37
  * This is the configuration object for a property.
@@ -66,5 +70,5 @@ export type PropertyConfig = {
66
70
  */
67
71
  description?: string;
68
72
  };
69
- export type PropertyConfigId = "text_field" | "multiline" | "markdown" | "url" | "email" | "user_select" | "select" | "multi_select" | "number_input" | "number_select" | "multi_number_select" | "file_upload" | "multi_file_upload" | "group" | "key_value" | "reference" | "reference_as_string" | "multi_references" | "relation" | "switch" | "date_time" | "repeat" | "custom_array" | "block";
73
+ export type PropertyConfigId = "text_field" | "multiline" | "markdown" | "url" | "email" | "user_select" | "select" | "multi_select" | "number_input" | "number_select" | "multi_number_select" | "file_upload" | "multi_file_upload" | "group" | "key_value" | "reference" | "reference_as_string" | "multi_references" | "relation" | "switch" | "date_time" | "repeat" | "custom_array" | "block" | "vector_input";
70
74
  export {};
@@ -17,7 +17,7 @@ export interface Relation {
17
17
  /**
18
18
  * The final collection you want to retrieve records from.
19
19
  */
20
- target: () => EntityCollection;
20
+ target: (() => EntityCollection) | any;
21
21
  /**
22
22
  * The nature of the relationship, determining if one or many records are returned.
23
23
  */
@@ -30,6 +30,8 @@ export interface RebaseTranslations {
30
30
  copy: string;
31
31
  delete: string;
32
32
  delete_not_allowed: string;
33
+ edit_entity?: string;
34
+ back_to_detail?: string;
33
35
  delete_confirmation_title: string;
34
36
  delete_confirmation_body: string;
35
37
  delete_multiple_confirmation_body: string;
@@ -392,6 +394,12 @@ export interface RebaseTranslations {
392
394
  submit: string;
393
395
  no_filterable_properties: string;
394
396
  apply_filters: string;
397
+ /** Label shown on the filter presets dropdown trigger */
398
+ filter_presets?: string;
399
+ /** Tooltip shown when hovering over a preset entry */
400
+ filter_preset_apply?: string;
401
+ /** Shown when a preset is active, with {{label}} interpolation */
402
+ filter_preset_active?: string;
395
403
  list: string;
396
404
  table_view_mode: string;
397
405
  cards: string;
@@ -42,5 +42,10 @@ export type User = {
42
42
  * The date and time when the user was created.
43
43
  */
44
44
  createdAt?: Date | string | null;
45
+ /**
46
+ * Additional metadata/custom claims associated with the user.
47
+ * Accessible by the frontend, but only writable by the backend.
48
+ */
49
+ readonly metadata?: Record<string, any>;
45
50
  getIdToken?: (forceRefresh?: boolean) => Promise<string>;
46
51
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgresql",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.2.1",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -51,7 +51,7 @@
51
51
  "exports": {
52
52
  ".": {
53
53
  "types": "./dist/server-postgresql/src/index.d.ts",
54
- "development": "./src/index.ts",
54
+ "development": "./dist/index.es.js",
55
55
  "import": "./dist/index.es.js",
56
56
  "require": "./dist/index.umd.js"
57
57
  },
@@ -61,23 +61,29 @@
61
61
  "arg": "^5.0.2",
62
62
  "chalk": "^4.1.2",
63
63
  "chokidar": "5.0.0",
64
- "drizzle-orm": "^0.44.4",
65
- "execa": "^4.1.0",
66
- "pg": "^8.11.3",
67
- "@rebasepro/server-core": "0.1.2",
68
- "@rebasepro/types": "0.1.2",
69
- "@rebasepro/common": "0.1.2",
70
- "@rebasepro/utils": "0.1.2"
64
+ "dotenv": "^16.6.1",
65
+ "drizzle-orm": "^0.44.7",
66
+ "execa": "^9.6.1",
67
+ "hono": "^4.12.21",
68
+ "pg": "^8.21.0",
69
+ "ws": "^8.20.1",
70
+ "@rebasepro/sdk-generator": "0.2.1",
71
+ "@rebasepro/common": "0.2.1",
72
+ "@rebasepro/types": "0.2.1",
73
+ "@rebasepro/server-core": "0.2.1",
74
+ "@rebasepro/utils": "0.2.1"
71
75
  },
72
76
  "devDependencies": {
73
77
  "@types/jest": "^29.5.14",
74
- "@types/node": "^20.10.5",
75
- "@types/pg": "^8.6.5",
76
- "drizzle-kit": "^0.31.4",
78
+ "@types/node": "^20.19.41",
79
+ "@types/pg": "^8.20.0",
80
+ "@types/ws": "^8.18.1",
81
+ "@vitejs/plugin-react": "^4.7.0",
82
+ "drizzle-kit": "^0.31.10",
77
83
  "jest": "^29.7.0",
78
- "ts-jest": "^29.4.5",
79
- "typescript": "^5.0.0",
80
- "vite": "^5.0.0"
84
+ "ts-jest": "^29.4.10",
85
+ "typescript": "^5.9.3",
86
+ "vite": "^5.4.21"
81
87
  },
82
88
  "gitHead": "d935eefa5aa8d1009a2398cfac2c1e4ee9aeb6b6",
83
89
  "publishConfig": {
@@ -0,0 +1,59 @@
1
+ import { DatabaseAdapter, InitializedDriver, RealtimeProvider, DataDriver, DatabaseAdmin, BootstrappedAuth } from "@rebasepro/types";
2
+ import { createPostgresBootstrapper } from "./PostgresBootstrapper";
3
+ // @ts-ignore
4
+ import type { PostgresDriverConfig } from "@rebasepro/server-core";
5
+
6
+ /**
7
+ * Creates a Postgres database adapter for Rebase.
8
+ */
9
+ export function createPostgresAdapter(pgConfig: PostgresDriverConfig): DatabaseAdapter {
10
+ const bootstrapper = createPostgresBootstrapper(pgConfig);
11
+
12
+ return {
13
+ type: bootstrapper.type,
14
+
15
+ async initializeDriver(config) {
16
+ return bootstrapper.initializeDriver(config);
17
+ },
18
+
19
+ async initializeRealtime(driverResult) {
20
+ if (bootstrapper.initializeRealtime) {
21
+ return bootstrapper.initializeRealtime({}, driverResult);
22
+ }
23
+ return undefined;
24
+ },
25
+
26
+ async initializeAuth(config, driverResult) {
27
+ if (bootstrapper.initializeAuth) {
28
+ return bootstrapper.initializeAuth(config, driverResult);
29
+ }
30
+ return undefined;
31
+ },
32
+
33
+ async initializeHistory(config, driverResult) {
34
+ if (bootstrapper.initializeHistory) {
35
+ return bootstrapper.initializeHistory(config, driverResult);
36
+ }
37
+ return undefined;
38
+ },
39
+
40
+ initializeWebsockets(server, realtimeService, driver, config) {
41
+ if (bootstrapper.initializeWebsockets) {
42
+ return bootstrapper.initializeWebsockets(server, realtimeService, driver, config);
43
+ }
44
+ },
45
+
46
+ getAdmin(driverResult) {
47
+ if (bootstrapper.getAdmin) {
48
+ return bootstrapper.getAdmin(driverResult);
49
+ }
50
+ return undefined;
51
+ },
52
+
53
+ mountRoutes(app, basePath, driverResult) {
54
+ if (bootstrapper.mountRoutes) {
55
+ bootstrapper.mountRoutes(app, basePath, driverResult);
56
+ }
57
+ }
58
+ };
59
+ }
@@ -27,7 +27,8 @@ import {
27
27
  TablePolicyInfo,
28
28
  SQLAdmin,
29
29
  SchemaAdmin,
30
- DatabaseAdmin
30
+ DatabaseAdmin,
31
+ RestFetchService
31
32
  } from "@rebasepro/types";
32
33
  import { buildRebaseData } from "@rebasepro/common";
33
34
  // @ts-ignore
@@ -537,23 +538,33 @@ propertyCallbacks: undefined };
537
538
  } as unknown as RebaseCallContext;
538
539
 
539
540
  if (callbacks?.beforeDelete || propertyCallbacks?.beforeDelete) {
541
+ let preventDefault = false;
540
542
  if (callbacks?.beforeDelete) {
541
- await callbacks.beforeDelete({
543
+ const result = await callbacks.beforeDelete({
542
544
  collection: resolvedCollection as EntityCollection<M>,
543
545
  path: entity.path,
544
546
  entityId: entity.id,
545
547
  entity,
546
548
  context: contextForCallback
547
549
  });
550
+ if (result === false) {
551
+ preventDefault = true;
552
+ }
548
553
  }
549
554
  if (propertyCallbacks?.beforeDelete) {
550
- await propertyCallbacks.beforeDelete({
555
+ const result = await propertyCallbacks.beforeDelete({
551
556
  collection: resolvedCollection as EntityCollection<M>,
552
557
  path: entity.path,
553
558
  entityId: entity.id,
554
559
  entity,
555
560
  context: contextForCallback
556
561
  });
562
+ if (result === false) {
563
+ preventDefault = true;
564
+ }
565
+ }
566
+ if (preventDefault) {
567
+ return;
557
568
  }
558
569
  }
559
570
 
@@ -664,7 +675,23 @@ searchString }
664
675
  const targetDb = this.getTargetDb(options?.database);
665
676
 
666
677
  try {
667
- if (options?.role) {
678
+ // Determine if we actually need to switch roles.
679
+ // Skip SET LOCAL ROLE when the requested role matches the current session role,
680
+ // as it's a no-op that can fail on managed Postgres setups where the connection
681
+ // user doesn't have permission to SET ROLE.
682
+ let needsRoleSwitch = false;
683
+ if (options?.role && process.env.DISABLE_DB_ROLE_SWITCHING !== "true") {
684
+ try {
685
+ const currentRoleResult = await targetDb.execute(drizzleSql.raw("SELECT current_user AS role"));
686
+ const currentRole = (currentRoleResult.rows?.[0] as Record<string, unknown>)?.role as string | undefined;
687
+ needsRoleSwitch = !!currentRole && currentRole !== options.role;
688
+ } catch {
689
+ // If we can't determine the current role, attempt the switch anyway
690
+ needsRoleSwitch = true;
691
+ }
692
+ }
693
+
694
+ if (needsRoleSwitch && options?.role) {
668
695
  const safeRole = options.role.replace(/"/g, '""');
669
696
  return await targetDb.transaction(async (tx) => {
670
697
  await tx.execute(drizzleSql.raw(`SET LOCAL ROLE "${safeRole}"`));
@@ -711,7 +738,9 @@ searchString }
711
738
  }
712
739
 
713
740
  async fetchAvailableRoles(): Promise<string[]> {
714
- const result = await this.executeSql("SELECT rolname FROM pg_roles;");
741
+ const result = await this.executeSql(
742
+ "SELECT rolname FROM pg_roles WHERE pg_has_role(current_user, rolname, 'member') ORDER BY rolname;"
743
+ );
715
744
  return result.map((r: Record<string, unknown>) => r.rolname as string);
716
745
  }
717
746
 
@@ -823,6 +852,7 @@ searchString }
823
852
  }
824
853
  }
825
854
  }
855
+ // SAFETY: Raw SQL result rows are typed as QueryResultRow[]; the query shape matches TableColumnInfo
826
856
  const typedColumns = columns as unknown as TableColumnInfo[];
827
857
 
828
858
  // 2. Fetch Foreign Keys
@@ -841,7 +871,8 @@ searchString }
841
871
  AND ccu.table_schema = tc.table_schema
842
872
  WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = ${safeName};
843
873
  `);
844
- const foreignKeys = fkResult.rows as unknown as TableForeignKeyInfo[];
874
+ // SAFETY: Raw SQL result rows match TableForeignKeyInfo shape from the SELECT aliases
875
+ const foreignKeys = fkResult.rows as TableForeignKeyInfo[];
845
876
 
846
877
  // 3. Fetch Junction Tables (Many-to-Many)
847
878
  // A simple junction table is one that has foreign keys to our table and other tables
@@ -861,7 +892,8 @@ searchString }
861
892
  AND ccu1.table_name = ${safeName}
862
893
  AND ccu2.table_name != ${safeName};
863
894
  `);
864
- const junctions = junctionsResult.rows as unknown as TableJunctionInfo[];
895
+ // SAFETY: Raw SQL result rows match TableJunctionInfo shape from the SELECT aliases
896
+ const junctions = junctionsResult.rows as TableJunctionInfo[];
865
897
 
866
898
  // 4. Fetch RLS Policies
867
899
  const policiesResult = await this.db.execute(drizzleSql`
@@ -874,7 +906,8 @@ searchString }
874
906
  FROM pg_policy
875
907
  WHERE polrelid = (SELECT oid FROM pg_class WHERE relname = ${safeName} AND relnamespace = 'public'::regnamespace);
876
908
  `);
877
- const policies = policiesResult.rows as unknown as TablePolicyInfo[];
909
+ // SAFETY: Raw SQL result rows match TablePolicyInfo shape from the SELECT aliases
910
+ const policies = policiesResult.rows as TablePolicyInfo[];
878
911
 
879
912
  return {
880
913
  columns: typedColumns,
@@ -921,6 +954,22 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
921
954
  */
922
955
  admin: DatabaseAdmin;
923
956
 
957
+ get restFetchService(): RestFetchService | undefined {
958
+ if (!this.delegate.restFetchService) return undefined;
959
+ return {
960
+ fetchCollectionForRest: async (collectionPath, options, include) => {
961
+ return this.withTransaction(async (delegate) => {
962
+ return delegate.restFetchService.fetchCollectionForRest(collectionPath, options, include);
963
+ });
964
+ },
965
+ fetchEntityForRest: async (collectionPath, entityId, include, databaseId) => {
966
+ return this.withTransaction(async (delegate) => {
967
+ return delegate.restFetchService.fetchEntityForRest(collectionPath, entityId, include, databaseId);
968
+ });
969
+ }
970
+ };
971
+ }
972
+
924
973
  private async withTransaction<T>(
925
974
  operation: (delegate: PostgresBackendDriver) => Promise<T>
926
975
  ): Promise<T> {