payservedb 8.3.8 → 8.4.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/ZOHO_INTEGRATION_SCHEMA.md +644 -0
- package/index.js +267 -266
- package/package.json +1 -1
- package/src/models/facilityInvoice.js +25 -8
- package/src/models/facilityInvoicePayment.js +5 -0
- package/src/models/water_meter_account.js +7 -1
- package/src/models/water_meter_settings.js +7 -3
- package/src/models/zohoIntegration.js +262 -0
- package/.idea/material_theme_project_new.xml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/psdb.iml +0 -12
- package/.idea/vcs.xml +0 -6
package/index.js
CHANGED
|
@@ -6,301 +6,302 @@ const connections = {};
|
|
|
6
6
|
|
|
7
7
|
// Utility function to connect to MongoDB
|
|
8
8
|
async function connectToMongoDB(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
dbName,
|
|
10
|
+
secured,
|
|
11
|
+
username,
|
|
12
|
+
password,
|
|
13
|
+
url,
|
|
14
|
+
port,
|
|
15
15
|
) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
} catch (err) {
|
|
34
|
-
console.error("Error connecting to MongoDB:", err);
|
|
35
|
-
throw err;
|
|
16
|
+
try {
|
|
17
|
+
if (secured === false) {
|
|
18
|
+
const url_ = url + ":" + port;
|
|
19
|
+
const connectionString = `mongodb://${url_}/${dbName}`;
|
|
20
|
+
await mongoose.connect(connectionString, {
|
|
21
|
+
useNewUrlParser: true,
|
|
22
|
+
});
|
|
23
|
+
console.log("Connected to MongoDB");
|
|
24
|
+
} else {
|
|
25
|
+
const url_ = `${username}:${password}@${url}:${port}`;
|
|
26
|
+
const source = "?authSource=admin";
|
|
27
|
+
const connectionString = `mongodb://${url_}/${dbName}${source}`;
|
|
28
|
+
await mongoose.connect(connectionString, {
|
|
29
|
+
useNewUrlParser: true,
|
|
30
|
+
});
|
|
31
|
+
console.log("Connected to MongoDB");
|
|
36
32
|
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
console.error("Error connecting to MongoDB:", err);
|
|
35
|
+
throw err;
|
|
36
|
+
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// Function to switch databases dynamically
|
|
40
40
|
async function switchDB(dbName) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
// Check if there's already a connection to the desired database
|
|
42
|
+
if (connections[dbName]) {
|
|
43
|
+
return connections[dbName]; // Return existing connection
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
try {
|
|
47
|
+
// Use environment variables for connection parameters - same as in connectToMongoDB
|
|
48
|
+
const username = process.env.MONGODB_USER;
|
|
49
|
+
const password = process.env.MONGODB_PASSWORD;
|
|
50
|
+
const url = process.env.MONGODB_HOST;
|
|
51
|
+
const port = process.env.MONGODB_PORT;
|
|
52
|
+
const source = "?authSource=admin";
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
const connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}${source}`;
|
|
55
|
+
const dbConnection = await mongoose.createConnection(connectionString, {
|
|
56
|
+
useNewUrlParser: true,
|
|
57
|
+
});
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
connections[dbName] = dbConnection; // Store the connection for reuse
|
|
60
|
+
console.log(`Switched to database: ${dbName}`);
|
|
61
|
+
return dbConnection;
|
|
62
|
+
} catch (err) {
|
|
63
|
+
console.error(`Error switching to database: ${dbName}`, err);
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// Importing all model files (Note: models may need to be re-registered for different connections)
|
|
69
69
|
// This maintains backward compatibility for existing services
|
|
70
70
|
const models = {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
71
|
+
User: require("./src/models/user"),
|
|
72
|
+
Company: require("./src/models/company"),
|
|
73
|
+
Customer: require("./src/models/customer"),
|
|
74
|
+
FacilityEmailDetails: require("./src/models/email"),
|
|
75
|
+
Module: require("./src/models/module"),
|
|
76
|
+
Facility: require("./src/models/facility"),
|
|
77
|
+
RefreshToken: require("./src/models/refresh_token"),
|
|
78
|
+
ApiLog: require("./src/models/apilog"),
|
|
79
|
+
ArchivedApiLog: require("./src/models/archivedapilog"),
|
|
80
|
+
FacilityAsset: require("./src/models/facilityasset"),
|
|
81
|
+
Unit: require("./src/models/units"),
|
|
82
|
+
FAQ: require("./src/models/faq"),
|
|
83
|
+
UnitAsset: require("./src/models/unitasset"),
|
|
84
|
+
CombinedUnit: require("./src/models/combinedUnits"),
|
|
85
|
+
Message: require("./src/models/message"),
|
|
86
|
+
SMSAfricastalking: require("./src/models/sms_africastalking"),
|
|
87
|
+
SMSMeliora: require("./src/models/sms_meliora"),
|
|
88
|
+
EntryExit: require("./src/models/entry_exit"),
|
|
89
|
+
Guard: require("./src/models/guard"),
|
|
90
|
+
Visitor: require("./src/models/visitor"),
|
|
91
|
+
VisitLog: require("./src/models/visitLog"),
|
|
92
|
+
Settings: require("./src/models/settings"),
|
|
93
|
+
Levy: require("./src/models/levy"),
|
|
94
|
+
LevyType: require("./src/models/levytype"),
|
|
95
|
+
LevyContract: require("./src/models/levycontract"),
|
|
96
|
+
Invoice: require("./src/models/invoice"),
|
|
97
|
+
ShortUrl: require("./src/models/short_urls"),
|
|
98
|
+
InvoicingSchedule: require("./src/models/invoicing_schedule"),
|
|
99
|
+
Reminder: require("./src/models/reminder"),
|
|
100
|
+
Penalty: require("./src/models/penalty"),
|
|
101
|
+
Notifiaction: require("./src/models/notification"),
|
|
102
|
+
Resident: require("./src/models/resident"),
|
|
103
|
+
Asset: require("./src/models/asset"),
|
|
104
|
+
DutyRoster: require("./src/models/dutyroster"),
|
|
105
|
+
LeaseTemplate: require("./src/models/leasetemplate"),
|
|
106
|
+
Report: require("./src/models/report"),
|
|
107
|
+
Ticket: require("./src/models/tickets"),
|
|
108
|
+
Stocksandspare: require("./src/models/stocksandspare"),
|
|
109
|
+
ServiceVendor: require("./src/models/maintenance_service_vendor"),
|
|
110
|
+
MaintenanceServices: require("./src/models/maintenance_services"),
|
|
111
|
+
StockRequisition: require("./src/models/maintenancerequisition"),
|
|
112
|
+
LeaseAgreement: require("./src/models/leaseagreement"),
|
|
113
|
+
WorkOrder: require("./src/models/workorder"),
|
|
114
|
+
ValueAddedService: require("./src/models/valueaddedservices"),
|
|
115
|
+
VasInvoice: require("./src/models/vasinvoice"),
|
|
116
|
+
VasVendor: require("./src/models/vasvendor"),
|
|
117
|
+
Staff: require("./src/models/staff"),
|
|
118
|
+
ServiceRequest: require("./src/models/servicerequest"),
|
|
119
|
+
CountryTaxRate: require("./src/models/country_tax"),
|
|
120
|
+
WaterMeter: require("./src/models/water_meters"),
|
|
121
|
+
DailyConsumption: require("./src/models/smart_meter_daily_consumption"),
|
|
122
|
+
WaterMeterSettings: require("./src/models/water_meter_settings"),
|
|
123
|
+
AnalogBilling: require("./src/models/water_meter_billing"),
|
|
124
|
+
MeterSize: require("./src/models/water_meter_size"),
|
|
125
|
+
WaterInvoice: require("./src/models/water_invoice"),
|
|
126
|
+
MeterProtocol: require("./src/models/water_meter_communication"),
|
|
127
|
+
MeterManufacturer: require("./src/models/water_meter_manufacturer"),
|
|
128
|
+
MeterIotCard: require("./src/models/water_meter_iot_cards"),
|
|
129
|
+
MetersDelivery: require("./src/models/water_meters_delivery"),
|
|
130
|
+
Notification: require("./src/models/notification"),
|
|
131
|
+
Concentrator: require("./src/models/water_meter_concentrator"),
|
|
132
|
+
Handover: require("./src/models/handover"),
|
|
133
|
+
Budget: require("./src/models/budget"),
|
|
134
|
+
BudgetCategory: require("./src/models/budgetCategory"),
|
|
135
|
+
Expense: require("./src/models/expense"),
|
|
136
|
+
ExpenseCategory: require("./src/models/expense_category"),
|
|
137
|
+
InvoiceSettings: require("./src/models/levy_invoice_settings"),
|
|
138
|
+
Account: require("./src/models/account"),
|
|
139
|
+
FacilityPaymentDetails: require("./src/models/facility_payment_details"),
|
|
140
|
+
DefaultPaymentDetails: require("./src/models/default_payment_details"),
|
|
141
|
+
Currency: require("./src/models/currency_settings"),
|
|
142
|
+
WaterMeterAccount: require("./src/models/water_meter_account"),
|
|
143
|
+
SingleDayWaterMeterHistory: require("./src/models/water_meter_single_day_history"),
|
|
144
|
+
DailyWaterMeterHistory: require("./src/models/water_meter_daily_history"),
|
|
145
|
+
MonthlyWaterMeterHistory: require("./src/models/water_meter_monthly_history"),
|
|
146
|
+
WaterPrepaidCredit: require("./src/models/water_prepaid_credit"),
|
|
147
|
+
WaterPrepaidDebit: require("./src/models/water_prepaid_debit"),
|
|
148
|
+
MeterLog: require("./src/models/water_meter_communication_logs"),
|
|
149
|
+
CashPayment: require("./src/models/cashpayment"),
|
|
150
|
+
VasPayment: require("./src/models/vas_payments"),
|
|
151
|
+
VasInvoicesQuickBooks: require("./src/models/vas_invoices_upload"),
|
|
152
|
+
ServiceChargePayment: require("./src/models/service_charge_payments"),
|
|
153
|
+
ServiceChargeInvoiceUpload: require("./src/models/service_charge_invoice_upload"),
|
|
154
|
+
Campaign: require("./src/models/campaigns"),
|
|
155
|
+
InspectionItem: require("./src/models/item_inspection"),
|
|
156
|
+
Supplier: require("./src/models/suppliers"),
|
|
157
|
+
PurchaseRequest: require("./src/models/purchase_request"),
|
|
158
|
+
PurchaseOrder: require("./src/models/purchase_order"),
|
|
159
|
+
PaymentTermMark: require("./src/models/paymentTermsMarks"),
|
|
160
|
+
DeliveryTimeMark: require("./src/models/deliveryTimeMarks"),
|
|
161
|
+
RFQDetails: require("./src/models/rfq_details"),
|
|
162
|
+
RFQResponse: require("./src/models/rfq_response"),
|
|
163
|
+
ApprovalWorkflow: require("./src/models/approvalsWorkflows"),
|
|
164
|
+
CommonAreaElectricityReading: require("./src/models/common_area_electricity"),
|
|
165
|
+
CommonAreaWaterReading: require("./src/models/common_area_water"),
|
|
166
|
+
CommonAreaGeneratorReading: require("./src/models/common_area_generator"),
|
|
167
|
+
CommonAreaUtilityAlert: require("./src/models/common_area_utility_alert"),
|
|
168
|
+
BookingProperty: require("./src/models/bookingproperty"),
|
|
169
|
+
BookingReservation: require("./src/models/bookingreservation"),
|
|
170
|
+
BookingConfig: require("./src/models/bookingconfig"),
|
|
171
|
+
BookingAnalytics: require("./src/models/bookinganalytics"),
|
|
172
|
+
BookingInvoice: require("./src/models/booking_invoice"),
|
|
173
|
+
BankDetails: require("./src/models/bankdetails"),
|
|
174
|
+
RevenueRecord: require("./src/models/bookingrevenuerecord"),
|
|
175
|
+
GLAccount: require("./src/models/gl_accounts"),
|
|
176
|
+
GLEntry: require("./src/models/gl_entries"),
|
|
177
|
+
GLAccountDoubleEntries: require("./src/models/gl_account_double_entries"),
|
|
178
|
+
PendingCredential: require("./src/models/pendingCredentials"),
|
|
179
|
+
UnitManagementTemplate: require("./src/models/unitManagementTemplate"),
|
|
180
|
+
PropertyManagerRevenue: require("./src/models/propertyManagerRevenue"),
|
|
181
|
+
PropertyManagerContract: require("./src/models/propertyManagerContract"),
|
|
182
|
+
BillerAddress: require("./src/models/billerAddress"),
|
|
183
|
+
AssetAssignment: require("./src/models/assetsAssignment"),
|
|
184
|
+
Wallet: require("./src/models/wallet"),
|
|
185
|
+
WalletTransaction: require("./src/models/wallet_transactions"),
|
|
186
|
+
GoodsReceivedNote: require("./src/models/goodsReceivedNotes"),
|
|
187
|
+
CommunicationStatus: require("./src/models/communication_status"),
|
|
188
|
+
MeterCommandQueue: require("./src/models/water_meter_Command_Queue"),
|
|
189
|
+
FacilityDepartment: require("./src/models/facility_departements"),
|
|
190
|
+
EmailSmsQueue: require("./src/models/email_sms_queue"),
|
|
191
|
+
PurchaseOrderInvoice: require("./src/models/purchaseOrderInvoice"),
|
|
192
|
+
PowerMeterCustomerBand: require("./src/models/powerMeterCustomerBand"),
|
|
193
|
+
PowerMeterCommunicationProtocol: require("./src/models/powerMeterCommunicationProtocol"),
|
|
194
|
+
PowerMeterDailyReading: require("./src/models/powerMeterDailyReading"),
|
|
195
|
+
PowerMeterPowerCharge: require("./src/models/powerMeterPowerCharges"),
|
|
196
|
+
PowerMeterGateway: require("./src/models/powerMeterGateways"),
|
|
197
|
+
PowerMeters: require("./src/models/powerMeters"),
|
|
198
|
+
PowerMeterSingleDayReading: require("./src/models/powerMeterSingleDayReading"),
|
|
199
|
+
PowerMeterAccount: require("./src/models/power_meter_account"),
|
|
200
|
+
PowerMeterLog: require("./src/models/power_meter_command_logs"),
|
|
201
|
+
PowerMeterManufacturer: require("./src/models/powerMetersManufacturer"),
|
|
202
|
+
PowerMeterMonthlyReading: require("./src/models/powerMeterMonthlyReading"),
|
|
203
|
+
PowerMeterSettings: require("./src/models/powerMeterSettings"),
|
|
204
|
+
PowerPrepaidDebit: require("./src/models/power_prepaid_debits"),
|
|
205
|
+
PowerPrepaidCredit: require("./src/models/power_prepaid_credits"),
|
|
206
|
+
PowerNegativeBalance: require("./src/models/power_meter_negative_balance"),
|
|
207
|
+
PowerCommandQueue: require("./src/models/power_meter_command_queue"),
|
|
208
|
+
PowerNotification: require("./src/models/power_sms_notification"),
|
|
209
|
+
PowerPrepaidOrder: require("./src/models/power_prepaid_orders"),
|
|
210
|
+
FacilityWalletTransactionsMetadata: require("./src/models/facilityWalletTransactionsMetadata"),
|
|
211
|
+
NegativeBalance: require("./src/models/water_meter_negative_amounts"),
|
|
212
|
+
MasterWorkplan: require("./src/models/master_workplan"),
|
|
213
|
+
MasterWorkplanChild: require("./src/models/master_workplan_child"),
|
|
214
|
+
DutyRosterChecklist: require("./src/models/dutyRosterChecklist"),
|
|
215
|
+
AuditTrail: require("./src/models/auditTrail"),
|
|
216
|
+
DailyChecklist: require("./src/models/dailyChecklist"),
|
|
217
|
+
CoreInvoiceSettings: require("./src/models/coreInvoiceSettings"),
|
|
218
|
+
FacilityInvoice: require("./src/models/facilityInvoice"),
|
|
219
|
+
CoreBaseSettings: require("./src/models/coreBaseSettings"),
|
|
220
|
+
Recipient: require("./src/models/facilityInvoiceRecipient"),
|
|
221
|
+
FacilityBillingPrice: require("./src/models/facilityBillingPrices"),
|
|
222
|
+
FacilityInvoicePayment: require("./src/models/facilityInvoicePayment"),
|
|
223
|
+
CommunicationUserOpt: require("./src/models/communication_user_opt"),
|
|
224
|
+
Agent: require("./src/models/agents"),
|
|
225
|
+
AgentPerformance: require("./src/models/agent_performance"),
|
|
226
|
+
CustomerTicket: require("./src/models/customer_tickets"),
|
|
227
|
+
CustomerSurvey: require("./src/models/customer_surveys"),
|
|
228
|
+
KnowledgeBase: require("./src/models/knowledge_base"),
|
|
229
|
+
KnowledgeBaseRating: require("./src/models/knowledge_base_rating"),
|
|
230
|
+
AgentNotification: require("./src/models/agent_notifications"),
|
|
231
|
+
TicketCategory: require("./src/models/tickets_category"),
|
|
232
|
+
InspectionCategory: require("./src/models/inspection_category"),
|
|
233
|
+
AgentRole: require("./src/models/agent_roles"),
|
|
234
|
+
CustomerSatisfactionSurvey: require("./src/models/customer_satisfaction_survey"),
|
|
235
|
+
AgentDepartment: require("./src/models/agent_departments"),
|
|
236
|
+
DocumentType: require("./src/models/document_type"),
|
|
237
|
+
Counter: require("./src/models/counter_schema"),
|
|
238
|
+
ZohoIntegration: require("./src/models/zohoIntegration"),
|
|
238
239
|
};
|
|
239
240
|
|
|
240
241
|
// Function to get models dynamically from a specific database connection
|
|
241
242
|
async function getModelFromDB(dbConnection, modelName, schema) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
243
|
+
if (!dbConnection.models[modelName]) {
|
|
244
|
+
return dbConnection.model(modelName, schema); // Register the model in the db connection
|
|
245
|
+
}
|
|
246
|
+
return dbConnection.models[modelName]; // Return existing model if already registered
|
|
246
247
|
}
|
|
247
248
|
|
|
248
249
|
// Function to initialize service with specific models - NEW FEATURE
|
|
249
250
|
function initializeService(modelNames = []) {
|
|
250
|
-
|
|
251
|
+
let currentConnection = null;
|
|
251
252
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
253
|
+
// Enhanced connect function that only registers specified models
|
|
254
|
+
async function connectWithModels(
|
|
255
|
+
dbName,
|
|
256
|
+
secured,
|
|
257
|
+
username,
|
|
258
|
+
password,
|
|
259
|
+
url,
|
|
260
|
+
port,
|
|
261
|
+
) {
|
|
262
|
+
await connectToMongoDB(dbName, secured, username, password, url, port);
|
|
263
|
+
currentConnection = mongoose.connection;
|
|
263
264
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
265
|
+
// Only register the models specified in modelNames
|
|
266
|
+
modelNames.forEach((modelName) => {
|
|
267
|
+
if (models[modelName]) {
|
|
268
|
+
let schema;
|
|
268
269
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
270
|
+
// Extract schema from the imported model
|
|
271
|
+
if (models[modelName].schema) {
|
|
272
|
+
schema = models[modelName].schema;
|
|
273
|
+
} else {
|
|
274
|
+
console.warn(`Unable to extract schema from model ${modelName}`);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
276
277
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
278
|
+
if (!currentConnection.models[modelName]) {
|
|
279
|
+
const modelInstance = currentConnection.model(modelName, schema);
|
|
280
|
+
// Make model globally accessible through payservedb
|
|
281
|
+
module.exports[modelName] = modelInstance;
|
|
282
|
+
} else {
|
|
283
|
+
module.exports[modelName] = currentConnection.models[modelName];
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
console.warn(`Model ${modelName} not found in models registry`);
|
|
287
|
+
}
|
|
288
|
+
});
|
|
288
289
|
|
|
289
|
-
|
|
290
|
-
|
|
290
|
+
return module.exports;
|
|
291
|
+
}
|
|
291
292
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
293
|
+
return {
|
|
294
|
+
connectToMongoDB: connectWithModels,
|
|
295
|
+
switchDB,
|
|
296
|
+
getModelFromDB,
|
|
297
|
+
getCurrentConnection: () => currentConnection,
|
|
298
|
+
};
|
|
298
299
|
}
|
|
299
300
|
|
|
300
301
|
module.exports = {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
302
|
+
connectToMongoDB,
|
|
303
|
+
switchDB,
|
|
304
|
+
getModelFromDB,
|
|
305
|
+
initializeService,
|
|
306
|
+
...models,
|
|
306
307
|
};
|