import pg from 'pg'

const { Pool } = pg

const adminPoolConfig: any = {
  host: process.env.ADMIN_DB_HOST || '127.0.0.1',
  port: parseInt(process.env.ADMIN_DB_PORT || '5432'),
  database: process.env.ADMIN_DB_NAME || 'sbrain_admin',
  user: process.env.ADMIN_DB_USER || 'sbrain_admin',
  max: 5,
}

const adminDbPassword = process.env.ADMIN_DB_PASSWORD
if (adminDbPassword) {
  adminPoolConfig.password = adminDbPassword
}

const adminPool = new Pool(adminPoolConfig)

adminPool.on('error', (err) => {
  console.error('Unexpected error on admin pool', err)
})

export default adminPool
