@rebasepro/server-postgres 0.0.1-canary.fc811d7 → 0.9.1-canary.0de22e0

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 (66) hide show
  1. package/README.md +21 -0
  2. package/dist/PostgresBackendDriver.d.ts +43 -2
  3. package/dist/PostgresBootstrapper.d.ts +17 -1
  4. package/dist/auth/services.d.ts +16 -0
  5. package/dist/collections/buildRegistry.d.ts +27 -0
  6. package/dist/connection.d.ts +21 -0
  7. package/dist/data-transformer.d.ts +9 -2
  8. package/dist/index.es.js +2502 -2624
  9. package/dist/index.es.js.map +1 -1
  10. package/dist/module-dir.d.ts +1 -0
  11. package/dist/schema/doctor.d.ts +1 -1
  12. package/dist/schema/introspect-db-logic.d.ts +0 -5
  13. package/dist/schema/introspect-db-naming.d.ts +10 -0
  14. package/dist/security/policy-drift.d.ts +46 -5
  15. package/dist/security/rls-enforcement.d.ts +28 -3
  16. package/dist/services/FetchService.d.ts +4 -24
  17. package/dist/services/PersistService.d.ts +27 -1
  18. package/dist/services/RelationService.d.ts +34 -1
  19. package/dist/services/channel-history.d.ts +118 -0
  20. package/dist/services/collection-helpers.d.ts +79 -14
  21. package/dist/services/dataService.d.ts +3 -1
  22. package/dist/services/index.d.ts +1 -1
  23. package/dist/services/realtimeService.d.ts +76 -2
  24. package/dist/services/row-pipeline.d.ts +63 -0
  25. package/package.json +15 -40
  26. package/src/PostgresBackendDriver.ts +183 -18
  27. package/src/PostgresBootstrapper.ts +86 -27
  28. package/src/auth/ensure-tables.ts +73 -11
  29. package/src/auth/services.ts +49 -19
  30. package/src/cli-helpers.ts +2 -20
  31. package/src/cli.ts +60 -0
  32. package/src/collections/buildRegistry.ts +59 -0
  33. package/src/connection.ts +61 -1
  34. package/src/data-transformer.ts +11 -9
  35. package/src/databasePoolManager.ts +2 -0
  36. package/src/module-dir.ts +7 -0
  37. package/src/schema/doctor-cli.ts +5 -1
  38. package/src/schema/doctor.ts +45 -20
  39. package/src/schema/generate-drizzle-schema-logic.ts +24 -29
  40. package/src/schema/generate-postgres-ddl-logic.ts +76 -28
  41. package/src/schema/introspect-db-inference.ts +1 -1
  42. package/src/schema/introspect-db-logic.ts +1 -10
  43. package/src/schema/introspect-db-naming.ts +15 -0
  44. package/src/schema/introspect-db.ts +19 -2
  45. package/src/schema/introspect-runtime.ts +1 -1
  46. package/src/security/anonymous-grants.test.ts +71 -0
  47. package/src/security/policy-drift.test.ts +153 -14
  48. package/src/security/policy-drift.ts +128 -10
  49. package/src/security/rls-enforcement.ts +67 -6
  50. package/src/services/BranchService.ts +42 -10
  51. package/src/services/FetchService.ts +65 -270
  52. package/src/services/PersistService.ts +130 -14
  53. package/src/services/RelationService.ts +153 -94
  54. package/src/services/channel-history.ts +343 -0
  55. package/src/services/collection-helpers.ts +164 -47
  56. package/src/services/dataService.ts +3 -2
  57. package/src/services/index.ts +1 -0
  58. package/src/services/realtimeService.ts +237 -28
  59. package/src/services/row-pipeline.ts +239 -0
  60. package/src/utils/drizzle-conditions.ts +13 -0
  61. package/src/websocket.ts +34 -12
  62. package/dist/chunk-DSJWtz9O.js +0 -40
  63. package/dist/schema/auth-default-policies.d.ts +0 -10
  64. package/dist/src-Eh-CZosp.js +0 -595
  65. package/dist/src-Eh-CZosp.js.map +0 -1
  66. package/src/schema/auth-default-policies.ts +0 -125
