serverless-pg icon indicating copy to clipboard operation
serverless-pg copied to clipboard

feat: make ServerlessClient EventEmitter

Open perrin4869 opened this issue 7 months ago • 0 comments

I've been running this patch locally for a while. It is technically a breaking change, because .on would stop attaching events directly to the internal client... But I think this is an important change, because otherwise we don't have any way to be notified of when a client is initiated. I think another problem with the old approach is that, if the _client gets refreshed at any point during the runtime, for example, by calling end, the events attached to it would disappear.

The way this issue is solved with this PR would be:

        client.on("init", (client) => {
            client.on("error", logger.error.bind(logger));
            client.on("notice", logger.info.bind(logger, "notice: "));
            client.on("notification", logger.info.bind(logger, "notification: "));
            client.on("end", logger.info.bind(logger, "end: "));
            client.on("drain", logger.info.bind(logger, "drain: "));
        });

With this method, if end and connect are called, the events will be reattached to the new client

perrin4869 avatar May 13 '25 14:05 perrin4869