Skip to content

Safe Manual Forwarding

Vitaly Tomilov edited this page Jul 4, 2020 · 3 revisions

If you choose to forward events manually, and somehow fail to pass in the correct and complete set of expected parameters, the monitor may not handle the notification correctly, and even throw an error.

The following approach (in TypeScript) makes it less error-prone, by making sure that you do not need to know event parameters, in order to pass them all correctly into the monitor.

import {IInitOptions} from 'pg-promise';
import * as monitor from 'pg-monitor';

// helps forwarding all event parameters into the monitor correctly always:
function forward(event: monitor.LogEvent, args: IArguments) {
    (monitor as any)[event].apply(monitor, [...args]);
}

// example of forwarding events manually:
const options: IInitOptions = {
    connect() {
        forward('connect', arguments);
    },
    disconnect() {
        forward('disconnect', arguments);
    },
    query() {
        forward('query', arguments);
    },
    error() {
        forward('error', arguments);
    },
    task() {
        forward('task', arguments);
    },
    transact() {
        forward('transact', arguments);
    }
};
Clone this wiki locally