@rebasepro/server-postgres 0.10.1-canary.a54c057 → 0.10.1-canary.af2af91

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 (69) hide show
  1. package/dist/PostgresBootstrapper.d.ts +7 -3
  2. package/dist/auth/services.d.ts +43 -4
  3. package/dist/backup/backup-logic.d.ts +23 -0
  4. package/dist/backup/backup-service.d.ts +44 -2
  5. package/dist/backup/pg-tools.d.ts +41 -1
  6. package/dist/chunk-DSJWtz9O.js +40 -0
  7. package/dist/cli-helpers.d.ts +33 -1
  8. package/dist/ensure-collection-tables-BVvtkkRm.js +304 -0
  9. package/dist/ensure-collection-tables-BVvtkkRm.js.map +1 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.es.js +1854 -4745
  12. package/dist/index.es.js.map +1 -1
  13. package/dist/schema/auth-schema.d.ts +170 -0
  14. package/dist/schema/destructive-sql.d.ts +49 -0
  15. package/dist/schema/ensure-collection-tables.d.ts +79 -0
  16. package/dist/schema/generate-postgres-ddl-logic.d.ts +4 -1
  17. package/dist/services/FetchService.d.ts +21 -8
  18. package/dist/services/PersistService.d.ts +12 -0
  19. package/dist/services/RelationService.d.ts +30 -0
  20. package/dist/services/cdc/CdcListener.d.ts +7 -14
  21. package/dist/services/cdc/junction-tables.d.ts +38 -0
  22. package/dist/services/channel-bus/ChannelBus.d.ts +29 -0
  23. package/dist/services/channel-bus/PostgresChannelBus.d.ts +111 -0
  24. package/dist/services/channel-bus/index.d.ts +55 -0
  25. package/dist/services/channel-history.d.ts +11 -0
  26. package/dist/services/channel-presence.d.ts +66 -0
  27. package/dist/services/nested-path.d.ts +59 -0
  28. package/dist/services/pg-notify-listener.d.ts +47 -0
  29. package/dist/services/realtimeService.d.ts +133 -6
  30. package/dist/src-CBgtrPhJ.js +336 -0
  31. package/dist/src-CBgtrPhJ.js.map +1 -0
  32. package/dist/src-lcfUP4xg.js +4106 -0
  33. package/dist/src-lcfUP4xg.js.map +1 -0
  34. package/dist/utils/drizzle-conditions.d.ts +41 -0
  35. package/package.json +8 -9
  36. package/src/PostgresBootstrapper.ts +82 -4
  37. package/src/auth/ensure-tables.ts +70 -56
  38. package/src/auth/services.ts +186 -48
  39. package/src/backup/backup-cli.ts +60 -1
  40. package/src/backup/backup-cron.ts +24 -1
  41. package/src/backup/backup-logic.ts +62 -0
  42. package/src/backup/backup-service.ts +132 -13
  43. package/src/backup/pg-tools.ts +70 -2
  44. package/src/cli-helpers.ts +82 -27
  45. package/src/cli.ts +152 -6
  46. package/src/index.ts +4 -0
  47. package/src/schema/auth-schema.ts +41 -3
  48. package/src/schema/destructive-sql.ts +94 -0
  49. package/src/schema/doctor.ts +6 -6
  50. package/src/schema/ensure-collection-tables.test.ts +156 -0
  51. package/src/schema/ensure-collection-tables.ts +297 -0
  52. package/src/schema/generate-drizzle-schema-logic.ts +13 -9
  53. package/src/schema/generate-postgres-ddl-logic.ts +22 -15
  54. package/src/schema/introspect-db-inference.ts +13 -13
  55. package/src/schema/introspect-db-logic.ts +6 -6
  56. package/src/services/FetchService.ts +104 -114
  57. package/src/services/PersistService.ts +127 -85
  58. package/src/services/RelationService.ts +95 -6
  59. package/src/services/cdc/CdcListener.ts +27 -91
  60. package/src/services/cdc/junction-tables.ts +91 -0
  61. package/src/services/channel-bus/ChannelBus.ts +44 -0
  62. package/src/services/channel-bus/PostgresChannelBus.ts +299 -0
  63. package/src/services/channel-bus/index.ts +123 -0
  64. package/src/services/channel-history.ts +35 -0
  65. package/src/services/channel-presence.ts +148 -0
  66. package/src/services/nested-path.ts +130 -0
  67. package/src/services/pg-notify-listener.ts +137 -0
  68. package/src/services/realtimeService.ts +443 -14
  69. package/src/utils/drizzle-conditions.ts +154 -0
