payservedb 6.1.3 → 6.1.5

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
@@ -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 also registers models
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
- // Register models with the current connection and make them globally accessible
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 = models[modelName];
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "6.1.3",
3
+ "version": "6.1.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,40 +1,9 @@
1
1
  const mongoose = require('mongoose');
2
2
 
3
3
  const powerMeterCustomerBandSchema = new mongoose.Schema({
4
- facilityId: {
5
- type: mongoose.Schema.Types.ObjectId,
6
- ref: 'Facility',
7
- required: true
8
- },
9
- meterSerialNumber: {
10
- type: String,
11
- required: true
12
- },
13
- deviceId: {
14
- type: Number,
15
- required: false
16
- },
17
- gatewayId: {
18
- type: String,
19
- required: true
20
- },
21
- meterReading: {
22
- type: Number,
23
- required: true
24
- },
25
- lastUpdated: {
26
- type: Date,
27
- required: true
28
- },
29
- manufacturer: {
4
+ name: {
30
5
  type: String,
31
6
  required: true
32
- },
33
- type: {
34
- type: String,
35
- required: true,
36
- enum: ['2 phase', '3 phase'],
37
- default: '2 phase'
38
7
  }
39
8
  }, {
40
9
  timestamps: true