@rebasepro/server-postgresql 0.3.0 → 0.5.0
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/README.md +69 -89
- package/dist/common/src/collections/default-collections.d.ts +5 -8
- package/dist/common/src/data/query_builder.d.ts +6 -2
- package/dist/common/src/util/permissions.d.ts +14 -6
- package/dist/index.es.js +379 -611
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +375 -607
- package/dist/index.umd.js.map +1 -1
- package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +2 -0
- package/dist/server-postgresql/src/auth/ensure-tables.d.ts +7 -4
- package/dist/server-postgresql/src/auth/services.d.ts +17 -42
- package/dist/server-postgresql/src/data-transformer.d.ts +0 -3
- package/dist/server-postgresql/src/databasePoolManager.d.ts +1 -1
- package/dist/server-postgresql/src/schema/auth-schema.d.ts +87 -340
- package/dist/server-postgresql/src/services/EntityFetchService.d.ts +2 -1
- package/dist/server-postgresql/src/services/EntityPersistService.d.ts +4 -0
- package/dist/server-postgresql/src/services/entityService.d.ts +4 -0
- package/dist/server-postgresql/src/types.d.ts +3 -0
- package/dist/server-postgresql/src/utils/drizzle-conditions.d.ts +5 -1
- package/dist/server-postgresql/src/websocket.d.ts +8 -3
- package/dist/types/src/controllers/auth.d.ts +2 -2
- package/dist/types/src/controllers/client.d.ts +25 -40
- package/dist/types/src/controllers/data.d.ts +21 -3
- package/dist/types/src/controllers/data_driver.d.ts +5 -0
- package/dist/types/src/controllers/email.d.ts +2 -0
- package/dist/types/src/types/auth_adapter.d.ts +3 -56
- package/dist/types/src/types/backend.d.ts +38 -3
- package/dist/types/src/types/backend_hooks.d.ts +2 -17
- package/dist/types/src/types/collections.d.ts +30 -6
- package/dist/types/src/types/entity_views.d.ts +19 -28
- package/dist/types/src/types/properties.d.ts +9 -15
- package/dist/types/src/types/user_management_delegate.d.ts +16 -53
- package/dist/types/src/users/index.d.ts +0 -1
- package/dist/types/src/users/user.d.ts +0 -1
- package/package.json +6 -6
- package/src/PostgresBackendDriver.ts +10 -0
- package/src/PostgresBootstrapper.ts +27 -22
- package/src/auth/ensure-tables.ts +82 -129
- package/src/auth/services.ts +99 -197
- package/src/cli.ts +50 -23
- package/src/data-transformer.ts +57 -95
- package/src/databasePoolManager.ts +2 -1
- package/src/schema/auth-schema.ts +13 -69
- package/src/schema/doctor.ts +44 -3
- package/src/schema/generate-drizzle-schema-logic.ts +33 -3
- package/src/schema/generate-drizzle-schema.ts +2 -6
- package/src/schema/introspect-db-logic.ts +7 -0
- package/src/services/EntityFetchService.ts +13 -1
- package/src/services/EntityPersistService.ts +38 -12
- package/src/services/entityService.ts +7 -0
- package/src/types.ts +4 -0
- package/src/utils/drizzle-conditions.ts +40 -5
- package/src/websocket.ts +38 -25
- package/test/auth-services.test.ts +7 -150
- package/test/doctor.test.ts +6 -2
- package/test/relation-pipeline-gaps.test.ts +315 -0
- package/dist/server-postgresql/src/schema/default-collections.d.ts +0 -2
- package/dist/types/src/users/roles.d.ts +0 -14
- package/drizzle.test.config.ts +0 -10
- package/src/schema/default-collections.ts +0 -69
package/README.md
CHANGED
|
@@ -1,106 +1,86 @@
|
|
|
1
|
-
# @rebasepro/postgresql
|
|
1
|
+
# @rebasepro/server-postgresql
|
|
2
2
|
|
|
3
|
-
PostgreSQL
|
|
4
|
-
|
|
5
|
-
This package provides a complete client-side implementation for connecting Rebase applications to PostgreSQL backends, featuring real-time synchronization via WebSockets.
|
|
3
|
+
PostgreSQL database driver for Rebase, built on Drizzle ORM.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
|
|
8
|
+
pnpm add @rebasepro/server-postgresql
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
##
|
|
11
|
+
## What This Package Does
|
|
14
12
|
|
|
15
|
-
|
|
13
|
+
Implements the Rebase `DatabaseAdapter` / `BackendBootstrapper` interfaces for PostgreSQL. It provides connection pooling, a Drizzle-based data driver, Postgres LISTEN/NOTIFY realtime, auth table management, entity history, schema generation, branching, read replicas, and WebSocket support. Plug it into `@rebasepro/server-core` via `createPostgresAdapter()` or `createPostgresBootstrapper()`.
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
import { usePostgresDataSource } from "@rebasepro/postgresql";
|
|
19
|
-
import { Rebase } from "@rebasepro/core";
|
|
20
|
-
|
|
21
|
-
function App() {
|
|
22
|
-
const dataSource = usePostgresDataSource({
|
|
23
|
-
baseUrl: "http://localhost:3001",
|
|
24
|
-
websocketUrl: "ws://localhost:3001", // Optional, will be inferred from baseUrl
|
|
25
|
-
headers: { // Optional
|
|
26
|
-
"Authorization": "Bearer your-token"
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return (
|
|
31
|
-
<Rebase
|
|
32
|
-
dataSource={dataSource}
|
|
33
|
-
collections={collections}
|
|
34
|
-
// ... other props
|
|
35
|
-
/>
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
```
|
|
15
|
+
## Key Exports
|
|
39
16
|
|
|
40
|
-
|
|
17
|
+
| Export | Description |
|
|
18
|
+
|--------|-------------|
|
|
19
|
+
| `createPostgresAdapter(config)` | Creates a `DatabaseAdapter` for use with `initializeRebaseBackend({ database: ... })`. Recommended API. |
|
|
20
|
+
| `createPostgresBootstrapper(config)` | Lower-level `BackendBootstrapper` factory. Used internally by the adapter. |
|
|
21
|
+
| `createPostgresDatabaseConnection(url, schema?, poolConfig?)` | Creates a production-grade pooled Drizzle connection. Returns `{ db, pool, connectionString }`. |
|
|
22
|
+
| `createDirectDatabaseConnection(url, schema?, poolConfig?)` | Non-pooled connection for LISTEN/NOTIFY and advisory locks (bypasses PgBouncer). |
|
|
23
|
+
| `createReadReplicaConnection(url, schema?, poolConfig?)` | Read-only connection for routing reads to replicas. |
|
|
24
|
+
| `PostgresBackendDriver` | The `DataDriver` implementation — CRUD, filtering, RLS, subcollections, admin SQL. |
|
|
25
|
+
| `RealtimeService` | Postgres LISTEN/NOTIFY-based `RealtimeProvider`. |
|
|
26
|
+
| `DatabasePoolManager` | Per-branch/per-tenant dynamic pool management (used with `ADMIN_CONNECTION_STRING`). |
|
|
27
|
+
| `PostgresCollectionRegistry` | Collection → Drizzle table registry with enum and relation tracking. |
|
|
28
|
+
| `BranchService` | Database branching (schema-level isolation). |
|
|
29
|
+
| `generateDrizzleSchema(collections)` | Generates Drizzle schema code from collection definitions. |
|
|
30
|
+
| `createAuthSchema(schemaName?)` | Generates Drizzle tables for the auth system (`users`, `roles`, `user_roles`). |
|
|
41
31
|
|
|
42
|
-
|
|
43
|
-
import { createPostgresDataSource } from "@rebasepro/postgresql";
|
|
32
|
+
## Quick Start
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
```typescript
|
|
35
|
+
import { createPostgresDatabaseConnection } from "@rebasepro/server-postgresql";
|
|
36
|
+
import { createPostgresAdapter } from "@rebasepro/server-postgresql";
|
|
37
|
+
import { initializeRebaseBackend } from "@rebasepro/server-core";
|
|
38
|
+
import * as schema from "./generated/schema";
|
|
39
|
+
|
|
40
|
+
// Create connection
|
|
41
|
+
const { db, pool } = createPostgresDatabaseConnection(
|
|
42
|
+
process.env.DATABASE_URL,
|
|
43
|
+
schema
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Create adapter and pass to server-core
|
|
47
|
+
const database = createPostgresAdapter({
|
|
48
|
+
connection: db,
|
|
49
|
+
connectionString: process.env.DATABASE_URL,
|
|
50
|
+
schema: { tables: schema },
|
|
48
51
|
});
|
|
49
|
-
```
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
## API
|
|
60
|
-
|
|
61
|
-
### Configuration
|
|
53
|
+
const backend = await initializeRebaseBackend({
|
|
54
|
+
app,
|
|
55
|
+
server,
|
|
56
|
+
database,
|
|
57
|
+
collections,
|
|
58
|
+
auth: { /* ... */ },
|
|
59
|
+
});
|
|
62
60
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
61
|
+
// Graceful shutdown
|
|
62
|
+
process.on("SIGTERM", async () => {
|
|
63
|
+
await backend.shutdown();
|
|
64
|
+
await pool.end();
|
|
65
|
+
});
|
|
69
66
|
```
|
|
70
67
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- `FETCH_ENTITY`
|
|
91
|
-
- `SAVE_ENTITY`
|
|
92
|
-
- `DELETE_ENTITY`
|
|
93
|
-
- `CHECK_UNIQUE_FIELD`
|
|
94
|
-
- `GENERATE_ENTITY_ID`
|
|
95
|
-
- `COUNT_ENTITIES`
|
|
96
|
-
- `subscribe_collection`
|
|
97
|
-
- `subscribe_entity`
|
|
98
|
-
- `unsubscribe`
|
|
99
|
-
|
|
100
|
-
## Development
|
|
101
|
-
|
|
102
|
-
This package is part of the Rebase monorepo. For development instructions, see the main repository README.
|
|
103
|
-
|
|
104
|
-
## License
|
|
105
|
-
|
|
106
|
-
MIT
|
|
68
|
+
## Connection Pool Defaults
|
|
69
|
+
|
|
70
|
+
| Option | Default |
|
|
71
|
+
|--------|---------|
|
|
72
|
+
| `max` | 20 |
|
|
73
|
+
| `idleTimeoutMillis` | 30,000 |
|
|
74
|
+
| `connectionTimeoutMillis` | 10,000 |
|
|
75
|
+
| `queryTimeout` | 30,000 |
|
|
76
|
+
| `statementTimeout` | 30,000 |
|
|
77
|
+
| `keepAlive` | true |
|
|
78
|
+
|
|
79
|
+
## Related Packages
|
|
80
|
+
|
|
81
|
+
| Package | Role |
|
|
82
|
+
|---------|------|
|
|
83
|
+
| `@rebasepro/server-core` | Backend orchestrator that consumes this adapter |
|
|
84
|
+
| `@rebasepro/types` | Shared interfaces (`DatabaseAdapter`, `BackendBootstrapper`, `DataDriver`) |
|
|
85
|
+
| `@rebasepro/sdk-generator` | Generates typed SDKs from collections |
|
|
86
|
+
| `@rebasepro/common` | Default collections and shared utilities |
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { PostgresCollection } from "@rebasepro/types";
|
|
1
|
+
import type { PostgresCollection } from "@rebasepro/types";
|
|
2
2
|
/**
|
|
3
|
-
* Default users collection
|
|
3
|
+
* Default users collection.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* (Map keyed by slug, last-write-wins) so that developer-defined
|
|
9
|
-
* collections with the same slug override this default — no hardcoded
|
|
10
|
-
* string checks required.
|
|
5
|
+
* Prepended to the developer's collections array by the admin and server.
|
|
6
|
+
* Slug-based dedup (Map keyed by slug, last-write-wins) lets developers
|
|
7
|
+
* override by defining their own collection with `slug: "users"`.
|
|
11
8
|
*/
|
|
12
9
|
export declare const defaultUsersCollection: PostgresCollection;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { FindResponse, CollectionAccessor, QueryBuilderInterface, FilterOperator } from "@rebasepro/types";
|
|
1
|
+
import { FindResponse, CollectionAccessor, QueryBuilderInterface, FilterOperator, LogicalCondition, WhereValue, FilterCondition } from "@rebasepro/types";
|
|
2
|
+
export declare function or(...conditions: (FilterCondition | LogicalCondition)[]): LogicalCondition;
|
|
3
|
+
export declare function and(...conditions: (FilterCondition | LogicalCondition)[]): LogicalCondition;
|
|
4
|
+
export declare function cond(column: string, operator: FilterOperator, value: unknown): FilterCondition;
|
|
2
5
|
export declare class QueryBuilder<M extends Record<string, unknown> = Record<string, unknown>> implements QueryBuilderInterface<M> {
|
|
3
6
|
private collection;
|
|
4
7
|
private params;
|
|
@@ -8,7 +11,8 @@ export declare class QueryBuilder<M extends Record<string, unknown> = Record<str
|
|
|
8
11
|
* @example
|
|
9
12
|
* client.collection('users').where('age', '>=', 18).find()
|
|
10
13
|
*/
|
|
11
|
-
where
|
|
14
|
+
where<K extends keyof M & string>(column: K, operator: FilterOperator, value: WhereValue<M[K]>): this;
|
|
15
|
+
where(logicalCondition: LogicalCondition): this;
|
|
12
16
|
/**
|
|
13
17
|
* Order the results by a specific column.
|
|
14
18
|
* @example
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Entity, EntityCollection, User } from "@rebasepro/types";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal auth context for permission checking.
|
|
4
|
+
* Only requires the user object — avoids forcing callers to construct
|
|
5
|
+
* a full AuthController just to check permissions.
|
|
6
|
+
*/
|
|
7
|
+
export interface AuthContext<USER extends User = User> {
|
|
8
|
+
user: USER | null;
|
|
9
|
+
}
|
|
10
|
+
export declare function checkOperation<M extends Record<string, unknown>, USER extends User>(collection: EntityCollection<M>, authContext: AuthContext<USER>, entity: Entity<M> | null, targetOperation: "select" | "insert" | "update" | "delete"): boolean;
|
|
11
|
+
export declare function canReadCollection<M extends Record<string, unknown>, USER extends User>(collection: EntityCollection<M>, authContext: AuthContext<USER>): boolean;
|
|
12
|
+
export declare function canEditEntity<M extends Record<string, unknown>, USER extends User>(collection: EntityCollection<M>, authContext: AuthContext<USER>, path: string, entity: Entity<M> | null): boolean;
|
|
13
|
+
export declare function canCreateEntity<M extends Record<string, unknown>, USER extends User>(collection: EntityCollection<M>, authContext: AuthContext<USER>, path: string, entity: Entity<M> | null): boolean;
|
|
14
|
+
export declare function canDeleteEntity<M extends Record<string, unknown>, USER extends User>(collection: EntityCollection<M>, authContext: AuthContext<USER>, path: string, entity: Entity<M> | null): boolean;
|