├── .gitignore ├── src ├── config.js ├── index.js ├── models │ └── Task.js ├── db.js ├── commands.js └── controllers │ └── task.controller.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env -------------------------------------------------------------------------------- /src/config.js: -------------------------------------------------------------------------------- 1 | require("dotenv").config(); 2 | 3 | module.exports = { 4 | MONGODB_URI: process.env.MONGODB_URI || "mongodb://localhost/tasksdb", 5 | }; 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const { connectDB } = require("./db"); 3 | require("./commands"); 4 | // User Questions 5 | 6 | async function main() { 7 | await connectDB(); 8 | } 9 | 10 | main(); 11 | -------------------------------------------------------------------------------- /src/models/Task.js: -------------------------------------------------------------------------------- 1 | const { Schema, model } = require("mongoose"); 2 | 3 | const taskSchema = new Schema( 4 | { 5 | title: { type: String }, 6 | description: { type: String }, 7 | }, 8 | { 9 | timestamps: true, 10 | versionKey: false, 11 | } 12 | ); 13 | 14 | module.exports = model("Task", taskSchema); 15 | -------------------------------------------------------------------------------- /src/db.js: -------------------------------------------------------------------------------- 1 | const { connect, connection } = require("mongoose"); 2 | const { MONGODB_URI } = require("./config"); 3 | 4 | const connectDB = async () => { 5 | await connect(MONGODB_URI); 6 | // console.log(db.connection.name); 7 | }; 8 | 9 | connection.on("error", (err) => { 10 | console.log(err); 11 | }); 12 | 13 | module.exports = { connectDB, connection }; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-mongodb-cli-crud", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "preferGlobal": true, 7 | "bin": { 8 | "taskcli": "./src/index.js" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "ISC", 16 | "dependencies": { 17 | "commander": "^8.3.0", 18 | "dotenv": "^10.0.0", 19 | "inquirer": "^8.2.0", 20 | "mongoose": "^6.1.4" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Tasks CLI 2 | 3 | Tasks CLI is a program to manage your tasks in a database using terminal or console. This is a sample project for beginners 4 | 5 | ### Requirements 6 | 7 | - Nodejs 8 | - Mongodb Local Installation or Cloud 9 | 10 | ### Installation 11 | 12 | ```sh 13 | npm install 14 | ``` 15 | 16 | ```sh 17 | npm link 18 | ``` 19 | 20 | ### Commands 21 | 22 | ``` 23 | tasks-cli --version 24 | ``` 25 | 26 | ``` 27 | tasks-cli --help 28 | ``` 29 | 30 | ``` 31 | tasks-cli list 32 | tasks-cli l 33 | ``` 34 | 35 | ``` 36 | tasks-cli find title 37 | ``` 38 | 39 | ```sh 40 | tasks-cli save 41 | tasks-cli s 42 | ``` 43 | 44 | ```sh 45 | tasks-cli update 46 | ``` 47 | 48 | ```sh 49 | tasks-cli delete 50 | tasks-cli d 51 | ``` 52 | -------------------------------------------------------------------------------- /src/commands.js: -------------------------------------------------------------------------------- 1 | const { program } = require("commander"); 2 | const { 3 | addTask, 4 | findTask, 5 | updateTask, 6 | removeTask, 7 | listTasks, 8 | } = require("./controllers/task.controller"); 9 | const { prompt } = require("inquirer"); 10 | 11 | const taskQuestion = [ 12 | { 13 | type: "input", 14 | name: "title", 15 | message: "Task Title", 16 | }, 17 | { 18 | type: "input", 19 | name: "description", 20 | message: "Task Description", 21 | }, 22 | ]; 23 | 24 | program.version("0.0.1").description("Task Management CLI"); 25 | 26 | program 27 | .command("save") 28 | .alias("s") 29 | .description("Save a new task") 30 | .action(async () => { 31 | const answers = await prompt(taskQuestion); 32 | addTask(answers); 33 | }); 34 | 35 | program 36 | .command("list") 37 | .alias("l") 38 | .description("list all tasks") 39 | .action(async () => listTasks()); 40 | 41 | program 42 | .command("find ") 43 | .alias("f") 44 | .description("find a task") 45 | .action((text) => findTask(text)); 46 | 47 | program 48 | .command("update ") 49 | .alias("u") 50 | .description("update a task") 51 | .action(async (_id) => { 52 | const answers = await prompt(taskQuestion); 53 | await updateTask(_id, answers); 54 | }); 55 | 56 | program 57 | .command("delete ") 58 | .alias("d") 59 | .description("remove a task") 60 | .action((_id) => removeTask(_id)); 61 | 62 | program.parse(process.argv); 63 | -------------------------------------------------------------------------------- /src/controllers/task.controller.js: -------------------------------------------------------------------------------- 1 | const { connection } = require("../db"); 2 | const Task = require("../models/Task"); 3 | 4 | const addTask = async (task) => { 5 | await Task.create(task); 6 | console.log("New Task Created"); 7 | await connection.close(); 8 | }; 9 | 10 | const findTask = async (title) => { 11 | const search = new RegExp(title, "i"); 12 | const tasks = await Task.find({ 13 | $or: [{ title: search }, { description: search }], 14 | }).lean(); 15 | // console.log(JSON.stringify(task, null, 2)); 16 | // console.log(tasks[0]); 17 | // console.table(Object.entries(tasks[0])); 18 | 19 | if (tasks.length === 0) { 20 | console.log("No tasks Found"); 21 | await connection.close(); 22 | process.exit(0); 23 | } 24 | 25 | console.table({ 26 | id: tasks[0]._id.toString(), 27 | title: tasks[0].title, 28 | description: tasks[0].description, 29 | }); 30 | console.log(`${tasks.length} matches`); 31 | await connection.close(); 32 | process.exit(0); 33 | }; 34 | 35 | const updateTask = async (_id, newTask) => { 36 | await Task.updateOne({ _id }, newTask); 37 | console.info("Task Updated"); 38 | await connection.close(); 39 | }; 40 | 41 | const removeTask = async (_id) => { 42 | await Task.deleteOne({ _id }); 43 | console.info("Task Deleted"); 44 | await connection.close(); 45 | }; 46 | 47 | const listTasks = async () => { 48 | const tasks = await Task.find().lean(); 49 | console.log(`Total Tasks Result: ${tasks.length}`); 50 | // console.info(tasks); 51 | console.table( 52 | tasks.map((task) => ({ 53 | _id: task._id.toString(), 54 | title: task.title, 55 | description: task.description, 56 | })) 57 | ); 58 | await connection.close(); 59 | process.exit(0); 60 | }; 61 | 62 | module.exports = { 63 | addTask, 64 | findTask, 65 | updateTask, 66 | removeTask, 67 | listTasks, 68 | }; 69 | --------------------------------------------------------------------------------