@@ -0,0 +1,130 @@
1
+ import { CollectionConfig, Relation } from "@rebasepro/types";
2
+ import { findRelation, resolveCollectionRelations } from "@rebasepro/common";
3
+ import { ApiError } from "@rebasepro/server";
4
+
5
+ import { getCollectionByPath } from "./collection-helpers";
6
+ import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
7
+
8
+ /**
9
+ * The last hop of a nested collection path, e.g. `authors/1/posts`.
10
+ *
11
+ * The walk that produces this was written out four separate times — in
12
+ * `FetchService.fetchCollectionFromPath`, `FetchService.countEntitiesFromPath`,
13
+ * `PersistService.save` and `CollectionRegistry.getCollectionByPath` — and had
14
+ * drifted, so the read path and the write path did not agree on which relation
15
+ * a path named. It lives here once now.
16
+ */
17
+ export interface NestedPathHop {
18
+ /** The collection the final relation is declared on (e.g. `authors`). */
19
+ parentCollection: CollectionConfig;
20
+ /** The parent's id as it appeared in the path, unparsed. */
21
+ parentId: string;
22
+ /** The path segment that named the relation (e.g. `posts`). */
23
+ relationKey: string;
24
+ relation: Relation;
25
+ /** `relation.target()`, resolved once. */
26
+ targetCollection: CollectionConfig;
27
+ }
28
+
29
+ /**
30
+ * True when `path` addresses rows through a relation rather than a root
31
+ * collection.
32
+ *
33
+ * Any separator at all counts — a root collection slug never contains one — so
34
+ * a malformed path like `collection/id` is a *broken* nested path and gets
35
+ * reported as one by {@link resolveNestedPath}, rather than being looked up as
36
+ * a root collection whose slug happens to contain a slash.
37
+ */
38
+ export function isNestedPath(path: string): boolean {
39
+ return path.includes("/");
40
+ }
41
+
42
+ export function splitPathSegments(path: string): string[] {
43
+ return path.split("/").filter(s => s && s !== "undefined");
44
+ }
45
+
46
+ /**
47
+ * Walk a nested collection path down to the relation it ends in.
48
+ *
49
+ * Returns `undefined` for a plain root-collection path so callers can keep the
50
+ * root case on its existing code path. Throws when the path is malformed, or
51
+ * when a segment names a relation that does not exist — the same errors the
52
+ * individual walks used to raise, with the available names attached.
53
+ */
54
+ export function resolveNestedPath(
55
+ path: string,
56
+ registry: PostgresCollectionRegistry
57
+ ): NestedPathHop | undefined {
58
+ if (!isNestedPath(path)) return undefined;
59
+
60
+ const segments = splitPathSegments(path);
61
+ if (segments.length < 3 || segments.length % 2 === 0) {
62
+ throw new Error(`Invalid relation path: ${path}. Expected format: collection/id/relation`);
63
+ }
64
+
65
+ let parentCollection = getCollectionByPath(segments[0], registry);
66
+ let parentId = segments[1];
67
+
68
+ for (let i = 2; i < segments.length; i += 2) {
69
+ const relationKey = segments[i];
70
+ const resolvedRelations = resolveCollectionRelations(parentCollection);
71
+ const relation = findRelation(resolvedRelations, relationKey);
72
+
73
+ if (!relation) {
74
+ const available = Object.keys(resolvedRelations).join(", ") || "(none)";
75
+ throw new Error(
76
+ `Relation '${relationKey}' not found in collection '${parentCollection.slug}'. Available relations: [${available}]`
77
+ );
78
+ }
79
+
80
+ const targetCollection = relation.target();
81
+
82
+ if (i === segments.length - 1) {
83
+ return {
84
+ parentCollection,
85
+ parentId,
86
+ relationKey,
87
+ relation,
88
+ targetCollection
89
+ };
90
+ }
91
+
92
+ parentCollection = targetCollection;
93
+ parentId = segments[i + 1];
94
+ }
95
+
96
+ // Unreachable: the loop returns on the final segment, and the odd-length
97
+ // check above guarantees there is one.
98
+ throw new Error(`Unable to resolve path: ${path}`);
99
+ }
100
+
101
+ /**
102
+ * A relation reached through a junction table — many-to-many, or a multi-hop
103
+ * `joinPath`. The target row is shared with other parents, so writing "through"
104
+ * such a path addresses the *link*, not the row.
105
+ */
106
+ export function isJunctionBackedRelation(relation: Relation): boolean {
107
+ return Boolean(relation.through) || Boolean(relation.joinPath && relation.joinPath.length > 1);
108
+ }
109
+
110
+ /**
111
+ * Reject a nested write whose final segment is a to-one relation.
112
+ *
113
+ * There is no column on the target row that records a to-one parent — the
114
+ * foreign key lives on the *parent* table. The write path used to fall through
115
+ * to `relation.localKey` here and stamp the parent's own FK column onto the
116
+ * target row, which either raised an opaque "column does not exist" or, when a
117
+ * column of that name happened to exist on the target, silently wrote the wrong
118
+ * one.
119
+ */
120
+ export function assertWritableThrough(hop: NestedPathHop, path: string): void {
121
+ if (hop.relation.cardinality !== "many") {
122
+ throw ApiError.badRequest(
123
+ `"${path}" ends in the to-one relation '${hop.relationKey}', which cannot be written through: ` +
124
+ `the foreign key for a to-one relation lives on '${hop.parentCollection.slug}', not on ` +
125
+ `'${hop.targetCollection.slug}'. Write the target row at "${hop.targetCollection.slug}" and set ` +
126
+ `'${hop.relationKey}' on the parent instead.`,
127
+ "RELATION_NOT_WRITABLE"
128
+ );
129
+ }
130
+ }
@@ -0,0 +1,137 @@
1
+ /**
2
+ * A dedicated, self-healing Postgres `LISTEN` connection.
3
+ *
4
+ * Every cross-instance feature in the backend needs the same thing: one
5
+ * connection *outside* the Drizzle pool that stays open, holds a `LISTEN`, and
6
+ * comes back on its own after the database or the network drops it. CDC needed
7
+ * it first; the channel bus needs it too. This is that connection, with the one
8
+ * behaviour that matters to callers preserved: the **first** connect is
9
+ * validated and rethrown, so a caller can fall back to a different strategy,
10
+ * while every later drop is repaired quietly in the background.
11
+ *
12
+ * `LISTEN` is session state, so this connection must not go through a
13
+ * transaction-mode pooler (PgBouncer): give it the direct database URL.
14
+ */
15
+
16
+ import { Client as PgClient } from "pg";
17
+ import { logger } from "@rebasepro/server";
18
+
19
+ export interface PgNotifyListenerOptions {
20
+ /** Direct Postgres connection string (must bypass a transaction-mode pooler). */
21
+ connectionString: string;
22
+ /** NOTIFY channel to LISTEN on. Must be a plain identifier — it is interpolated. */
23
+ channel: string;
24
+ /** Called for every notification payload received. */
25
+ onPayload: (payload: string) => void | Promise<void>;
26
+ /** Prefix for log lines, e.g. `"[CDC]"`. */
27
+ logLabel: string;
28
+ /** Delay before a reconnect attempt. */
29
+ reconnectDelayMs?: number;
30
+ }
31
+
32
+ const DEFAULT_RECONNECT_DELAY_MS = 3000;
33
+ /** Guards the identifier interpolated into `LISTEN`. */
34
+ const SAFE_CHANNEL = /^[A-Za-z_][A-Za-z0-9_]*$/;
35
+
36
+ export class PgNotifyListener {
37
+ private client?: PgClient;
38
+ private running = false;
39
+ private reconnectTimer?: ReturnType<typeof setTimeout>;
40
+
41
+ constructor(private readonly options: PgNotifyListenerOptions) {
42
+ if (!SAFE_CHANNEL.test(options.channel)) {
43
+ throw new Error(`Unsafe NOTIFY channel name "${options.channel}" — expected a plain SQL identifier.`);
44
+ }
45
+ }
46
+
47
+ /** Whether the listener is meant to be connected right now. */
48
+ get active(): boolean {
49
+ return this.running;
50
+ }
51
+
52
+ /**
53
+ * Connect and begin listening. Idempotent.
54
+ *
55
+ * Rejects if the *initial* connection or `LISTEN` fails, leaving the
56
+ * listener stopped — callers use that to degrade deliberately instead of
57
+ * running blind against a channel nothing is delivering.
58
+ */
59
+ async start(): Promise<void> {
60
+ if (this.running) return;
61
+ this.running = true;
62
+ try {
63
+ await this.connect({ initial: true });
64
+ } catch (err) {
65
+ this.running = false;
66
+ throw err;
67
+ }
68
+ }
69
+
70
+ /** Stop listening and release the connection. Idempotent. */
71
+ async stop(): Promise<void> {
72
+ this.running = false;
73
+ if (this.reconnectTimer) {
74
+ clearTimeout(this.reconnectTimer);
75
+ this.reconnectTimer = undefined;
76
+ }
77
+ if (this.client) {
78
+ try {
79
+ await this.client.end();
80
+ } catch { /* ignore close errors */ }
81
+ this.client = undefined;
82
+ }
83
+ }
84
+
85
+ private async connect({ initial = false }: { initial?: boolean } = {}): Promise<void> {
86
+ const { connectionString, channel, onPayload, logLabel } = this.options;
87
+ try {
88
+ const client = new PgClient({ connectionString });
89
+
90
+ client.on("error", (err) => {
91
+ logger.error(`❌ ${logLabel} LISTEN client error`, { detail: err.message });
92
+ this.scheduleReconnect();
93
+ });
94
+
95
+ client.on("end", () => {
96
+ if (this.running) {
97
+ logger.warn(`⚠️ ${logLabel} LISTEN client disconnected unexpectedly.`);
98
+ this.scheduleReconnect();
99
+ }
100
+ });
101
+
102
+ client.on("notification", (msg) => {
103
+ if (!msg.payload) return;
104
+ // A handler rejection must never surface as an unhandled
105
+ // rejection inside the pg client's event emitter.
106
+ Promise.resolve(onPayload(msg.payload)).catch((err) =>
107
+ logger.error(`❌ ${logLabel} Error handling notification`, { error: err })
108
+ );
109
+ });
110
+
111
+ await client.connect();
112
+ await client.query(`LISTEN ${channel}`);
113
+ this.client = client;
114
+ logger.info(`📡 ${logLabel} Listening on channel "${channel}".`);
115
+ } catch (err) {
116
+ // Surface the initial failure so callers can choose to fall back;
117
+ // for reconnects, keep retrying quietly in the background.
118
+ if (initial) throw err;
119
+ logger.error(`❌ ${logLabel} Failed to connect LISTEN client`, { error: err });
120
+ this.scheduleReconnect();
121
+ }
122
+ }
123
+
124
+ private scheduleReconnect(): void {
125
+ if (!this.running || this.reconnectTimer) return;
126
+
127
+ this.reconnectTimer = setTimeout(async () => {
128
+ this.reconnectTimer = undefined;
129
+ if (!this.running) return;
130
+ if (this.client) {
131
+ try { await this.client.end(); } catch { /* ignore */ }
132
+ this.client = undefined;
133
+ }
134
+ await this.connect();
135
+ }, this.options.reconnectDelayMs ?? DEFAULT_RECONNECT_DELAY_MS);
136
+ }
137
+ }