Skip to content

Commit b647074

Browse files
committed
fix issues with db connection in backup process
1 parent 9f64fcd commit b647074

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

src/config.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,9 @@ function getEnvOrThrow(key: string): string {
2929
}
3030

3131
export function loadConfig(): Config {
32-
let auth = '';
33-
let host = process.env.MONGODB_HOST || 'localhost:27017';
34-
let srv = process.env.MONGO_SRV ? '+srv' : '';
35-
36-
if (process.env.MONGO_INITDB_ROOT_USERNAME) {
37-
auth = `${process.env.MONGO_INITDB_ROOT_USERNAME}:${process.env.MONGO_INITDB_ROOT_PASSWORD}@`;
38-
}
39-
4032
return {
4133
mongodb: {
42-
uri: `mongodb${srv}://${auth}${host}`,
34+
uri: buildMongoUri(),
4335
database: getEnvOrThrow("MONGODB_DATABASE"),
4436
},
4537
aws: {
@@ -55,3 +47,23 @@ export function loadConfig(): Config {
5547
mongodumpPath: process.env.MONGODUMP_PATH || "mongodump",
5648
};
5749
}
50+
51+
function buildMongoUri(): string {
52+
let auth = '';
53+
let host = process.env.MONGODB_HOST || 'localhost:27017';
54+
host = host.endsWith('/') ? host.slice(0, -1) : host;
55+
let srv = process.env.MONGO_SRV ? '+srv' : '';
56+
let queryParams = '';
57+
58+
if (process.env.MONGO_INITDB_ROOT_USERNAME) {
59+
const username = encodeURIComponent(process.env.MONGO_INITDB_ROOT_USERNAME);
60+
const password = encodeURIComponent(process.env.MONGO_INITDB_ROOT_PASSWORD || '');
61+
auth = `${username}:${password}@`;
62+
}
63+
64+
// Add authSource if specified
65+
if (process.env.MONGO_AUTH_SOURCE) {
66+
queryParams = `authSource=${process.env.MONGO_AUTH_SOURCE}`;
67+
}
68+
return `mongodb${srv}://${auth}${host}/?${queryParams}`;
69+
}

0 commit comments

Comments
 (0)