Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const MONGODB_URI="+srv://irshadmdk99:irshamongo@cluster0.dapdax3.mongodb.net/"
44 changes: 40 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,55 @@ const Recipe = require('./models/Recipe.model');
// Import of the data from './data.json'
const data = require('./data');

const MONGODB_URI = 'mongodb://localhost:27017/recipe-app';
const MONGODB_URI = process.env.MONGODB_URI

// Connection to the database "recipe-app"
mongoose
.connect(MONGODB_URI)
.then(x => {
console.log(`Connected to the database: "${x.connection.name}"`);
// Before adding any recipes to the database, let's remove all existing ones
return Recipe.deleteMany()
return Recipe.deleteMany();
})
.then(() => {
// Run your code here, after you have insured that the connection was made
// Iteration 2 - Create a recipe
return Recipe.create({
title: 'Pasta Carbonara',
level: 'Amateur Chef',
ingredients: ['Pasta', 'Eggs', 'Pancetta', 'Parmesan Cheese'],
cuisine: 'Italian',
dishType: 'main_course',
image: '',
duration: 30,
creator: 'Chef John'
});
})
.then(newRecipe => {
console.log(`Recipe created: ${newRecipe.title}`);
// Iteration 3 - Insert multiple recipes
return Recipe.insertMany(data);
})
.then(allRecipes => {
allRecipes.forEach(recipe => console.log(`Inserted recipe: ${recipe.title}`));
// Iteration 4 - Update a recipe
return Recipe.findOneAndUpdate(
{ title: 'Rigatoni alla Genovese' },
{ duration: 100 },
{ new: true }
);
})
.then(updatedRecipe => {
console.log(`Updated recipe: ${updatedRecipe.title}, duration is now ${updatedRecipe.duration} minutes`);
// Iteration 5 - Remove a recipe
return Recipe.deleteOne({ title: 'Carrot Cake' });
})
.then(() => {
console.log('Successfully removed Carrot Cake');
// Iteration 6 - Close the database
mongoose.connection.close(() => {
console.log('Database connection closed');
});
})
.catch(error => {
console.error('Error connecting to the database', error);
console.error('Error performing operations on the database', error);
});
21 changes: 19 additions & 2 deletions models/Recipe.model.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const { Schema } = mongoose;

const recipeSchema = new Schema({
// TODO: write the schema
title: { type: String, required: true, unique: true },
level: {
type: String,
enum: ['Easy Peasy', 'Amateur Chef', 'UltraPro Chef']
},
ingredients: [String],
cuisine: { type: String, required: true },
dishType: {
type: String,
enum: ['breakfast', 'main_course', 'soup', 'snack', 'drink', 'dessert', 'other']
},
image: {
type: String,
default: 'https://images.media-allrecipes.com/images/75131.jpg'
},
duration: { type: Number, min: 0 },
creator: String,
created: { type: Date, default: Date.now }
});

const Recipe = mongoose.model('Recipe', recipeSchema);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"nodemon": "^2.0.2"
},
"dependencies": {
"mongoose": "^6.1.2"
"mongoose": "^6.13.5"
}
}