@loomcore/api 0.2.31 → 0.2.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,13 +1,22 @@
|
|
|
1
|
+
function isAtlasSrvHost(host) {
|
|
2
|
+
return host.toLowerCase().endsWith(".mongodb.net");
|
|
3
|
+
}
|
|
1
4
|
export function buildMongoUrl(config) {
|
|
2
5
|
const { database } = config;
|
|
3
6
|
if (!database) {
|
|
4
7
|
throw new Error("Database configuration is required to build the MongoDB URL.");
|
|
5
8
|
}
|
|
6
9
|
const { username, password, host, port, name } = database;
|
|
7
|
-
if (!username || !password || !host || !
|
|
8
|
-
throw new Error("Database configuration must include user, password, host,
|
|
10
|
+
if (!username || !password || !host || !name) {
|
|
11
|
+
throw new Error("Database configuration must include user, password, host, and name to build the MongoDB URL.");
|
|
9
12
|
}
|
|
10
13
|
const encodedUsername = encodeURIComponent(username);
|
|
11
14
|
const encodedPassword = encodeURIComponent(password);
|
|
15
|
+
if (isAtlasSrvHost(host)) {
|
|
16
|
+
return `mongodb+srv://${encodedUsername}:${encodedPassword}@${host}/${name}?authSource=admin&retryWrites=true&w=majority`;
|
|
17
|
+
}
|
|
18
|
+
if (!port) {
|
|
19
|
+
throw new Error("Database configuration must include port to build a non-SRV MongoDB URL.");
|
|
20
|
+
}
|
|
12
21
|
return `mongodb://${encodedUsername}:${encodedPassword}@${host}:${port}/${name}`;
|
|
13
22
|
}
|