cleaning up npm packages
This commit is contained in:
@@ -7,14 +7,37 @@ let pool: pg.Pool | null = null;
|
||||
|
||||
function getPool(): pg.Pool {
|
||||
if (pool) return pool;
|
||||
const connectionString = env.DATABASE_URL;
|
||||
if (!connectionString) {
|
||||
|
||||
// Advanced override: a full connection string still wins if someone sets it.
|
||||
if (env.DATABASE_URL) {
|
||||
pool = new pg.Pool({ connectionString: env.DATABASE_URL });
|
||||
return pool;
|
||||
}
|
||||
|
||||
// Normal path: build the connection from individually-named settings so the
|
||||
// username, password, host, etc. are each easy to find and change in .env.
|
||||
const host = env.DB_HOST || 'localhost';
|
||||
const port = Number(env.DB_PORT || '5432');
|
||||
const user = env.DB_USER;
|
||||
const password = env.DB_PASSWORD;
|
||||
const database = env.DB_NAME;
|
||||
|
||||
const missing = [
|
||||
['DB_USER', user],
|
||||
['DB_PASSWORD', password],
|
||||
['DB_NAME', database]
|
||||
]
|
||||
.filter(([, v]) => !v)
|
||||
.map(([k]) => k);
|
||||
if (missing.length) {
|
||||
// Fail loudly on first query rather than mysteriously later.
|
||||
throw new Error(
|
||||
'DATABASE_URL is not set. Copy .env.example to .env (or set it in docker-compose) and try again.'
|
||||
`Database is not configured. Missing: ${missing.join(', ')}. ` +
|
||||
'Copy .env.example to .env and fill in the DB_* values (or set them in docker-compose).'
|
||||
);
|
||||
}
|
||||
pool = new pg.Pool({ connectionString });
|
||||
|
||||
pool = new pg.Pool({ host, port, user, password, database });
|
||||
return pool;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user