@rebasepro/server-postgresql 0.0.1-canary.dbf160a → 0.0.1-canary.e17585f

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 (74) hide show
  1. package/dist/index.es.js +683 -1362
  2. package/dist/index.es.js.map +1 -1
  3. package/dist/index.umd.js +614 -1293
  4. package/dist/index.umd.js.map +1 -1
  5. package/dist/server-postgresql/src/PostgresAdapter.d.ts +6 -0
  6. package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +8 -1
  7. package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +0 -5
  8. package/dist/server-postgresql/src/index.d.ts +1 -0
  9. package/dist/server-postgresql/src/schema/introspect-db-inference.d.ts +5 -0
  10. package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +44 -9
  11. package/dist/server-postgresql/src/services/EntityPersistService.d.ts +9 -0
  12. package/dist/server-postgresql/src/services/realtimeService.d.ts +12 -0
  13. package/dist/server-postgresql/src/websocket.d.ts +2 -1
  14. package/dist/types/src/controllers/auth.d.ts +8 -2
  15. package/dist/types/src/controllers/client.d.ts +13 -0
  16. package/dist/types/src/controllers/collection_registry.d.ts +2 -1
  17. package/dist/types/src/controllers/data_driver.d.ts +36 -1
  18. package/dist/types/src/controllers/navigation.d.ts +18 -6
  19. package/dist/types/src/controllers/registry.d.ts +9 -1
  20. package/dist/types/src/controllers/side_entity_controller.d.ts +7 -0
  21. package/dist/types/src/rebase_context.d.ts +17 -0
  22. package/dist/types/src/types/auth_adapter.d.ts +354 -0
  23. package/dist/types/src/types/backend_hooks.d.ts +187 -0
  24. package/dist/types/src/types/collections.d.ts +75 -11
  25. package/dist/types/src/types/component_ref.d.ts +47 -0
  26. package/dist/types/src/types/cron.d.ts +1 -1
  27. package/dist/types/src/types/database_adapter.d.ts +90 -0
  28. package/dist/types/src/types/entity_views.d.ts +6 -7
  29. package/dist/types/src/types/formex.d.ts +40 -0
  30. package/dist/types/src/types/index.d.ts +5 -0
  31. package/dist/types/src/types/plugins.d.ts +6 -3
  32. package/dist/types/src/types/properties.d.ts +72 -88
  33. package/dist/types/src/types/slots.d.ts +20 -10
  34. package/dist/types/src/types/translations.d.ts +12 -0
  35. package/package.json +5 -5
  36. package/src/PostgresAdapter.ts +52 -0
  37. package/src/PostgresBackendDriver.ts +49 -7
  38. package/src/PostgresBootstrapper.ts +4 -7
  39. package/src/auth/ensure-tables.ts +3 -121
  40. package/src/cli.ts +10 -2
  41. package/src/data-transformer.ts +84 -1
  42. package/src/index.ts +1 -0
  43. package/src/schema/doctor.ts +14 -2
  44. package/src/schema/generate-drizzle-schema-logic.ts +59 -30
  45. package/src/schema/introspect-db-inference.ts +238 -0
  46. package/src/schema/introspect-db-logic.ts +365 -61
  47. package/src/schema/introspect-db.ts +66 -23
  48. package/src/services/EntityFetchService.ts +16 -0
  49. package/src/services/EntityPersistService.ts +95 -13
  50. package/src/services/realtimeService.ts +35 -0
  51. package/src/utils/drizzle-conditions.ts +6 -0
  52. package/src/websocket.ts +60 -11
  53. package/test/generate-drizzle-schema.test.ts +342 -0
  54. package/test/introspect-db-generation.test.ts +32 -10
  55. package/test/property-ordering.test.ts +395 -0
  56. package/test/relations.test.ts +4 -4
  57. package/jest-all.log +0 -3128
  58. package/jest.log +0 -49
  59. package/scratch.ts +0 -41
  60. package/test-drizzle-bug.ts +0 -18
  61. package/test-drizzle-out/0000_cultured_freak.sql +0 -7
  62. package/test-drizzle-out/0001_tiresome_professor_monster.sql +0 -1
  63. package/test-drizzle-out/meta/0000_snapshot.json +0 -55
  64. package/test-drizzle-out/meta/0001_snapshot.json +0 -63
  65. package/test-drizzle-out/meta/_journal.json +0 -20
  66. package/test-drizzle-prompt.sh +0 -2
  67. package/test-policy-prompt.sh +0 -3
  68. package/test-programmatic.ts +0 -30
  69. package/test-programmatic2.ts +0 -59
  70. package/test-schema-no-policies.ts +0 -12
  71. package/test_drizzle_mock.js +0 -3
  72. package/test_find_changed.mjs +0 -32
  73. package/test_hash.js +0 -14
  74. package/test_output.txt +0 -3145
