Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict'
const objection = require('objection')
// -- Knex/PG issue: https://github.com/tgriesser/knex/issues/927
const pg = require('pg')
pg.types.setTypeParser(20, 'text', parseInt)
pg.types.setTypeParser(1700, 'text', parseFloat)
// -- end --
const knexLib = require('knex')
const R = require('ramda')
const config = require('../config')
const knexEnvConfig = require('./knexfile')[config.env]
const knexConfig = R.mergeDeepWith({}, knexEnvConfig, objection.knexSnakeCaseMappers())
const knex = knexLib(knexConfig)
const Model = objection.Model
Model.knex(knex)
const transaction = objection.transaction
function connect() {
// Knex does not have an explicit `.connect()` method so we issue a query and consider the
// connection to be open once we get the response back.
return knex.raw('select 1 + 1')
}
function close() {
return knex.destroy()
}
setupKnex() {
let { knex, log } = this.config
if (knex?.client) {
const snakeCaseOptions = knex.normalizeDbNames === true
? {}
: knex.normalizeDbNames
if (snakeCaseOptions) {
knex = {
...knex,
...knexSnakeCaseMappers(snakeCaseOptions)
}
}
this.knex = Knex(knex)
if (log.sql) {
this.setupKnexLogging()
}
}
}
export function connect() {
if (!knexInstance) {
knexInstance = knex({
...config.get('pg'),
...knexSnakeCaseMappers(),
})
Model.knex(knexInstance)
}
return knexInstance
}
export function createKnex() {
return knex({
...config.get('pg'),
...knexSnakeCaseMappers(),
})
}