payservedb 6.1.2 → 6.1.3

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.
Files changed (2) hide show
  1. package/index.js +21 -56
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -208,7 +208,6 @@ async function getModelFromDB(dbConnection, modelName, schema) {
208
208
 
209
209
  // Function to initialize service with specific models
210
210
  function initializeService(modelNames = []) {
211
- const serviceModels = {};
212
211
  let currentConnection = null;
213
212
 
214
213
  // Enhanced connect function that also registers models
@@ -216,72 +215,38 @@ function initializeService(modelNames = []) {
216
215
  await connectToMongoDB(dbName, secured, username, password, url, port);
217
216
  currentConnection = mongoose.connection;
218
217
 
219
- // Register models with the current connection
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 = {};
218
+ // Register models with the current connection and make them globally accessible
238
219
  modelNames.forEach(modelName => {
239
220
  if (models[modelName]) {
240
- if (!dbConnection.models[modelName]) {
241
- dbModels[modelName] = dbConnection.model(modelName, models[modelName]);
221
+ // Check if it's already a mongoose model or if it has a schema property
222
+ let schema;
223
+ if (models[modelName].schema) {
224
+ 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;
242
229
  } else {
243
- dbModels[modelName] = dbConnection.models[modelName];
230
+ schema = models[modelName];
231
+ }
232
+
233
+ if (!currentConnection.models[modelName]) {
234
+ const modelInstance = currentConnection.model(modelName, schema);
235
+ // Make model globally accessible
236
+ module.exports[modelName] = modelInstance;
237
+ } else {
238
+ module.exports[modelName] = currentConnection.models[modelName];
244
239
  }
245
240
  }
246
241
  });
247
242
 
248
- return { connection: dbConnection, models: dbModels };
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];
243
+ return module.exports;
278
244
  }
279
245
 
280
246
  return {
281
247
  connectToMongoDB: connectWithModels,
282
- switchDB: switchDBWithModels,
283
- getModels,
284
- getModel,
248
+ switchDB,
249
+ getModelFromDB,
285
250
  getCurrentConnection: () => currentConnection
286
251
  };
287
252
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "6.1.2",
3
+ "version": "6.1.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"