UNPKG

785 BJavaScriptView Raw
1const fs = require('fs')
2const Knex = require('knex')
3const exitHook = require('async-exit-hook')
4const { NODE_ENV } = require('@sharyn/env')
5
6let knexConfig = require('./knex-config')
7
8let customKnexConfig
9if (fs.existsSync('src/_db/knex-config.js')) {
10 // eslint-disable-next-line import/no-unresolved, global-require
11 customKnexConfig = require('../../../src/_db/knex-config')
12}
13
14if (customKnexConfig) {
15 knexConfig = customKnexConfig
16}
17
18const knex = Knex(knexConfig)
19
20// This call is useful to test the connecion. In test this async call doesn't have time to resolve.
21if (NODE_ENV !== 'test') {
22 // eslint-disable-next-line no-console
23 knex.raw('').catch(err => console.error(err))
24}
25
26exitHook(async callback => {
27 await knex.destroy()
28 callback()
29})
30
31module.exports = knex