This repository has been archived on 2024-08-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
nubian/src/utils/postgres.js

31 lines
1,000 B
JavaScript

// Imports
const { migrate } = require("postgres-migrations");
const pgp = require("pg-promise")();
const moment = require("moment");
// Ensure dates get saved as UTC date strings
// This prevents the parser from doing strange datetime operations
pgp.pg.types.setTypeParser(1114, (str) => moment.utc(str).format());
// Constants
const NUBIAN_POSTGRES_DISABLED = process.env.NUBIAN_POSTGRES_DISABLED ?? false;
const database = "nubian";
const dbConfig = {
database,
user: process.env.NUBIAN_POSTGRES_USER,
password: process.env.NUBIAN_POSTGRES_PASSWORD,
host: process.env.NUBIAN_POSTGRES_HOST,
port: 5432,
ensureDatabaseExists: true,
};
const configure = async () => {
if (NUBIAN_POSTGRES_DISABLED) return logInfo("POSTGRES", "Postgres Disabled");
await migrate(dbConfig, "src/db-migrations");
// Override the global variable DB
global.PG = pgp(dbConfig);
await PG.connect();
logSuccess("POSTGRES", `Connected to database ${database}!`);
};
module.exports = { configure };