@haathie/pgmb 0.2.5 → 0.2.7
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 +31 -0
- package/lib/client.js +9 -8
- package/lib/index.js +1 -0
- package/lib/utils.js +11 -0
- package/package.json +12 -5
- package/src/abortable-async-iterator.ts +98 -0
- package/src/batcher.ts +90 -0
- package/src/client.ts +699 -0
- package/src/consts.ts +1 -0
- package/src/index.ts +6 -0
- package/src/queries.ts +570 -0
- package/src/query-types.ts +21 -0
- package/src/retry-handler.ts +125 -0
- package/src/sse.ts +148 -0
- package/src/types.ts +267 -0
- package/src/utils.ts +71 -0
- package/src/webhook-handler.ts +91 -0
- package/lib/abortable-async-iterator.d.ts +0 -14
- package/lib/batcher.d.ts +0 -12
- package/lib/client.d.ts +0 -76
- package/lib/consts.d.ts +0 -1
- package/lib/index.d.ts +0 -5
- package/lib/queries.d.ts +0 -453
- package/lib/query-types.d.ts +0 -17
- package/lib/retry-handler.d.ts +0 -11
- package/lib/sse.d.ts +0 -4
- package/lib/types.d.ts +0 -223
- package/lib/utils.d.ts +0 -15
- package/lib/webhook-handler.d.ts +0 -6
package/lib/types.d.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import type { IDatabaseConnection } from '@pgtyped/runtime';
|
|
2
|
-
import type { IncomingMessage } from 'node:http';
|
|
3
|
-
import type { Logger } from 'pino';
|
|
4
|
-
import type { HeaderRecord } from 'undici-types/header.js';
|
|
5
|
-
import type { AbortableAsyncIterator } from './abortable-async-iterator.ts';
|
|
6
|
-
import type { IAssertSubscriptionParams, IFindEventsParams, IFindEventsResult, IReadNextEventsParams, IReadNextEventsResult } from './queries.ts';
|
|
7
|
-
import type { PgClientLike } from './query-types.ts';
|
|
8
|
-
export type ISplitFn<T extends IEventData> = (event: IReadEvent<T>) => IReadEvent<T>[];
|
|
9
|
-
export type SerialisedEvent = {
|
|
10
|
-
body: Buffer | string;
|
|
11
|
-
contentType: string;
|
|
12
|
-
};
|
|
13
|
-
export type WebhookInfo = {
|
|
14
|
-
id: string;
|
|
15
|
-
url: string | URL;
|
|
16
|
-
};
|
|
17
|
-
export type GetWebhookInfoFn = (subscriptionIds: string[]) => Promise<{
|
|
18
|
-
[id: string]: WebhookInfo[];
|
|
19
|
-
}> | {
|
|
20
|
-
[id: string]: WebhookInfo[];
|
|
21
|
-
};
|
|
22
|
-
export type PgmbWebhookOpts<T extends IEventData> = {
|
|
23
|
-
/**
|
|
24
|
-
* Maximum time to wait for webhook request to complete
|
|
25
|
-
* @default 5 seconds
|
|
26
|
-
*/
|
|
27
|
-
timeoutMs?: number;
|
|
28
|
-
headers?: HeaderRecord;
|
|
29
|
-
/**
|
|
30
|
-
* Configure retry intervals in seconds for failed webhook requests.
|
|
31
|
-
* If null, a failed handler will fail the event processor. Use carefully.
|
|
32
|
-
*/
|
|
33
|
-
retryOpts?: IRetryHandlerOpts | null;
|
|
34
|
-
splitBy?: ISplitFn<T>;
|
|
35
|
-
jsonifier?: JSONifier;
|
|
36
|
-
serialiseEvent?(ev: IReadEvent, logger: Logger): SerialisedEvent;
|
|
37
|
-
};
|
|
38
|
-
export interface IEventData {
|
|
39
|
-
topic: string;
|
|
40
|
-
payload: unknown;
|
|
41
|
-
metadata?: unknown;
|
|
42
|
-
}
|
|
43
|
-
export type IEvent<T extends IEventData> = (T & {
|
|
44
|
-
id: string;
|
|
45
|
-
});
|
|
46
|
-
export type PGMBEventBatcherOpts<T extends IEventData> = {
|
|
47
|
-
/**
|
|
48
|
-
* Whether a particular published message should be logged.
|
|
49
|
-
* By default, all messages are logged -- in case of certain
|
|
50
|
-
* failures, the logs can be used to replay the messages.
|
|
51
|
-
*/
|
|
52
|
-
shouldLog?(msg: T): boolean;
|
|
53
|
-
publish(...msgs: T[]): Promise<{
|
|
54
|
-
id: string;
|
|
55
|
-
}[]>;
|
|
56
|
-
logger?: Logger;
|
|
57
|
-
/**
|
|
58
|
-
* Automatically flush after this interval.
|
|
59
|
-
* Set to undefined or 0 to disable. Will need to
|
|
60
|
-
* manually call `flush()` to publish messages.
|
|
61
|
-
* @default undefined
|
|
62
|
-
*/
|
|
63
|
-
flushIntervalMs?: number;
|
|
64
|
-
/**
|
|
65
|
-
* Max number of messages to send in a batch
|
|
66
|
-
* @default 2500
|
|
67
|
-
*/
|
|
68
|
-
maxBatchSize?: number;
|
|
69
|
-
};
|
|
70
|
-
export type IReadNextEventsFn = (parmas: IReadNextEventsParams, db: IDatabaseConnection) => Promise<IReadNextEventsResult[]>;
|
|
71
|
-
export type IFindEventsFn = (parmas: IFindEventsParams, db: IDatabaseConnection) => Promise<IFindEventsResult[]>;
|
|
72
|
-
export type Pgmb2ClientOpts<T extends IEventData> = {
|
|
73
|
-
client: PgClientLike;
|
|
74
|
-
/**
|
|
75
|
-
* Globally unique identifier for this Pgmb2Client instance. All subs
|
|
76
|
-
* registered with this client will use this groupId.
|
|
77
|
-
*/
|
|
78
|
-
groupId: string;
|
|
79
|
-
logger?: Logger;
|
|
80
|
-
/** How long to sleep between polls & read fn calls */
|
|
81
|
-
sleepDurationMs?: number;
|
|
82
|
-
/**
|
|
83
|
-
* How often to mark subscriptions as active,
|
|
84
|
-
* and remove expired ones.
|
|
85
|
-
* @default 1 minute
|
|
86
|
-
*/
|
|
87
|
-
subscriptionMaintenanceMs?: number;
|
|
88
|
-
/** How often to maintain the events tables
|
|
89
|
-
* (drop old partitions, create new ones, etc)
|
|
90
|
-
* @default 5 minutes
|
|
91
|
-
*/
|
|
92
|
-
tableMaintainanceMs?: number;
|
|
93
|
-
readChunkSize?: number;
|
|
94
|
-
/**
|
|
95
|
-
* As we process in batches, a single handler taking time to finish
|
|
96
|
-
* can lead to buildup of unprocessed checkpoints. To avoid this,
|
|
97
|
-
* we keep moving forward while handlers run in the background, but
|
|
98
|
-
* to avoid an unbounded number of items being backlogged, we limit
|
|
99
|
-
* how much further we can go ahead from the earliest uncompleted checkpoint.
|
|
100
|
-
* @default 10
|
|
101
|
-
*/
|
|
102
|
-
maxActiveCheckpoints?: number;
|
|
103
|
-
/**
|
|
104
|
-
* Should this client poll for new events?
|
|
105
|
-
* @default true
|
|
106
|
-
*/
|
|
107
|
-
poll?: boolean;
|
|
108
|
-
webhookHandlerOpts?: Partial<PgmbWebhookOpts<T>>;
|
|
109
|
-
getWebhookInfo?: GetWebhookInfoFn;
|
|
110
|
-
/**
|
|
111
|
-
* Override the default readNextEvents implementation
|
|
112
|
-
*/
|
|
113
|
-
readNextEvents?: IReadNextEventsFn;
|
|
114
|
-
/**
|
|
115
|
-
* Override the default findEvents implementation
|
|
116
|
-
*/
|
|
117
|
-
findEvents?: IFindEventsFn;
|
|
118
|
-
} & Pick<PGMBEventBatcherOpts<IEventData>, 'flushIntervalMs' | 'maxBatchSize' | 'shouldLog'>;
|
|
119
|
-
export type IReadEvent<T extends IEventData = IEventData> = {
|
|
120
|
-
items: IEvent<T>[];
|
|
121
|
-
retry?: IRetryEventPayload;
|
|
122
|
-
};
|
|
123
|
-
export type RegisterSubscriptionParams = Omit<IAssertSubscriptionParams, 'groupId'>;
|
|
124
|
-
export type registerReliableHandlerParams<T extends IEventData = IEventData> = RegisterSubscriptionParams & {
|
|
125
|
-
/**
|
|
126
|
-
* Name for the retry handler, used to ensure retries for a particular
|
|
127
|
-
* handler are not mixed with another handler. This name need only be
|
|
128
|
-
* unique for a particular subscription.
|
|
129
|
-
*/
|
|
130
|
-
name?: string;
|
|
131
|
-
retryOpts?: IRetryHandlerOpts;
|
|
132
|
-
/**
|
|
133
|
-
* If provided, will split an incoming event into multiple events
|
|
134
|
-
* as determined by the function.
|
|
135
|
-
*/
|
|
136
|
-
splitBy?: ISplitFn<T>;
|
|
137
|
-
};
|
|
138
|
-
export type CreateTopicalSubscriptionOpts<T extends IEventData> = {
|
|
139
|
-
/**
|
|
140
|
-
* The topics to subscribe to.
|
|
141
|
-
*/
|
|
142
|
-
topics: T['topic'][];
|
|
143
|
-
/**
|
|
144
|
-
* To scale out processing, you can partition the subscriptions.
|
|
145
|
-
* For example, with `current: 0, total: 3`, only messages
|
|
146
|
-
* where `hashtext(e.id) % 3 == 0` will be received by this subscription.
|
|
147
|
-
* This will result in an approximate even split for all processors, the only
|
|
148
|
-
* caveat being it requires knowing the number of event processors on this
|
|
149
|
-
* subscription beforehand.
|
|
150
|
-
*/
|
|
151
|
-
partition?: {
|
|
152
|
-
current: number;
|
|
153
|
-
total: number;
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* Add any additional params to filter by.
|
|
157
|
-
* i.e "s.params @> jsonb_build_object(...additionalFilters)"
|
|
158
|
-
* The value should be a valid SQL snippet.
|
|
159
|
-
*/
|
|
160
|
-
additionalFilters?: Record<string, string>;
|
|
161
|
-
/** JSON to populate params */
|
|
162
|
-
additionalParams?: Record<string, any>;
|
|
163
|
-
expiryInterval?: RegisterSubscriptionParams['expiryInterval'];
|
|
164
|
-
};
|
|
165
|
-
export interface IEphemeralListener<T extends IEventData> extends AbortableAsyncIterator<IReadEvent<T>> {
|
|
166
|
-
id: string;
|
|
167
|
-
}
|
|
168
|
-
export type IEventHandlerContext = {
|
|
169
|
-
logger: Logger;
|
|
170
|
-
client: PgClientLike;
|
|
171
|
-
subscriptionId: string;
|
|
172
|
-
/** registered name of the handler */
|
|
173
|
-
name: string;
|
|
174
|
-
extra?: unknown;
|
|
175
|
-
};
|
|
176
|
-
export type IEventHandler<T extends IEventData = IEventData> = (item: IReadEvent<T>, ctx: IEventHandlerContext) => Promise<void>;
|
|
177
|
-
export type IRetryEventPayload = {
|
|
178
|
-
ids: string[];
|
|
179
|
-
handlerName: string;
|
|
180
|
-
retryNumber: number;
|
|
181
|
-
};
|
|
182
|
-
type SSESubscriptionOpts = Pick<RegisterSubscriptionParams, 'conditionsSql' | 'params'>;
|
|
183
|
-
export type SSERequestHandlerOpts = {
|
|
184
|
-
getSubscriptionOpts(req: IncomingMessage): Promise<SSESubscriptionOpts> | SSESubscriptionOpts;
|
|
185
|
-
/**
|
|
186
|
-
* Maximum interval to replay events for an SSE subscription.
|
|
187
|
-
* @default 5 minutes
|
|
188
|
-
*/
|
|
189
|
-
maxReplayIntervalMs?: number;
|
|
190
|
-
/**
|
|
191
|
-
* Max number of events to replay for an SSE subscription.
|
|
192
|
-
* Set to 0 to disable replaying events.
|
|
193
|
-
* @default 1000
|
|
194
|
-
*/
|
|
195
|
-
maxReplayEvents?: number;
|
|
196
|
-
jsonifier?: JSONifier;
|
|
197
|
-
};
|
|
198
|
-
export type IRetryHandlerOpts = {
|
|
199
|
-
retriesS: number[];
|
|
200
|
-
};
|
|
201
|
-
export interface JSONifier {
|
|
202
|
-
stringify(data: unknown): string;
|
|
203
|
-
parse(data: string): unknown;
|
|
204
|
-
}
|
|
205
|
-
export type ITableMutationEventData<T, N extends string> = {
|
|
206
|
-
topic: `${N}.insert`;
|
|
207
|
-
payload: T;
|
|
208
|
-
metadata: {};
|
|
209
|
-
} | {
|
|
210
|
-
topic: `${N}.delete`;
|
|
211
|
-
payload: T;
|
|
212
|
-
metadata: {};
|
|
213
|
-
} | {
|
|
214
|
-
topic: `${N}.update`;
|
|
215
|
-
/**
|
|
216
|
-
* The fields that were updated in the row
|
|
217
|
-
*/
|
|
218
|
-
payload: Partial<T>;
|
|
219
|
-
metadata: {
|
|
220
|
-
old: T;
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
export {};
|
package/lib/utils.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { CreateTopicalSubscriptionOpts, IEventData, RegisterSubscriptionParams } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Extract the date from a message ID, same as the PG function
|
|
4
|
-
*/
|
|
5
|
-
export declare function getDateFromMessageId(messageId: string): Date | undefined;
|
|
6
|
-
/**
|
|
7
|
-
* Extract the date from a subscription ID
|
|
8
|
-
*/
|
|
9
|
-
export declare function getCreateDateFromSubscriptionId(id: string): Date | undefined;
|
|
10
|
-
/**
|
|
11
|
-
* Creates subscription params for a subscription that matches
|
|
12
|
-
* 1 or more topics. Also supports partitioning the subscription
|
|
13
|
-
* such that only a subset of messages are received.
|
|
14
|
-
*/
|
|
15
|
-
export declare function createTopicalSubscriptionParams<T extends IEventData>({ topics, partition, additionalFilters, additionalParams, ...rest }: CreateTopicalSubscriptionOpts<T>): RegisterSubscriptionParams;
|
package/lib/webhook-handler.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { IEventData, IEventHandler, PgmbWebhookOpts } from './types.ts';
|
|
2
|
-
/**
|
|
3
|
-
* Create a handler that sends events to a webhook URL via HTTP POST.
|
|
4
|
-
* @param url Where to send the webhook requests
|
|
5
|
-
*/
|
|
6
|
-
export declare function createWebhookHandler<T extends IEventData>({ timeoutMs, headers, retryOpts, jsonifier, serialiseEvent }: Partial<PgmbWebhookOpts<T>>): IEventHandler;
|