forked from andywer/pg-listen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.d.ts
74 lines (74 loc) · 3.08 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import TypedEventEmitter from "typed-emitter";
import pg = require("pg");
export interface PgParsedNotification {
processId: number;
channel: string;
payload?: any;
}
interface PgListenEvents {
connected: () => void;
error: (error: Error) => void;
notification: (notification: PgParsedNotification) => void;
reconnect: (attempt: number) => void;
}
declare type EventsToEmitterHandlers<Events extends Record<string, any>> = {
[channelName in keyof Events]: (payload: Events[channelName]) => void;
};
export interface Options {
/**
* Using native PG client? Defaults to false.
*/
native?: boolean;
/**
* Interval in ms to run a trivial query on the DB to see if
* the database connection still works.
* Defaults to 30s.
*/
paranoidChecking?: number | false;
/**
* How much time to wait between reconnection attempts (if failed).
* Can also be a callback returning a delay in milliseconds.
* Defaults to 500 ms.
*/
retryInterval?: number | ((attempt: number) => number);
/**
* How many attempts to reconnect after connection loss.
* Defaults to no limit, but a default retryTimeout is set.
*/
retryLimit?: number;
/**
* Timeout in ms after which to stop retrying and just fail. Defaults to 3000 ms.
*/
retryTimeout?: number;
/**
* Custom function to control how the payload data is stringified on `.notify()`.
* Use together with the `serialize` option. Defaults to `JSON.parse`.
*/
parse?: (serialized: string) => any;
/**
* Custom function to control how the payload data is stringified on `.notify()`.
* Use together with the `parse` option. Defaults to `JSON.stringify`.
*/
serialize?: (data: any) => string;
}
export interface Subscriber<Events extends Record<string, any> = {
[channel: string]: any;
}> {
/** Emits events: "error", "notification" & "redirect" */
events: TypedEventEmitter<PgListenEvents>;
/** For convenience: Subscribe to distinct notifications here, event name = channel name */
notifications: TypedEventEmitter<EventsToEmitterHandlers<Events>>;
/** Don't forget to call this asyncronous method before doing your thing */
connect(): Promise<void>;
close(): Promise<void>;
getSubscribedChannels(): string[];
listenTo(channelName: string): Promise<pg.QueryResult> | undefined;
notify<EventName extends keyof Events>(channelName: any extends Events[EventName] ? EventName : void extends Events[EventName] ? never : EventName, payload: Events[EventName] extends void ? never : Events[EventName]): Promise<pg.QueryResult>;
notify<EventName extends keyof Events>(channelName: void extends Events[EventName] ? EventName : never): Promise<pg.QueryResult>;
unlisten(channelName: string): Promise<pg.QueryResult> | undefined;
unlistenAll(): Promise<pg.QueryResult>;
}
declare function createPostgresSubscriber<Events extends Record<string, any> = {
[channel: string]: any;
}>(connectionConfig?: pg.ClientConfig, options?: Options): Subscriber<Events>;
export default createPostgresSubscriber;