@rebasepro/server-postgresql 0.1.2 → 0.2.3

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 (71) hide show
  1. package/LICENSE +22 -6
  2. package/dist/common/src/data/query_builder.d.ts +51 -0
  3. package/dist/common/src/index.d.ts +1 -0
  4. package/dist/common/src/util/entities.d.ts +2 -2
  5. package/dist/common/src/util/relations.d.ts +1 -1
  6. package/dist/index.es.js +1435 -738
  7. package/dist/index.es.js.map +1 -1
  8. package/dist/index.umd.js +1433 -736
  9. package/dist/index.umd.js.map +1 -1
  10. package/dist/server-postgresql/src/PostgresAdapter.d.ts +6 -0
  11. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +2 -1
  12. package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +0 -5
  13. package/dist/server-postgresql/src/auth/ensure-tables.d.ts +2 -1
  14. package/dist/server-postgresql/src/auth/services.d.ts +37 -15
  15. package/dist/server-postgresql/src/index.d.ts +1 -0
  16. package/dist/server-postgresql/src/schema/auth-schema.d.ts +43 -856
  17. package/dist/server-postgresql/src/schema/default-collections.d.ts +2 -0
  18. package/dist/server-postgresql/src/schema/doctor.d.ts +10 -1
  19. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +1 -0
  20. package/dist/server-postgresql/src/services/entity-helpers.d.ts +1 -1
  21. package/dist/server-postgresql/src/services/realtimeService.d.ts +12 -0
  22. package/dist/server-postgresql/src/websocket.d.ts +2 -1
  23. package/dist/types/src/controllers/auth.d.ts +9 -8
  24. package/dist/types/src/controllers/client.d.ts +3 -0
  25. package/dist/types/src/controllers/data.d.ts +21 -0
  26. package/dist/types/src/types/auth_adapter.d.ts +356 -0
  27. package/dist/types/src/types/collections.d.ts +67 -2
  28. package/dist/types/src/types/database_adapter.d.ts +94 -0
  29. package/dist/types/src/types/entity_actions.d.ts +7 -1
  30. package/dist/types/src/types/entity_callbacks.d.ts +1 -1
  31. package/dist/types/src/types/entity_views.d.ts +36 -1
  32. package/dist/types/src/types/index.d.ts +2 -0
  33. package/dist/types/src/types/plugins.d.ts +1 -1
  34. package/dist/types/src/types/properties.d.ts +24 -5
  35. package/dist/types/src/types/property_config.d.ts +6 -2
  36. package/dist/types/src/types/relations.d.ts +1 -1
  37. package/dist/types/src/types/translations.d.ts +8 -0
  38. package/dist/types/src/users/user.d.ts +5 -0
  39. package/package.json +22 -15
  40. package/src/PostgresAdapter.ts +59 -0
  41. package/src/PostgresBackendDriver.ts +66 -13
  42. package/src/PostgresBootstrapper.ts +35 -15
  43. package/src/auth/ensure-tables.ts +82 -189
  44. package/src/auth/services.ts +421 -170
  45. package/src/cli.ts +49 -13
  46. package/src/data-transformer.ts +78 -8
  47. package/src/history/HistoryService.ts +25 -2
  48. package/src/index.ts +1 -0
  49. package/src/schema/auth-schema.ts +130 -98
  50. package/src/schema/default-collections.ts +69 -0
  51. package/src/schema/doctor-cli.ts +5 -1
  52. package/src/schema/doctor.ts +166 -48
  53. package/src/schema/generate-drizzle-schema-logic.ts +74 -27
  54. package/src/schema/generate-drizzle-schema.ts +13 -3
  55. package/src/schema/introspect-db-inference.ts +5 -5
  56. package/src/schema/introspect-db-logic.ts +9 -2
  57. package/src/schema/introspect-db.ts +14 -3
  58. package/src/services/EntityFetchService.ts +5 -5
  59. package/src/services/RelationService.ts +2 -2
  60. package/src/services/entity-helpers.ts +1 -1
  61. package/src/services/realtimeService.ts +145 -136
  62. package/src/utils/drizzle-conditions.ts +16 -2
  63. package/src/websocket.ts +113 -37
  64. package/test/auth-services.test.ts +163 -74
  65. package/test/data-transformer-hardening.test.ts +57 -0
  66. package/test/data-transformer.test.ts +43 -0
  67. package/test/generate-drizzle-schema.test.ts +7 -5
  68. package/test/introspect-db-utils.test.ts +4 -1
  69. package/test/postgresDataDriver.test.ts +147 -1
  70. package/test/realtimeService.test.ts +7 -7
  71. 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.3",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -43,6 +43,7 @@
