@itentialopensource/adapter-generic 0.1.8 → 0.2.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.
Files changed (43) hide show
  1. package/.eslintrc.js +1 -0
  2. package/AUTH.md +6 -6
  3. package/BROKER.md +4 -4
  4. package/CALLS.md +9 -9
  5. package/ENHANCE.md +3 -3
  6. package/PROPERTIES.md +24 -9
  7. package/README.md +24 -23
  8. package/SUMMARY.md +2 -2
  9. package/TAB1.md +1 -1
  10. package/TAB2.md +6 -6
  11. package/TROUBLESHOOT.md +10 -1
  12. package/UTILITIES.md +473 -0
  13. package/adapter.js +5 -5
  14. package/adapterBase.js +52 -16
  15. package/package.json +24 -28
  16. package/pronghorn.json +15 -13
  17. package/propertiesSchema.json +68 -7
  18. package/report/adapterInfo.json +7 -7
  19. package/report/updateReport1748551642673.json +120 -0
  20. package/sampleProperties.json +4 -0
  21. package/test/integration/adapterTestBasicGet.js +88 -54
  22. package/test/integration/adapterTestConnectivity.js +15 -16
  23. package/test/integration/adapterTestIntegration.js +12 -45
  24. package/test/unit/adapterBaseTestUnit.js +641 -39
  25. package/test/unit/adapterTestUnit.js +116 -149
  26. package/utils/adapterInfo.js +114 -164
  27. package/utils/argParser.js +44 -0
  28. package/utils/checkMigrate.js +77 -38
  29. package/utils/entitiesToDB.js +53 -42
  30. package/utils/logger.js +26 -0
  31. package/utils/modify.js +56 -55
  32. package/utils/mongoDbConnection.js +79 -0
  33. package/utils/mongoUtils.js +162 -0
  34. package/utils/taskMover.js +31 -32
  35. package/utils/tbScript.js +36 -172
  36. package/utils/tbUtils.js +84 -226
  37. package/utils/troubleshootingAdapter.js +68 -84
  38. package/utils/updateAdapterConfig.js +158 -0
  39. package/utils/addAuth.js +0 -94
  40. package/utils/artifactize.js +0 -146
  41. package/utils/basicGet.js +0 -50
  42. package/utils/packModificationScript.js +0 -35
  43. package/utils/patches2bundledDeps.js +0 -90
@@ -0,0 +1,44 @@
1
+ const customLevels = {
2
+ spam: 6,
3
+ trace: 5,
4
+ debug: 4,
5
+ info: 3,
6
+ warn: 2,
7
+ error: 1,
8
+ none: 0
9
+ };
10
+
11
+ function parseArgs(argv = process.argv) {
12
+ let properties = null;
13
+ let logLevel = 'none';
14
+ let maxCalls = 5;
15
+ let host = null;
16
+
17
+ argv.forEach((val) => {
18
+ if (val.startsWith('--PROPS=')) {
19
+ // get the properties
20
+ const inputVal = val.split('=')[1];
21
+ properties = JSON.parse(inputVal);
22
+ } else if (val.startsWith('--LOG=')) {
23
+ // get the desired log level
24
+ const level = val.split('=')[1];
25
+ // validate the log level is supported, if so set it
26
+ if (Object.hasOwnProperty.call(customLevels, level)) {
27
+ logLevel = level;
28
+ }
29
+ } else if (val.startsWith('--MAXCALLS=')) {
30
+ const override = parseInt(val.split('=')[1], 10);
31
+ if (!Number.isNaN(override) && override > 0) {
32
+ maxCalls = override;
33
+ }
34
+ } else if (val.startsWith('--HOST=')) {
35
+ [, host] = val.split('=');
36
+ }
37
+ });
38
+
39
+ return {
40
+ properties, logLevel, maxCalls, host
41
+ };
42
+ }
43
+
44
+ module.exports = { parseArgs };
@@ -1,63 +1,102 @@
1
+ /* @copyright Itential, LLC 2025 */
2
+
3
+ /**
4
+ * This script will determine if the adapter needs to be upgraded, a migration is needed or
5
+ * a remediation is needed. This is self contained and depends on accessing GitLab repos as well as
6
+ * finding files within the adapter to gather and compare information.
7
+ *
8
+ * This utility is executed from a script in the package.json by `npm run adapter:checkMigrate`. As a result,
9
+ * this utility is exposed and available to customers but exclusively through the CLI.
10
+ */
11
+
1
12
  const { execSync } = require('child_process');
