payservedb 4.2.5 → 4.2.6
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 +11 -15
- package/package.json +1 -1
- package/src/models/water_meters.js +110 -0
package/index.js
CHANGED
|
@@ -9,22 +9,24 @@ const connections = {};
|
|
|
9
9
|
// Utility function to connect to MongoDB
|
|
10
10
|
async function connectToMongoDB(dbName, secured, username, password, url, port) {
|
|
11
11
|
try {
|
|
12
|
-
if (secured ===
|
|
13
|
-
|
|
14
|
-
const connectionString = `mongodb://${
|
|
12
|
+
if (secured === false) {
|
|
13
|
+
const url_ = url + ":" + port;
|
|
14
|
+
const connectionString = `mongodb://${url_}/${dbName}`;
|
|
15
15
|
await mongoose.connect(connectionString, {
|
|
16
16
|
useNewUrlParser: true,
|
|
17
17
|
});
|
|
18
|
-
console.log('Connected to MongoDB
|
|
18
|
+
console.log('Connected to MongoDB');
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
-
|
|
22
|
-
const
|
|
21
|
+
const url_ = `${username}:${password}@${url}${port}`;
|
|
22
|
+
const source = '?authSource=admin'
|
|
23
|
+
const connectionString = `mongodb://${url_}/${dbName}${source}`;
|
|
23
24
|
await mongoose.connect(connectionString, {
|
|
24
25
|
useNewUrlParser: true,
|
|
25
26
|
});
|
|
26
|
-
console.log('Connected to MongoDB
|
|
27
|
+
console.log('Connected to MongoDB');
|
|
27
28
|
}
|
|
29
|
+
|
|
28
30
|
} catch (err) {
|
|
29
31
|
console.error('Error connecting to MongoDB:', err);
|
|
30
32
|
throw err;
|
|
@@ -33,19 +35,13 @@ async function connectToMongoDB(dbName, secured, username, password, url, port)
|
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
// Function to switch databases dynamically
|
|
36
|
-
async function switchDB(dbName
|
|
38
|
+
async function switchDB(dbName) {
|
|
37
39
|
// Check if there's already a connection to the desired database
|
|
38
40
|
if (connections[dbName]) {
|
|
39
41
|
return connections[dbName]; // Return existing connection
|
|
40
42
|
}
|
|
41
43
|
try {
|
|
42
|
-
|
|
43
|
-
if (secured === true) {
|
|
44
|
-
connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}?authSource=admin`;
|
|
45
|
-
} else {
|
|
46
|
-
connectionString = `mongodb://${url}:${port}/${dbName}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
44
|
+
const connectionString = `mongodb://${url}/${dbName}${source}`;
|
|
49
45
|
const dbConnection = await mongoose.createConnection(connectionString, {
|
|
50
46
|
useNewUrlParser: true,
|
|
51
47
|
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
|
|
3
|
+
const meterSchema = new mongoose.Schema({
|
|
4
|
+
meterType: {
|
|
5
|
+
type: String,
|
|
6
|
+
required: true,
|
|
7
|
+
enum: ['analog', 'smart']
|
|
8
|
+
},
|
|
9
|
+
meterNumber: {
|
|
10
|
+
type: String,
|
|
11
|
+
required: true,
|
|
12
|
+
unique: true,
|
|
13
|
+
trim: true,
|
|
14
|
+
index: true
|
|
15
|
+
},
|
|
16
|
+
accountNumber: {
|
|
17
|
+
type: String,
|
|
18
|
+
required: true,
|
|
19
|
+
unique: true,
|
|
20
|
+
trim: true
|
|
21
|
+
},
|
|
22
|
+
facilityId: {
|
|
23
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
24
|
+
ref: 'Facility',
|
|
25
|
+
},
|
|
26
|
+
unitId: {
|
|
27
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
28
|
+
ref: 'Unit',
|
|
29
|
+
},
|
|
30
|
+
customerId: {
|
|
31
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
32
|
+
ref: 'Customer',
|
|
33
|
+
},
|
|
34
|
+
manufacturer: {
|
|
35
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
36
|
+
ref: 'MeterManufacturer',
|
|
37
|
+
},
|
|
38
|
+
protocol: {
|
|
39
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
40
|
+
ref: 'MeterProtocol',
|
|
41
|
+
},
|
|
42
|
+
size: {
|
|
43
|
+
type: mongoose.Schema.Types.ObjectId,
|
|
44
|
+
ref: 'MeterSize',
|
|
45
|
+
},
|
|
46
|
+
initialReading: {
|
|
47
|
+
type: Number,
|
|
48
|
+
required: true,
|
|
49
|
+
min: 0,
|
|
50
|
+
default: 0
|
|
51
|
+
},
|
|
52
|
+
previousReading: {
|
|
53
|
+
type: Number,
|
|
54
|
+
min: 0,
|
|
55
|
+
},
|
|
56
|
+
currentReading: {
|
|
57
|
+
type: Number,
|
|
58
|
+
min: 0,
|
|
59
|
+
default: 0
|
|
60
|
+
},
|
|
61
|
+
lastReadingDate: {
|
|
62
|
+
type: Date,
|
|
63
|
+
default: Date.now
|
|
64
|
+
},
|
|
65
|
+
readingHistory: [{
|
|
66
|
+
previousReading: Number,
|
|
67
|
+
currentReading: Number,
|
|
68
|
+
readingDate: {
|
|
69
|
+
type: Date,
|
|
70
|
+
default: Date.now
|
|
71
|
+
},
|
|
72
|
+
readBy: String,
|
|
73
|
+
consumption: Number
|
|
74
|
+
}],
|
|
75
|
+
status: {
|
|
76
|
+
type: String,
|
|
77
|
+
required: true,
|
|
78
|
+
enum: ['open', 'closed', 'maintenance', 'faulty'],
|
|
79
|
+
default: 'open',
|
|
80
|
+
},
|
|
81
|
+
customerType: {
|
|
82
|
+
type: String,
|
|
83
|
+
enum: ['postpaid', 'prepaid'],
|
|
84
|
+
},
|
|
85
|
+
isInstalled: {
|
|
86
|
+
type: Boolean,
|
|
87
|
+
default: false
|
|
88
|
+
},
|
|
89
|
+
valveType: { // Specific to smart meters
|
|
90
|
+
type: String,
|
|
91
|
+
enum: ['automatic', 'manual'],
|
|
92
|
+
default: 'automatic'
|
|
93
|
+
},
|
|
94
|
+
accountBalance: { // Specific to smart meters
|
|
95
|
+
type: Number,
|
|
96
|
+
default: 0
|
|
97
|
+
},
|
|
98
|
+
negativeBalance: { // Specific to smart meters
|
|
99
|
+
type: Number,
|
|
100
|
+
default: 0
|
|
101
|
+
},
|
|
102
|
+
}, {
|
|
103
|
+
timestamps: true
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
meterSchema.index({ meterNumber: 1, status: 1 });
|
|
107
|
+
|
|
108
|
+
const Meter = mongoose.model('Meter', meterSchema);
|
|
109
|
+
|
|
110
|
+
module.exports = Meter;
|