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:
log : A Logger object to output messages to the logging system
context : a container object with the following properties:
packet : the packet being processed
packetType : the type of the packet, equivalent to context.packet.getPacketType()
filterContext : the variables that persist through the life of the filter
adapterContext : the variables that persist through the life of this database connection
Request filters types
startup message filter: invoked when a startup message packet is received from the client. Can be used to reject login for certain users, or changing the default database.
command filter : invoked whenever any command is issued by the client
query filter : invoked whenever any query is issued by the client
logging filter : logs the most commonly seen client requests
JavaScript filter : can get called for any type of packet sent by the client
prepared statement execution filter: invoked when an already-prepared statement is executed
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;
}