-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating a read only client #569
Comments
Do you want to use the connection pool or do you want to create Client instances without the connection pool? If you use var pg = require('pg');
var client = new pg.Client('postgres://testing:testing@localhost:5432/testing');
client.connect(function connect_handler(error){
if(error) { console.error(error); }
else {
var result_handler = function result_handler(error, result) {
if(error) { console.error(error); }
else {
console.log(result);
}
};
client.query('CREATE TABLE foo (bar int);', result_handler);
client.query('INSERT INTO foo (bar) VALUES (42);', result_handler);
client.query('SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;', result_handler);
client.query('DELETE FROM foo;', result_handler);
client.query('SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;', result_handler);
}
}); If you use the connection pool I would call the function every time I want to issue a query. Another solution might be to attach some state to the client and check if it's a new client after requesting the client from the pool and issue the query then. As far as I know there is no way to check if a client is new but we have a feature request here: If you need the feature please participate in the discussion. |
I wanted to use the connection pool. Ideally I'd be able to define some |
Hey @dukedave ... I just hacked up a small gist which should do what you want: execute a custom query for every new client. HTH |
@dukedave It might be cool to make that it's own module. What would be ideal is breaking the pool out of node-postgres into it's own module, making it more extensible, and then having node-postgres consume that module for backwards compatibility. I've long had plans to do that, but I've been way too busy to get to it. :( |
@hoegaarden thanks! |
I do think we can close this one. @brianc should maybe create a ticket about restructuring node-postgres? |
Have there been any related changes to more easily allow read only clients since this was last discussed? |
I'd like to ensure all the queries going though a client are read only.
I can simulate this in
psql
usingSET SESSION CHARACTERISTICS
, thus:What would be a smart way to go about doing this in node-postgres?
The text was updated successfully, but these errors were encountered: