Move mongo wrappers to their own directory
All checks were successful
DRB Tests / drb_mocha_tests (push) Successful in 29s

This commit is contained in:
Logan Cusano
2024-05-18 23:16:42 -04:00
parent cfe2cd346d
commit 47070b08ab
9 changed files with 106 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
import { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js';
import { requestNodeJoinSystem, checkIfNodeIsConnectedToVC, checkIfNodeHasOpenDiscordClient, getNodeCurrentListeningSystem } from '../../modules/socketServerWrappers.mjs';
import { getSystemsByNuid, getAllSystems, getSystemByName } from '../../modules/mongoSystemsWrappers.mjs';
import { getSystemsByNuid, getAllSystems, getSystemByName } from '../../modules/mongo-wrappers/mongoSystemsWrappers.mjs';
import { getAvailableTokensInGuild } from '../modules/wrappers.mjs';
// Exporting data property

View File

@@ -1,5 +1,5 @@
import { checkIfNodeIsConnectedToVC, getNodeDiscordID, getNodeDiscordUsername } from '../../modules/socketServerWrappers.mjs';
import { getAllDiscordIDs } from '../../modules/mongoDiscordIDWrappers.mjs'
import { getAllDiscordIDs } from '../../modules/mongo-wrappers/mongoDiscordIDWrappers.mjs'
export const checkOnlineBotsInGuild = async (nodeIo, guildId) => {

View File

@@ -0,0 +1,100 @@
// Import necessary modules
import { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config()
// MongoDB connection URI
const uri = process.env.MONGO_URL;
// Function to connect to the database
export const connectToDatabase = async () => {
try {
const client = await MongoClient.connect(uri);
return client;
} catch (error) {
console.error('Error connecting to the database:', error);
throw error;
}
};
// Function to insert a document into the collection
export const insertDocument = async (collectionName, document) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.insertOne(document);
console.log('Document inserted:', result.insertedId);
return result.insertedId;
} catch (error) {
console.error('Error inserting document:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Function to retrieve documents from the collection
export const getDocuments = async (collectionName) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const documents = await collection.find({}).toArray();
console.log('Documents retrieved:', documents);
return documents;
} catch (error) {
console.error('Error retrieving documents:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Function to retrieve a document by a specific field
export const getDocumentByField = async (collectionName, field, value) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const document = await collection.findOne({ [field]: value });
return document;
} catch (error) {
console.error('Error retrieving document:', error);
throw error;
} finally {
await db.close();
}
};
// Function to update a document by a specific field
export const updateDocumentByField = async (collectionName, field, value, updatedFields) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.updateOne({ [field]: value }, { $set: updatedFields });
console.log('Document updated:', result.modifiedCount);
return result.modifiedCount;
} catch (error) {
console.error('Error updating document:', error);
throw error;
} finally {
await db.close();
}
};
// Function to delete a document by a specific field
export const deleteDocumentByField = async (collectionName, field, value) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.deleteOne({ [field]: value });
console.log('Document deleted:', result.deletedCount);
return result.deletedCount;
} catch (error) {
console.error('Error deleting document:', error);
throw error;
} finally {
await db.close();
}
};

View File

@@ -1,53 +0,0 @@
// Import necessary modules
import { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config()
// MongoDB connection URI
const uri = process.env.MONGO_URL;
// Function to connect to the database
export const connectToDatabase = async () => {
try {
const client = await MongoClient.connect(uri);
return client;
} catch (error) {
console.error('Error connecting to the database:', error);
throw error;
}
};
// Function to insert a document into the collection
export const insertDocument = async (collectionName, document) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const result = await collection.insertOne(document);
console.log('Document inserted:', result.insertedId);
return result.insertedId;
} catch (error) {
console.error('Error inserting document:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};
// Function to retrieve documents from the collection
export const getDocuments = async (collectionName) => {
const db = await connectToDatabase();
try {
const collection = db.db().collection(collectionName);
const documents = await collection.find({}).toArray();
console.log('Documents retrieved:', documents);
return documents;
} catch (error) {
console.error('Error retrieving documents:', error);
throw error;
} finally {
// Close the connection
await db.close();
}
};

View File

@@ -1,5 +1,5 @@
import { createNode, getNodeByNuid, updateNodeByNuid } from "./mongoNodesWrappers.mjs"
import { createSystem, getSystemByName, updateSystemByName, getSystemsByNuid, deleteSystemByName } from "./mongoSystemsWrappers.mjs"
import { createNode, getNodeByNuid, updateNodeByNuid } from "./mongo-wrappers/mongoNodesWrappers.mjs"
import { createSystem, getSystemByName, updateSystemByName, getSystemsByNuid, deleteSystemByName } from "./mongo-wrappers/mongoSystemsWrappers.mjs"
/**
* Description

View File

@@ -1,8 +1,8 @@
// Import necessary modules for testing
import { expect } from 'chai';
import ioClient from 'socket.io-client';
import { deleteNodeByNuid, getNodeByNuid } from '../modules/mongoNodesWrappers.mjs';
import { deleteSystemByName, getSystemByName } from '../modules/mongoSystemsWrappers.mjs';
import { deleteNodeByNuid, getNodeByNuid } from '../modules/mongo-wrappers/mongoNodesWrappers.mjs';
import { deleteSystemByName, getSystemByName } from '../modules/mongo-wrappers/mongoSystemsWrappers.mjs';
import { nodeDisconnectWrapper, checkIfNodeHasOpenDiscordClient, getNodeCurrentListeningSystem, checkIfNodeIsConnectedToVC, getNodeDiscordUsername, getNodeDiscordID, requestBotLeaveServer, requestNodeJoinSystem, requestNodeUpdate } from '../modules/socketServerWrappers.mjs';
import { nodeIo } from '../modules/socketServer.mjs';