Skip to content

Commit

Permalink
Y2024
Browse files Browse the repository at this point in the history
  • Loading branch information
vprtsingh committed Jul 27, 2024
1 parent 924a0a9 commit 2a97401
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/xml-to-mongodb-importer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ async function validateTags(xmlUrl, openingTag, closingTag) {
});
}

async function validateMongoDBConnection(connection) {
const { mongoURI, databaseName, collectionName } = connection;
let client;
try {
client = new MongoClient(mongoURI);
await client.connect();
const db = client.db(databaseName);
await db.command({ ping: 1 });
const collections = await db.listCollections({ name: collectionName }).toArray();
if (collections.length === 0) {
throw new Error(`Collection "${collectionName}" does not exist in database "${databaseName}"`);
}
return true;
} catch (error) {
throw new Error(`MongoDB connection validation failed: ${error.message}`);
} finally {
if (client) {
await client.close();
}
}
}

module.exports.runImporter = async (params) => {
return new Promise(async (resolve, reject) => {
try {
Expand All @@ -173,6 +195,7 @@ module.exports.runImporter = async (params) => {
throw new Error('Invalid connection: It should be an object with mongoURI, databaseName, and collectionName properties');
}
await validateTags(xmlUrl, openingTag, closingTag);
await validateMongoDBConnection(connection);
await createOutputFolder();
await XmlSplitter(xmlUrl, openingTag, closingTag, chunkSize);
await readFileAndSaveToMongoDB(connection, openingTag);
Expand Down

0 comments on commit 2a97401

Please sign in to comment.