@rebasepro/server-postgres 0.9.1-canary.73476f2 → 0.9.1-canary.7ba0e49

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 (43) hide show
  1. package/dist/PostgresBackendDriver.d.ts +25 -2
  2. package/dist/PostgresBootstrapper.d.ts +10 -0
  3. package/dist/auth/services.d.ts +16 -0
  4. package/dist/collections/buildRegistry.d.ts +27 -0
  5. package/dist/connection.d.ts +21 -0
  6. package/dist/data-transformer.d.ts +9 -2
  7. package/dist/index.es.js +1445 -571
  8. package/dist/index.es.js.map +1 -1
  9. package/dist/schema/doctor.d.ts +1 -1
  10. package/dist/services/FetchService.d.ts +4 -24
  11. package/dist/services/PersistService.d.ts +27 -1
  12. package/dist/services/RelationService.d.ts +34 -1
  13. package/dist/services/collection-helpers.d.ts +79 -14
  14. package/dist/services/dataService.d.ts +3 -1
  15. package/dist/services/index.d.ts +1 -1
  16. package/dist/services/realtimeService.d.ts +7 -0
  17. package/dist/services/row-pipeline.d.ts +63 -0
  18. package/package.json +11 -9
  19. package/src/PostgresBackendDriver.ts +127 -13
  20. package/src/PostgresBootstrapper.ts +62 -25
  21. package/src/auth/ensure-tables.ts +73 -11
  22. package/src/auth/services.ts +49 -19
  23. package/src/collections/buildRegistry.ts +59 -0
  24. package/src/connection.ts +61 -1
  25. package/src/data-transformer.ts +11 -9
  26. package/src/databasePoolManager.ts +2 -0
  27. package/src/schema/doctor.ts +45 -20
  28. package/src/schema/generate-drizzle-schema-logic.ts +24 -29
  29. package/src/schema/generate-postgres-ddl-logic.ts +76 -28
  30. package/src/schema/introspect-db.ts +19 -2
  31. package/src/services/BranchService.ts +42 -10
  32. package/src/services/FetchService.ts +65 -270
  33. package/src/services/PersistService.ts +130 -14
  34. package/src/services/RelationService.ts +153 -94
  35. package/src/services/collection-helpers.ts +164 -47
  36. package/src/services/dataService.ts +3 -2
  37. package/src/services/index.ts +1 -0
  38. package/src/services/realtimeService.ts +40 -19
  39. package/src/services/row-pipeline.ts +215 -0
  40. package/src/utils/drizzle-conditions.ts +13 -0
  41. package/src/websocket.ts +4 -1
  42. package/dist/schema/auth-default-policies.d.ts +0 -10
  43. package/src/schema/auth-default-policies.ts +0 -132
@@ -1,132 +0,0 @@
1
- import { CollectionConfig, SecurityRule, SecurityOperation, AuthCollectionConfig, PolicyExpression, isPostgresCollectionConfig, policy } from "@rebasepro/types";
2
- import { getTableName } from "@rebasepro/common";
3
-
4
- /**
5
- * Default RLS policies injected by the schema generator.
6
- *
7
- * Rebase's enforcement model is unified: authenticated (user-context) requests
8
- * run under the restricted `rebase_user` role, so Postgres RLS binds *every*
9
- * statement — reads and writes. A collection's `securityRules` are the whole
10
- * authorization model. The server context (auth flows, migrations,
11
- * `dataAsAdmin`) runs as the owner and bypasses RLS.
12
- *
13
- * Because RLS default-denies, every collection is **locked by default**: with
14
- * no rules, only the server context and admins can touch it. The generator
15
- * injects that safe baseline:
16
- *
17
- * **For every collection**
18
- * 1. A permissive **server-or-admin SELECT** grant.
19
- * 2. A permissive **server-or-admin write** grant (insert/update/delete).
20
- *
21
- * Author `securityRules` are permissive and OR together, so explicit rules only
22
- * *broaden* access from this locked baseline (e.g. "users read/write their own
23
- * rows").
24
- *
25
- * **For auth collections additionally**
26
- * 3. A permissive **self SELECT** grant (`id = auth.uid()`), so users can read
27
- * their own row (profile, session bootstrap) without every app re-declaring
28
- * it.
29
- * 4. A **restrictive** admin write gate. Restrictive policies are AND'd with
30
- * every other policy, so a write is rejected unless the caller is an admin
31
- * (or the server context) — even if the author also wrote a permissive rule
32
- * such as "a user may edit their own row". Without this, a permissive owner
33
- * rule would let a user change their own `roles`.
34
- *
35
- * The server context is recognised as `auth.uid() IS NULL` (`policy.serverContext()`)
36
- * — the built-in flows that run without a user (signup, migrations) set no user
37
- * GUC — which also lets the owner connection satisfy these policies even under
38
- * FORCE RLS. A *user* request never reaches that state: an anonymous one carries
39
- * `ANONYMOUS_USER_ID`, precisely so it cannot pass for the server here.
40
- *
41
- * Opt out with `disableDefaultPolicies: true` to take full responsibility for
42
- * the collection's RLS.
43
- */
44
- // Expressed structurally (not as raw SQL) so the admin UI can evaluate it
45
- // exactly — the framework's most security-critical policies must be reflected
46
- // precisely, not left as un-evaluable raw clauses. Compiles to
47
- // `auth.uid() IS NULL OR (string_to_array(auth.roles(), ',') && ARRAY['admin'])`.
48
- //
49
- // `serverContext()`, emphatically not `not(authenticated())`: the server arm of
50
- // this grant must match the server context and nothing else. Anonymous visitors
51
- // are not signed in either, so a negated `authenticated()` would hand them the
52
- // server-or-admin grant on every collection's default policy.
53
- const SERVER_OR_ADMIN_EXPR: PolicyExpression = policy.or(
54
- policy.serverContext(),
55
- policy.rolesOverlap(["admin"])
56
- );
57
-
58
- /** Write operations that must be admin-gated by default on auth collections. */
59
- const DEFAULT_GUARDED_OPS: SecurityOperation[] = ["insert", "update", "delete"];
60
-
61
- /** Whether a collection is flagged as an authentication collection. */
62
- function isAuthCollection(collection: CollectionConfig): boolean {
63
- const auth = collection.auth;
64
- return auth === true || (typeof auth === "object" && (auth as AuthCollectionConfig)?.enabled === true);
65
- }
66
-
67
- /** The property marked as the row id (falls back to `id`). */
68
- function getIdPropertyName(collection: CollectionConfig): string {
69
- for (const [name, prop] of Object.entries(collection.properties ?? {})) {
70
- if (prop && typeof prop === "object" && "isId" in prop && (prop as { isId?: unknown }).isId) {
71
- return name;
72
- }
73
- }
74
- return "id";
75
- }
76
-
77
- /**
78
- * Returns the security rules that should be applied to a collection: the
79
- * author's explicit `securityRules` plus the framework defaults described in
80
- * the module doc (baseline server/admin read for all collections; self-read
81
- * and the admin write gate for auth collections).
82
- *
83
- * Collections that opt out via `disableDefaultPolicies` are returned unchanged.
84
- */
85
- export function getEffectiveSecurityRules(collection: CollectionConfig): SecurityRule[] {
86
- const explicit = [...((isPostgresCollectionConfig(collection) ? collection.securityRules : undefined) ?? [])];
87
-
88
- if (collection.disableDefaultPolicies) {
89
- return explicit;
90
- }
91
-
92
- const tableName = getTableName(collection);
93
- const injected: SecurityRule[] = [];
94
-
95
- // Baseline read + write: the server context and admins can always operate.
96
- // RLS default-denies under the user role, so without these a rule-less
97
- // collection would be locked to everyone — including the admin studio.
98
- // Author rules are permissive and broaden access from here.
99
- injected.push({
100
- name: `${tableName}_default_admin_read`,
101
- operations: ["select"],
102
- condition: SERVER_OR_ADMIN_EXPR
103
- });
104
- injected.push({
105
- name: `${tableName}_default_admin_write`,
106
- operations: [...DEFAULT_GUARDED_OPS],
107
- condition: SERVER_OR_ADMIN_EXPR,
108
- check: SERVER_OR_ADMIN_EXPR
109
- });
110
-
111
- if (isAuthCollection(collection)) {
112
- // Self-read: a user can always read their own row.
113
- injected.push({
114
- name: `${tableName}_default_self_read`,
115
- operations: ["select"],
116
- condition: policy.compare(policy.field(getIdPropertyName(collection)), "eq", policy.authUid())
117
- });
118
-
119
- // Restrictive gate: AND'd with all other policies, so no permissive rule
120
- // (e.g. an owner "edit your own row" rule) can let a non-admin change
121
- // privileged columns like `roles`.
122
- injected.push({
123
- name: `${tableName}_require_admin_write`,
124
- mode: "restrictive",
125
- operations: [...DEFAULT_GUARDED_OPS],
126
- condition: SERVER_OR_ADMIN_EXPR,
127
- check: SERVER_OR_ADMIN_EXPR
128
- });
129
- }
130
-
131
- return [...explicit, ...injected];
132
- }