Migrate to Postgres over Mongo
This commit is contained in:
parent
23b1033085
commit
94e416fd35
23 changed files with 534 additions and 466 deletions
31
src/utils/postgres.js
Normal file
31
src/utils/postgres.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
// 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 };
|
Reference in a new issue