onebeyond/systemic-knex

View on GitHub
index.js

Summary

Maintainability
A
25 mins
Test Coverage
B
84%
const knex = require('knex')

module.exports = () => {
  let instance
  let hasPool

  const start = async ({ config: configuration }) => {
    const config = configuration.knex || configuration
    try {
      instance = knex(config)
      hasPool = Boolean(config.pool) || config.client === 'pg' || config.client === 'mysql'
      return instance
    } catch (error) {
      throw new Error(error)
    }
  }

  const stop = async () => {
    if (!hasPool) { return }
    return instance.destroy()
  }

  return {
    dependsOn: ['config'],
    start,
    stop
  }
}