Nexus
Discord
  • 📗Getting Started
    • Nexus Website
  • 👥Contributors
  • 🤝Code of Conduct
  • 🚨License
  • 📔Contributing
  • git
    • 🍎Installing Git on MacOS
    • 🪟Installing Git on Windows
    • 🐧Installing Git on Linux
    • 🛠️Git Guide
  • Building the Bot
    • Before you begin
    • First Steps
    • The .env file
    • Custom Bot Functions
  • Commands
    • Creating Commands
    • Creating User Installed Commands
    • Creating Developer Commands
    • Developer Guild Commands
    • Autocomplete
  • Client Modifications
    • client.m
  • handler
    • Handling Discord Events
    • Handling Components
  • Scripts
    • Our customs scripts
    • package.json
    • cleardb
    • db
    • client
    • fetch
    • reset
  • Database
    • MongoDB Connection
    • MongoDB Schemas
  • Resources
    • Privacy Policy
    • Terms of Service
    • NodeJS Download
    • Discord.js
    • Discord.js v14 Guide
    • Discord.js v14 Docs
  • Code Editors
    • Finding your best Code editor
    • Visual Studio IDE Download
    • Visual Studio Code Download
    • VS Code Insiders Download
    • Jetbrains WebStorm Download
    • Jetbrains Fleet Download
    • Jetbrains Aqua Download
Powered by GitBook
On this page
  1. Scripts

cleardb

What does this script?

This script clears your WHOLE MONGODB DATABASE! You cant execute it accidentaly. You have to confirm with y/n to proceed.

Script Code:

const { MongoClient } = require('mongodb');
const readlineSync = require('readline-sync');
const chalk = require('chalk');
require('dotenv').config();

const mongoUrl = process.env.MONGOURI; 

async function clearDatabase() {
    const client = new MongoClient(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true });

    try {
        await client.connect();
        console.log(chalk.green('Connected to the database successfully!'));

        const db = client.db(); 
        const collections = await db.collections();

        if (collections.length === 0) {
            console.log(chalk.yellow('The database is already empty.'));
            return;
        }

        // List all collections
        console.log(chalk.cyan('Collections in the database:'));
        collections.forEach((collection) => console.log(chalk.blue(`- ${collection.collectionName}`)));

        // Confirmation prompt
        const userConfirmation = readlineSync.question(chalk.red('Are you sure you want to delete all collections in the database? (y/n): '));

        if (userConfirmation.toLowerCase() === 'y') {
            // Drop each collection
            for (let collection of collections) {
                await db.collection(collection.collectionName).drop();
                console.log(chalk.magenta(`Collection ${collection.collectionName} has been deleted.`));
            }

            console.log(chalk.green('All collections have been deleted successfully!'));
        } else {
            console.log(chalk.yellow('Operation cancelled by the user.'));
        }
    } catch (error) {
        console.error(chalk.red('Error occurred while connecting to the database:', error));
    } finally {
        await client.close();
        console.log(chalk.cyan('Database connection closed.'));
    }
}

// Execute the script
clearDatabase();
Previouspackage.jsonNextdb

Last updated 9 months ago