package/src/websocket.ts CHANGED
@@ -27,7 +27,7 @@ interface WsAuthConfig {
27
27
  * Normalized user identity for WebSocket sessions.
28
28
  */
29
29
  interface WsUserIdentity {
30
- userId: string;
30
+ uid: string;
31
31
  roles: string[];
32
32
  isAdmin: boolean;
33
33
  }
@@ -183,7 +183,7 @@ code } }
183
183
 
184
184
  if (adapterUser) {
185
185
  verifiedUser = {
186
- userId: adapterUser.uid,
186
+ uid: adapterUser.uid,
187
187
  roles: adapterUser.roles,
188
188
  isAdmin: adapterUser.isAdmin
189
189
  };
@@ -195,13 +195,13 @@ code } }
195
195
  // Service key: a static secret, not a JWT. Checked
196
196
  // before verification, mirroring the HTTP middleware —
197
197
  // verifying it as a JWT can only ever fail.
198
- verifiedUser = { userId: "service", roles: ["admin"], isAdmin: true };
198
+ verifiedUser = { uid: "service", roles: ["admin"], isAdmin: true };
199
199
  } else {
200
200
  // Standard JWT path
201
201
  const jwtPayload = extractUserFromToken(token);
202
202
  if (jwtPayload) {
203
203
  verifiedUser = {
204
- userId: jwtPayload.userId,
204
+ uid: jwtPayload.uid,
205
205
  roles: jwtPayload.roles ?? [],
206
206
  isAdmin: (jwtPayload.roles ?? []).some((r: string) => r === "admin")
207
207
  };
@@ -218,10 +218,10 @@ code } }
218
218
  ws.send(JSON.stringify({
219
219
  type: "AUTH_SUCCESS",
220
220
  requestId,
221
- payload: { userId: verifiedUser.userId,
221
+ payload: { uid: verifiedUser.uid,
222
222
  roles: verifiedUser.roles }
223
223
  }));
224
- wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.userId}`);
224
+ wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.uid}`);
225
225
  } else {
226
226
  wsDebug(`[WS] replying AUTH_ERROR for requestId ${requestId} (invalid token)`);
227
227
  sendError("AUTH_ERROR", "INVALID_TOKEN", "Invalid or expired token");
@@ -272,7 +272,7 @@ roles: verifiedUser.roles }
272
272
  try {
273
273
  const userForAuth: User = session?.user
274
274
  ? {
275
- uid: session.user.userId,
275
+ uid: session.user.uid,
276
276
  displayName: null,
277
277
  email: null,
278
278
  photoURL: null,
@@ -421,7 +421,7 @@ colors: true }));
421
421
  sql: typeof sql === "string" ? sql.substring(0, 500) : sql,
422
422
  options,
423
423
  resultRows: Array.isArray(result) ? result.length : "unknown",
424
- userId: auditSession?.user?.userId ?? "unknown",
424
+ uid: auditSession?.user?.uid ?? "unknown",
425
425
  roles: auditSession?.user?.roles ?? [],
426
426
  isAdmin: auditSession?.user?.isAdmin ?? false,
427
427
  }));
@@ -476,6 +476,24 @@ colors: true }));
476
476
  }
477
477
  break;
478
478
 
479
+ case "FETCH_APPLICATION_ROLES": {
480
+ wsDebug("👤 [WebSocket Server] Processing FETCH_APPLICATION_ROLES request");
481
+ const delegate = await getScopedDelegate();
482
+ const admin = delegate.admin;
483
+ let roles: string[] = [];
484
+ if (isSQLAdmin(admin) && admin.fetchApplicationRoles) {
485
+ roles = await admin.fetchApplicationRoles();
486
+ }
487
+ wsDebug(`👤 [WebSocket Server] Fetched ${roles.length} application roles.`);
488
+ const response = {
489
+ type: "FETCH_APPLICATION_ROLES_SUCCESS",
490
+ payload: { roles },
491
+ requestId
492
+ };
493
+ ws.send(JSON.stringify(response));
494
+ }
495
+ break;
496
+
479
497
  case "FETCH_CURRENT_DATABASE": {
480
498
  wsDebug("📚 [WebSocket Server] Processing FETCH_CURRENT_DATABASE request");
481
499
  const delegate = await getScopedDelegate();
@@ -594,14 +612,15 @@ colors: true }));
594
612
  case "broadcast":
