payservedb 5.9.9 → 6.0.0
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 +217 -127
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -4,6 +4,9 @@ require("dotenv").config();
|
|
|
4
4
|
// Maintain a record of open connections for each database
|
|
5
5
|
const connections = {};
|
|
6
6
|
|
|
7
|
+
// Model registry to track loaded models
|
|
8
|
+
const modelRegistry = {};
|
|
9
|
+
|
|
7
10
|
// Utility function to connect to MongoDB
|
|
8
11
|
async function connectToMongoDB(
|
|
9
12
|
dbName,
|
|
@@ -57,139 +60,150 @@ async function switchDB(dbName) {
|
|
|
57
60
|
});
|
|
58
61
|
|
|
59
62
|
connections[dbName] = dbConnection; // Store the connection for reuse
|
|
60
|
-
console.log(
|
|
63
|
+
// console.log(Switched to database: ${dbName});
|
|
61
64
|
return dbConnection;
|
|
62
65
|
} catch (err) {
|
|
63
|
-
console.error(
|
|
66
|
+
// console.error(Error switching to database: ${dbName}, err);
|
|
64
67
|
throw err;
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
//
|
|
69
|
-
const
|
|
70
|
-
User:
|
|
71
|
-
AuditLog:
|
|
72
|
-
Company:
|
|
73
|
-
Customer:
|
|
74
|
-
FacilityEmailDetails:
|
|
75
|
-
Module:
|
|
76
|
-
Facility:
|
|
77
|
-
RefreshToken:
|
|
78
|
-
ApiLog:
|
|
79
|
-
ArchivedApiLog:
|
|
80
|
-
ArchivedAuditLog:
|
|
81
|
-
FacilityAsset:
|
|
82
|
-
Unit:
|
|
83
|
-
FAQ:
|
|
84
|
-
UnitAsset:
|
|
85
|
-
CombinedUnit:
|
|
86
|
-
Message:
|
|
87
|
-
SMSAfricastalking:
|
|
88
|
-
SMSMeliora:
|
|
89
|
-
EntryExit:
|
|
90
|
-
Guard:
|
|
91
|
-
Visitor:
|
|
92
|
-
VisitLog:
|
|
93
|
-
Settings:
|
|
94
|
-
Levy:
|
|
95
|
-
LevyType:
|
|
96
|
-
LevyContract:
|
|
97
|
-
Invoice:
|
|
98
|
-
Reminder:
|
|
99
|
-
Penalty:
|
|
100
|
-
|
|
101
|
-
Resident:
|
|
102
|
-
Asset:
|
|
103
|
-
DutyRoster:
|
|
104
|
-
LeaseTemplate:
|
|
105
|
-
Report:
|
|
106
|
-
Ticket:
|
|
107
|
-
Stocksandspare:
|
|
108
|
-
ServiceVendor:
|
|
109
|
-
MaintenanceServices:
|
|
110
|
-
StockRequisition:
|
|
111
|
-
LeaseAgreement:
|
|
112
|
-
WorkOrder:
|
|
113
|
-
ValueAddedService:
|
|
114
|
-
VasInvoice:
|
|
115
|
-
VasVendor:
|
|
116
|
-
Staff:
|
|
117
|
-
ServiceRequest:
|
|
118
|
-
CountryTaxRate:
|
|
119
|
-
WaterMeter:
|
|
120
|
-
DailyConsumption:
|
|
121
|
-
WaterMeterSettings:
|
|
122
|
-
AnalogBilling:
|
|
123
|
-
MeterSize:
|
|
124
|
-
WaterInvoice:
|
|
125
|
-
MeterProtocol:
|
|
126
|
-
MeterManufacturer:
|
|
127
|
-
MeterIotCard:
|
|
128
|
-
MetersDelivery:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
PurchaseOrderInvoice: require("./src/models/purchaseOrderInvoice")
|
|
190
|
-
|
|
71
|
+
// Model path registry - maps model names to their file paths
|
|
72
|
+
const modelPaths = {
|
|
73
|
+
User: "./src/models/user",
|
|
74
|
+
AuditLog: "./src/models/auditlog",
|
|
75
|
+
Company: "./src/models/company",
|
|
76
|
+
Customer: "./src/models/customer",
|
|
77
|
+
FacilityEmailDetails: "./src/models/email",
|
|
78
|
+
Module: "./src/models/module",
|
|
79
|
+
Facility: "./src/models/facility",
|
|
80
|
+
RefreshToken: "./src/models/refresh_token",
|
|
81
|
+
ApiLog: "./src/models/apilog",
|
|
82
|
+
ArchivedApiLog: "./src/models/archivedapilog",
|
|
83
|
+
ArchivedAuditLog: "./src/models/archivedauditlog",
|
|
84
|
+
FacilityAsset: "./src/models/facilityasset",
|
|
85
|
+
Unit: "./src/models/units",
|
|
86
|
+
FAQ: "./src/models/faq",
|
|
87
|
+
UnitAsset: "./src/models/unitasset",
|
|
88
|
+
CombinedUnit: "./src/models/combinedUnits",
|
|
89
|
+
Message: "./src/models/message",
|
|
90
|
+
SMSAfricastalking: "./src/models/sms_africastalking",
|
|
91
|
+
SMSMeliora: "./src/models/sms_meliora",
|
|
92
|
+
EntryExit: "./src/models/entry_exit",
|
|
93
|
+
Guard: "./src/models/guard",
|
|
94
|
+
Visitor: "./src/models/visitor",
|
|
95
|
+
VisitLog: "./src/models/visitLog",
|
|
96
|
+
Settings: "./src/models/settings",
|
|
97
|
+
Levy: "./src/models/levy",
|
|
98
|
+
LevyType: "./src/models/levytype",
|
|
99
|
+
LevyContract: "./src/models/levycontract",
|
|
100
|
+
Invoice: "./src/models/invoice",
|
|
101
|
+
Reminder: "./src/models/reminder",
|
|
102
|
+
Penalty: "./src/models/penalty",
|
|
103
|
+
Notification: "./src/models/notification",
|
|
104
|
+
Resident: "./src/models/resident",
|
|
105
|
+
Asset: "./src/models/asset",
|
|
106
|
+
DutyRoster: "./src/models/dutyroster",
|
|
107
|
+
LeaseTemplate: "./src/models/leasetemplate",
|
|
108
|
+
Report: "./src/models/report",
|
|
109
|
+
Ticket: "./src/models/tickets",
|
|
110
|
+
Stocksandspare: "./src/models/stocksandspare",
|
|
111
|
+
ServiceVendor: "./src/models/maintenance_service_vendor",
|
|
112
|
+
MaintenanceServices: "./src/models/maintenance_services",
|
|
113
|
+
StockRequisition: "./src/models/maintenancerequisition",
|
|
114
|
+
LeaseAgreement: "./src/models/leaseagreement",
|
|
115
|
+
WorkOrder: "./src/models/workorder",
|
|
116
|
+
ValueAddedService: "./src/models/valueaddedservices",
|
|
117
|
+
VasInvoice: "./src/models/vasinvoice",
|
|
118
|
+
VasVendor: "./src/models/vasvendor",
|
|
119
|
+
Staff: "./src/models/staff",
|
|
120
|
+
ServiceRequest: "./src/models/servicerequest",
|
|
121
|
+
CountryTaxRate: "./src/models/country_tax",
|
|
122
|
+
WaterMeter: "./src/models/water_meters",
|
|
123
|
+
DailyConsumption: "./src/models/smart_meter_daily_consumption",
|
|
124
|
+
WaterMeterSettings: "./src/models/water_meter_settings",
|
|
125
|
+
AnalogBilling: "./src/models/water_meter_billing",
|
|
126
|
+
MeterSize: "./src/models/water_meter_size",
|
|
127
|
+
WaterInvoice: "./src/models/water_invoice",
|
|
128
|
+
MeterProtocol: "./src/models/water_meter_communication",
|
|
129
|
+
MeterManufacturer: "./src/models/water_meter_manufacturer",
|
|
130
|
+
MeterIotCard: "./src/models/water_meter_iot_cards",
|
|
131
|
+
MetersDelivery: "./src/models/water_meters_delivery",
|
|
132
|
+
Concentrator: "./src/models/water_meter_concentrator",
|
|
133
|
+
Handover: "./src/models/handover",
|
|
134
|
+
Budget: "./src/models/budget",
|
|
135
|
+
BudgetCategory: "./src/models/budgetCategory",
|
|
136
|
+
Expense: "./src/models/expense",
|
|
137
|
+
ExpenseCategory: "./src/models/expense_category",
|
|
138
|
+
InvoiceSettings: "./src/models/levy_invoice_settings",
|
|
139
|
+
Account: "./src/models/account",
|
|
140
|
+
FacilityPaymentDetails: "./src/models/facility_payment_details",
|
|
141
|
+
DefaultPaymentDetails: "./src/models/default_payment_details",
|
|
142
|
+
Currency: "./src/models/currency_settings",
|
|
143
|
+
WaterMeterAccount: "./src/models/water_meter_account",
|
|
144
|
+
SingleDayWaterMeterHistory: "./src/models/water_meter_single_day_history",
|
|
145
|
+
DailyWaterMeterHistory: "./src/models/water_meter_daily_history",
|
|
146
|
+
MonthlyWaterMeterHistory: "./src/models/water_meter_monthly_history",
|
|
147
|
+
WaterPrepaidCredit: "./src/models/water_prepaid_credit",
|
|
148
|
+
WaterPrepaidDebit: "./src/models/water_prepaid_debit",
|
|
149
|
+
MeterLog: "./src/models/water_meter_communication_logs",
|
|
150
|
+
CashPayment: "./src/models/cashpayment",
|
|
151
|
+
VasPayment: "./src/models/vas_payments",
|
|
152
|
+
VasInvoicesQuickBooks: "./src/models/vas_invoices_upload",
|
|
153
|
+
ServiceChargePayment: "./src/models/service_charge_payments",
|
|
154
|
+
ServiceChargeInvoiceUpload: "./src/models/service_charge_invoice_upload",
|
|
155
|
+
Campaign: "./src/models/campaigns",
|
|
156
|
+
InspectionItem: "./src/models/item_inspection",
|
|
157
|
+
Supplier: "./src/models/suppliers",
|
|
158
|
+
PurchaseRequest: "./src/models/purchase_request",
|
|
159
|
+
PurchaseOrder: "./src/models/purchase_order",
|
|
160
|
+
PaymentTermMark: "./src/models/paymentTermsMarks",
|
|
161
|
+
DeliveryTimeMark: "./src/models/deliveryTimeMarks",
|
|
162
|
+
RFQDetails: "./src/models/rfq_details",
|
|
163
|
+
RFQResponse: "./src/models/rfq_response",
|
|
164
|
+
ApprovalWorkflow: "./src/models/approvalsWorkflows",
|
|
165
|
+
CommonAreaElectricityReading: "./src/models/common_area_electricity",
|
|
166
|
+
CommonAreaWaterReading: "./src/models/common_area_water",
|
|
167
|
+
CommonAreaGeneratorReading: "./src/models/common_area_generator",
|
|
168
|
+
CommonAreaUtilityAlert: "./src/models/common_area_utility_alert",
|
|
169
|
+
BookingProperty: "./src/models/bookingproperty",
|
|
170
|
+
BookingReservation: "./src/models/bookingreservation",
|
|
171
|
+
BookingConfig: "./src/models/bookingconfig",
|
|
172
|
+
BookingAnalytics: "./src/models/bookinganalytics",
|
|
173
|
+
BookingInvoice: "./src/models/booking_invoice",
|
|
174
|
+
BankDetails: "./src/models/bankdetails",
|
|
175
|
+
RevenueRecord: "./src/models/bookingrevenuerecord",
|
|
176
|
+
GLAccount: "./src/models/gl_accounts",
|
|
177
|
+
GLEntry: "./src/models/gl_entries",
|
|
178
|
+
GLAccountDoubleEntries: "./src/models/gl_account_double_entries",
|
|
179
|
+
PendingCredential: "./src/models/pendingCredentials",
|
|
180
|
+
UnitManagementTemplate: "./src/models/unitManagementTemplate",
|
|
181
|
+
PropertyManagerRevenue: "./src/models/propertyManagerRevenue",
|
|
182
|
+
PropertyManagerContract: "./src/models/propertyManagerContract",
|
|
183
|
+
BillerAddress: "./src/models/billerAddress",
|
|
184
|
+
AssetAssignment: "./src/models/assetsAssignment",
|
|
185
|
+
Wallet: "./src/models/wallet",
|
|
186
|
+
WalletTransaction: "./src/models/wallet_transactions",
|
|
187
|
+
GoodsReceivedNote: "./src/models/goodsReceivedNotes",
|
|
188
|
+
MeterCommandQueue: "./src/models/water_meter_Command_Queue",
|
|
189
|
+
FacilityDepartment: "./src/models/facility_departements",
|
|
190
|
+
EmailSmsQueue: "./src/models/email_sms_queue",
|
|
191
|
+
PurchaseOrderInvoice: "./src/models/purchaseOrderInvoice"
|
|
191
192
|
};
|
|
192
193
|
|
|
194
|
+
// Function to load specific models on demand
|
|
195
|
+
function loadModel(modelName) {
|
|
196
|
+
if (!modelPaths[modelName]) {
|
|
197
|
+
throw new Error(Model in registry);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!modelRegistry[modelName]) {
|
|
201
|
+
modelRegistry[modelName] = require(modelPaths[modelName]);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return modelRegistry[modelName];
|
|
205
|
+
}
|
|
206
|
+
|
|
193
207
|
// Function to get models dynamically from a specific database connection
|
|
194
208
|
async function getModelFromDB(dbConnection, modelName, schema) {
|
|
195
209
|
if (!dbConnection.models[modelName]) {
|
|
@@ -198,9 +212,85 @@ async function getModelFromDB(dbConnection, modelName, schema) {
|
|
|
198
212
|
return dbConnection.models[modelName]; // Return existing model if already registered
|
|
199
213
|
}
|
|
200
214
|
|
|
215
|
+
// Function to load multiple models at once
|
|
216
|
+
function loadModels(modelNames) {
|
|
217
|
+
const models = {};
|
|
218
|
+
modelNames.forEach(modelName => {
|
|
219
|
+
models[modelName] = loadModel(modelName);
|
|
220
|
+
});
|
|
221
|
+
return models;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Function to get available model names
|
|
225
|
+
function getAvailableModels() {
|
|
226
|
+
return Object.keys(modelPaths);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Function to initialize service with specific models
|
|
230
|
+
function initializeService(modelNames = []) {
|
|
231
|
+
const serviceSchemas = {};
|
|
232
|
+
const serviceModels = {};
|
|
233
|
+
let currentConnection = null;
|
|
234
|
+
|
|
235
|
+
// Load schemas for specified models
|
|
236
|
+
modelNames.forEach(modelName => {
|
|
237
|
+
if (!modelPaths[modelName]) {
|
|
238
|
+
// console.warn(Warning: Model ${modelName} not found in registry);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
serviceSchemas[modelName] = loadModel(modelName);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Enhanced connect function that also registers models
|
|
245
|
+
async function connectWithModels(dbName, secured, username, password, url, port) {
|
|
246
|
+
await connectToMongoDB(dbName, secured, username, password, url, port);
|
|
247
|
+
currentConnection = mongoose.connection;
|
|
248
|
+
|
|
249
|
+
// Register models with the current connection
|
|
250
|
+
modelNames.forEach(modelName => {
|
|
251
|
+
if (serviceSchemas[modelName] && !currentConnection.models[modelName]) {
|
|
252
|
+
serviceModels[modelName] = currentConnection.model(modelName, serviceSchemas[modelName]);
|
|
253
|
+
} else if (currentConnection.models[modelName]) {
|
|
254
|
+
serviceModels[modelName] = currentConnection.models[modelName];
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
return serviceModels;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Enhanced switchDB function that also registers models
|
|
262
|
+
async function switchDBWithModels(dbName) {
|
|
263
|
+
const dbConnection = await switchDB(dbName);
|
|
264
|
+
|
|
265
|
+
// Register models with the new connection
|
|
266
|
+
const dbModels = {};
|
|
267
|
+
modelNames.forEach(modelName => {
|
|
268
|
+
if (serviceSchemas[modelName]) {
|
|
269
|
+
dbModels[modelName] = getModelFromDB(dbConnection, modelName, serviceSchemas[modelName]);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
return { connection: dbConnection, models: dbModels };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
schemas: serviceSchemas,
|
|
278
|
+
models: serviceModels,
|
|
279
|
+
connectToMongoDB: connectWithModels,
|
|
280
|
+
switchDB: switchDBWithModels,
|
|
281
|
+
getModelFromDB,
|
|
282
|
+
loadModel,
|
|
283
|
+
loadModels,
|
|
284
|
+
getCurrentConnection: () => currentConnection
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
201
288
|
module.exports = {
|
|
202
289
|
connectToMongoDB,
|
|
203
290
|
switchDB,
|
|
204
291
|
getModelFromDB,
|
|
205
|
-
|
|
206
|
-
|
|
292
|
+
loadModel,
|
|
293
|
+
loadModels,
|
|
294
|
+
getAvailableModels,
|
|
295
|
+
initializeService
|
|
296
|
+
};
|