Skip to content

Commit 5d55db8

Browse files
committed
fix: fix mongourl parser for atlas
1 parent 33ae46c commit 5d55db8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/imports/database/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ import querystring from 'node:querystring';
33

44
import { logger } from '../utils/logger';
55

6-
const MONGODB_URL_REGEX = /^mongodb:\/\/(?:([^:]+):([^@]+)@)?([^/:]+)(?::(\d+))?\/([^?]+)(?:\?(.+))?$/;
6+
const MONGODB_URL_REGEX = /^(mongodb|mongodb\+srv):\/\/(?:([^:]+):([^@]+)@)?([^/:]+)(?::(\d+))?\/([^?]+)(?:\?(.+))?$/;
77

88
const MONGO_URL = process.env.MONGO_URL ?? 'mongodb://localhost:27017';
99

10-
const [, username, password, host, port, database, urioptions] = MONGO_URL.match(MONGODB_URL_REGEX) || [];
10+
const [, protocol, username, password, host, port, database, urioptions] = MONGO_URL.match(MONGODB_URL_REGEX) || [];
1111

1212
const options = querystring.parse(urioptions ?? {});
1313

1414
const getMongoUrl = () => {
1515
if (username != null && password != null) {
16-
return `mongodb://${username}:${password}@${host}:${port ?? 27017}`;
16+
if (protocol === 'mongodb+srv') {
17+
return `${protocol}://${username}:${password}@${host}/${database}`;
18+
}
19+
return `${protocol}://${username}:${password}@${host}:${port ?? 27017}`;
1720
}
18-
return `mongodb://${host}:${port ?? 27017}`;
21+
if (protocol === 'mongodb+srv') {
22+
return `${protocol}://${host}/${database}`;
23+
}
24+
return `${protocol}://${host}:${port ?? 27017}`;
1925
};
2026

2127
const mongoUrl = getMongoUrl();

0 commit comments

Comments
 (0)