2025-01-24 10:33:13 +00:00
|
|
|
require('dotenv').config({
|
|
|
|
|
debug: true,
|
|
|
|
|
encoding: 'utf8',
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-23 10:04:10 +00:00
|
|
|
const {
|
|
|
|
|
POSTGRES_USER = 'postgres',
|
2025-01-23 13:29:58 +00:00
|
|
|
POSTGRES_PASSWORD = null,
|
2022-06-23 10:04:10 +00:00
|
|
|
POSTGRES_HOST = 'localhost',
|
|
|
|
|
POSTGRES_PORT = 5432,
|
|
|
|
|
POSTGRES_DB = 'registry',
|
2022-10-05 09:48:05 +00:00
|
|
|
POSTGRES_SSL = null,
|
2022-06-23 10:04:10 +00:00
|
|
|
POSTGRES_CONNECTION_STRING = null,
|
|
|
|
|
} = process.env;
|
|
|
|
|
|
|
|
|
|
function cn(dbName = POSTGRES_DB) {
|
2025-01-23 13:29:58 +00:00
|
|
|
const user = encodeURIComponent(POSTGRES_USER);
|
|
|
|
|
const password =
|
|
|
|
|
typeof POSTGRES_PASSWORD === 'string' ? `:${encodeURIComponent(POSTGRES_PASSWORD)}` : '';
|
|
|
|
|
const host = encodeURIComponent(POSTGRES_HOST);
|
|
|
|
|
const dbNameEncoded = encodeURIComponent(dbName);
|
|
|
|
|
const sslMode = POSTGRES_SSL ? 'require' : 'disable';
|
|
|
|
|
|
2022-06-23 10:04:10 +00:00
|
|
|
return (
|
|
|
|
|
POSTGRES_CONNECTION_STRING ||
|
2025-01-23 13:29:58 +00:00
|
|
|
`postgres://${user}${password}@${host}:${POSTGRES_PORT}/${dbNameEncoded}?sslmode=${sslMode}`
|
2022-06-23 10:04:10 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = cn;
|