forked from ironhack-labs/lab-mongoose-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (53 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const mongoose = require('mongoose');
// Import of the model Recipe from './models/Recipe.model.js'
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';
// Connection to the database "recipe-app"
mongoose
.connect(MONGODB_URI, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(self => {
console.log(`Connected to the database: "${self.connection.name}"`);
// Before adding any recipes to the database, let's remove all existing ones
return Recipe.deleteMany()
})
.then(() => {
// Run your code here, after you have insured that the connection was made
return Recipe.create({
title: 'Arroz a la cubana',
level: 'Easy Peasy',
cuisine:'Canada',
ingredients: ['Eggs', 'Rice', 'Tomatoe'],
dishType: 'main_course',
image: "https://t2.rg.ltmcdn.com/es/images/9/8/0/arroz_a_la_cubana_facil_y_rapido_72089_600_square.jpg",
duration: 25,
creator: 'Dani',
})
})
.then(()=>{
return Recipe.insertMany(data);
})
.then(()=>{
return Recipe.findOneAndUpdate(
{title:'Rigatoni alla Genovese'},
{duration: 41200},
{new:true, useFindAndModify: false},
console.log('Duration modified.'))
})
.then(()=>{
return Recipe.deleteOne(
{title: 'Carrot Cake'},
console.log(`Recipe deleted.`)
)
})
.then(()=>{
mongoose.connection.close()
})
.catch(error => {
console.error('Error connecting to the database', error);
});