@rebasepro/server-postgres 0.9.1-canary.fd3754b → 0.10.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 +21 -0
- package/dist/PostgresBackendDriver.d.ts +43 -2
- package/dist/PostgresBootstrapper.d.ts +17 -1
- package/dist/auth/services.d.ts +68 -52
- package/dist/collections/buildRegistry.d.ts +27 -0
- package/dist/connection.d.ts +21 -0
- package/dist/data-transformer.d.ts +9 -2
- package/dist/index.es.js +2711 -2772
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-bootstrap-sql.d.ts +1 -1
- package/dist/schema/auth-schema.d.ts +24 -24
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/schema/introspect-db-logic.d.ts +0 -5
- package/dist/schema/introspect-db-naming.d.ts +10 -0
- package/dist/security/policy-drift.d.ts +70 -5
- package/dist/security/rls-enforcement.d.ts +29 -4
- package/dist/services/FetchService.d.ts +4 -24
- package/dist/services/PersistService.d.ts +27 -1
- package/dist/services/RelationService.d.ts +34 -1
- package/dist/services/channel-history.d.ts +118 -0
- package/dist/services/collection-helpers.d.ts +79 -14
- package/dist/services/dataService.d.ts +3 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/realtimeService.d.ts +76 -2
- package/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +15 -40
- package/src/PostgresBackendDriver.ts +183 -18
- package/src/PostgresBootstrapper.ts +86 -27
- package/src/auth/ensure-tables.ts +170 -28
- package/src/auth/services.ts +181 -150
- package/src/cli-helpers.ts +2 -20
- package/src/cli.ts +60 -0
- package/src/collections/buildRegistry.ts +59 -0
- package/src/connection.ts +61 -1
- package/src/data-transformer.ts +11 -9
- package/src/databasePoolManager.ts +2 -0
- package/src/schema/auth-bootstrap-sql.ts +7 -1
- package/src/schema/auth-schema.ts +13 -13
- package/src/schema/doctor-cli.ts +5 -1
- package/src/schema/doctor.ts +45 -20
- package/src/schema/generate-drizzle-schema-logic.ts +24 -29
- package/src/schema/generate-postgres-ddl-logic.ts +76 -28
- package/src/schema/introspect-db-inference.ts +1 -1
- package/src/schema/introspect-db-logic.ts +1 -10
- package/src/schema/introspect-db-naming.ts +15 -0
- package/src/schema/introspect-db.ts +19 -2
- package/src/schema/introspect-runtime.ts +1 -1
- package/src/security/policy-drift.test.ts +199 -14
- package/src/security/policy-drift.ts +197 -13
- package/src/security/rls-enforcement.ts +74 -7
- package/src/services/BranchService.ts +42 -10
- package/src/services/FetchService.ts +65 -270
- package/src/services/PersistService.ts +130 -14
- package/src/services/RelationService.ts +153 -94
- package/src/services/channel-history.ts +343 -0
- package/src/services/collection-helpers.ts +164 -47
- package/src/services/dataService.ts +3 -2
- package/src/services/index.ts +1 -0
- package/src/services/realtimeService.ts +238 -29
- package/src/services/row-pipeline.ts +239 -0
- package/src/utils/drizzle-conditions.ts +13 -0
- package/src/websocket.ts +34 -12
- package/dist/chunk-DSJWtz9O.js +0 -40
- package/dist/schema/auth-default-policies.d.ts +0 -10
- package/dist/src-Eh-CZosp.js +0 -595
- package/dist/src-Eh-CZosp.js.map +0 -1
- package/src/schema/auth-default-policies.ts +0 -125
|
@@ -21,4 +21,4 @@
|
|
|
21
21
|
* `DROP SCHEMA "rebase" CASCADE` on the next diff. The `auth` schema is safe
|
|
22
22
|
* because the generated schema.sql declares it.
|
|
23
23
|
*/
|
|
24
|
-
export declare const AUTH_BOOTSTRAP_SQL = "-- Auth schema + RLS helper functions (required by the policies below)\nCREATE SCHEMA IF NOT EXISTS auth;\n\nCREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$\n SELECT NULLIF(current_setting('app.user_id', true), '');\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.jwt', true), ''),\n '{}'\n )::jsonb;\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.roles() RETURNS text AS $$\n SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');\n$$ LANGUAGE sql STABLE;\n";
|
|
24
|
+
export declare const AUTH_BOOTSTRAP_SQL = "-- Auth schema + RLS helper functions (required by the policies below)\nCREATE SCHEMA IF NOT EXISTS auth;\n\n-- Falls back to the pre-rename `app.user_id` so a database that has taken the\n-- new schema but is still served by an older backend keeps resolving the\n-- principal. Drop the COALESCE once no such deployment remains.\nCREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.uid', true), ''),\n NULLIF(current_setting('app.user_id', true), '')\n );\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$\n SELECT COALESCE(\n NULLIF(current_setting('app.jwt', true), ''),\n '{}'\n )::jsonb;\n$$ LANGUAGE sql STABLE;\n\nCREATE OR REPLACE FUNCTION auth.roles() RETURNS text AS $$\n SELECT COALESCE(NULLIF(current_setting('app.user_roles', true), ''), '');\n$$ LANGUAGE sql STABLE;\n";
|
|
@@ -290,8 +290,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
290
290
|
identity: undefined;
|
|
291
291
|
generated: undefined;
|
|
292
292
|
}, {}, {}>;
|
|
293
|
-
|
|
294
|
-
name: "
|
|
293
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
294
|
+
name: "uid";
|
|
295
295
|
tableName: "refresh_tokens";
|
|
296
296
|
dataType: "string";
|
|
297
297
|
columnType: "PgUUID";
|
|
@@ -422,8 +422,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
422
422
|
identity: undefined;
|
|
423
423
|
generated: undefined;
|
|
424
424
|
}, {}, {}>;
|
|
425
|
-
|
|
426
|
-
name: "
|
|
425
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
426
|
+
name: "uid";
|
|
427
427
|
tableName: "password_reset_tokens";
|
|
428
428
|
dataType: "string";
|
|
429
429
|
columnType: "PgUUID";
|
|
@@ -593,8 +593,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
593
593
|
identity: undefined;
|
|
594
594
|
generated: undefined;
|
|
595
595
|
}, {}, {}>;
|
|
596
|
-
|
|
597
|
-
name: "
|
|
596
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
597
|
+
name: "uid";
|
|
598
598
|
tableName: "user_identities";
|
|
599
599
|
dataType: "string";
|
|
600
600
|
columnType: "PgUUID";
|
|
@@ -723,8 +723,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
723
723
|
identity: undefined;
|
|
724
724
|
generated: undefined;
|
|
725
725
|
}, {}, {}>;
|
|
726
|
-
|
|
727
|
-
name: "
|
|
726
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
727
|
+
name: "uid";
|
|
728
728
|
tableName: "mfa_factors";
|
|
729
729
|
dataType: "string";
|
|
730
730
|
columnType: "PgUUID";
|
|
@@ -983,8 +983,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
983
983
|
identity: undefined;
|
|
984
984
|
generated: undefined;
|
|
985
985
|
}, {}, {}>;
|
|
986
|
-
|
|
987
|
-
name: "
|
|
986
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
987
|
+
name: "uid";
|
|
988
988
|
tableName: "recovery_codes";
|
|
989
989
|
dataType: "string";
|
|
990
990
|
columnType: "PgUUID";
|
|
@@ -1077,8 +1077,8 @@ export declare function createAuthSchema(usersSchemaName?: string): {
|
|
|
1077
1077
|
identity: undefined;
|
|
1078
1078
|
generated: undefined;
|
|
1079
1079
|
}, {}, {}>;
|
|
1080
|
-
|
|
1081
|
-
name: "
|
|
1080
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
1081
|
+
name: "uid";
|
|
1082
1082
|
tableName: "magic_link_tokens";
|
|
1083
1083
|
dataType: "string";
|
|
1084
1084
|
columnType: "PgUUID";
|
|
@@ -1456,8 +1456,8 @@ export declare const refreshTokens: import("drizzle-orm/pg-core").PgTableWithCol
|
|
|
1456
1456
|
identity: undefined;
|
|
1457
1457
|
generated: undefined;
|
|
1458
1458
|
}, {}, {}>;
|
|
1459
|
-
|
|
1460
|
-
name: "
|
|
1459
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
1460
|
+
name: "uid";
|
|
1461
1461
|
tableName: "refresh_tokens";
|
|
1462
1462
|
dataType: "string";
|
|
1463
1463
|
columnType: "PgUUID";
|
|
@@ -1588,8 +1588,8 @@ export declare const passwordResetTokens: import("drizzle-orm/pg-core").PgTableW
|
|
|
1588
1588
|
identity: undefined;
|
|
1589
1589
|
generated: undefined;
|
|
1590
1590
|
}, {}, {}>;
|
|
1591
|
-
|
|
1592
|
-
name: "
|
|
1591
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
1592
|
+
name: "uid";
|
|
1593
1593
|
tableName: "password_reset_tokens";
|
|
1594
1594
|
dataType: "string";
|
|
1595
1595
|
columnType: "PgUUID";
|
|
@@ -1759,8 +1759,8 @@ export declare const userIdentities: import("drizzle-orm/pg-core").PgTableWithCo
|
|
|
1759
1759
|
identity: undefined;
|
|
1760
1760
|
generated: undefined;
|
|
1761
1761
|
}, {}, {}>;
|
|
1762
|
-
|
|
1763
|
-
name: "
|
|
1762
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
1763
|
+
name: "uid";
|
|
1764
1764
|
tableName: "user_identities";
|
|
1765
1765
|
dataType: "string";
|
|
1766
1766
|
columnType: "PgUUID";
|
|
@@ -1889,8 +1889,8 @@ export declare const mfaFactors: import("drizzle-orm/pg-core").PgTableWithColumn
|
|
|
1889
1889
|
identity: undefined;
|
|
1890
1890
|
generated: undefined;
|
|
1891
1891
|
}, {}, {}>;
|
|
1892
|
-
|
|
1893
|
-
name: "
|
|
1892
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
1893
|
+
name: "uid";
|
|
1894
1894
|
tableName: "mfa_factors";
|
|
1895
1895
|
dataType: "string";
|
|
1896
1896
|
columnType: "PgUUID";
|
|
@@ -2149,8 +2149,8 @@ export declare const recoveryCodes: import("drizzle-orm/pg-core").PgTableWithCol
|
|
|
2149
2149
|
identity: undefined;
|
|
2150
2150
|
generated: undefined;
|
|
2151
2151
|
}, {}, {}>;
|
|
2152
|
-
|
|
2153
|
-
name: "
|
|
2152
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
2153
|
+
name: "uid";
|
|
2154
2154
|
tableName: "recovery_codes";
|
|
2155
2155
|
dataType: "string";
|
|
2156
2156
|
columnType: "PgUUID";
|
|
@@ -2243,8 +2243,8 @@ export declare const magicLinkTokens: import("drizzle-orm/pg-core").PgTableWithC
|
|
|
2243
2243
|
identity: undefined;
|
|
2244
2244
|
generated: undefined;
|
|
2245
2245
|
}, {}, {}>;
|
|
2246
|
-
|
|
2247
|
-
name: "
|
|
2246
|
+
uid: import("drizzle-orm/pg-core").PgColumn<{
|
|
2247
|
+
name: "uid";
|
|
2248
2248
|
tableName: "magic_link_tokens";
|
|
2249
2249
|
dataType: "string";
|
|
2250
2250
|
columnType: "PgUUID";
|
package/dist/schema/doctor.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { CollectionConfig, Property } from "@rebasepro/types";
|
|
|
2
2
|
export type IssueSeverity = "error" | "warning" | "info";
|
|
3
3
|
export interface DoctorIssue {
|
|
4
4
|
severity: IssueSeverity;
|
|
5
|
-
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale";
|
|
5
|
+
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale" | "sdk_not_generated";
|
|
6
6
|
table?: string;
|
|
7
7
|
column?: string;
|
|
8
8
|
expected?: string;
|
|
@@ -32,11 +32,6 @@ export interface TableMeta {
|
|
|
32
32
|
fks: ForeignKeyRow[];
|
|
33
33
|
}
|
|
34
34
|
export declare function singularize(word: string): string;
|
|
35
|
-
/**
|
|
36
|
-
* Convert a snake_case name to a human-readable Title Case label.
|
|
37
|
-
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
38
|
-
*/
|
|
39
|
-
export declare function humanize(snakeName: string): string;
|
|
40
35
|
/**
|
|
41
36
|
* Convert a snake_case table name to a camelCase + "Collection" variable name.
|
|
42
37
|
* e.g. "company_token" -> "companyTokenCollection"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naming helpers shared by the introspection modules. These live apart from
|
|
3
|
+
* `introspect-db-logic.ts` because the inference pass needs them too, and
|
|
4
|
+
* importing them from there would close a cycle back through this module.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Convert a snake_case name to a human-readable Title Case label.
|
|
8
|
+
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
9
|
+
*/
|
|
10
|
+
export declare function humanize(snakeName: string): string;
|
|
@@ -22,6 +22,18 @@ export interface PolicyRef {
|
|
|
22
22
|
roles: string[];
|
|
23
23
|
/** SELECT / INSERT / UPDATE / DELETE / ALL. */
|
|
24
24
|
command: string;
|
|
25
|
+
/** Whether a USING clause is present at all (not what it says). */
|
|
26
|
+
hasUsing: boolean;
|
|
27
|
+
/** Whether a WITH CHECK clause is present at all (not what it says). */
|
|
28
|
+
hasWithCheck: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* The live clause text, when read from `pg_policies`. Present only for live
|
|
31
|
+
* policies (the expected side is parsed from DDL and does not carry it).
|
|
32
|
+
* Used solely for the insecure-tautology scan, not for divergence — Postgres
|
|
33
|
+
* rewrites this text, so it is not safe to diff against expected.
|
|
34
|
+
*/
|
|
35
|
+
qual?: string | null;
|
|
36
|
+
withCheck?: string | null;
|
|
25
37
|
}
|
|
26
38
|
export interface PolicyDrift {
|
|
27
39
|
/** Described by the collections, absent from the database. */
|
|
@@ -34,6 +46,22 @@ export interface PolicyDrift {
|
|
|
34
46
|
actual: PolicyRef;
|
|
35
47
|
differences: string[];
|
|
36
48
|
}[];
|
|
49
|
+
/**
|
|
50
|
+
* A live policy whose expression is the known-permissive tautology
|
|
51
|
+
* `auth.uid() IS NOT NULL` — true for anonymous visitors too, because the
|
|
52
|
+
* user path coerces a blank id to the `'anonymous'` sentinel. This is what
|
|
53
|
+
* `policy.authenticated()` used to compile to, so a database pushed before
|
|
54
|
+
* that fix carries it, and neither the name, roles, command nor clause
|
|
55
|
+
* *presence* differs from the corrected policy — the only thing that changed
|
|
56
|
+
* is the expression text, which this checker otherwise (correctly) ignores.
|
|
57
|
+
* So it is the one drift that hides from every other check here.
|
|
58
|
+
*
|
|
59
|
+
* @see reason a sentence naming the clause and what to do.
|
|
60
|
+
*/
|
|
61
|
+
insecure: {
|
|
62
|
+
policy: PolicyRef;
|
|
63
|
+
reason: string;
|
|
64
|
+
}[];
|
|
37
65
|
}
|
|
38
66
|
export interface Queryable {
|
|
39
67
|
query<R>(text: string, values?: unknown[]): Promise<{
|
|
@@ -45,13 +73,50 @@ export declare function parseExpectedPolicies(ddl: string): PolicyRef[];
|
|
|
45
73
|
/**
|
|
46
74
|
* Diff expected against live.
|
|
47
75
|
*
|
|
48
|
-
* Compares names, roles and
|
|
49
|
-
* *
|
|
50
|
-
* `with_check` when storing them (parenthesising, casting,
|
|
51
|
-
* so text comparison reports drift that does not exist, and
|
|
52
|
-
*
|
|
76
|
+
* Compares names, roles, command, and whether each clause exists — all exact
|
|
77
|
+
* values. Policy expression *text* is deliberately not compared: Postgres
|
|
78
|
+
* rewrites `qual`/`with_check` when storing them (parenthesising, casting,
|
|
79
|
+
* schema-qualifying), so text comparison reports drift that does not exist, and
|
|
80
|
+
* a check that cries wolf gets ignored.
|
|
81
|
+
*
|
|
82
|
+
* Presence is not text, though. A NULL `qual` is not a rewrite of an
|
|
83
|
+
* expression, it is the absence of one, and absence has no false-positive risk:
|
|
84
|
+
* either the generator emitted a clause or it did not. That distinction is worth
|
|
85
|
+
* the extra comparison — a production database was found with a SELECT policy
|
|
86
|
+
* whose `qual` was NULL, matching on every field this checked and denying 100%
|
|
87
|
+
* of reads. The same blindness would hide a policy that fails open.
|
|
53
88
|
*/
|
|
54
89
|
export declare function checkPolicyDrift(client: Queryable, collections: CollectionConfig[]): Promise<PolicyDrift>;
|
|
90
|
+
/**
|
|
91
|
+
* Does this name look like one the generator produced for this table?
|
|
92
|
+
*
|
|
93
|
+
* Unnamed rules compile to `<table>_<op>_<sha1[0:7]>` (plus `_<idx>` when one
|
|
94
|
+
* rule spans several operations), and the hash covers the rule's semantics — so
|
|
95
|
+
* *editing* a rule renames its policy. The policy under the old name is left
|
|
96
|
+
* behind by `db push`, which only DROPs the names it is about to CREATE, and
|
|
97
|
+
* Postgres ORs PERMISSIVE policies together: a superseded `USING (true)` keeps
|
|
98
|
+
* granting everything no matter how tight its replacement is.
|
|
99
|
+
*
|
|
100
|
+
* Matching the shape is what makes dropping them safe. A hand-written policy
|
|
101
|
+
* would have to collide with a 7-hex digest to be mistaken for generated one;
|
|
102
|
+
* a policy named anything else is left alone and merely reported, because a
|
|
103
|
+
* custom name is indistinguishable from one someone wrote in SQL on purpose.
|
|
104
|
+
*/
|
|
105
|
+
export declare function isGeneratedPolicyName(name: string, table: string): boolean;
|
|
106
|
+
export interface OrphanCleanup {
|
|
107
|
+
/** Superseded generated policies that were dropped. */
|
|
108
|
+
dropped: PolicyRef[];
|
|
109
|
+
/** Orphans left in place because their names are not generator-shaped. */
|
|
110
|
+
kept: PolicyRef[];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Drop the policies an earlier push superseded but never removed.
|
|
114
|
+
*
|
|
115
|
+
* Only touches tables the collections describe — a table with no expected
|
|
116
|
+
* policy is not ours to reconcile, and scanning by schema alone would sweep up
|
|
117
|
+
* policies belonging to something else sharing the database.
|
|
118
|
+
*/
|
|
119
|
+
export declare function dropOrphanedPolicies(client: Queryable, drift: PolicyDrift, collections: CollectionConfig[]): Promise<OrphanCleanup>;
|
|
55
120
|
export declare const hasDrift: (d: PolicyDrift) => boolean;
|
|
56
121
|
/** Human-readable report; empty string when the database matches the config. */
|
|
57
122
|
export declare function formatPolicyDrift(drift: PolicyDrift): string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SQL } from "drizzle-orm";
|
|
2
|
+
import { SecurityRule } from "@rebasepro/types";
|
|
2
3
|
/**
|
|
3
4
|
* Unified RLS enforcement — the "user context vs server context" model.
|
|
4
5
|
*
|
|
@@ -50,7 +51,7 @@ export interface ConnectionPosture {
|
|
|
50
51
|
privileged: boolean;
|
|
51
52
|
}
|
|
52
53
|
export interface AuthContext {
|
|
53
|
-
|
|
54
|
+
uid: string;
|
|
54
55
|
/** Raw roles as carried on the user (strings or `{ id }` objects). */
|
|
55
56
|
roles: unknown[];
|
|
56
57
|
}
|
|
@@ -92,12 +93,36 @@ export declare function ensureAppRole(run: RawSqlRunner, schemas: string[]): Pro
|
|
|
92
93
|
* SECURITY: this function is only ever called on the **user** path (the server
|
|
93
94
|
* context uses the base/owner driver and never calls it). The default policies
|
|
94
95
|
* treat `auth.uid() IS NULL` as the trusted server context, and `auth.uid()`
|
|
95
|
-
* is `NULLIF(current_setting('app.
|
|
96
|
+
* is `NULLIF(current_setting('app.uid'), '')` — so an EMPTY user id would
|
|
96
97
|
* be read as NULL and silently escalate a user request to server privileges.
|
|
97
|
-
* Coerce empty/blank ids to
|
|
98
|
-
* than trusting every caller (e.g. realtime subscription auth) to do it.
|
|
98
|
+
* Coerce empty/blank ids to `ANONYMOUS_USER_ID` here, at the single chokepoint,
|
|
99
|
+
* rather than trusting every caller (e.g. realtime subscription auth) to do it.
|
|
100
|
+
* That sentinel is exported from `@rebasepro/types` because it leaks into rule
|
|
101
|
+
* semantics: it is why `auth.uid() IS NOT NULL` is true for anonymous requests.
|
|
99
102
|
*/
|
|
100
103
|
export declare function applyAuthContext(tx: SqlTx, auth: AuthContext, userRole?: string): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Warn about rules that read as "signed-in users only" but admit anonymous
|
|
106
|
+
* callers — `auth.uid() IS NOT NULL`, or a comparison against another
|
|
107
|
+
* platform's magic user id such as `'anon'`.
|
|
108
|
+
*
|
|
109
|
+
* The sibling of {@link validatePolicyPgRoles}, for the more dangerous spelling
|
|
110
|
+
* of the same habit. A foreign `pgRoles` value makes a policy unreachable and
|
|
111
|
+
* the table reads empty — loud, and that guard throws. These do the opposite:
|
|
112
|
+
* the rule compiles to a grant, and nothing looks wrong until the data is
|
|
113
|
+
* already public.
|
|
114
|
+
*
|
|
115
|
+
* Warns rather than throws. Unlike an unreachable `pgRoles`, these rules are
|
|
116
|
+
* serving traffic today: refusing to boot would take an app offline to report a
|
|
117
|
+
* problem it already has, and on the read path it would take it offline
|
|
118
|
+
* *because* its data was exposed. Rewriting the author's SQL is not an option
|
|
119
|
+
* either — this is the escape hatch whose whole promise is that it means what it
|
|
120
|
+
* says. So: say so, loudly, and leave the rule alone.
|
|
121
|
+
*/
|
|
122
|
+
export declare function warnOnAnonymousGrants(collections: {
|
|
123
|
+
slug?: string;
|
|
124
|
+
securityRules?: readonly SecurityRule[];
|
|
125
|
+
}[]): void;
|
|
101
126
|
/**
|
|
102
127
|
* Reject `pgRoles` that this server can never satisfy.
|
|
103
128
|
*
|
|
@@ -40,44 +40,22 @@ export declare class FetchService {
|
|
|
40
40
|
* the target relation so actual row data is returned.
|
|
41
41
|
*/
|
|
42
42
|
private buildWithConfig;
|
|
43
|
-
/**
|
|
44
|
-
* Detect if a many-to-many relation uses a junction table in the Drizzle schema.
|
|
45
|
-
*/
|
|
46
|
-
private isJunctionRelation;
|
|
47
43
|
/**
|
|
48
44
|
* Get the Drizzle relation name on the junction table that points to the actual target row.
|
|
49
45
|
* For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
|
|
50
46
|
*/
|
|
51
47
|
private getJunctionTargetRelationName;
|
|
52
|
-
/**
|
|
53
|
-
* Convert a db.query result row (with nested relation objects) to a flat row.
|
|
54
|
-
* Handles:
|
|
55
|
-
* - Placing `id` at the top level as a string
|
|
56
|
-
* - Type normalization (dates, numbers, NaN) via normalizeDbValues
|
|
57
|
-
* - Converting nested relation objects to { id, path, __type: "relation" } for CMS
|
|
58
|
-
* - Flattening junction-table many-to-many results
|
|
59
|
-
*/
|
|
60
|
-
private drizzleResultToRow;
|
|
61
48
|
/**
|
|
62
49
|
* Post-fetch joinPath relations for a single flat row.
|
|
63
50
|
* joinPath relations cannot be expressed via Drizzle's `with` config,
|
|
64
51
|
* so they must be loaded separately after the primary query.
|
|
65
52
|
*/
|
|
66
53
|
private resolveJoinPathRelations;
|
|
67
|
-
/**
|
|
68
|
-
* Post-fetch joinPath relations for a batch of flat rows.
|
|
69
|
-
* Uses batch fetching to avoid N+1 queries for list views.
|
|
70
|
-
*/
|
|
71
|
-
private resolveJoinPathRelationsBatch;
|
|
72
54
|
/**
|
|
73
55
|
* Resolves joinPath relations for raw REST rows and directly injects them.
|
|
74
56
|
* Uses RelationService to query the database and maps results back to the flattened objects.
|
|
75
57
|
*/
|
|
76
58
|
private resolveJoinPathRelationsBatchRest;
|
|
77
|
-
/**
|
|
78
|
-
* Convert a db.query result row to a flat REST-style object with populated relations.
|
|
79
|
-
*/
|
|
80
|
-
private drizzleResultToRestRow;
|
|
81
59
|
/**
|
|
82
60
|
* Build db.query-compatible options from standard fetch options.
|
|
83
61
|
* Handles filter, search, orderBy, limit, and cursor-based pagination.
|
|
@@ -108,8 +86,10 @@ export declare class FetchService {
|
|
|
108
86
|
}): Promise<Record<string, unknown>[]>;
|
|
109
87
|
/**
|
|
110
88
|
* Fallback path used when db.query is unavailable.
|
|
111
|
-
*
|
|
112
|
-
*
|
|
89
|
+
*
|
|
90
|
+
* The primary path runs the results through `toCmsRow`, which maps
|
|
91
|
+
* relations from what drizzle already nested — no query per row. This one
|
|
92
|
+
* has no nesting to read, so it resolves relations itself, in batches.
|
|
113
93
|
*
|
|
114
94
|
* Process raw database results into flat rows with relations.
|
|
115
95
|
*/
|
|
@@ -12,6 +12,24 @@ export declare class PersistService {
|
|
|
12
12
|
private relationService;
|
|
13
13
|
private fetchService;
|
|
14
14
|
constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
|
|
15
|
+
/**
|
|
16
|
+
* Explain a write that matched no rows.
|
|
17
|
+
*
|
|
18
|
+
* Row-level security filters UPDATE and DELETE through the policy's USING
|
|
19
|
+
* clause instead of raising: a denied write is reported by Postgres exactly
|
|
20
|
+
* like a successful one that happened to match nothing. Left unchecked, a
|
|
21
|
+
* caller cannot tell "denied" from "done" — the write returns 200/204 and
|
|
22
|
+
* the row is untouched.
|
|
23
|
+
*
|
|
24
|
+
* Re-reading the target over the *same* RLS-scoped handle separates the two
|
|
25
|
+
* cases. A visible row means the policy rejected the write (403); an
|
|
26
|
+
* invisible one means there is nothing there to write for this caller (404,
|
|
27
|
+
* matching what a GET would say). The re-read is bound by the caller's own
|
|
28
|
+
* policies, so it discloses nothing a plain read wouldn't.
|
|
29
|
+
*
|
|
30
|
+
* Only reached when zero rows matched, so the happy path pays nothing.
|
|
31
|
+
*/
|
|
32
|
+
private explainZeroRowWrite;
|
|
15
33
|
/**
|
|
16
34
|
* Delete an row by ID
|
|
17
35
|
*/
|
|
@@ -22,8 +40,16 @@ export declare class PersistService {
|
|
|
22
40
|
deleteAll(collectionPath: string, _databaseId?: string): Promise<void>;
|
|
23
41
|
/**
|
|
24
42
|
* Save an row (create or update)
|
|
43
|
+
*
|
|
44
|
+
* With `options.upsert`, the row is written with INSERT ... ON CONFLICT DO
|
|
45
|
+
* UPDATE against the primary key rather than a plain UPDATE. That is one
|
|
46
|
+
* statement, so it cannot lose a race the way a read-then-write can, and it
|
|
47
|
+
* does not care whether the row already exists — which is what a re-runnable
|
|
48
|
+
* import needs.
|
|
25
49
|
*/
|
|
26
|
-
save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string
|
|
50
|
+
save<M extends Record<string, unknown>>(collectionPath: string, values: Partial<M>, id?: string | number, databaseId?: string, options?: {
|
|
51
|
+
upsert?: boolean;
|
|
52
|
+
}): Promise<Record<string, unknown>>;
|
|
27
53
|
/**
|
|
28
54
|
* Get the RelationService instance for external use
|
|
29
55
|
*/
|
|
@@ -20,6 +20,40 @@ export declare class RelationService {
|
|
|
20
20
|
private db;
|
|
21
21
|
private registry;
|
|
22
22
|
constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
|
|
23
|
+
/**
|
|
24
|
+
* One target row, as the {@link RelatedRow} everything here returns.
|
|
25
|
+
*
|
|
26
|
+
* Eight sites built this by hand, which is how the address came to be the
|
|
27
|
+
* target's first key column in all eight — one edit, eight places to miss.
|
|
28
|
+
*
|
|
29
|
+
* `resolveNested` is the one thing they did not agree on, and the
|
|
30
|
+
* disagreement was invisible: the single-parent fetches pass `db` and
|
|
31
|
+
* `registry` to `parseDataFromServer`, so the target's *own* relations get
|
|
32
|
+
* resolved too, while the batch paths deliberately do not — a query per
|
|
33
|
+
* target row is the N+1 the batching exists to avoid. Naming the parameter
|
|
34
|
+
* makes that a decision rather than a difference between two call sites
|
|
35
|
+
* nobody was comparing.
|
|
36
|
+
*/
|
|
37
|
+
private toRelatedRow;
|
|
38
|
+
/**
|
|
39
|
+
* A WHERE matching any of `parentIds`, by the whole key.
|
|
40
|
+
*
|
|
41
|
+
* A single key is an `IN (…)`. A composite one cannot be: matching
|
|
42
|
+
* `tenant_id IN (1, 1)` collects every row of tenant 1, so two parents that
|
|
43
|
+
* share their first column each receive the other's relations. It becomes
|
|
44
|
+
* an OR of ANDs — one exact address per parent — which Postgres indexes the
|
|
45
|
+
* same way it would a multi-column key lookup.
|
|
46
|
+
*/
|
|
47
|
+
private parentKeyCondition;
|
|
48
|
+
/**
|
|
49
|
+
* Reject a relation that cannot express a composite-keyed parent.
|
|
50
|
+
*
|
|
51
|
+
* `localKey` and `foreignKeyOnTarget` are single column names: one column
|
|
52
|
+
* cannot reference a two-column key, so such a relation has no correct
|
|
53
|
+
* reading. Left alone it would silently match on the first key column and
|
|
54
|
+
* hand a tenant's rows to its neighbour — say so instead.
|
|
55
|
+
*/
|
|
56
|
+
private assertSingleKeyAddressable;
|
|
23
57
|
/**
|
|
24
58
|
* Fetch rows related to a parent row through a specific relation
|
|
25
59
|
*/
|
|
@@ -72,7 +106,6 @@ export declare class RelationService {
|
|
|
72
106
|
relationKey: string;
|
|
73
107
|
relation: Relation;
|
|
74
108
|
newValue: unknown;
|
|
75
|
-
currentId?: string | number;
|
|
76
109
|
}>): Promise<void>;
|
|
77
110
|
/**
|
|
78
111
|
* Handle inverse relations with joinPath
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered, replayable per-channel message history.
|
|
3
|
+
*
|
|
4
|
+
* Broadcast on its own is fire-and-forget to whoever is connected at the
|
|
5
|
+
* instant it is sent: fine for presence and for "someone saved" notifications,
|
|
6
|
+
* not enough for op-based collaborative editing, where a client that blinks
|
|
7
|
+
* out for two seconds has to resync a whole document rather than catch up on
|
|
8
|
+
* the four operations it missed. This adds the missing half — every retained
|
|
9
|
+
* broadcast gets a per-channel sequence number, and a client can ask for
|
|
10
|
+
* everything after the last one it saw.
|
|
11
|
+
*
|
|
12
|
+
* Three decisions worth stating, because each rules out a simpler-looking one:
|
|
13
|
+
*
|
|
14
|
+
* - **Retention is server-side and opt-in.** A channel is created by whoever
|
|
15
|
+
* names it, so a client-supplied history depth would let any visitor commit
|
|
16
|
+
* the backend to unbounded storage. And presence channels — the common case
|
|
17
|
+
* — must not pay for this: with no rules configured nothing is written, no
|
|
18
|
+
* table is created, and `broadcast` runs exactly the code it ran before.
|
|
19
|
+
*
|
|
20
|
+
* - **Sequence numbers come from the database, not from a counter in this
|
|
21
|
+
* process.** They have to survive a restart and be shared across instances;
|
|
22
|
+
* an in-memory counter would restart at 1 after a deploy and hand a
|
|
23
|
+
* reconnecting client a replay from the wrong era, silently.
|
|
24
|
+
*
|
|
25
|
+
* - **The cursor row outlives the messages it numbered.** Pruning is what
|
|
26
|
+
* makes retention affordable, but pruning the cursor along with the messages
|
|
27
|
+
* would restart the sequence and make `sinceSeq` mean something different
|
|
28
|
+
* before and after — the worst kind of bug, because replay would still
|
|
29
|
+
* return rows and they would look plausible. Cursors are tiny and are kept
|
|
30
|
+
* forever; see {@link prune}, which touches only `channel_messages`.
|
|
31
|
+
*/
|
|
32
|
+
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
33
|
+
import type { ChannelHistoryEntry, ChannelRetentionRule } from "@rebasepro/types";
|
|
34
|
+
/**
|
|
35
|
+
* Parse a retention TTL into milliseconds.
|
|
36
|
+
*
|
|
37
|
+
* Accepts a raw millisecond count or a short duration string (`"30s"`, `"15m"`,
|
|
38
|
+
* `"24h"`, `"7d"`). Returns undefined for anything unparseable, which the
|
|
39
|
+
* caller treats as "no TTL" — a misspelt duration must not silently become an
|
|
40
|
+
* aggressive one.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseTtlMs(ttl: number | string | undefined): number | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Whether `channel` is covered by `rule`.
|
|
45
|
+
*
|
|
46
|
+
* Exact match, or a trailing `*` acting as a prefix. Not a general glob: this
|
|
47
|
+
* decides what reaches disk, and a pattern language whose reach is not obvious
|
|
48
|
+
* at a glance is the wrong tool for that job.
|
|
49
|
+
*/
|
|
50
|
+
export declare function channelMatchesRule(channel: string, rule: ChannelRetentionRule): boolean;
|
|
51
|
+
/** A rule with its TTL already resolved to milliseconds. */
|
|
52
|
+
export interface ResolvedRetention {
|
|
53
|
+
limit?: number;
|
|
54
|
+
ttlMs?: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Persistence and replay for retained channels.
|
|
58
|
+
*
|
|
59
|
+
* Inert unless constructed with at least one rule: {@link enabled} is false,
|
|
60
|
+
* {@link ensureTables} does nothing, and {@link retentionFor} answers undefined
|
|
61
|
+
* for every channel, so the realtime service never reaches the SQL below.
|
|
62
|
+
*/
|
|
63
|
+
export declare class ChannelHistoryStore {
|
|
64
|
+
private db;
|
|
65
|
+
private rules;
|
|
66
|
+
/** Resolved rule per channel name, so the match runs once per channel. */
|
|
67
|
+
private resolved;
|
|
68
|
+
/** Channel → timestamp of its last prune, for {@link PRUNE_THROTTLE_MS}. */
|
|
69
|
+
private lastPruned;
|
|
70
|
+
private tablesReady;
|
|
71
|
+
constructor(db: NodePgDatabase<Record<string, unknown>>, rules?: ChannelRetentionRule[]);
|
|
72
|
+
/** Whether any channel retains anything at all. */
|
|
73
|
+
get enabled(): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The retention that applies to `channel`, or undefined when none does.
|
|
76
|
+
*
|
|
77
|
+
* First matching rule wins, so callers order them most-specific first.
|
|
78
|
+
*/
|
|
79
|
+
retentionFor(channel: string): ResolvedRetention | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* Create the history tables. Idempotent, and a no-op when no rule is set —
|
|
82
|
+
* a deployment that never retains anything gets no schema for it.
|
|
83
|
+
*/
|
|
84
|
+
ensureTables(): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Append a broadcast and return the sequence number it was given.
|
|
87
|
+
*
|
|
88
|
+
* The sequence is allocated by the same statement that stores the message,
|
|
89
|
+
* so a crash between the two is not a possibility. `ON CONFLICT DO UPDATE`
|
|
90
|
+
* takes a row lock on the channel's cursor, which is what makes concurrent
|
|
91
|
+
* broadcasts to one channel line up in a single order — and what keeps
|
|
92
|
+
* different channels from contending with each other at all.
|
|
93
|
+
*/
|
|
94
|
+
append(channel: string, event: string, payload: unknown, senderId?: string): Promise<{
|
|
95
|
+
seq: number;
|
|
96
|
+
at: string;
|
|
97
|
+
}>;
|
|
98
|
+
/**
|
|
99
|
+
* Everything retained for `channel` after `sinceSeq`, oldest first.
|
|
100
|
+
*
|
|
101
|
+
* `latestSeq` is reported whether or not the messages were capped, so a
|
|
102
|
+
* client that is further behind than one page can tell.
|
|
103
|
+
*/
|
|
104
|
+
replay(channel: string, sinceSeq?: number, limit?: number): Promise<{
|
|
105
|
+
messages: ChannelHistoryEntry[];
|
|
106
|
+
latestSeq: number;
|
|
107
|
+
}>;
|
|
108
|
+
/**
|
|
109
|
+
* Enforce a channel's retention bounds.
|
|
110
|
+
*
|
|
111
|
+
* Throttled per channel, so a burst of operations prunes once rather than
|
|
112
|
+
* once per message — the cost then tracks elapsed time instead of write
|
|
113
|
+
* volume, which is what makes retention affordable on a hot channel.
|
|
114
|
+
*/
|
|
115
|
+
prune(channel: string, retention: ResolvedRetention): Promise<number>;
|
|
116
|
+
/** Forget throttle and match caches. Called on shutdown. */
|
|
117
|
+
clear(): void;
|
|
118
|
+
}
|