← back to Vendor Recrawl Dispatcher

lib/db.js

28 lines

'use strict';
// Postgres pool for the LOCAL dw_unified mirror on mac3.
// Reads DATABASE_URL from the DW ImportNewSkufromURL/.env.local; falls back to a
// trust-auth local connection string (dw_admin connects without a password locally).
const fs = require('fs');
const { Pool } = require('pg');
const cfg = require('./config');

function loadDbUrl() {
  if (process.env.DATABASE_URL) return process.env.DATABASE_URL;
  try {
    const txt = fs.readFileSync(cfg.DB_ENV_FILE, 'utf8');
    const m = txt.match(/^DATABASE_URL=(.+)$/m);
    if (m) return m[1].trim();
  } catch (_) { /* fall through */ }
  // trust-auth fallback (verified: local dw_admin needs no password)
  return 'postgresql://dw_admin@127.0.0.1:5432/dw_unified';
}

const pool = new Pool({
  connectionString: loadDbUrl(),
  max: 4,
  connectionTimeoutMillis: 5000,
  idleTimeoutMillis: 10000,
});

module.exports = { pool };