API data in Database using Express & Mongoose
Stored API data (JSON) in Database using Express & Mongoose
▶️ Create a JSON file
- Go to main directory
cd ..
- Create a JSON file using
echo > tasks.json
&echo > tasksDB.js
- In
tasks.json
file, add the following code:
[
{
"description": "Buy groceries",
"status": "incomplete",
"priority": "high",
"dueDate": "2023-03-20T12:00:00Z",
"owner": "616a58a4567d8930d8816c34"
},
{
"description": "Clean the kitchen",
"status": "incomplete",
"priority": "medium",
"dueDate": "2023-03-19T09:00:00Z",
"owner": "616a58a4567d8930d8816c34"
},
{
"description": "Finish report for work",
"status": "incomplete",
"priority": "urgent",
"dueDate": "2023-03-25T17:00:00Z",
"owner": "616a58a4567d8930d8816c35"
},
{
"description": "Read book for book club",
"status": "completed",
"priority": "low",
"dueDate": "2023-03-18T20:00:00Z",
"owner": "616a58a4567d8930d8816c35"
},
{
"description": "Go for a run",
"status": "incomplete",
"priority": "medium",
"dueDate": "2023-03-17T06:00:00Z",
"owner": "616a58a4567d8930d8816c36"
},
{
"description": "Finish homework",
"status": "incomplete",
"priority": "high",
"dueDate": "2023-03-20T12:00:00Z",
"owner": "616a58a4567d8930d8816c36"
},
{
"description": "Clean the kitchen",
"status": "incomplete",
"priority": "medium",
"dueDate": "2023-03-19T09:00:00Z",
"owner": "616a58a4567d8930d8816c36"
},
{
"description": "Finish report for work",
"status": "incomplete",
"priority": "urgent",
"dueDate": "2023-03-25T17:00:00Z",
"owner": "616a58a4567d8930d8816c37"
},
{
"description": "Read book for book club",
"status": "completed",
"priority": "low",
"dueDate": "2023-03-18T20:00:00Z",
"owner": "616a58a4567d8930d8816c37"
},
{
"description": "Go for a run",
"status": "incomplete",
"priority": "medium",
"dueDate": "2023-03-17T06:00:00Z",
"owner": "616a58a4567d8930d8816c37"
}
]
▶️ DB Setup
- In
tasksDB.js
file, add the following code:
You can understand the code from the comments in the code
// Load the dotenv module to access environment variables
require("dotenv").config();
// Load the connectDB module to establish a connection to MongoDB
const connectDB = require("./db/connect");
// Load the Task model to interact with the tasks collection
const Task = require("./models/task");
// Load the tasksJSON data from a local file
const tasksJSON = require("./tasks.json");
// Define an async function to start the process
const start = async () => {
try {
// Connect to MongoDB using the URL from the environment variable
await connectDB(process.env.MONGODB_URL);
// Create new documents in the tasks collection using the tasksJSON data
await Task.create(tasksJSON);
// Log a success message if everything goes well
console.log("Tasks created successfully");
// Exit the process with a success code (0)
process.exit(0);
} catch (error) {
// Log an error message if something goes wrong
console.log(error);
// Exit the process with a failure code (1)
process.exit(1);
}
};
// Call the start function to execute it
start();
-
Calls the connectDB function to connect to a MongoDB database using the URL specified in the MONGODB_URL environment variable.
-
Uses the Task model to create new documents in the database using the data from the tasks.json file.
-
Logs a success message to the console if the documents were created successfully.
-
Exits the Node.js process with a status code of 0 (success).
▶️ Run the script
- Run the script using
node tasksDB.js
in the terminal
▶️ Check the database
- Go to MongoDB Atlas and check the database to see if the data has been added
- go to the
Collections
tab - click on the
tasks
collection