43
43
  "node"
44
44
  ],
45
45
  "moduleNameMapper": {
46
+ "^@rebasepro/client$": "<rootDir>/../client/src/index.ts",
46
47
  "^@rebasepro/common$": "<rootDir>/../common/src/index.ts",
47
48
  "^@rebasepro/types$": "<rootDir>/../types/src/index.ts",
48
49
  "^@rebasepro/utils$": "<rootDir>/../utils/src/index.ts"
@@ -51,7 +52,7 @@
51
52
  "exports": {
52
53
  ".": {
53
54
  "types": "./dist/server-postgresql/src/index.d.ts",
54
- "development": "./src/index.ts",
55
+ "development": "./dist/index.es.js",
55
56
  "import": "./dist/index.es.js",
56
57
  "require": "./dist/index.umd.js"
57
58
  },
@@ -61,23 +62,29 @@
61
62
  "arg": "^5.0.2",
62
63
  "chalk": "^4.1.2",
63
64
  "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"
65
+ "dotenv": "^16.6.1",
66
+ "drizzle-orm": "^0.44.7",
67
+ "execa": "^9.6.1",
68
+ "hono": "^4.12.21",
69
+ "pg": "^8.21.0",
70
+ "ws": "^8.20.1",
71
+ "@rebasepro/common": "0.2.3",
72
+ "@rebasepro/server-core": "0.2.3",
73
+ "@rebasepro/types": "0.2.3",
74
+ "@rebasepro/utils": "0.2.3",
75
+ "@rebasepro/sdk-generator": "0.2.3"
71
76
  },
72
77
  "devDependencies": {
73
78
  "@types/jest": "^29.5.14",
74
- "@types/node": "^20.10.5",
75
- "@types/pg": "^8.6.5",
76
- "drizzle-kit": "^0.31.4",
79
+ "@types/node": "^20.19.41",
80
+ "@types/pg": "^8.20.0",
81
+ "@types/ws": "^8.18.1",
82
+ "@vitejs/plugin-react": "^4.7.0",
83
+ "drizzle-kit": "^0.31.10",
77
84
  "jest": "^29.7.0",
78
- "ts-jest": "^29.4.5",
79
- "typescript": "^5.0.0",
80
- "vite": "^5.0.0"
85
+ "ts-jest": "^29.4.10",
86
+ "typescript": "^5.9.3",
87
+ "vite": "^5.4.21"
81
88
  },
82
89
  "gitHead": "d935eefa5aa8d1009a2398cfac2c1e4ee9aeb6b6",
83
90
  "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
@@ -116,7 +117,7 @@ export class PostgresBackendDriver implements DataDriver {
116
117
  if (!collection && !path) return { collection: undefined,
117
118
  callbacks: undefined,
118
119
  propertyCallbacks: undefined };
119
- const registryCollection = this.registry.getCollectionByPath(path);
120
+ const registryCollection = this.registry?.getCollectionByPath(path);
120
121
  const resolvedCollection = registryCollection
121
122
  ? { ...collection,
122
123
  ...registryCollection } as EntityCollection<M>
@@ -165,7 +166,8 @@ propertyCallbacks: undefined };
165
166
  user: this.user,
166
167
  driver: this,
167
168
  data: this.data,
168
- client: this.client
169
+ client: this.client,
170
+ storageSource: this.client?.storage
169
171
  } as unknown as RebaseCallContext; // Backend context
