import _ from "lodash"
import {Pool} from "generic-pool"
import {native as pg} from "pg"
import config from "./config"

let pool = new Pool({
  create(next) {
    let client = new pg.Client({
      user: config.get("user"),
      password: config.get("password"),
      database: config.get("name"),
      port: config.get("port"),
      host: config.get("host"),
    })

    client.connect(function(err) {
      if (err) next(err)
      else next(null, client)
    })
  },

  destroy(client) {
    client.end()
  },

  // Maximum number of resources to create at any given time (default=1)
  // TODO: Make configurable
  max: 20,

  // Minimum number of resources to keep in pool at any given time
  // TODO: Make configurable
  min: 0,

  // Max milliseconds a resource can go unused before it should be destroyed
  idleTimeoutMillis: 30000,

  // if true, logs via console.log - can also be a function
  // NOTE: Investigate
  log: false
})

export default pool

// Hook into termination and interrupt signals to gracefully exit
let gracefulExit = _.once(function() {
  pool.drain(function() {
    pool.destroyAllNow()
  })
})

process.on("SIGTERM", gracefulExit)
process.on("SIGINT", gracefulExit)

@@ -962,241 +962,4 @@
 ool%0A
-%0A// Hook into termination and interrupt signals to gracefully exit%0Alet gracefulExit = _.once(function() %7B%0A  pool.drain(function() %7B%0A    pool.destroyAllNow()%0A  %7D)%0A%7D)%0A%0Aprocess.on(%22SIGTERM%22, gracefulExit)%0Aprocess.on(%22SIGINT%22, gracefulExit)%0A

Remove gracefulExit