Skip to content

added postConnectionQuery configuration #137

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ config: {
ssl: false
};
```
Optionally, *postConnectionQuery* can be used to specify any SQL that should be run after opening a connection, prior to running any other query. This is intended for setting [client connection defaults](http://www.postgresql.org/docs/9.4/static/runtime-config-client.html). For example:
```javascript
config: {
url: 'postgres://username:password@hostname:port/database',
postConnectionQuery: 'SET extra_float_digits = 3;'
};
```

We are also testing features for future versions of waterline in postgresql. One of these is case sensitive string searching. In order to enable this feature today you can add the following config flag:

Expand Down
38 changes: 30 additions & 8 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,8 @@ module.exports = (function() {
if(!connectionObject) return cb(Errors.InvalidConnection);

// If the connection details were supplied as a URL use that. Otherwise,
// connect using the configuration object as is.
var connectionConfig = connectionObject.config;
// connect using the configuration object as is (minus 'postConnectionQuery').
var connectionConfig = _.omit(connectionObject.config, 'postConnectionQuery');
if(_.has(connectionConfig, 'url')) {
var connectionUrl = url.parse(connectionConfig.url);
connectionUrl.query = _.omit(connectionConfig, 'url');
Expand All @@ -1020,7 +1020,9 @@ module.exports = (function() {
after(err, client, done);
});

// Run logic using connection, then release/close it
// Run postConnectionQuery if it exists
// , then run logic using connection
// , then release/close the connection
function after(err, client, done) {
if(err) {
// console.error("Error creating a connection to Postgresql: " + err);
Expand Down Expand Up @@ -1123,12 +1125,32 @@ module.exports = (function() {
return cb(err);
}

logic(client, function(err, result) {
// If the connectionObject config has a 'postConnectionQuery'
// then run it if it hasn't already been run
var postConnectionQuery = connectionObject.config.postConnectionQuery;
if(postConnectionQuery && !client.connection._sails_hasRunPostConnectionQuery) {
client.query(postConnectionQuery, function(err, result) {
if(err) {
// be sure to release connection
done();
return cb(err);
}
client.connection._sails_hasRunPostConnectionQuery = true;
// Run logic using connection, then release/close it
runLogic();
});
} else {
// Run logic using connection, then release/close it
runLogic();
}

// release client connection
done();
return cb(err, result);
});
function runLogic() {
logic(client, function(err, result) {
// release client connection
done();
return cb(err, result);
});
}
}
}

Expand Down