595
613
  case "presence_track":
596
614
  case "presence_untrack":
597
- case "presence_state": {
615
+ case "presence_state":
616
+ case "channel_history": {
598
617
  wsDebug("🔄 [WebSocket Server] Routing realtime message to RealtimeService:", type);
599
618
  // Attach auth context from the WS session so RLS-aware refetches work
600
619
  const session = clientSessions.get(clientId);
601
620
  const authContext = session?.user
602
- ? { userId: session.user.userId,
621
+ ? { uid: session.user.uid,
603
622
  roles: session.user.roles ?? [] }
604
- : { userId: "anon",
623
+ : { uid: "anon",
605
624
  roles: ["anon"] };
606
625
  // Let RealtimeService handle these messages
607
626
  await realtimeService.handleClientMessage(clientId, {
@@ -620,9 +639,12 @@ roles: ["anon"] };
620
639
  if (error instanceof Error) {
621
640
  logger.error("Stack trace", { detail: error.stack });
622
641
  }
642
+ // Unwrap the cause chain: a Drizzle failure reports itself as
643
+ // "Failed query: <sql> params:", which tells the user nothing and
644
+ // echoes the statement back at them. The reason is in the cause.
623
645
  const errorMessage = process.env.NODE_ENV === "production"
624
646
  ? "An unexpected error occurred"
625
- : (error instanceof Error ? error.message : "An unexpected error occurred");
647
+ : extractErrorMessage(error);
626
648
  const errorResponse = {
627
649
  type: "ERROR",
628
650
  requestId,
@@ -1,40 +0,0 @@
1
- import { createRequire as __createRequire } from "module";
2
- import "process";
3
- const require = __createRequire(import.meta.url);
4
- //#region \0rolldown/runtime.js
5
- var __create = Object.create;
6
- var __defProp = Object.defineProperty;
7
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
8
- var __getOwnPropNames = Object.getOwnPropertyNames;
9
- var __getProtoOf = Object.getPrototypeOf;
10
- var __hasOwnProp = Object.prototype.hasOwnProperty;
11
- var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
12
- var __exportAll = (all, no_symbols) => {
13
- let target = {};
14
- for (var name in all) __defProp(target, name, {
15
- get: all[name],
16
- enumerable: true
17
- });
18
- if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
19
- return target;
20
- };
21
- var __copyProps = (to, from, except, desc) => {
22
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
23
- key = keys[i];
24
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
25
- get: ((k) => from[k]).bind(null, key),
26
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
27
- });
28
- }
29
- return to;
30
- };
31
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
32
- value: mod,
33
- enumerable: true
34
- }) : target, mod));
35
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
36
- if (typeof require !== "undefined") return require.apply(this, arguments);
37
- throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
38
- });
39
- //#endregion
40
- export { __toESM as i, __exportAll as n, __require as r, __commonJSMin as t };
@@ -1,10 +0,0 @@
1
- import { CollectionConfig, SecurityRule } from "@rebasepro/types";
2
- /**
3
- * Returns the security rules that should be applied to a collection: the
4
- * author's explicit `securityRules` plus the framework defaults described in
5
- * the module doc (baseline server/admin read for all collections; self-read
6
- * and the admin write gate for auth collections).
7
- *
8
- * Collections that opt out via `disableDefaultPolicies` are returned unchanged.
9
- */
10
- export declare function getEffectiveSecurityRules(collection: CollectionConfig): SecurityRule[];