payservedb 6.1.2 → 6.1.4
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 +25 -59
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -66,6 +66,7 @@ async function switchDB(dbName) {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// Importing all model files (Note: models may need to be re-registered for different connections)
|
|
69
|
+
// This maintains backward compatibility for existing services
|
|
69
70
|
const models = {
|
|
70
71
|
User: require("./src/models/user"),
|
|
71
72
|
AuditLog: require("./src/models/auditlog"),
|
|
@@ -206,82 +207,47 @@ async function getModelFromDB(dbConnection, modelName, schema) {
|
|
|
206
207
|
return dbConnection.models[modelName]; // Return existing model if already registered
|
|
207
208
|
}
|
|
208
209
|
|
|
209
|
-
// Function to initialize service with specific models
|
|
210
|
+
// Function to initialize service with specific models - NEW FEATURE
|
|
210
211
|
function initializeService(modelNames = []) {
|
|
211
|
-
const serviceModels = {};
|
|
212
212
|
let currentConnection = null;
|
|
213
213
|
|
|
214
|
-
// Enhanced connect function that
|
|
214
|
+
// Enhanced connect function that only registers specified models
|
|
215
215
|
async function connectWithModels(dbName, secured, username, password, url, port) {
|
|
216
216
|
await connectToMongoDB(dbName, secured, username, password, url, port);
|
|
217
217
|
currentConnection = mongoose.connection;
|
|
218
218
|
|
|
219
|
-
//
|
|
220
|
-
modelNames.forEach(modelName => {
|
|
221
|
-
if (models[modelName] && !currentConnection.models[modelName]) {
|
|
222
|
-
serviceModels[modelName] = currentConnection.model(modelName, models[modelName]);
|
|
223
|
-
} else if (currentConnection.models[modelName]) {
|
|
224
|
-
serviceModels[modelName] = currentConnection.models[modelName];
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
return serviceModels;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Enhanced switchDB function that also registers models
|
|
232
|
-
async function switchDBWithModels(dbName) {
|
|
233
|
-
const dbConnection = await switchDB(dbName);
|
|
234
|
-
currentConnection = dbConnection;
|
|
235
|
-
|
|
236
|
-
// Register models with the new connection
|
|
237
|
-
const dbModels = {};
|
|
219
|
+
// Only register the models specified in modelNames
|
|
238
220
|
modelNames.forEach(modelName => {
|
|
239
221
|
if (models[modelName]) {
|
|
240
|
-
|
|
241
|
-
|
|
222
|
+
let schema;
|
|
223
|
+
|
|
224
|
+
// Extract schema from the imported model
|
|
225
|
+
if (models[modelName].schema) {
|
|
226
|
+
schema = models[modelName].schema;
|
|
242
227
|
} else {
|
|
243
|
-
|
|
228
|
+
console.warn(`Unable to extract schema from model ${modelName}`);
|
|
229
|
+
return;
|
|
244
230
|
}
|
|
231
|
+
|
|
232
|
+
if (!currentConnection.models[modelName]) {
|
|
233
|
+
const modelInstance = currentConnection.model(modelName, schema);
|
|
234
|
+
// Make model globally accessible through payservedb
|
|
235
|
+
module.exports[modelName] = modelInstance;
|
|
236
|
+
} else {
|
|
237
|
+
module.exports[modelName] = currentConnection.models[modelName];
|
|
238
|
+
}
|
|
239
|
+
} else {
|
|
240
|
+
console.warn(`Model ${modelName} not found in models registry`);
|
|
245
241
|
}
|
|
246
242
|
});
|
|
247
243
|
|
|
248
|
-
return
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Function to get models for the current connection
|
|
252
|
-
function getModels() {
|
|
253
|
-
if (!currentConnection) {
|
|
254
|
-
throw new Error('No active database connection. Please connect first.');
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
const connectionModels = {};
|
|
258
|
-
modelNames.forEach(modelName => {
|
|
259
|
-
if (currentConnection.models[modelName]) {
|
|
260
|
-
connectionModels[modelName] = currentConnection.models[modelName];
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
return connectionModels;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// Function to get a specific model
|
|
268
|
-
function getModel(modelName) {
|
|
269
|
-
if (!currentConnection) {
|
|
270
|
-
throw new Error('No active database connection. Please connect first.');
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (!modelNames.includes(modelName)) {
|
|
274
|
-
throw new Error(`Model ${modelName} not initialized in this service`);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return currentConnection.models[modelName];
|
|
244
|
+
return module.exports;
|
|
278
245
|
}
|
|
279
246
|
|
|
280
247
|
return {
|
|
281
248
|
connectToMongoDB: connectWithModels,
|
|
282
|
-
switchDB
|
|
283
|
-
|
|
284
|
-
getModel,
|
|
249
|
+
switchDB,
|
|
250
|
+
getModelFromDB,
|
|
285
251
|
getCurrentConnection: () => currentConnection
|
|
286
252
|
};
|
|
287
253
|
}
|
|
@@ -291,5 +257,5 @@ module.exports = {
|
|
|
291
257
|
switchDB,
|
|
292
258
|
getModelFromDB,
|
|
293
259
|
initializeService,
|
|
294
|
-
...models, // Spread operator to export all models
|
|
260
|
+
...models, // Spread operator to export all models for backward compatibility
|
|
295
261
|
};
|