2
- const fs = require('fs');
3
13
  const semver = require('semver');
4
- const axios = require('axios');
5
14
  const packageJson = require('../package.json');
15
+ const { get } = require('./tbUtils');
6
16
 
17
+ const localAdaptVer = packageJson.version;
7
18
  const localEngineVer = packageJson.engineVersion;
8
19
  const localUtils = execSync('npm list @itentialopensource/adapter-utils', { encoding: 'utf-8' });
9
20
  const localUtilsVer = localUtils.split('@').pop().replace(/(\r\n|\n|\r| )/gm, '');
10
21
 
11
22
  /**
12
- * @summary Makes a GET call using axios
23
+ * @summary Checks if adapter is the latest version
13
24
  *
14
- * @function get
15
- * @param {String} url - url to make the call to
25
+ * @function updateNeeded
16
26
  */
17
- function get(url) {
18
- const config = {
19
- method: 'get',
20
- url
21
- };
22
- return axios(config);
27
+ async function updateNeeded() {
28
+ const adapterUrl = `https://registry.npmjs.org/${packageJson.name}`;
29
+ const latestAdapterVer = (await get(adapterUrl)).data['dist-tags'].latest;
30
+ console.log('\n[Upgrade Check]');
31
+ console.log(`Local Adapter Version : ${localAdaptVer}`);
32
+ console.log(`Latest Adapter Version: ${latestAdapterVer}`);
33
+ return semver.lt(localAdaptVer, latestAdapterVer);
23
34
  }
24
35
 
25
36
  /**
26
- * @summary Checks if adapter can be migrated using migration package
37
+ * @summary Checks if adapter is up-to-date or if migration is needed
27
38
  *
28
- * @function migratePossible
39
+ * @function migrateNeeded
29
40
  */
30
- function migratePossible() {
31
- const adapterTestUnit = fs.readFileSync('./test/unit/adapterTestUnit.js', { encoding: 'utf-8' });
32
- const readme = fs.readFileSync('./README.md', { encoding: 'utf-8' });
33
- return packageJson.keywords !== null && adapterTestUnit.indexOf('DO NOT REMOVE THIS COMMENT BLOCK') !== -1
34
- && readme.indexOf('available at ') !== -1 && readme.indexOf('You will need to change the credentials and possibly the host information below.') !== -1;
41
+ async function migrateNeeded() {
42
+ const engineUrl = 'https://adapters.itential.io/engineVersion';
43
+ const latestEngineVer = (await get(engineUrl)).data;
44
+ console.log('\n[Migration Check]');
45
+ console.log(`Local Engine Version : ${localEngineVer}`);
46
+ console.log(`Latest Engine Version: ${latestEngineVer}`);
47
+ return semver.lt(localEngineVer, latestEngineVer);
35
48
  }
36
49
 
37
50
  /**
38
- * @summary Checks if adapter is up-to-date or if migration is needed
51
+ * @summary Checks if adapter is up-to-date or if remediation is needed
39
52
  *
40
- * @function migrateNeeded
53
+ * @function remediationNeeded
41
54
  */
42
- async function migrateNeeded() {
43
- const engineUrl = 'https://adapters.itential.io/engineVersion';
55
+ async function remediationNeeded() {
44
56
  const utilsUrl = 'https://registry.npmjs.org/@itentialopensource/adapter-utils';
45
- const latestEngineVer = (await get(engineUrl)).data;
46
57
  const latestUtilsVer = (await get(utilsUrl)).data['dist-tags'].latest;
47
- return semver.lt(localEngineVer, latestEngineVer) || semver.lt(localUtilsVer, latestUtilsVer);
58
+ console.log('\n[Remediation Check]');
59
+ console.log(`Local Utils Version : ${localUtilsVer}`);
60
+ console.log(`Latest Utils Version: ${latestUtilsVer}`);
61
+ return semver.lt(localUtilsVer, latestUtilsVer);
48
62
  }
49
63
 
50
- // Main Script
51
- if (migratePossible()) {
52
- migrateNeeded().then((needed) => {
53
- if (needed) {
54
- console.log('Migration is needed and possible -- go to dev site to download migration package');
55
- } else {
56
- console.log('Migration is possible but not needed at the current time.');
57
- }
58
- }).catch((error) => {
59
- console.log('Could not get latest engine or utils version.', error.message);
60
- });
61
- } else {
62
- console.log('Migration is not possible. Please contact Itential support for assistance');
63
- }
64
+ /**
65
+ * @summary Main Script (rest of file)
66
+ *
67
+ * Input - None
68
+ * Process - gets the adapter version from the package.json and compares it to the latest version of the adapter,
69
+ * then get the engine version from the package.json and compare it to the adapter engine version in the repo.
70
+ * then get the local adapter-utils version and compare that to the latest version of adapter-utils.
71
+ * Output - console logs providing state and path forward
72
+ *
73
+ */
74
+ updateNeeded().then((needed) => {
75
+ if (needed) {
76
+ console.log('You should update the adapter to the latest available version -- git pull');
77
+ } else {
78
+ console.log('Update is not needed at the current time.');
79
+ }
80
+ }).catch((error) => {
81
+ console.log('Could not get latest adapter version. Confirm the adapter is an open source adapter.', error.message);
82
+ });
83
+
84
+ migrateNeeded().then((needed) => {
85
+ if (needed) {
86
+ console.log('Migration is needed -- if open source, request Itential migrate the adapter');
87
+ } else {
88
+ console.log('Migration is not needed at the current time.');
89
+ }
90
+ }).catch((error) => {
91
+ console.log('Could not get latest engine version. Confirm the adapter was built by the Itential Adapter Builder.', error.message);
92
+ });
93
+
94
+ remediationNeeded().then((needed) => {
95
+ if (needed) {
96
+ console.log('Remediation is needed -- update the version of adapter-utils in the package.json, remove node modules and package-lock and run npm install');
97
+ } else {
98
+ console.log('Remediation is not needed at the current time.');
99
+ }
100
+ }).catch((error) => {
101
+ console.log('Could not get latest utils version. Confirm the adapter utilizes the Itential adapter foundation.', error.message);
102
+ });
@@ -16,18 +16,19 @@
16
16
 
17
17
  const fs = require('fs');
18
18
  const path = require('path');
19
- const utils = require('./tbUtils');
20
-
21
- // get the pronghorn database information
22
- const getPronghornProps = async (iapDir) => {
23
- log.trace('Retrieving properties.json file...');
24
- const rawProps = require(path.join(iapDir, 'properties.json'));
25
- log.trace('Decrypting properties...');
26
- const { PropertyEncryption } = require('@itential/itential-utils');
27
- const propertyEncryption = new PropertyEncryption();
28
- const pronghornProps = await propertyEncryption.decryptProps(rawProps);
29
- log.trace('Found properties.\n');
30
- return pronghornProps;
19
+ const tbUtils = require('./tbUtils');
20
+ const mongoUtils = require('./mongoUtils');
21
+
22
+ /**
23
+ * Function to load sample properties from the file system
24
+ */
25
+ const loadSampleProperties = async () => {
26
+ const samplePath = path.join(__dirname, '../sampleProperties.json');
27
+ if (fs.existsSync(samplePath)) {
28
+ const fullProps = JSON.parse(fs.readFileSync(samplePath, 'utf-8'));
29
+ return fullProps.properties.mongo;
30
+ }
31
+ throw new Error('sampleProperties.json not found');
31
32
  };
32
33
 
33
34
  /**
@@ -77,22 +78,23 @@ const buildDoc = (pathstring) => {
77
78
  /**
78
79
  * Function used to get the database from the options or a provided directory
79
80
  */
80
- const optionsHandler = (options) => {
81
- // if the database properties were provided in the options - return them
82
- if (options.pronghornProps) {
83
- if (typeof options.pronghornProps === 'string') {
84
- return JSON.parse(options.pronghornProps);
85
- }
86
- return new Promise((resolve, reject) => resolve(options.pronghornProps));
87
- }
88
-
89
- // if the directory was provided, get the pronghorn props from the directory
90
- if (options.iapDir) {
91
- return getPronghornProps(options.iapDir);
81
+ const optionsHandler = async (options) => {
82
+ try {
83
+ // Try mongo properties from service config first
84
+ const mongoPronghornProps = options?.pronghornProps?.mongo;
85
+ const validatedPronghornProps = mongoPronghornProps ? mongoUtils.getAndValidateMongoProps(mongoPronghornProps) : undefined;
86
+ if (validatedPronghornProps) return validatedPronghornProps;
87
+
88
+ // Fallback to sample properties
89
+ const sampleProps = await loadSampleProperties();
90
+ const validatedSampleProps = mongoUtils.getAndValidateMongoProps(sampleProps);
91
+ if (validatedSampleProps) return validatedSampleProps;
92
+
93
+ throw new Error('No mongo properties provided! Need either a complete URL or both host and database');
94
+ } catch (error) {
95
+ log.error('Error in optionsHandler:', error.message);
96
+ throw error;
92
97
  }
93
-
94
- // if nothing was provided, error
95
- return new Promise((resolve, reject) => reject(new Error('Neither pronghornProps nor iapDir defined in options!')));
96
98
  };
97
99
 
98
100
  /**
@@ -102,6 +104,7 @@ const moveEntitiesToDB = async (targetPath, options) => {
102
104
  // set local variables
103
105
  let myOpts = options;
104
106
  let myPath = targetPath;
107
+ let mongoConnection = null;
105
108
 
106
109
  // if we got a string parse into a JSON object
107
110
  if (typeof myOpts === 'string') {
@@ -118,15 +121,17 @@ const moveEntitiesToDB = async (targetPath, options) => {
118
121
  throw new Error('Adapter ID required!');
119
122
  }
120
123
 
121
- // get the pronghorn database properties
122
- return optionsHandler(options).then(async (currentProps) => {
123
- // Check valid filepath provided
124
- if (!myPath) {
125
- // if no path use the current directory without the utils
126
- myPath = path.join(__dirname, '../');
127
- } else if (myPath.slice(-1) === '/') {
128
- myPath = myPath.slice(0, -1);
129
- }
124
+ // Check valid filepath provided
125
+ if (!myPath) {
126
+ // if no path use the current directory without the utils
127
+ myPath = path.join(__dirname, '../');
128
+ } else if (myPath.slice(-1) === '/') {
129
+ myPath = myPath.slice(0, -1);
130
+ }
131
+
132
+ try {
133
+ // get the database properties from adapter service instance configs
134
+ const currentProps = await optionsHandler(options);
130
135
 
131
136
  // verify set the entity path
132
137
  const entitiesPath = `${myPath}/entities`;
@@ -165,15 +170,21 @@ const moveEntitiesToDB = async (targetPath, options) => {
165
170
  });
166
171
 
167
172
  // Upload documents to db collection
168
- const db = await utils.connect(currentProps).catch((err) => { console.error(err); throw err; });
169
- if (!db) {
170
- console.error('Error occured when connectiong to database', currentProps);
173
+ mongoConnection = await tbUtils.connect(currentProps).catch((err) => { log.error(err); throw err; });
174
+ if (!mongoConnection) {
175
+ log.error('Error occurred when connecting to database', currentProps);
171
176
  throw new Error('Database not found');
172
177
  }
173
- const collection = db.collection(myOpts.targetCollection);
174
- const res = await collection.insertMany(docs, { checkKeys: false }).catch((err) => { console.error(err); throw err; });
178
+
179
+ const collection = mongoConnection.db.collection(myOpts.targetCollection);
180
+ const res = await collection.insertMany(docs, { checkKeys: false }).catch((err) => { log.error(err); throw err; });
175
181
  return res;
176
- });
182
+ } finally {
183
+ // Ensure connection is closed even if an error occurs
184
+ if (mongoConnection) {
185
+ await tbUtils.closeConnection(mongoConnection);
186
+ }
187
+ }
177
188
  };
178
189
 
179
190
  module.exports = { moveEntitiesToDB };
@@ -0,0 +1,26 @@
1
+ // utils/logger.js
2
+ const winston = require('winston');
3
+ const { parseArgs } = require('./argParser');
4
+
5
+ const customLevels = {
6
+ spam: 6,
7
+ trace: 5,
8
+ debug: 4,
9
+ info: 3,
10
+ warn: 2,
11
+ error: 1,
12
+ none: 0
13
+ };
14
+
15
+ // Only set global logger if it doesn't already exist (i.e., not provided by app)
16
+ if (!global.log) {
17
+ const { logLevel = 'info' } = parseArgs();
18
+
19
+ global.log = winston.createLogger({
20
+ level: logLevel,
21
+ levels: customLevels,
22
+ transports: [new winston.transports.Console()]
23
+ });
24
+ }
25
+
26
+ module.exports = global.log;
package/utils/modify.js CHANGED
@@ -1,29 +1,7 @@
1
1
  const { execSync } = require('child_process');
2
2
  const fs = require('fs-extra');
3
- const Ajv = require('ajv');
4
3
  const rls = require('readline-sync');
5
4
  const { existsSync } = require('fs-extra');
6
- const { getAdapterConfig } = require('./tbUtils');
7
- const { name } = require('../package.json');
8
- const propertiesSchema = require('../propertiesSchema.json');
9
-
10
- const flags = process.argv[2];
11
-
12
- /**
13
- * @summary Updates database instance with new adapter properties
14
- *
15
- * @function updateServiceItem
16
- */
17
- async function updateServiceItem() {
18
- const { database, serviceItem } = await getAdapterConfig();
19
- const currentProps = serviceItem.properties.properties;
20
- const ajv = new Ajv({ allErrors: true, useDefaults: true });
21
- const validate = ajv.compile(propertiesSchema);
22
- validate(currentProps);
23
- console.log('Updating Properties...');
24
- await database.collection('service_configs').updateOne({ model: name }, { $set: serviceItem });
25
- console.log('Properties Updated');
26
- }
27
5
 
28
6
  /**
29
7
  * @summary Creates a backup zip file of current adapter
@@ -70,21 +48,20 @@ function revertMod() {
70
48
  fs.removeSync(file);
71
49
  }
72
50
  });
73
- // // unzip previousVersion, reinstall dependencies and delete zipfile
51
+ // unzip previousVersion, reinstall dependencies and delete zipfile
74
52
  execSync('unzip -o previousVersion.zip && rm -rf node_modules && rm package-lock.json && npm install', { maxBuffer: 1024 * 1024 * 2 });
75
53
  execSync('rm previousVersion.zip');
76
54
  console.log('Changes have been reverted');
77
55
  }
78
56
 
79
- // Main Script
80
-
81
- // Migrate
82
- if (flags === '-m') {
83
- if (!fs.existsSync('migrationPackage.zip')) {
84
- console.log('Migration Package not found. Download and place migrationPackage in the adapter root directory');
85
- process.exit();
57
+ /**
58
+ * @summary Handle migration logic
59
+ */
60
+ function handleMigration() {
61
+ if (!existsSync('migrationPackage.zip')) {
62
+ throw new Error('Migration Package not found. Download and place migrationPackage in the adapter root directory');
86
63
  }
87
- // Backup current adapter
64
+
88
65
  backup();
89
66
  console.log('Migrating adapter and running tests...');
90
67
  const migrateCmd = 'unzip -o migrationPackage.zip'
@@ -92,61 +69,85 @@ if (flags === '-m') {
92
69
  + ' && node migrate';
93
70
  const migrateOutput = execSync(migrateCmd, { encoding: 'utf-8' });
94
71
  console.log(migrateOutput);
95
- if (migrateOutput.indexOf('Lint exited with code 1') >= 0
96
- || migrateOutput.indexOf('Tests exited with code 1') >= 0) {
72
+
73
+ if (migrateOutput.includes('Lint exited with code 1') || migrateOutput.includes('Tests exited with code 1')) {
97
74
  if (rls.keyInYN('Adapter failed tests or lint after migrating. Would you like to revert the changes?')) {
98
75
  console.log('Reverting changes...');
99
76
  revertMod();
100
- process.exit();
77
+ throw new Error('Adapter failed tests or lint after migrating. Changes reverted');
101
78
  }
102
79
  console.log('Adapter Migration will continue. If you want to revert the changes, run the command npm run adapter:revert');
103
80
  }
81
+
104
82
  console.log('Installing new dependencies..');
105
83
  const updatePackageCmd = 'rm -rf node_modules && rm package-lock.json && npm install';
106
84
  const updatePackageOutput = execSync(updatePackageCmd, { encoding: 'utf-8' });
107
85
  console.log(updatePackageOutput);
108
86
  console.log('New dependencies installed');
109
- console.log('Updating adapter properties..');
110
- updateServiceItem().then(() => {
111
- console.log('Adapter Successfully Migrated. Restart adapter in IAP to apply the changes');
112
- archiveMod('MIG');
113
- process.exit();
114
- });
87
+ archiveMod('MIG');
115
88
  }
116
89
 
117
- // Update
118
- if (flags === '-u') {
119
- if (!fs.existsSync('updatePackage.zip')) {
120
- console.log('Update Package not found. Download and place updateAdapter.zip in the adapter root directory');
121
- process.exit();
90
+ /**
91
+ * @summary Handle update logic
92
+ */
93
+ function handleUpdate() {
94
+ if (!existsSync('updatePackage.zip')) {
95
+ throw new Error('Update Package not found. Download and place updateAdapter.zip in the adapter root directory');
122
96
  }
123
- // Backup current adapter
97
+
124
98
  backup();
125
99
  const updateCmd = 'unzip -o updatePackage.zip'
126
100
  + ' && cd adapter_modifications'
127
101
  + ' && node update.js updateFiles';
128
102
  execSync(updateCmd, { encoding: 'utf-8' });
129
103
  const updateOutput = execSync(updateCmd, { encoding: 'utf-8' });
130
- if (updateOutput.indexOf('Lint exited with code 1') >= 0
131
- || updateOutput.indexOf('Tests exited with code 1') >= 0) {
104
+
105
+ if (updateOutput.includes('Lint exited with code 1') || updateOutput.includes('Tests exited with code 1')) {
132
106
  if (rls.keyInYN('Adapter failed tests or lint after updating. Would you like to revert the changes?')) {
133
107
  console.log('Reverting changes...');
134
108
  revertMod();
135
- process.exit();
109
+ throw new Error('Adapter failed tests or lint after updating. Changes reverted');
136
110
  }
137
111
  console.log('Adapter Update will continue. If you want to revert the changes, run the command npm run adapter:revert');
138
112
  }
113
+
139
114
  console.log(updateOutput);
140
115
  console.log('Adapter Successfully Updated. Restart adapter in IAP to apply the changes');
141
116
  archiveMod('UPD');
142
- process.exit();
143
117
  }
144
118
 
145
- // Revert
146
- if (flags === '-r') {
147
- if (!fs.existsSync('previousVersion.zip')) {
148
- console.log('Previous adapter version not found. There are no changes to revert');
149
- process.exit();
119
+ /**
120
+ * @summary Handle revert logic
121
+ */
122
+ function handleRevert() {
123
+ if (!existsSync('previousVersion.zip')) {
124
+ throw new Error('Previous adapter version not found. There are no changes to revert');
150
125
  }
151
126
  revertMod();
152
127
  }
128
+
129
+ /**
130
+ * @summary Entrypoint for the script
131
+ */
132
+ function main() {
133
+ const flags = process.argv[2];
134
+
135
+ switch (flags) {
136
+ case '-m':
137
+ return handleMigration();
138
+ case '-u':
139
+ return handleUpdate();
140
+ case '-r':
141
+ return handleRevert();
142
+ default:
143
+ throw new Error('Invalid flag. Use -m for migrate, -u for update, or -r for revert.');
144
+ }
145
+ }
146
+
147
+ try {
148
+ main();
149
+ process.exit(0);
150
+ } catch (error) {
151
+ console.error(error.message || error);
152
+ process.exit(1);
153
+ }
@@ -0,0 +1,79 @@
1
+ // Set globals
2
+ /* global log */
3
+
4
+ const { MongoClient } = require('mongodb');
5
+ const MongoUtils = require('./mongoUtils');
6
+
7
+ class MongoDBConnection {
8
+ constructor(properties) {
9
+ this.properties = properties;
10
+ this.initialize(properties);
11
+ }
12
+
13
+ initialize(properties) {
14
+ const {
15
+ url, database, maxPoolSize, appname
16
+ } = properties;
17
+
18
+ // Handle URL first - if provided, it takes precedence
19
+ if (url) {
20
+ const urlObj = new URL(url);
21
+ const urlDbName = urlObj.pathname.slice(1);
22
+ this.dbName = database || urlDbName;
23
+
24
+ // Update URL if database name is different
25
+ if (this.dbName !== urlDbName) {
26
+ urlObj.pathname = `${this.dbName}`;
27
+ }
28
+
29
+ this.url = urlObj.toString();
30
+ } else {
31
+ const connectionObj = MongoUtils.generateConnectionObj(properties);
32
+ this.url = MongoUtils.generateConnectionString(connectionObj);
33
+ this.dbName = database;
34
+ }
35
+
36
+ // Set options using generateTlsSettings
37
+ this.options = MongoUtils.generateTlsSettings(properties);
38
+
39
+ // Add maxPoolSize if configured
40
+ if (maxPoolSize > 0 && maxPoolSize <= 65535) {
41
+ this.options.maxPoolSize = maxPoolSize;
42
+ }
43
+
44
+ // Add application name if provided
45
+ if (appname) {
46
+ this.options.appname = appname;
47
+ }
48
+ }
49
+
50
+ async closeConnection() {
51
+ if (this.connection && this.connection.close) {
52
+ try {
53
+ await this.connection.close();
54
+ } catch (err) {
55
+ log.error(`Failed to close MongoDB connection - ${err.message}`);
56
+ }
57
+ }
58
+ }
59
+
60
+ async connect() {
61
+ const client = new MongoClient(this.url, this.options);
62
+
63
+ client.on('serverHeartbeatSucceeded', (msg) => log.info(`Connection established and heartbeat succeeded - ${JSON.stringify(msg)}`));
64
+ client.on('connectionClosed', (msg) => log.info(`Connection closed - ${JSON.stringify(msg)}`));
65
+ client.on('error', (msg) => log.error(`Connection error - ${JSON.stringify(msg)}`));
66
+ client.on('commandFailed', (msg) => log.error(`Command failed - ${JSON.stringify(msg)}`));
67
+ client.on('serverHeartbeatFailed', (msg) => log.error(`Connection timeout - ${JSON.stringify(msg)}`));
68
+
69
+ await client.connect().catch((error) => {
70
+ throw new Error(MongoUtils.resolveMongoError(error));
71
+ });
72
+
73
+ this.db = client.db(this.dbName);
74
+ this.connection = client;
75
+ return this;
76
+ }
77
+ }
78
+
79
+ module.exports = MongoDBConnection;