Request Filters - Postgres

Request filters are filters that are invoked when a request is received from the database client. Request filters are given the opportunity to look at the request and do whatever they want with it: let it through, modify it, or reject it.

Request filters can potentially be called for every single type of request, such as authentication, close result set, and so on, but they are usually most useful for command requests.

A typical example is a query filter, which is called whenever a database query is received from the client. Query filters are given a chance to look at the request and do with it as they please.

Context

All request filters define the following variables, which can be accessed from your JavaScript code:

Request filters types


Advanced error fields

If a request filter rejects the request packet by calling:

context.result.success = false;

it can also optionally define a new variable called errorFields in the context object, with keys consisting of single characters, and string values, which will be used to create the error packet sent back to the client.  The exact meaning of these fields is described in the Postgres documentation about error fields and error codes (note that some fields are required).

if (context.packet.getParameter("user") === 'jdoe') {
    context.result.success = false;
    context.errorFields = {
        S: 'ERROR',
        V: 'ERROR',
        C: '00666',
        M: "I don't like you"
    };
    context.result.closeConnection = true;
}