payservedb 6.1.3 → 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 +12 -11
- 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,37 +207,37 @@ 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
212
|
let currentConnection = null;
|
|
212
213
|
|
|
213
|
-
// Enhanced connect function that
|
|
214
|
+
// Enhanced connect function that only registers specified models
|
|
214
215
|
async function connectWithModels(dbName, secured, username, password, url, port) {
|
|
215
216
|
await connectToMongoDB(dbName, secured, username, password, url, port);
|
|
216
217
|
currentConnection = mongoose.connection;
|
|
217
218
|
|
|
218
|
-
//
|
|
219
|
+
// Only register the models specified in modelNames
|
|
219
220
|
modelNames.forEach(modelName => {
|
|
220
221
|
if (models[modelName]) {
|
|
221
|
-
// Check if it's already a mongoose model or if it has a schema property
|
|
222
222
|
let schema;
|
|
223
|
+
|
|
224
|
+
// Extract schema from the imported model
|
|
223
225
|
if (models[modelName].schema) {
|
|
224
226
|
schema = models[modelName].schema;
|
|
225
|
-
} else if (typeof models[modelName] === 'function' && models[modelName].prototype) {
|
|
226
|
-
// It's likely already a model, skip it
|
|
227
|
-
console.warn(`Model ${modelName} appears to already be a Mongoose model`);
|
|
228
|
-
return;
|
|
229
227
|
} else {
|
|
230
|
-
schema
|
|
228
|
+
console.warn(`Unable to extract schema from model ${modelName}`);
|
|
229
|
+
return;
|
|
231
230
|
}
|
|
232
231
|
|
|
233
232
|
if (!currentConnection.models[modelName]) {
|
|
234
233
|
const modelInstance = currentConnection.model(modelName, schema);
|
|
235
|
-
// Make model globally accessible
|
|
234
|
+
// Make model globally accessible through payservedb
|
|
236
235
|
module.exports[modelName] = modelInstance;
|
|
237
236
|
} else {
|
|
238
237
|
module.exports[modelName] = currentConnection.models[modelName];
|
|
239
238
|
}
|
|
239
|
+
} else {
|
|
240
|
+
console.warn(`Model ${modelName} not found in models registry`);
|
|
240
241
|
}
|
|
241
242
|
});
|
|
242
243
|
|
|
@@ -256,5 +257,5 @@ module.exports = {
|
|
|
256
257
|
switchDB,
|
|
257
258
|
getModelFromDB,
|
|
258
259
|
initializeService,
|
|
259
|
-
...models, // Spread operator to export all models
|
|
260
|
+
...models, // Spread operator to export all models for backward compatibility
|
|
260
261
|
};
|