<commit_before>'use strict';

const fs = require('fs');
const escape = require('pg-escape');

const hasDBAvailable = Boolean(process.env.DB);

if (!hasDBAvailable) {
  module.exports = {};
  return;
}

const pgConfig = {
  host: 'localhost',
  user: process.env.PGUSER || 'hmbserver',
  password: process.env.PGPASSWORD || fs.readFileSync('db-password', {encoding: 'utf8'}),
  database: process.env.PGDATABASE || 'hmb'
};

const Pool = require('pg').Pool;
const pgPool = new Pool(pgConfig);
pgPool.on('error', (err, client) => {
  logger.error('Idle psql client error', {
    errorMsg: err.message,
    errorStack: err.stack,
    client
  });
});

function insertUser(username, email, passwordHash) {
  return pgPool.query('insert into users values($1, $2, \'\', $3)', [
    escape.string(username),
    escape.string(email),
    passwordHash
  ]);
}

module.exports = {
  hasDBAvailable,
  pgPool,
  insertUser
};
<commit_msg>Fix path handling for db-password<commit_after>'use strict';

const fs = require('fs');
const path = require('path');
const escape = require('pg-escape');

const hasDBAvailable = Boolean(process.env.DB);

if (!hasDBAvailable) {
  module.exports = {};
  return;
}

const pgPass = fs.readFileSync(path.join(__dirname, 'db-password'), {encoding: 'utf8'});
const pgConfig = {
  host: 'localhost',
  user: process.env.PGUSER || 'hmbserver',
  password: process.env.PGPASSWORD || pgPass,
  database: process.env.PGDATABASE || 'hmb'
};

const Pool = require('pg').Pool;
const pgPool = new Pool(pgConfig);
pgPool.on('error', (err, client) => {
  logger.error('Idle psql client error', {
    errorMsg: err.message,
    errorStack: err.stack,
    client
  });
});

function insertUser(username, email, passwordHash) {
  return pgPool.query('insert into users values($1, $2, \'\', $3)', [
    escape.string(username),
    escape.string(email),
    passwordHash
  ]);
}

module.exports = {
  hasDBAvailable,
  pgPool,
  insertUser
};

{'fraction_non_alphanumeric': 0.1698306824012314, 'fraction_numerical': 0.004104669061056952, 'mean_word_length': 4.327868852459017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '874968', 'n_tokens_mistral': 654, 'n_tokens_neox': 663, 'n_words': 145}