graphile-realtime-subscriptions 0.2.0 → 0.4.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/esm/plugin.js CHANGED
@@ -5,24 +5,104 @@
5
5
  * subscription fields (onXxxChanged) that use PostgreSQL LISTEN/NOTIFY
6
6
  * for real-time event delivery.
7
7
  *
8
- * Subscription modes (Phase 3a):
9
- * - Single record: onXxxChanged(id: UUID!) — subscribe to changes on one row
8
+ * Subscription modes:
9
+ * - Specific rows: onXxxChanged(ids: [UUID!]) — subscribe to changes on specific rows
10
10
  * - Full collection: onXxxChanged (no args) — subscribe to any change on the table
11
11
  *
12
+ * NOTIFY payload format (from emit_change trigger):
13
+ * - Normal: "INSERT:uuid1,uuid2,..." or "UPDATE:uuid1" or "DELETE:uuid1"
14
+ * - Overflow: "INVALIDATE" (when a single statement affects > 50 rows)
15
+ *
12
16
  * Event flow:
13
17
  * 1. A row is inserted/updated/deleted
14
- * 2. The emit_change trigger fires pg_notify('realtime:{schema}.{table}', TG_OP)
18
+ * 2. The emit_change trigger fires pg_notify with TG_OP:row_ids or INVALIDATE
15
19
  * 3. PostGraphile's pgSubscriber receives the NOTIFY
16
- * 4. The subscription re-queries the source table with RLS enforced
17
- * 5. The client receives { event, row } where row reflects the current state
20
+ * 4. The plugin parses the payload and fetches the specific changed row(s)
21
+ * 5. The client receives { event, row, rowId, overflow }
22
+ *
23
+ * Cursor tracking (at-least-once delivery):
24
+ * The CursorTracker class provides a complementary polling-based delivery
25
+ * path via drain_changes(). It manages the listener_node lifecycle:
26
+ * - start() registers the node via touch_listener()
27
+ * - Periodic polling via drain_changes() fetches change_log entries
28
+ * - Periodic heartbeat via touch_listener() keeps the node alive
29
+ * - stop() calls cleanup_ephemeral() to remove ephemeral subscriptions
30
+ * This enables at-least-once semantics by tracking cursor position.
31
+ *
32
+ * Overflow protection:
33
+ * - Database-side: statements affecting > 50 rows send INVALIDATE
34
+ * - Plugin-side: per-subscriber throttle (default 50 events/second/table)
35
+ * drops individual events and sends a single INVALIDATE when exceeded
18
36
  *
19
- * RLS enforcement is automatic — resource.get() queries through the
20
- * authenticated user's connection with their JWT role applied.
37
+ * Security / RLS enforcement:
38
+ * - Row data is always fetched via resource.get() which runs through the
39
+ * authenticated user's connection with their JWT role and pgSettings applied.
40
+ * - For INSERT/UPDATE events, if RLS denies access (resource.get returns null),
41
+ * the rowId is masked (set to null) to prevent metadata leaks.
42
+ * - For DELETE events, row is naturally null (the row no longer exists).
43
+ * - For INVALIDATE (overflow), the client should refetch via a normal query
44
+ * which is also RLS-gated.
45
+ * - When ids are provided, only events for those specific rows are delivered,
46
+ * preventing cross-tenant event leaks.
21
47
  */
22
- import { context as grafastContext, listen, object, constant } from 'grafast';
48
+ import { context as grafastContext, listen, object, constant, lambda } from 'grafast';
23
49
  import { extendSchema } from 'graphile-utils';
24
50
  import { Logger } from '@pgpmjs/logger';
25
51
  const log = new Logger('graphile-realtime-subscriptions');
