forked from denilsonjvv/RandomQuestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeds.js
78 lines (77 loc) · 2.49 KB
/
seeds.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const mongoose = require("mongoose");
const Question = require("./models/question");
const Comment = require("./models/comment");
const Updates = require("./models/updates");
const Topic = require("./models/topic");
const { ObjectID } = require("bson");
// This is the object _id of the first user in the database
const userObj = new ObjectID("618376fa3f01fc1d59f6b239");
let questionData = {
title: "What is the largest thing on earth?",
description: "Just a question i wanted to ask okay? :D",
author: userObj
}
async function seedDB() {
await Question.remove({}, (err) => {
if (err) return console.log(err);
console.log("removed questions!");
})
await Updates.remove({}, (err) => {
if (err) return console.log(err);
console.log("removed comments!");
})
await Comment.remove({}, (err) => {
if (err) return console.log(err);
console.log("removed updates!");
})
await Topic.remove({}, (err) => {
if (err) return console.log(err);
console.log("removed topics!");
})
try {
const newQuestion = await Question.create(questionData);
const updatesData = {
author: questionData.author,
question: newQuestion,
action: "created a question",
};
const newUpdate = await Updates.create(updatesData);
const commentData = {
author: userObj,
question: newQuestion,
text: "Awesome questtionnnnn!"
}
const newComment = await Comment.create(commentData);
const topicsData = [
{
title: "developers",
questions: newQuestion
},
{
title: "all",
questions: newQuestion
},
{
title: "these",
questions: newQuestion
},
{
title: "topics",
questions: newQuestion
}
]
const newTopic = await Topic.insertMany(topicsData);
newTopic.forEach((topic) => {
newQuestion.topics.push(topic);
})
newQuestion.comments.push(newComment);
newQuestion.updates.push(newUpdate);
await newComment.save();
await newUpdate.save();
await newQuestion.save();
console.log("Saved new updates and question, check it out below here");
} catch (err) {
console.log("there was an error:", err)
}
}
module.exports = seedDB;