This Node.js application facilitates MongoDB database operations such as user management and data export. It provides an
interactive CLI for creating, deleting, and updating user passwords (which are securely salted and hashed using
bcrypt), as well as exporting and listing data from a MongoDB collection, including exporting data to Excel using an
external API.
- Create User: Add new users to the database. Passwords are securely salted and hashed using
bcryptbefore storage. - Delete User: Remove users from the database.
- Change Password: Update passwords for existing users. New passwords are securely salted and hashed using
bcryptbefore being updated in the database. - List Users: Display all registered users.
- Export Data: Export all documents from a specified collection into a JSON file.
- Export Data to Excel: Export all documents from a specified collection to an Excel file using an external API ( optional; requires additional repository).
- Export Data: Export all documents from a specified collection into a JSON file.
- Export Data to Excel: Export all documents from a specified collection to an Excel file using an external API.
- List Exported Files: View all previously exported files in the
exportsdirectory.
- Modify User Collection: Change the name of the MongoDB collection used for user data.
- Modify Data Collection: Change the name of the MongoDB collection used for exporting data.
- Node.js: Version 14 or higher.
- Excel Export Dependency (optional): If you want to enable Excel export, ensure you set up the external API from the repository Excel-JSON-Converter-API.
- MongoDB: Connection URI with a placeholder
<password>for secure integration. - Environment Variables:
MONGO_URL: MongoDB connection URI.DATA_COLLECTION_NAME: Name of the data collection to export.USER_COLLECTION_NAME: Name of the user collection for user management.ENABLE_EXCEL_EXPORT: Enable or disable the Excel export feature (true/false).CONVERTER_API_URL: The API endpoint for converting JSON data to Excel.
-
Clone the repository:
git clone https://github.com/cicarulez/mongo-manager/ cd src -
Install dependencies:
npm install
-
Set up the environment variables:
- Create a
.envfile in the root of the project with the following variables:MONGO_URL=mongodb+srv://<username>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority DATA_COLLECTION_NAME=data_collection_name USER_COLLECTION_NAME=user_collection_name ENABLE_EXCEL_EXPORT=true CONVERTER_API_URL=http://localhost:3000/api/files/convertJsonToExcel
- Replace
<username>,<password>,data_collection_name, anduser_collection_namewith your MongoDB credentials and collection names.
- Create a
-
Run the application:
npm run start:dev
- Build the Docker image:
docker build -t mongo-manager-cli . - Run the container:
docker run --tty --interactive --name mongo-manager-cli --rm -v $(pwd)/exports:/usr/app/exports --env-file envs/.env.test mongo-manager-cli
- Create or edit the
.envfile as described above. - Start the application with Docker Compose:
docker-compose run --rm mongo-manager-cli
- Access the application logs:
docker-compose logs -f
To ensure both excel-json-converter and mongo-manager-cli containers are started correctly and in the right order,
you can use the provided startup script. This script will automatically:
- Start the
excel-json-converterservice. - Wait until the
excel-json-converterservice is fully ready by checking its custom health endpoint. - Launch the
mongo-manager-clitty service.
To run the script, simply execute:
./start-mongo-manager.sh- When you start the script, you will be prompted to enter your MongoDB password.
- If the connection is successful, the main menu will be displayed:
============================= π Main Menu ============================= 1. π€ User Management 2. π Export Options 3. βοΈ Settings 4. β Exit
- Select 1 to manage users. Submenu options:
- Create User: Enter a username, password, first name, last name, and email to add a new user. After creation, you can opt to add another user by responding yes.
- Delete User: Provide a username to delete a user. After deletion, you can delete another user by responding * yes*.
- Change Password: Update an existing user's password. After updating, you can change another password by responding yes.
- List Users: View all registered users.
- Select 2 to access the export options submenu:
- Export Data: Export all documents from the specified collection to a JSON file.
- Export Data to Excel: Export all documents from the specified collection to an Excel file using an external
API (only visible if
ENABLE_EXCEL_EXPORT=trueand requires the Excel-JSON-Converter-API). - List Exported Files: Display a list of previously exported files.
- Select 3 to modify collection names:
- Modify User Collection: Change the name of the user collection used in MongoDB.
- Modify Data Collection: Change the name of the data collection used for export.
- Select 4 to safely exit the application.
The project is structured into clear, modular components to ensure scalability and maintainability.
Contains core business logic:
user-management.service.js: Handles user creation, deletion, password updates, and listing.data-management.service.js: Manages data export and lists exported files.excel-export.service.js: Handles data export to Excel using an external API.
Manages the user interface:
main-menu.cli.js: Displays the main menu and handles navigation.user-management.cli.js: Implements user management operations via CLI.export-options.cli.js: Implements data export and listing functionality, including Excel export.settings.cli.js: Provides options to modify collection names via CLI.prompt-user.cli.js: Handles reusable CLI prompts for user input.
Defines the database schema:
user.model.js: Mongoose schema for the user collection.
db.js: Handles MongoDB connection setup with password integration.
config.js: Provides getter and setter methods for managing environment configurations.
validator.js: Validates required environment variables before starting the application.
main.js: Starts the application and initializes the main menu.
- Password Security: User passwords are securely hashed using
bcryptwith a salt to ensure high security before being stored or updated in the database. - Excel Export: Allows exporting data to an Excel file via an external API (requires the repository Excel-JSON-Converter-API), providing an additional format for data usage.
- Sequential Actions: During user creation, deletion, or password updates, users can continue performing the same action sequentially by responding with "yes" or "y" to prompts, streamlining repetitive operations.
- Error Handling: Proper validation ensures environment variables are set, and MongoDB connection issues are handled gracefully.
- Interactive CLI: Designed for ease of use with structured menus, clear prompts, and streamlined workflows.
- Graceful Shutdown: The application ensures proper disconnection from MongoDB during shutdown or when receiving
termination signals (e.g.,
Ctrl+C).
-
Start the script and enter the MongoDB password:
Enter MongoDB password: ******** β Connected to MongoDB. -
Create a new user:
π€ User Management ============================= 1. π Create User 2. π Delete User 3. π Change Password 4. π List Users 5. β¬ Back to Main Menu Choose an option: 1 Enter username for new user: testuser Enter password for new user: password123 β User "testuser" successfully created. -
Export data to JSON:
π Export Options ============================= 1. Export Data 2. Export Data to Excel 3. List Data 4. β¬ Back to Main Menu Choose an option: 1 β Data exported to ./exports/data_collection_name_2024-12-11T12-00-00.json -
Export data to Excel:
π Export Options ============================= 1. Export Data 2. Export Data to Excel 3. List Data 4. β¬ Back to Main Menu Choose an option: 2 β Data successfully sent to the external API for Excel conversion. β Excel file saved to: ./exports/data_collection_name_2024-12-11T12-00-00.xlsx -
Exit:
β Exiting...
-
Error: Missing environment variables
- Ensure all required variables (
MONGO_URL,DATA_COLLECTION_NAME,USER_COLLECTION_NAME,ENABLE_EXCEL_EXPORT,CONVERTER_API_URL) are set in the.envfile.
- Ensure all required variables (
-
Error: Failed to connect to MongoDB
- Verify that the
MONGO_URLis correctly formatted and the database credentials are valid.
- Verify that the
-
Error: Collection name cannot be empty
- If using the Settings menu, ensure that you provide a valid collection name when prompted.
-
Error during data export
- Check if the
exportsdirectory is writable and there is sufficient disk space.
- Check if the
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.