52
+ /** Default overflow threshold: events per second per table per subscriber */
53
+ const DEFAULT_OVERFLOW_THRESHOLD = 50;
54
+ /**
55
+ * Parse the NOTIFY payload from emit_change.
56
+ * Format: "TG_OP:id1,id2,..." or "INVALIDATE"
57
+ */
58
+ function parseNotifyPayload(raw) {
59
+ if (raw === 'INVALIDATE') {
60
+ return { event: 'INVALIDATE', rowIds: [], overflow: true };
61
+ }
62
+ const colonIdx = raw.indexOf(':');
63
+ if (colonIdx === -1) {
64
+ return { event: raw || 'UNKNOWN', rowIds: [], overflow: false };
65
+ }
66
+ const event = raw.substring(0, colonIdx);
67
+ const idsPart = raw.substring(colonIdx + 1);
68
+ const rowIds = idsPart.length > 0 ? idsPart.split(',') : [];
69
+ return { event, rowIds, overflow: false };
70
+ }
71
+ /**
72
+ * Per-subscriber, per-table event rate tracker.
73
+ * Counts events in a sliding 1-second window.
74
+ */
75
+ class EventThrottle {
76
+ threshold;
77
+ windowStart = 0;
78
+ eventCount = 0;
79
+ overflowSent = false;
80
+ constructor(threshold) {
81
+ this.threshold = threshold;
82
+ }
83
+ /**
84
+ * Record an event and return whether it should be delivered.
85
+ * Returns 'deliver' for normal events, 'overflow' when the threshold
86
+ * is first exceeded, or 'drop' for subsequent events in the same window.
87
+ */
88
+ check() {
89
+ const now = Date.now();
90
+ if (now - this.windowStart >= 1000) {
91
+ this.windowStart = now;
92
+ this.eventCount = 0;
93
+ this.overflowSent = false;
94
+ }
95
+ this.eventCount++;
96
+ if (this.eventCount <= this.threshold) {
97
+ return 'deliver';
98
+ }
99
+ if (!this.overflowSent) {
100
+ this.overflowSent = true;
101
+ return 'overflow';
102
+ }
103
+ return 'drop';
104
+ }
105
+ }
26
106
  function discoverRealtimeTables(build) {
27
107
  const { pgRegistry } = build.input;
28
108
  const resources = pgRegistry.pgResources;
@@ -58,32 +138,59 @@ function discoverRealtimeTables(build) {
58
138
  }
59
139
  function buildTypeDefs(tables) {
60
140
  const subscriptionFields = tables
61
- .map(({ fieldName, payloadTypeName }) => ` """Subscribe to changes on this table. Pass an id to watch a specific record."""\n ${fieldName}(id: UUID): ${payloadTypeName}`)
141
+ .map(({ fieldName, payloadTypeName }) => ` """Subscribe to changes on this table. Pass ids to watch specific rows, or no args for the full collection."""\n ${fieldName}(ids: [UUID!]): ${payloadTypeName}`)
62
142
  .join('\n');
63
143
  const payloadTypes = tables
64
144
  .map(({ payloadTypeName, typeName, rowFieldName }) => `"""Payload delivered when a ${typeName} row changes."""\n` +
65
145
  `type ${payloadTypeName} {\n` +
66
- ` """The DML operation: INSERT, UPDATE, or DELETE."""\n` +
146
+ ` """The DML operation: INSERT, UPDATE, DELETE, or INVALIDATE."""\n` +
67
147
  ` event: String!\n` +
68
- ` """The current state of the row (null for DELETE or if RLS denies access)."""\n` +
148
+ ` """The current state of the row (null for DELETE, INVALIDATE, or if RLS denies access)."""\n` +
69
149
  ` ${rowFieldName}: ${typeName}\n` +
150
+ ` """The ID of the changed row (null for INVALIDATE, or masked when RLS denies access)."""\n` +
151
+ ` rowId: UUID\n` +
152
+ ` """True when too many changes occurred and the client should refetch."""\n` +
153
+ ` overflow: Boolean!\n` +
70
154
  `}`)
71
155
  .join('\n\n');
72
156
  return `extend type Subscription {\n${subscriptionFields}\n}\n\n${payloadTypes}`;
73
157
  }
74
- function buildPlans(tables) {
158
+ function buildPlans(tables, overflowThreshold) {
75
159
  const subscriptionPlans = {};
76
160
  const allPlans = {};
77
161
  for (const { resource, fieldName, payloadTypeName, rowFieldName, notifyChannel } of tables) {
162
+ const throttle = new EventThrottle(overflowThreshold);
78
163
  subscriptionPlans[fieldName] = {
79
164
  subscribePlan(_$root, args) {
80
165
  const $pgSubscriber = grafastContext().get('pgSubscriber');
81
166
  const $topic = constant(notifyChannel);
82
- const $id = args.get('id');
167
+ const $ids = args.get('ids');
83
168
  return listen($pgSubscriber, $topic, ($payload) => {
169
+ const $parsed = lambda([$payload, $ids], (pair) => {
170
+ const [raw, subscribedIds] = pair;
171
+ const parsed = parseNotifyPayload(String(raw));
172
+ const action = parsed.overflow ? 'deliver' : throttle.check();
173
+ if (action === 'drop') {
174
+ return null;
175
+ }
176
+ if (action === 'overflow') {
177
+ return {
178
+ event: 'INVALIDATE',
179
+ rowIds: [],
180
+ overflow: true,
181
+ };
182
+ }
183
+ // Sparse set filtering: only deliver events for subscribed row IDs
184
+ if (subscribedIds && subscribedIds.length > 0) {
185
+ const hasMatch = parsed.rowIds.some((rid) => subscribedIds.includes(rid));
186
+ if (!hasMatch)
187
+ return null;
188
+ }
189
+ return parsed;
190
+ });
84
191
  return object({
85
- event: $payload,
86
- subscribedId: $id,
192
+ parsed: $parsed,
193
+ subscribedIds: $ids,
87
194
  });
88
195
  });
89
196
  },
@@ -93,18 +200,50 @@ function buildPlans(tables) {
93
200
  };
94
201
  allPlans[payloadTypeName] = {
95
202
  event($parent) {
96
- return $parent.get('event');
203
+ const $parsed = $parent.get('parsed');
204
+ return lambda($parsed, (p) => p?.event ?? 'UNKNOWN');
205
+ },
206
+ rowId($parent) {
207
+ const $parsed = $parent.get('parsed');
208
+ const $subscribedIds = $parent.get('subscribedIds');
209
+ return lambda([$parsed, $subscribedIds], (pair) => {
210
+ const [p, subscribedIds] = pair;
211
+ if (!p || p.overflow || p.rowIds.length === 0)
212
+ return null;
213
+ // When ids are provided, return the first matching row ID
214
+ if (subscribedIds && subscribedIds.length > 0) {
215
+ return p.rowIds.find((rid) => subscribedIds.includes(rid)) ?? null;
216
+ }
217
+ return p.rowIds[0];
218
+ });
219
+ },
220
+ overflow($parent) {
221
+ const $parsed = $parent.get('parsed');
222
+ return lambda($parsed, (p) => p?.overflow ?? false);
97
223
  },
98
224
  [rowFieldName]($parent) {
99
- const $id = $parent.get('subscribedId');
100
- return resource.get({ id: $id });
225
+ const $parsed = $parent.get('parsed');
226
+ const $subscribedIds = $parent.get('subscribedIds');
227
+ const $rowId = lambda([$parsed, $subscribedIds], (tuple) => {
228
+ const [p, subscribedIds] = tuple;
229
+ if (!p || p.overflow || p.rowIds.length === 0)
230
+ return null;
231
+ // When ids are provided, return first matching row ID
232
+ if (subscribedIds && subscribedIds.length > 0) {
233
+ return p.rowIds.find((rid) => subscribedIds.includes(rid)) ?? null;
234
+ }
235
+ // Full collection mode: return first row ID
236
+ return p.rowIds[0];
237
+ });
238
+ return resource.get({ id: $rowId });
101
239
  },
102
240
  };
103
241
  }
104
242
  allPlans['Subscription'] = subscriptionPlans;
105
243
  return allPlans;
106
244
  }
107
- export function createRealtimeSubscriptionsPlugin(_options = {}) {
245
+ export function createRealtimeSubscriptionsPlugin(options = {}) {
246
+ const overflowThreshold = options.overflowThreshold ?? DEFAULT_OVERFLOW_THRESHOLD;
108
247
  return extendSchema((build) => {
109
248
  const tables = discoverRealtimeTables(build);
110
249
  if (tables.length === 0) {
@@ -112,9 +251,15 @@ export function createRealtimeSubscriptionsPlugin(_options = {}) {
112
251
  return { typeDefs: '', plans: {} };
113
252
  }
114
253
  log.info(`Generating subscription fields for ${tables.length} realtime table(s)`);
254
+ log.info(`Overflow threshold: ${overflowThreshold} events/second/table`);
115
255
  const typeDefs = buildTypeDefs(tables);
116
- const plans = buildPlans(tables);
256
+ const plans = buildPlans(tables, overflowThreshold);
117
257
  return { typeDefs, plans };
118
258
  }, 'RealtimeSubscriptionsPlugin');
119
259
  }
120
260
  export { createRealtimeSubscriptionsPlugin as RealtimeSubscriptionsPlugin };
261
+ // Re-export CursorTracker and RealtimeManager for convenience
262
+ export { CursorTracker } from './cursor-tracker';
263
+ export { RealtimeManager } from './realtime-manager';
264
+ // Exported for testing
265
+ export { parseNotifyPayload, EventThrottle, DEFAULT_OVERFLOW_THRESHOLD };
@@ -0,0 +1,67 @@
1
+ /**
2
+ * RealtimeManager — bridges CursorTracker (polling drain_changes) into
3
+ * PostGraphile's PgSubscriber so cursor-tracked events flow through the
4
+ * same subscription plans as NOTIFY events.
5
+ *
6
+ * Architecture:
7
+ * PgSubscriber uses an internal EventEmitter. NOTIFY payloads arrive via
8
+ * pg's `notification` event and are emitted as `eventEmitter.emit(channel, payload)`.
9
+ * The `listen()` step in grafast subscribes to the same EventEmitter.
10
+ *
11
+ * RealtimeManager converts ChangeLogEntry objects from drain_changes() into
12
+ * the same NOTIFY payload format ("OP:rowId1,rowId2,...") and emits them on
13
+ * the PgSubscriber's EventEmitter, so existing subscription plans handle
14
+ * them identically to real NOTIFY events.
15
+ *
16
+ * This provides at-least-once delivery: NOTIFY is instant but best-effort;
17
+ * cursor polling catches up on anything missed (disconnects, restarts).
18
+ * Duplicates are expected and acceptable — clients should be idempotent.
19
+ *
20
+ * Lifecycle:
21
+ * 1. start() → registers listener node, begins polling + heartbeat
22
+ * 2. drain_changes() results are converted and emitted on PgSubscriber
23
+ * 3. stop() → cleans up ephemeral subscriptions, removes listener node
24
+ */
25
+ import type { ChangeLogEntry, RealtimeManagerOptions } from './types';
26
+ /**
27
+ * Extract row IDs from a ChangeLogEntry.
28
+ *
29
+ * For INSERT/UPDATE the row ID lives in payload_after.id;
30
+ * for DELETE it lives in payload_before.id.
31
+ * Falls back to the change_log entry's own id if payloads are missing.
32
+ */
33
+ declare function extractRowId(entry: ChangeLogEntry): string | null;
34
+ /**
35
+ * Convert a ChangeLogEntry into the NOTIFY payload format used by emit_change.
36
+ * Format: "OPERATION:rowId" (e.g. "INSERT:550e8400-...")
37
+ */
38
+ declare function entryToNotifyPayload(entry: ChangeLogEntry): string;
39
+ /**
40
+ * Build the NOTIFY channel name for a change_log entry.
41
+ * Matches the channel format used by emit_change: "realtime:{schema}.{table}"
42
+ */
43
+ declare function entryToChannel(entry: ChangeLogEntry): string;
44
+ export declare class RealtimeManager {
45
+ private readonly cursorTracker;
46
+ private readonly subscriber;
47
+ private started;
48
+ constructor(options: RealtimeManagerOptions);
49
+ get nodeId(): string;
50
+ get isRunning(): boolean;
51
+ start(): Promise<void>;
52
+ stop(): Promise<void>;
53
+ /**
54
+ * Convert ChangeLogEntry objects to NOTIFY-format payloads and emit
55
+ * them on the PgSubscriber's internal EventEmitter.
56
+ */
57
+ private dispatchEntries;
58
+ /**
59
+ * Access PgSubscriber's internal EventEmitter.
60
+ *
61
+ * PgSubscriber from @dataplan/pg stores an EventEmitter3 instance as
62
+ * `this.eventEmitter`. It is private but stable across v1.x releases.
63
+ * This is the same emitter that NOTIFY events are dispatched through.
64
+ */
65
+ private getEventEmitter;
66
+ }
67
+ export { extractRowId, entryToNotifyPayload, entryToChannel };
@@ -0,0 +1,134 @@
1
+ /**
2
+ * RealtimeManager — bridges CursorTracker (polling drain_changes) into
3
+ * PostGraphile's PgSubscriber so cursor-tracked events flow through the
4
+ * same subscription plans as NOTIFY events.
5
+ *
6
+ * Architecture:
7
+ * PgSubscriber uses an internal EventEmitter. NOTIFY payloads arrive via
8
+ * pg's `notification` event and are emitted as `eventEmitter.emit(channel, payload)`.
9
+ * The `listen()` step in grafast subscribes to the same EventEmitter.
10
+ *
11
+ * RealtimeManager converts ChangeLogEntry objects from drain_changes() into
12
+ * the same NOTIFY payload format ("OP:rowId1,rowId2,...") and emits them on
13
+ * the PgSubscriber's EventEmitter, so existing subscription plans handle
14
+ * them identically to real NOTIFY events.
15
+ *
16
+ * This provides at-least-once delivery: NOTIFY is instant but best-effort;
17
+ * cursor polling catches up on anything missed (disconnects, restarts).
18
+ * Duplicates are expected and acceptable — clients should be idempotent.
19
+ *
20
+ * Lifecycle:
21
+ * 1. start() → registers listener node, begins polling + heartbeat
22
+ * 2. drain_changes() results are converted and emitted on PgSubscriber
23
+ * 3. stop() → cleans up ephemeral subscriptions, removes listener node
24
+ */
25
+ import { Logger } from '@pgpmjs/logger';
26
+ import { CursorTracker } from './cursor-tracker';
27
+ const log = new Logger('realtime-manager');
28
+ /**
29
+ * Extract row IDs from a ChangeLogEntry.
30
+ *
31
+ * For INSERT/UPDATE the row ID lives in payload_after.id;
32
+ * for DELETE it lives in payload_before.id.
33
+ * Falls back to the change_log entry's own id if payloads are missing.
34
+ */
35
+ function extractRowId(entry) {
36
+ if (entry.operation === 'DELETE') {
37
+ return entry.payload_before?.id ?? null;
38
+ }
39
+ return entry.payload_after?.id ?? null;
40
+ }
41
+ /**
42
+ * Convert a ChangeLogEntry into the NOTIFY payload format used by emit_change.
43
+ * Format: "OPERATION:rowId" (e.g. "INSERT:550e8400-...")
44
+ */
45
+ function entryToNotifyPayload(entry) {
46
+ const rowId = extractRowId(entry);
47
+ if (!rowId) {
48
+ return entry.operation;
49
+ }
50
+ return `${entry.operation}:${rowId}`;
51
+ }
52
+ /**
53
+ * Build the NOTIFY channel name for a change_log entry.
54
+ * Matches the channel format used by emit_change: "realtime:{schema}.{table}"
55
+ */
56
+ function entryToChannel(entry) {
57
+ return `realtime:${entry.source_schema}.${entry.source_table}`;
58
+ }
59
+ export class RealtimeManager {
60
+ cursorTracker;
61
+ subscriber;
62
+ started = false;
63
+ constructor(options) {
64
+ const { pgSubscriber, withPgClient, ...cursorOpts } = options;
65
+ this.subscriber = pgSubscriber;
66
+ this.cursorTracker = new CursorTracker({
67
+ nodeId: cursorOpts.nodeId,
68
+ schema: cursorOpts.schema,
69
+ pollIntervalMs: cursorOpts.pollIntervalMs,
70
+ heartbeatIntervalMs: cursorOpts.heartbeatIntervalMs,
71
+ batchLimit: cursorOpts.batchLimit,
72
+ withPgClient,
73
+ onChanges: (entries) => this.dispatchEntries(entries),
74
+ onError: cursorOpts.onError ?? ((err) => {
75
+ log.error(`RealtimeManager error: ${err.message}`);
76
+ }),
77
+ });
78
+ }
79
+ get nodeId() {
80
+ return this.cursorTracker.nodeId;
81
+ }
82
+ get isRunning() {
83
+ return this.started && this.cursorTracker.isRunning;
84
+ }
85
+ async start() {
86
+ if (this.started)
87
+ return;
88
+ this.started = true;
89
+ log.info(`Starting RealtimeManager: node=${this.nodeId}`);
90
+ await this.cursorTracker.start();
91
+ }
92
+ async stop() {
93
+ if (!this.started)
94
+ return;
95
+ this.started = false;
96
+ log.info(`Stopping RealtimeManager: node=${this.nodeId}`);
97
+ await this.cursorTracker.stop();
98
+ }
99
+ /**
100
+ * Convert ChangeLogEntry objects to NOTIFY-format payloads and emit
101
+ * them on the PgSubscriber's internal EventEmitter.
102
+ */
103
+ dispatchEntries(entries) {
104
+ const emitter = this.getEventEmitter();
105
+ if (!emitter) {
106
+ log.warn('PgSubscriber has no eventEmitter; cursor events cannot be dispatched');
107
+ return;
108
+ }
109
+ for (const entry of entries) {
110
+ const channel = entryToChannel(entry);
111
+ const payload = entryToNotifyPayload(entry);
112
+ emitter.emit(channel, payload);
113
+ }
114
+ log.info(`Dispatched ${entries.length} cursor-tracked event(s) to PgSubscriber`);
115
+ }
116
+ /**
117
+ * Access PgSubscriber's internal EventEmitter.
118
+ *
119
+ * PgSubscriber from @dataplan/pg stores an EventEmitter3 instance as
120
+ * `this.eventEmitter`. It is private but stable across v1.x releases.
121
+ * This is the same emitter that NOTIFY events are dispatched through.
122
+ */
123
+ getEventEmitter() {
124
+ const sub = this.subscriber;
125
+ if (sub && typeof sub === 'object' && 'eventEmitter' in sub) {
126
+ const ee = sub.eventEmitter;
127
+ if (typeof ee?.emit === 'function') {
128
+ return ee;
129
+ }
130
+ }
131
+ return null;
132
+ }
133
+ }
134
+ export { extractRowId, entryToNotifyPayload, entryToChannel };
package/esm/types.d.ts CHANGED
@@ -1,9 +1,146 @@
1
1
  /**
2
2
  * Configuration options for the Realtime Subscriptions Plugin.
3
- *
4
- * Phase 3a supports single-record and full-collection subscription modes
5
- * with no additional configuration required. Future phases will add
6
- * options for poll intervals, batch sizes, and throttling.
7
3
  */
8
4
  export interface RealtimeSubscriptionsPluginOptions {
5
+ /**
6
+ * Maximum number of events per table per second before switching to
7
+ * overflow (INVALIDATE) mode. When exceeded, the subscription sends
8
+ * a single { event: 'INVALIDATE', overflow: true } instead of
9
+ * individual row events, signaling the client to refetch.
10
+ *
11
+ * Default: 50
12
+ */
13
+ overflowThreshold?: number;
14
+ }
15
+ /**
16
+ * A minimal PostgreSQL client interface used by CursorTracker.
17
+ * Compatible with node-postgres (pg) Client or PoolClient.
18
+ */
19
+ export interface PgClient {
20
+ query<R = Record<string, unknown>>(text: string, values?: unknown[]): Promise<{
21
+ rows: R[];
22
+ }>;
23
+ }
24
+ /**
25
+ * Callback that provides a PgClient for executing queries.
26
+ * The client is released after the callback returns.
27
+ */
28
+ export type WithPgClient = <T>(callback: (client: PgClient) => Promise<T>) => Promise<T>;
29
+ /**
30
+ * A single entry from drain_changes(), representing a change_log row
31
+ * matched against subscriber tables.
32
+ */
33
+ export interface ChangeLogEntry {
34
+ id: string;
35
+ occurred_at: string;
36
+ source_schema: string;
37
+ source_table: string;
38
+ operation: string;
39
+ payload_after: Record<string, unknown> | null;
40
+ payload_before: Record<string, unknown> | null;
41
+ payload_diff: Record<string, unknown> | null;
42
+ subscriber_ids: string[];
43
+ }
44
+ /**
45
+ * Configuration for the CursorTracker background poller.
46
+ */
47
+ export interface CursorTrackerOptions {
48
+ /**
49
+ * Unique identifier for this listener node.
50
+ * Should be stable across restarts if you want cursor continuity.
51
+ * If not provided, a random ID is generated (ephemeral mode).
52
+ */
53
+ nodeId?: string;
54
+ /**
55
+ * The realtime_public schema name where drain_changes(),
56
+ * touch_listener(), and cleanup_ephemeral() live.
57
+ *
58
+ * Default: 'realtime_public'
59
+ */
60
+ schema?: string;
61
+ /**
62
+ * How often to poll drain_changes() for new events (milliseconds).
63
+ *
64
+ * Default: 5000 (5 seconds)
65
+ */
66
+ pollIntervalMs?: number;
67
+ /**
68
+ * How often to send a heartbeat via touch_listener() (milliseconds).
69
+ *
70
+ * Default: 30000 (30 seconds)
71
+ */
72
+ heartbeatIntervalMs?: number;
73
+ /**
74
+ * Maximum number of change_log rows to fetch per drain_changes() call.
75
+ *
76
+ * Default: 500
77
+ */
78
+ batchLimit?: number;
79
+ /**
80
+ * Function to acquire a PgClient for executing queries.
81
+ * The cursor tracker calls this for every poll and heartbeat cycle.
82
+ */
83
+ withPgClient: WithPgClient;
84
+ /**
85
+ * Called when drain_changes() returns new change_log entries.
86
+ * The entries include subscriber_ids for fan-out routing.
87
+ */
88
+ onChanges?: (entries: ChangeLogEntry[]) => void;
89
+ /**
90
+ * Called when an error occurs during polling, heartbeat, or cleanup.
91
+ * If not provided, errors are logged to the console.
92
+ */
93
+ onError?: (error: Error) => void;
94
+ }
95
+ /**
96
+ * Configuration for the RealtimeManager, which bridges CursorTracker
97
+ * events into PostGraphile's PgSubscriber for at-least-once delivery.
98
+ */
99
+ export interface RealtimeManagerOptions {
100
+ /**
101
+ * The PgSubscriber instance from PostGraphile's context.
102
+ * RealtimeManager emits cursor-tracked events on its internal EventEmitter
103
+ * so they flow through existing subscription plans.
104
+ */
105
+ pgSubscriber: unknown;
106
+ /**
107
+ * Function to acquire a PgClient for executing cursor tracking queries.
108
+ */
109
+ withPgClient: WithPgClient;
110
+ /**
111
+ * Unique identifier for this listener node.
112
+ * Should be stable across restarts if you want cursor continuity.
113
+ * If not provided, a random ID is generated (ephemeral mode).
114
+ */
115
+ nodeId?: string;
116
+ /**
117
+ * The realtime_public schema name where drain_changes(),
118
+ * touch_listener(), and cleanup_ephemeral() live.
119
+ *
120
+ * Default: 'realtime_public'
121
+ */
122
+ schema?: string;
123
+ /**
124
+ * How often to poll drain_changes() for new events (milliseconds).
125
+ *
126
+ * Default: 5000 (5 seconds)
127
+ */
128
+ pollIntervalMs?: number;
129
+ /**
130
+ * How often to send a heartbeat via touch_listener() (milliseconds).
131
+ *
132
+ * Default: 30000 (30 seconds)
133
+ */
134
+ heartbeatIntervalMs?: number;
135
+ /**
136
+ * Maximum number of change_log rows to fetch per drain_changes() call.
137
+ *
138
+ * Default: 500
139
+ */
140
+ batchLimit?: number;
141
+ /**
142
+ * Called when an error occurs during polling, heartbeat, or cleanup.
143
+ * If not provided, errors are logged via @pgpmjs/logger.
144
+ */
145
+ onError?: (error: Error) => void;
9
146
  }
package/index.d.ts CHANGED
@@ -18,4 +18,7 @@
18
18
  */
19
19
  export { createRealtimeSubscriptionsPlugin, RealtimeSubscriptionsPlugin } from './plugin';
20
20
  export { RealtimeSubscriptionsPreset } from './preset';
21
+ export { CursorTracker } from './cursor-tracker';
22
+ export { RealtimeManager } from './realtime-manager';
21
23
  export type { RealtimeSubscriptionsPluginOptions } from './types';
24
+ export type { CursorTrackerOptions, ChangeLogEntry, PgClient, WithPgClient, RealtimeManagerOptions, } from './types';
package/index.js CHANGED
@@ -18,9 +18,13 @@
18
18
  * ```
19
19
  */
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.RealtimeSubscriptionsPreset = exports.RealtimeSubscriptionsPlugin = exports.createRealtimeSubscriptionsPlugin = void 0;
21
+ exports.RealtimeManager = exports.CursorTracker = exports.RealtimeSubscriptionsPreset = exports.RealtimeSubscriptionsPlugin = exports.createRealtimeSubscriptionsPlugin = void 0;
22
22
  var plugin_1 = require("./plugin");
23
23
  Object.defineProperty(exports, "createRealtimeSubscriptionsPlugin", { enumerable: true, get: function () { return plugin_1.createRealtimeSubscriptionsPlugin; } });
24
24
  Object.defineProperty(exports, "RealtimeSubscriptionsPlugin", { enumerable: true, get: function () { return plugin_1.RealtimeSubscriptionsPlugin; } });
25
25
  var preset_1 = require("./preset");
26
26
  Object.defineProperty(exports, "RealtimeSubscriptionsPreset", { enumerable: true, get: function () { return preset_1.RealtimeSubscriptionsPreset; } });
27
+ var cursor_tracker_1 = require("./cursor-tracker");
28
+ Object.defineProperty(exports, "CursorTracker", { enumerable: true, get: function () { return cursor_tracker_1.CursorTracker; } });
29
+ var realtime_manager_1 = require("./realtime-manager");
30
+ Object.defineProperty(exports, "RealtimeManager", { enumerable: true, get: function () { return realtime_manager_1.RealtimeManager; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-realtime-subscriptions",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Realtime subscription plugin for PostGraphile v5 — per-table GraphQL subscriptions via LISTEN/NOTIFY",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/constructive",
@@ -40,7 +40,8 @@
40
40
  "url": "https://github.com/constructive-io/constructive/issues"
41
41
  },
42
42
  "dependencies": {
43
- "@pgpmjs/logger": "^2.9.0"
43
+ "@pgpmjs/logger": "^2.9.0",
44
+ "@pgsql/quotes": "^17.1.0"
44
45
  },
45
46
  "peerDependencies": {
46
47
  "grafast": "1.0.0",
@@ -55,5 +56,5 @@
55
56
  "@types/node": "^22.19.11",
56
57
  "makage": "^0.3.0"
57
58
  },
58
- "gitHead": "c87dafec1ccb2c3cc62f2304584104db108a810b"
59
+ "gitHead": "b54aef52c81be35a53e771003939d447aa34ba35"
59
60
  }