payservedb 6.1.1 → 6.1.2
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 +82 -1
- package/package.json +1 -1
- package/src/models/water_meter_concentrator.js +5 -2
package/index.js
CHANGED
|
@@ -206,9 +206,90 @@ 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
|
+
const serviceModels = {};
|
|
212
|
+
let currentConnection = null;
|
|
213
|
+
|
|
214
|
+
// Enhanced connect function that also registers models
|
|
215
|
+
async function connectWithModels(dbName, secured, username, password, url, port) {
|
|
216
|
+
await connectToMongoDB(dbName, secured, username, password, url, port);
|
|
217
|
+
currentConnection = mongoose.connection;
|
|
218
|
+
|
|
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 = {};
|
|
238
|
+
modelNames.forEach(modelName => {
|
|
239
|
+
if (models[modelName]) {
|
|
240
|
+
if (!dbConnection.models[modelName]) {
|
|
241
|
+
dbModels[modelName] = dbConnection.model(modelName, models[modelName]);
|
|
242
|
+
} else {
|
|
243
|
+
dbModels[modelName] = dbConnection.models[modelName];
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
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];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
connectToMongoDB: connectWithModels,
|
|
282
|
+
switchDB: switchDBWithModels,
|
|
283
|
+
getModels,
|
|
284
|
+
getModel,
|
|
285
|
+
getCurrentConnection: () => currentConnection
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
209
289
|
module.exports = {
|
|
210
290
|
connectToMongoDB,
|
|
211
291
|
switchDB,
|
|
212
292
|
getModelFromDB,
|
|
293
|
+
initializeService,
|
|
213
294
|
...models, // Spread operator to export all models
|
|
214
|
-
};
|
|
295
|
+
};
|
package/package.json
CHANGED
|
@@ -29,8 +29,8 @@ const concentratorSchema = new mongoose.Schema({
|
|
|
29
29
|
status: {
|
|
30
30
|
type: String,
|
|
31
31
|
required: true,
|
|
32
|
-
enum: ['
|
|
33
|
-
default: '
|
|
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
|