tatialveso / mongoose-recipes

🍳 projeto de receitas desenvolvido como solução para WDFT SAO Ironhack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo_ironhack_blue 7

Mongoose Recipes

Introduction

thai_style_chicken_noodle_soup_pieces_recipe_web

We've learned how to use Mongoose to create Schemas and then interact with our MongoDB database. In the following exercise, we will practice how to implement this by creating awesome recipes.

Requirements

  • Fork this repo
  • Clone this repo

Submission

  • Upon completion, run the following commands:

    git add .
    git commit -m "Completed lab"
    git push origin master
  • Create Pull Request so your TAs can check up your work.

Instructions

Iteration 0: Initial setup

To run the application, the first thing you have to do is install all of its dependencies. Run the following command:

npm install

Iteration 1 - Recipe Schema

Create a Recipe model inside of the file /models/Recipe.model.js. The schema should have the following fields:

  • title - Type String. It should be required and unique.
  • level - Type String. Can be one of the following values: Easy Peasy - Amateur Chef - UltraPro Chef (remember the enum validator 😉).
  • ingredients - Type Array of Strings (represented as [ String ]).
  • cuisine - Type String. Should be required.
  • dishType - Type String. Possible values: breakfast, main_course, soup, snack, drink, dessert or other.
  • image - Type String. Default value: "https://images.media-allrecipes.com/images/75131.jpg".
  • duration - Type Number. The minimum value should be 0.
  • creator - Type String.
  • created - Type Date. By default, today.

Iteration 2 - Create a recipe

In the index.js, we first connect to the database using mongoose.connect() and following the connection we call the method Recipe.deleteMany() to remove any existing documents from the recipes collection:

// ...

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();
  })
// ...

Then, you should add a new recipe document to the database by calling the Model.create method and passing it the recipe details as an object. After inserting the recipe, you should console.log the title of the recipe.

You can use MongoDB Compass to double-check that everything is working as intended.

To run your code, remember you should use:

node index.js

Iteration 3 - Insert multiple recipes

We are importing an array of recipes from the data.json file. Using the Model.insertMany method, you should add the entire array to the database. After inserting the documents, print the title of each recipe to the console.

Tip: Follow the same tip as in the previous step.

Iteration 4 - Update recipe

Now you should have six different recipes in the database, but there was a mistake in one of them. The Rigatoni alla Genovese does not take that long. You should update the duration field and set it to 100. You might want to use the Model.findOneAndUpdate method. After updating it, print a success message!

Iteration 5 - Remove a recipe

Oh oh! The Carrot Cake is no longer available, so we need to remove it from the database. Using the Model.deleteOne method, remove that recipe from the database and display a success message after doing it!

Iteration 6 - Close the Database

As the last step, you need to close the database. Otherwise, the connection will stay open until the Node.js process dies. Pay attention to the asynchronicity of the operation. You should only close the connection after everything is done! 😉

Happy coding! 💙

About

🍳 projeto de receitas desenvolvido como solução para WDFT SAO Ironhack


Languages

Language:JavaScript 99.4%Language:Shell 0.6%