@pioneer-platform/mongo-atlas 1.0.0 → 1.0.1
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.
- package/index.js +80 -4
- package/package.json +1 -1
package/index.js
CHANGED
@@ -52,10 +52,48 @@ const connection = {
|
|
52
52
|
* @returns {object} - MongoDB collection with added helper methods
|
53
53
|
*/
|
54
54
|
async get(collection, dbName, options = {}) {
|
55
|
-
|
55
|
+
// Get connection string from environment
|
56
|
+
let connectionString = process.env.MONGO_CONNECTION || 'mongodb://localhost:27017/pioneer';
|
56
57
|
const targetDb = dbName || process.env.MONGO_DEFAULT_DB || 'pioneer';
|
57
58
|
|
58
|
-
|
59
|
+
// FIXED: Strip off any database name from the connection string to ensure dbName parameter is respected
|
60
|
+
if (connectionString.includes('/')) {
|
61
|
+
// Handle MongoDB+SRV format
|
62
|
+
if (connectionString.startsWith('mongodb+srv://')) {
|
63
|
+
// For srv format, we need to handle it differently
|
64
|
+
const srvParts = connectionString.split('?');
|
65
|
+
const hostPart = srvParts[0].split('/');
|
66
|
+
// Remove any database part after the last slash before query params
|
67
|
+
if (hostPart.length > 3) {
|
68
|
+
// Take only the protocol and auth+host parts
|
69
|
+
connectionString = hostPart.slice(0, 3).join('/');
|
70
|
+
// Add back query parameters if they existed
|
71
|
+
if (srvParts.length > 1) {
|
72
|
+
connectionString += '?' + srvParts[1];
|
73
|
+
}
|
74
|
+
}
|
75
|
+
} else {
|
76
|
+
// Standard mongodb:// format
|
77
|
+
const urlParts = connectionString.split('/');
|
78
|
+
// Check if there's a database name after the host:port
|
79
|
+
if (urlParts.length > 3) {
|
80
|
+
// Get everything up to the host:port
|
81
|
+
let baseUrl = urlParts.slice(0, 3).join('/');
|
82
|
+
|
83
|
+
// Check if there are query parameters after the database name
|
84
|
+
let queryParams = '';
|
85
|
+
const dbAndParams = urlParts[3].split('?');
|
86
|
+
if (dbAndParams.length > 1) {
|
87
|
+
queryParams = '?' + dbAndParams[1];
|
88
|
+
}
|
89
|
+
|
90
|
+
// Reconstruct URL without database name
|
91
|
+
connectionString = baseUrl + queryParams;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
debug(`Getting collection ${collection} from database ${targetDb} using connection: ${connectionString}`);
|
59
97
|
|
60
98
|
try {
|
61
99
|
const client = await getOrCreateClient(connectionString, options);
|
@@ -80,10 +118,48 @@ const connection = {
|
|
80
118
|
* @returns {object} - MongoDB database instance
|
81
119
|
*/
|
82
120
|
async getDb(dbName) {
|
83
|
-
|
121
|
+
// Get connection string from environment
|
122
|
+
let connectionString = process.env.MONGO_CONNECTION || 'mongodb://localhost:27017/pioneer';
|
84
123
|
const targetDb = dbName || process.env.MONGO_DEFAULT_DB || 'pioneer';
|
85
124
|
|
86
|
-
|
125
|
+
// FIXED: Strip off any database name from the connection string to ensure dbName parameter is respected
|
126
|
+
if (connectionString.includes('/')) {
|
127
|
+
// Handle MongoDB+SRV format
|
128
|
+
if (connectionString.startsWith('mongodb+srv://')) {
|
129
|
+
// For srv format, we need to handle it differently
|
130
|
+
const srvParts = connectionString.split('?');
|
131
|
+
const hostPart = srvParts[0].split('/');
|
132
|
+
// Remove any database part after the last slash before query params
|
133
|
+
if (hostPart.length > 3) {
|
134
|
+
// Take only the protocol and auth+host parts
|
135
|
+
connectionString = hostPart.slice(0, 3).join('/');
|
136
|
+
// Add back query parameters if they existed
|
137
|
+
if (srvParts.length > 1) {
|
138
|
+
connectionString += '?' + srvParts[1];
|
139
|
+
}
|
140
|
+
}
|
141
|
+
} else {
|
142
|
+
// Standard mongodb:// format
|
143
|
+
const urlParts = connectionString.split('/');
|
144
|
+
// Check if there's a database name after the host:port
|
145
|
+
if (urlParts.length > 3) {
|
146
|
+
// Get everything up to the host:port
|
147
|
+
let baseUrl = urlParts.slice(0, 3).join('/');
|
148
|
+
|
149
|
+
// Check if there are query parameters after the database name
|
150
|
+
let queryParams = '';
|
151
|
+
const dbAndParams = urlParts[3].split('?');
|
152
|
+
if (dbAndParams.length > 1) {
|
153
|
+
queryParams = '?' + dbAndParams[1];
|
154
|
+
}
|
155
|
+
|
156
|
+
// Reconstruct URL without database name
|
157
|
+
connectionString = baseUrl + queryParams;
|
158
|
+
}
|
159
|
+
}
|
160
|
+
}
|
161
|
+
|
162
|
+
debug(`Getting database ${targetDb} using connection: ${connectionString}`);
|
87
163
|
|
88
164
|
try {
|
89
165
|
const client = await getOrCreateClient(connectionString);
|