Skip to content

Commit c533bfc

Browse files
committed
refactor: Update MongoDB query function to use switch statement
The MongoDB query function in main.ts was updated to use a switch statement instead of an if-else block. This improves code readability and maintainability.
1 parent 9dea5aa commit c533bfc

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

main.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const mapErrorToCompletion = (error: any, model: string): ErrorCompletion => {
7070
};
7171
};
7272

73-
// Test function to query data from MongoDB
73+
7474
async function testMongoDBQuery(query: any, connectionString: string) {
7575
console.log('Connecting to MongoDB');
7676
console.log(query);
@@ -90,10 +90,24 @@ async function testMongoDBQuery(query: any, connectionString: string) {
9090

9191
// Execute the query based on the operation
9292
let result;
93-
if (query.operation === 'find') {
94-
result = await collection.find(query.filters).toArray();
95-
} else {
96-
throw new Error(`Unsupported operation: ${query.operation}`);
93+
switch (query.operation) {
94+
case 'find':
95+
result = await collection.find(query.filters).toArray();
96+
break;
97+
case 'insert':
98+
result = await collection.insertMany(query.documents);
99+
break;
100+
case 'insertOne':
101+
result = await collection.insertOne(query.document);
102+
break;
103+
case 'update':
104+
result = await collection.updateMany(query.filters, { $set: query.update });
105+
break;
106+
case 'delete':
107+
result = await collection.deleteMany(query.filters);
108+
break;
109+
default:
110+
throw new Error(`Unsupported operation: ${query.operation}`);
97111
}
98112

99113
return result;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prompt-mixer-open-ai-with-mongodb-function-connector",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "This connector for Prompt Mixer allows you to access the OpenAI API from within Prompt Mixer. It now includes a function that can execute any query in MongoDB.",
55
"type": "module",
66
"main": "main.js",

0 commit comments

Comments
 (0)