|
| 1 | +const config = require('config'); |
| 2 | +const async = require('async'); |
| 3 | +const pg = require('pg'); |
| 4 | + |
| 5 | +// In order to enable SSL, it seems to be enough to set `pg.defaults.ssl` |
| 6 | +// to `true` or to an empty object. With Heroku however, we also need to |
| 7 | +// explicitly allow self-signed SSL certificates. That's why we set |
| 8 | +// `rejectUnauthorized` to `false`. |
| 9 | +if (config.get('database.ssl')) { |
| 10 | + pg.defaults.ssl = { |
| 11 | + rejectUnauthorized: false, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +module.exports = { |
| 16 | + client: 'pg', |
| 17 | + connection: config.get('database.url'), |
| 18 | + debug: config.get('database.debug'), |
| 19 | + migrations: { |
| 20 | + directory: './database/migrations', |
| 21 | + tableName: 'knex_migrations', |
| 22 | + }, |
| 23 | + seeds: { |
| 24 | + directory: './database/seeds', |
| 25 | + }, |
| 26 | + acquireConnectionTimeout: 10000, |
| 27 | + pool: { |
| 28 | + min: config.get('database.connections.min'), |
| 29 | + max: config.get('database.connections.max'), |
| 30 | + // This method can be used to execute SQL statements right after a |
| 31 | + // connection has been established. |
| 32 | + afterCreate: (conn, done) => { |
| 33 | + const queries = [ |
| 34 | + // Terminate any session with an open transaction that has been idle |
| 35 | + // for longer than the specified duration in milliseconds. |
| 36 | + `set idle_in_transaction_session_timeout to ${config.get('database.transactionTimeout')};`, |
| 37 | + |
| 38 | + // Abort any statement that takes more than the specified number of |
| 39 | + // milliseconds, starting from the time the command arrives at the |
| 40 | + // server from the client. |
| 41 | + `set statement_timeout to ${config.get('database.queryTimeout')};`, |
| 42 | + ]; |
| 43 | + |
| 44 | + async.eachSeries(queries, async (query) => new Promise((resolve, reject) => { |
| 45 | + // Execute the query and wait for the callback to check if any |
| 46 | + // error occurred. |
| 47 | + conn.query(query, (error) => { |
| 48 | + if (error) { |
| 49 | + // The query returned an error, we'll have to abort the further |
| 50 | + // execution of any remaining queries and return the error. |
| 51 | + reject(error); |
| 52 | + } else { |
| 53 | + // The query successfully completed, we can execute the next one. |
| 54 | + resolve(); |
| 55 | + } |
| 56 | + }); |
| 57 | + })) |
| 58 | + .then(() => { |
| 59 | + // All queries have been successfully run and no error occurred. |
| 60 | + // Tell Knex that we're done with the setup of the connection. |
| 61 | + done(false, conn); |
| 62 | + }) |
| 63 | + .catch((error) => { |
| 64 | + // If `error` is not falsy, the connection is discarded from the pool. |
| 65 | + // If connection acquire was triggered by a query, then the error is |
| 66 | + // passed to query promise. |
| 67 | + done(error, conn); |
| 68 | + }); |
| 69 | + }, |
| 70 | + }, |
| 71 | +}; |
0 commit comments