JavaScript request filter - Postgres

The JavaScript request filter is the most flexible of all request filters. It can potentially be called for any packet type, and can modify traffic in any way it sees fit.

Parameters

  • Packet types : optional. If specified, a comma-separated list of packet types for which this filter should be called, for instance: Parse,Bind,Close. Spaces are ignored.

Context

The following variables will always be defined when your JavaScript executes:

  • log : the log object

  • context : contains all the variables you'll commonly use:

    • packet : the packet being filtered

    • result : the result object, used to cancel the current packet if desired

    • filterContext: an object containing variables attached to this filter. Any changes to this object will be visible to all invocations of this filter.

    • connectionContext: an object containing variables attached to the connection. Any changes to this object will be visible to all filters defined on this connection.

Example

When connecting, it's common for a database client to issue the command set extra_float_digits = n (Postgres documentation). We could force it to a different value with the following filter defined for Parse packets:

let sql = context.packet.getQuery();
if (sql.toLowerCase().startsWith("set extra_float_digits = ")) {
log.info("Settings extra_float_digits to 2");
context.packet.setQuery("set extra_float_digits = 2");
}