170
172
  return Promise.all(entities.map(async (entity) => {
171
173
  let fetched = entity;
@@ -275,7 +277,8 @@ propertyCallbacks: undefined };
275
277
  user: this.user,
276
278
  driver: this,
277
279
  data: this.data,
278
- client: this.client
280
+ client: this.client,
281
+ storageSource: this.client?.storage
279
282
  } as unknown as RebaseCallContext; // Backend context
280
283
  if (callbacks?.afterRead) {
281
284
  entity = await callbacks.afterRead({
@@ -358,7 +361,8 @@ propertyCallbacks: undefined };
358
361
  user: this.user,
359
362
  driver: this,
360
363
  data: this.data,
361
- client: this.client
364
+ client: this.client,
365
+ storageSource: this.client?.storage
362
366
  } as unknown as RebaseCallContext;
363
367
 
364
368
  // Fetch previous values for callbacks AND history recording
@@ -533,27 +537,38 @@ propertyCallbacks: undefined };
533
537
  user: this.user,
534
538
  driver: this,
535
539
  data: this.data,
536
- client: this.client
540
+ client: this.client,
541
+ storageSource: this.client?.storage
537
542
  } as unknown as RebaseCallContext;
538
543
 
539
544
  if (callbacks?.beforeDelete || propertyCallbacks?.beforeDelete) {
545
+ let preventDefault = false;
540
546
  if (callbacks?.beforeDelete) {
541
- await callbacks.beforeDelete({
547
+ const result = await callbacks.beforeDelete({
542
548
  collection: resolvedCollection as EntityCollection<M>,
543
549
  path: entity.path,
544
550
  entityId: entity.id,
545
551
  entity,
546
552
  context: contextForCallback
547
553
  });
554
+ if (result === false) {
555
+ preventDefault = true;
556
+ }
548
557
  }
549
558
  if (propertyCallbacks?.beforeDelete) {
550
- await propertyCallbacks.beforeDelete({
559
+ const result = await propertyCallbacks.beforeDelete({
551
560
  collection: resolvedCollection as EntityCollection<M>,
552
561
  path: entity.path,
553
562
  entityId: entity.id,
554
563
  entity,
555
564
  context: contextForCallback
556
565
  });
566
+ if (result === false) {
567
+ preventDefault = true;
568
+ }
569
+ }
570
+ if (preventDefault) {
571
+ return;
557
572
  }
558
573
  }
559
574
 
@@ -664,7 +679,23 @@ searchString }
664
679
  const targetDb = this.getTargetDb(options?.database);
665
680
 
666
681
  try {
667
- if (options?.role) {
682
+ // Determine if we actually need to switch roles.
683
+ // Skip SET LOCAL ROLE when the requested role matches the current session role,
684
+ // as it's a no-op that can fail on managed Postgres setups where the connection
685
+ // user doesn't have permission to SET ROLE.
686
+ let needsRoleSwitch = false;
687
+ if (options?.role && process.env.DISABLE_DB_ROLE_SWITCHING !== "true") {
688
+ try {
689
+ const currentRoleResult = await targetDb.execute(drizzleSql.raw("SELECT current_user AS role"));
690
+ const currentRole = (currentRoleResult.rows?.[0] as Record<string, unknown>)?.role as string | undefined;
691
+ needsRoleSwitch = !!currentRole && currentRole !== options.role;
692
+ } catch {
693
+ // If we can't determine the current role, attempt the switch anyway
694
+ needsRoleSwitch = true;
695
+ }
696
+ }
697
+
698
+ if (needsRoleSwitch && options?.role) {
668
699
  const safeRole = options.role.replace(/"/g, '""');
669
700
  return await targetDb.transaction(async (tx) => {
670
701
  await tx.execute(drizzleSql.raw(`SET LOCAL ROLE "${safeRole}"`));
@@ -711,7 +742,9 @@ searchString }
711
742
  }
712
743
 
713
744
  async fetchAvailableRoles(): Promise<string[]> {
714
- const result = await this.executeSql("SELECT rolname FROM pg_roles;");
745
+ const result = await this.executeSql(
746
+ "SELECT rolname FROM pg_roles WHERE pg_has_role(current_user, rolname, 'member') ORDER BY rolname;"
747
+ );
715
748
  return result.map((r: Record<string, unknown>) => r.rolname as string);
716
749
  }
717
750
 
@@ -823,6 +856,7 @@ searchString }
823
856
  }
824
857
  }
825
858
  }
859
+ // SAFETY: Raw SQL result rows are typed as QueryResultRow[]; the query shape matches TableColumnInfo
826
860
  const typedColumns = columns as unknown as TableColumnInfo[];
827
861
 
828
862
  // 2. Fetch Foreign Keys
@@ -841,7 +875,8 @@ searchString }
841
875
  AND ccu.table_schema = tc.table_schema
842
876
  WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = ${safeName};
843
877
  `);
844
- const foreignKeys = fkResult.rows as unknown as TableForeignKeyInfo[];
878
+ // SAFETY: Raw SQL result rows match TableForeignKeyInfo shape from the SELECT aliases
879
+ const foreignKeys = fkResult.rows as TableForeignKeyInfo[];
845
880
 
846
881
  // 3. Fetch Junction Tables (Many-to-Many)
847
882
  // A simple junction table is one that has foreign keys to our table and other tables
@@ -861,7 +896,8 @@ searchString }
861
896
  AND ccu1.table_name = ${safeName}
862
897
  AND ccu2.table_name != ${safeName};
863
898
  `);
864
- const junctions = junctionsResult.rows as unknown as TableJunctionInfo[];
899
+ // SAFETY: Raw SQL result rows match TableJunctionInfo shape from the SELECT aliases
900
+ const junctions = junctionsResult.rows as TableJunctionInfo[];
865
901
 
866
902
  // 4. Fetch RLS Policies
867
903
  const policiesResult = await this.db.execute(drizzleSql`
@@ -874,7 +910,8 @@ searchString }
874
910
  FROM pg_policy
875
911
  WHERE polrelid = (SELECT oid FROM pg_class WHERE relname = ${safeName} AND relnamespace = 'public'::regnamespace);
876
912
  `);
877
- const policies = policiesResult.rows as unknown as TablePolicyInfo[];
913
+ // SAFETY: Raw SQL result rows match TablePolicyInfo shape from the SELECT aliases
914
+ const policies = policiesResult.rows as TablePolicyInfo[];
878
915
 
879
916
  return {
880
917
  columns: typedColumns,
@@ -921,6 +958,22 @@ export class AuthenticatedPostgresBackendDriver implements DataDriver {
921
958
  */
922
959
  admin: DatabaseAdmin;
923
960
 
961
+ get restFetchService(): RestFetchService | undefined {
962
+ if (!this.delegate.restFetchService) return undefined;
963
+ return {
964
+ fetchCollectionForRest: async (collectionPath, options, include) => {
965
+ return this.withTransaction(async (delegate) => {
966
+ return delegate.restFetchService.fetchCollectionForRest(collectionPath, options, include);
967
+ });
968
+ },
969
+ fetchEntityForRest: async (collectionPath, entityId, include, databaseId) => {
970
+ return this.withTransaction(async (delegate) => {
971
+ return delegate.restFetchService.fetchEntityForRest(collectionPath, entityId, include, databaseId);
972
+ });
973
+ }
974
+ };
975
+ }
976
+
924
977
  private async withTransaction<T>(
925
978
  operation: (delegate: PostgresBackendDriver) => Promise<T>
926
979
  ): Promise<T> {