@@ -0,0 +1,6 @@
1
+ import { DatabaseAdapter } from "@rebasepro/types";
2
+ import type { PostgresDriverConfig } from "@rebasepro/server-core";
3
+ /**
4
+ * Creates a Postgres database adapter for Rebase.
5
+ */
6
+ export declare function createPostgresAdapter(pgConfig: PostgresDriverConfig): DatabaseAdapter;
@@ -3,7 +3,7 @@ import { BranchService } from "./services/BranchService";
3
3
  import { RealtimeService } from "./services/realtimeService";
4
4
  import { DatabasePoolManager } from "./databasePoolManager";
5
5
  import { DrizzleClient } from "./interfaces";
6
- import { User } from "@rebasepro/types";
6
+ import { User, RebaseClient } from "@rebasepro/types";
7
7
  import { PostgresCollectionRegistry } from "./collections/PostgresCollectionRegistry";
8
8
  import { DataDriver, DeleteEntityProps, Entity, EntityCollection, FetchCollectionProps, FetchEntityProps, ListenCollectionProps, ListenEntityProps, SaveEntityProps, RebaseData, TableMetadata, DatabaseAdmin } from "@rebasepro/types";
9
9
  import { HistoryService } from "./history/HistoryService";
@@ -19,6 +19,7 @@ export declare class PostgresBackendDriver implements DataDriver {
19
19
  branchService?: BranchService;
20
20
  user?: User;
21
21
  data: RebaseData;
22
+ client?: RebaseClient;
22
23
  /**
23
24
  * When true, realtime notifications are deferred until after the
24
25
  * wrapping transaction commits. Set by `withAuth` → `withTransaction`.
@@ -37,6 +38,12 @@ export declare class PostgresBackendDriver implements DataDriver {
37
38
  * allowing test spies applied after construction to take effect.
38
39
  */
39
40
  get admin(): DatabaseAdmin;
41
+ /**
42
+ * REST-optimised fetch service (include-aware eager-loading).
43
+ * Delegates to the underlying EntityFetchService which already
44
+ * implements the matching method signatures.
45
+ */
46
+ get restFetchService(): import("./services").EntityFetchService;
40
47
  private resolveCollectionCallbacks;
41
48
  fetchCollection<M extends Record<string, unknown>>({ path, collection, filter, limit, offset, startAfter, orderBy, searchString, order }: FetchCollectionProps<M>): Promise<Entity<M>[]>;
42
49
  listenCollection<M extends Record<string, unknown>>({ path, collection, filter, limit, offset, startAfter, orderBy, searchString, order, onUpdate, onError }: ListenCollectionProps<M>): () => void;
@@ -2,11 +2,6 @@
2
2
  * PostgresBootstrapper
3
3
  *
4
4
  * Implements the `BackendBootstrapper` interface for PostgreSQL.
5
- * Encapsulates all Postgres-specific initialization logic that was previously
6
- * hardcoded inside `initializeRebaseBackend()`.
7
- *
8
- * Third-party drivers (MongoDB, MySQL, etc.) can implement their own
9
- * bootstrapper following this pattern and pass it to the coordinator.
10
5
  */
11
6
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
12
7
  import { BackendBootstrapper } from "@rebasepro/types";
@@ -11,3 +11,4 @@ export * from "./websocket";
11
11
  export * from "./collections/PostgresCollectionRegistry";
12
12
  export * from "./services/BranchService";
13
13
  export * from "./PostgresBootstrapper";
14
+ export * from "./PostgresAdapter";
@@ -0,0 +1,5 @@
1
+ export interface InferenceResult {
2
+ propType?: string;
3
+ extra?: string;
4
+ }
5
+ export declare function inferPropertyFromData(columnName: string, pgDataType: string, currentPropType: string, sampleValues: unknown[], isPk: boolean): InferenceResult;
@@ -1,11 +1,3 @@
1
- /**
2
- * Introspection logic — pure functions and the pipeline that transforms
3
- * raw PostgreSQL metadata into Rebase collection definition files.
4
- *
5
- * This module contains NO side-effects: no fs writes, no pg.Client creation,
6
- * no process.exit. It is imported by introspect-db.ts (the CLI entry-point)
7
- * and consumed directly by tests.
8
- */
9
1
  export interface TableRow {
10
2
  table_name: string;
11
3
  }
@@ -57,6 +49,49 @@ export declare function mapPgType(dataType: string): string;
57
49
  export declare function buildEnumMap(enumValues: EnumValue[]): Map<string, string[]>;
58
50
  export declare function buildTablesMap(tables: TableRow[], columns: TableColumn[], pks: PrimaryKeyRow[], fks: ForeignKeyRow[]): Map<string, TableMeta>;
59
51
  export declare function identifyJoinTables(tablesMap: Map<string, TableMeta>): Set<string>;
52
+ /**
53
+ * Property metadata used to compute display priority.
54
+ * Keeps computePropertyPriority free of any TableMeta coupling.
55
+ */
56
+ export interface PropertyOrderingContext {
57
+ /** The resolved Rebase property type (e.g. "string", "number", "date", "relation"). */
58
+ propType: string;
59
+ /** Whether this column is a primary key. */
60
+ isPk: boolean;
61
+ /** Whether this column is an enum (USER-DEFINED with matching values). */
62
+ isEnum: boolean;
63
+ /** Whether this is a storage/file-upload field (detected from column name). */
64
+ isStorage: boolean;
65
+ /** The PostgreSQL data_type (e.g. "text", "character varying", "jsonb"). */
66
+ pgDataType: string;
67
+ /** The original column index in PostgreSQL (for stable tiebreaking). */
68
+ originalIndex: number;
69
+ }
70
+ /**
71
+ * Compute a numeric priority score for a property.
72
+ * Lower scores appear first in the generated `propertiesOrder` array.
73
+ *
74
+ * The system uses 14 tiers (0–139), with the original column index
75
+ * added as a fractional tiebreaker (originalIndex / 10000) to
76
+ * guarantee stable ordering within the same tier.
77
+ *
78
+ * Pure function — no side effects.
79
+ */
80
+ export declare function computePropertyPriority(columnName: string, ctx: PropertyOrderingContext): number;
81
+ /**
82
+ * Sort a `propertiesOrder` array using the priority heuristic.
83
+ * Returns a new sorted array; does not mutate the input.
84
+ *
85
+ * @param entries - Array of { key, columnName, propType, ... } objects
86
+ * carrying the information needed to compute priority.
87
+ */
88
+ export interface PropertyOrderEntry {
89
+ /** The property key in the generated collection (may differ from columnName for relations). */
90
+ key: string;
91
+ /** The ordering context for this property. */
92
+ ctx: PropertyOrderingContext;
93
+ }
94
+ export declare function sortPropertiesOrder(entries: PropertyOrderEntry[]): string[];
60
95
  export interface GeneratedFile {
61
96
  tableName: string;
62
97
  fileName: string;
@@ -66,7 +101,7 @@ export interface GeneratedFile {
66
101
  * Generate the full TypeScript file content for a single collection.
67
102
  * Pure function — no I/O.
68
103
  */
69
- export declare function generateCollectionFile(tableName: string, meta: TableMeta, allFks: ForeignKeyRow[], joinTables: Set<string>, tablesMap: Map<string, TableMeta>, enumMap: Map<string, string[]>): string;
104
+ export declare function generateCollectionFile(tableName: string, meta: TableMeta, allFks: ForeignKeyRow[], joinTables: Set<string>, tablesMap: Map<string, TableMeta>, enumMap: Map<string, string[]>, sampleData?: Record<string, unknown>[]): string;
70
105
  /**
71
106
  * Generate the content for an index.ts file that re-exports all collections.
72
107
  */
@@ -33,6 +33,15 @@ export declare class EntityPersistService {
33
33
  * Translate raw PostgreSQL / Drizzle errors into user-friendly messages.
34
34
  */
35
35
  private toUserFriendlyError;
36
+ /**
37
+ * Walk the error cause chain and return the deepest meaningful message.
38
+ */
39
+ private extractCauseMessage;
40
+ /**
41
+ * Strip the raw SQL query from a Drizzle "Failed query: ..." message,
42
+ * keeping only the error description.
43
+ */
44
+ private stripSqlFromMessage;
36
45
  /**
37
46
  * Extract the underlying PostgreSQL error from a Drizzle wrapper.
38
47
  * Drizzle wraps PG errors in a `cause` property.
@@ -152,6 +152,18 @@ export declare class RealtimeService extends EventEmitter implements RealtimePro
152
152
  * Returns ["posts", "posts/70"] for the example above
153
153
  */
154
154
  private getParentPaths;
155
+ /**
156
+ * Gracefully tear down all realtime resources.
157
+ *
158
+ * This MUST be called during process shutdown, **before** `pool.end()`.
159
+ * It ensures:
160
+ * 1. All debounced refetch timers are cancelled (prevents queries after pool closes).
161
+ * 2. All subscription state and callbacks are cleared.
162
+ * 3. The dedicated LISTEN client (outside the pool) is disconnected.
163
+ * 4. All WebSocket clients are removed (but not forcefully closed — the
164
+ * HTTP server close will handle that).
165
+ */
166
+ destroy(): Promise<void>;
155
167
  /**
156
168
  * Enable cross-instance realtime broadcasting via Postgres LISTEN/NOTIFY.
157
169
  * Creates a dedicated pg.Client (outside the Drizzle pool) that stays
@@ -1,5 +1,6 @@
1
1
  import { RealtimeService } from "./services/realtimeService";
2
2
  import { PostgresBackendDriver } from "./PostgresBackendDriver";
3
+ import { AuthAdapter } from "@rebasepro/types";
3
4
  import { Server } from "http";
4
5
  import { AuthConfig } from "@rebasepro/server-core";
5
- export declare function createPostgresWebSocket(server: Server, realtimeService: RealtimeService, driver: PostgresBackendDriver, authConfig?: AuthConfig): void;
6
+ export declare function createPostgresWebSocket(server: Server, realtimeService: RealtimeService, driver: PostgresBackendDriver, authConfig?: AuthConfig, authAdapter?: AuthAdapter): void;
@@ -80,8 +80,14 @@ export type AuthController<USER extends User = User, ExtraData = unknown> = {
80
80
  export interface AuthControllerExtended<USER extends User = User, ExtraData = unknown> extends AuthController<USER, ExtraData> {
81
81
  /** Login with email and password */
82
82
  emailPasswordLogin?(email: string, password: string): Promise<void>;
83
- /** Login with a Google ID token or trigger Google popup */
84
- googleLogin?(idToken: string): Promise<void>;
83
+ /** Login with a Google token or authorization code */
84
+ googleLogin?: {
85
+ (token: string, tokenType?: "idToken" | "accessToken"): Promise<void>;
86
+ (payload: {
87
+ code: string;
88
+ redirectUri: string;
89
+ }): Promise<void>;
90
+ };
85
91
  /** Register a new user */
86
92
  register?(email: string, password: string, displayName?: string): Promise<void>;
87
93
  /** Skip login (for anonymous access if enabled) */
@@ -167,4 +167,17 @@ export interface RebaseClient<DB = unknown> {
167
167
  email?: EmailService;
168
168
  /** Admin API for user and role management */
169
169
  admin?: AdminAPI;
170
+ /**
171
+ * The base HTTP URL of the backend server.
172
+ * Exposed by the SDK client (`@rebasepro/client`) and used to auto-derive
173
+ * the `ApiConfigProvider` URL.
174
+ */
175
+ baseUrl?: string;
176
+ /**
177
+ * WebSocket client for realtime subscriptions and admin capabilities.
178
+ * Exposed by the SDK client (`@rebasepro/client`). The shape is intentionally
179
+ * left as `unknown` in the base interface — callers should narrow via feature
180
+ * detection (e.g. `typeof ws.executeSql === "function"`).
181
+ */
182
+ ws?: unknown;
170
183
  }
@@ -36,7 +36,8 @@ export type CollectionRegistryController<DB = Record<string, unknown>, EC extend
36
36
  * Retrieve all the related parent collection ids for a given path
37
37
  * @param path
38
38
  */
39
- getParentCollectionIds: (path: string) => string[];
39
+ getParentCollectionSlugs: (path: string) => string[];
40
+ getParentEntityIds: (path: string) => string[];
40
41
  /**
41
42
  * Resolve paths from a list of ids
42
43
  * @param ids
@@ -144,12 +144,19 @@ export interface DataDriver {
144
144
  path: string;
145
145
  databaseId?: string;
146
146
  collection: EntityCollection;
147
- parentCollectionIds?: string[];
147
+ parentCollectionSlugs?: string[];
148
+ parentEntityIds?: string[];
148
149
  }) => Promise<boolean>;
149
150
  /**
150
151
  * Flag to indicate if the driver has requested the initialization of the text search index
151
152
  */
152
153
  needsInitTextSearch?: boolean;
154
+ /**
155
+ * Optional REST-optimised fetch service. When present, the REST API
156
+ * generator uses these methods instead of the generic `fetchEntity` /
157
+ * `fetchCollection` pipeline, enabling include-aware eager-loading.
158
+ */
159
+ restFetchService?: RestFetchService;
153
160
  /**
154
161
  * Return the admin capabilities of this driver.
155
162
  * @see SQLAdmin
@@ -158,3 +165,31 @@ export interface DataDriver {
158
165
  */
159
166
  admin?: import("../types/backend").DatabaseAdmin;
160
167
  }
168
+ /**
169
+ * REST-optimised fetch service exposed by drivers that support
170
+ * eager-loading of relations via `include`.
171
+ *
172
+ * The methods return flattened rows (`{ id, ...columns }`) rather
173
+ * than the `Entity<M>` wrapper used by the generic DataDriver API.
174
+ *
175
+ * @group DataDriver
176
+ */
177
+ export interface RestFetchService {
178
+ /**
179
+ * Fetch a collection of flattened entities with optional relation includes.
180
+ */
181
+ fetchCollectionForRest(collectionPath: string, options?: {
182
+ filter?: FilterValues<string>;
183
+ orderBy?: string;
184
+ order?: "desc" | "asc";
185
+ limit?: number;
186
+ offset?: number;
187
+ startAfter?: Record<string, unknown>;
188
+ searchString?: string;
189
+ databaseId?: string;
190
+ }, include?: string[]): Promise<Record<string, unknown>[]>;
191
+ /**
192
+ * Fetch a single flattened entity with optional relation includes.
193
+ */
194
+ fetchEntityForRest(collectionPath: string, entityId: string | number, include?: string[], databaseId?: string): Promise<Record<string, unknown> | null>;
195
+ }
@@ -144,17 +144,18 @@ export interface AppView {
144
144
  * It will still be accessible if you reach the specified path
145
145
  */
146
146
  hideFromNavigation?: boolean;
147
+ /**
148
+ * Navigation group for this view.
149
+ * Views sharing the same group name will be visually grouped
150
+ * together in the drawer and home page. If not set, the view
151
+ * falls into the default "Views" group.
152
+ */
153
+ group?: string;
147
154
  /**
148
155
  * Component to be rendered. This can be any React component, and can use
149
156
  * any of the provided hooks
150
157
  */
151
158
  view: React.ReactNode;
152
- /**
153
- * Optional field used to group top level navigation entries under a
154
- * navigation view.
155
- * This prop is ignored for admin views.
156
- */
157
- group?: string;
158
159
  /**
159
160
  * If true, a wildcard route (slug/*) is automatically registered
160
161
  * alongside the base route, enabling nested navigation within this view.
@@ -193,6 +194,17 @@ export interface NavigationGroupMapping {
193
194
  * List of collection ids or view paths that belong to this group.
194
195
  */
195
196
  entries: string[];
197
+ /**
198
+ * Configure which groups start collapsed.
199
+ * Set to `true` to collapse in both drawer and home page,
200
+ * or use an object to control each independently.
201
+ *
202
+ * @defaultValue false (expanded)
203
+ */
204
+ collapsedByDefault?: boolean | {
205
+ drawer?: boolean;
206
+ home?: boolean;
207
+ };
196
208
  }
197
209
  export interface NavigationEntry {
198
210
  id: string;
@@ -3,7 +3,7 @@ import type { EntityCollection } from "../types/collections";
3
3
  import type { EntityCollectionsBuilder } from "../types/builders";
4
4
  import type { EntityCustomView } from "../types/entity_views";
5
5
  import type { EntityAction } from "../types/entity_actions";
6
- import type { AppView } from "./navigation";
6
+ import type { AppView, NavigationGroupMapping } from "./navigation";
7
7
  /**
8
8
  * Options to enable the built-in collection editor.
9
9
  * When provided to `<RebaseCMS>`, the editor is auto-wired as a native feature.
@@ -25,6 +25,14 @@ export interface RebaseCMSConfig<EC extends EntityCollection = any> {
25
25
  entityViews?: EntityCustomView<any>[];
26
26
  entityActions?: EntityAction[];
27
27
  plugins?: any[];
28
+ /**
29
+ * Centralized configuration for how collections and views are grouped
30
+ * in the navigation sidebar and home page.
31
+ * Each mapping defines a named group and the collection/view slugs
32
+ * that belong to it. The array order determines group display order.
33
+ * Entry order within each group determines card order.
34
+ */
35
+ navigationGroupMappings?: NavigationGroupMapping[];
28
36
  /**
29
37
  * Enable the built-in visual collection/schema editor.
30
38
  * Pass `true` for zero-config, or an options object for fine-grained control.
@@ -62,6 +62,13 @@ export interface EntitySidePanelProps<M extends Record<string, unknown> = Record
62
62
  * Allow the user to open the entity fullscreen
63
63
  */
64
64
  allowFullScreen?: boolean;
65
+ /**
66
+ * Pre-populate the form with these values when creating a new entity.
67
+ * Only applied when `entityId` is not set (i.e. the form is in "new" mode).
68
+ * Useful for actions that fetch data from an external source (e.g. a URL)
69
+ * and want to pre-fill the document before the user saves.
70
+ */
71
+ defaultValues?: Partial<M>;
65
72
  }
66
73
  /**
67
74
  * Controller to open the side dialog displaying entity forms
@@ -3,6 +3,7 @@ import type { AuthController } from "./controllers/auth";
3
3
  import type { StorageSource } from "./controllers/storage";
4
4
  import type { UserConfigurationPersistence } from "./controllers/local_config_persistence";
5
5
  import type { DatabaseAdmin } from "./types/backend";
6
+ import type { RebaseClient } from "./controllers/client";
6
7
  import type { RebaseData } from "./controllers/data";
7
8
  import type { User } from "./users";
8
9
  import type { UserManagementDelegate } from "./types/user_management_delegate";
@@ -12,6 +13,22 @@ import type { UserManagementDelegate } from "./types/user_management_delegate";
12
13
  * @group Hooks and utilities
13
14
  */
14
15
  export type RebaseCallContext<USER extends User = User> = {
16
+ /**
17
+ * The Rebase client instance.
18
+ * Available in all entity callbacks (beforeSave, afterSave, afterRead,
19
+ * beforeDelete, afterDelete) and in CollectionActionsProps via context.
20
+ * Use it to call backend functions, access data, storage, etc.
21
+ *
22
+ * @example
23
+ * // In a beforeSave callback:
24
+ * const result = await context.client.functions.invoke('my-function', { ... });
25
+ *
26
+ * @example
27
+ * // In a CollectionAction component:
28
+ * const { client } = props.context;
29
+ * const result = await client.functions.invoke('extract-job', { url });
30
+ */
31
+ client: RebaseClient<any>;
15
32
  /**
16
33
  * Unified data access — `context.data.products.create(...)`.
17
34
  * Access any collection as a dynamic property.