payservedb 6.1.1 → 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.
package/index.js CHANGED
@@ -206,9 +206,55 @@ async function getModelFromDB(dbConnection, modelName, schema) {
206
206
  return dbConnection.models[modelName]; // Return existing model if already registered
207
207
  }
208
208
 
209
+ // Function to initialize service with specific models
210
+ function initializeService(modelNames = []) {
211
+ let currentConnection = null;
212
+
213
+ // Enhanced connect function that also registers models
214
+ async function connectWithModels(dbName, secured, username, password, url, port) {
215
+ await connectToMongoDB(dbName, secured, username, password, url, port);
216
+ currentConnection = mongoose.connection;
217
+
218
+ // Register models with the current connection and make them globally accessible
219
+ modelNames.forEach(modelName => {
220
+ if (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;
229
+ } else {
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];
239
+ }
240
+ }
241
+ });
242
+
243
+ return module.exports;
244
+ }
245
+
246
+ return {
247
+ connectToMongoDB: connectWithModels,
248
+ switchDB,
249
+ getModelFromDB,
250
+ getCurrentConnection: () => currentConnection
251
+ };
252
+ }
253
+
209
254
  module.exports = {
210
255
  connectToMongoDB,
211
256
  switchDB,
212
257
  getModelFromDB,
258
+ initializeService,
213
259
  ...models, // Spread operator to export all models
214
- };
260
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "6.1.1",
3
+ "version": "6.1.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -29,8 +29,8 @@ const concentratorSchema = new mongoose.Schema({
29
29
  status: {
30
30
  type: String,
31
31
  required: true,
32
- enum: ['Online', 'Offline', 'Maintenance'],
33
- default: 'Offline'
32
+ enum: ['online', 'offline', 'maintenance'],
33
+ default: '0ffline'
34
34
  },
35
35
  meter_fails: {
36
36
  type: Number,
@@ -55,6 +55,9 @@ const concentratorSchema = new mongoose.Schema({
55
55
  min: -180,
56
56
  max: 180
57
57
  }
58
+ },
59
+ lastUpdated: {
60
+ type: String
58
61
  }
59
62
  }, {
60
63
  timestamps: true