-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
61 lines (61 loc) · 1.59 KB
/
repl.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
const mongoose = require("mongoose"),
Subscriber = require("./models/subscriber"),
Course = require("./models/course");
var testCourse, testSubscriber;
mongoose.connect(
"mongodb://localhost:27017/recipe_db",
{ useNewUrlParser: true }
);
mongoose.set("useCreateIndex", true);
mongoose.Promise = global.Promise;
Subscriber.remove({})
.then(items => console.log(`Removed ${items.n} records!`))
.then(() => {
return Course.remove({});
})
.then(items => console.log(`Removed ${items.n} records!`))
.then(() => {
return Subscriber.create({
name: "Jon",
email: "[email protected]",
zipCode: "12345"
});
})
.then(subscriber => {
console.log(`Created Subscriber: ${subscriber.getInfo()}`);
})
.then(() => {
return Subscriber.findOne({
name: "Jon"
});
})
.then(subscriber => {
testSubscriber = subscriber;
console.log(`Found one subscriber: ${subscriber.getInfo()}`);
})
.then(() => {
return Course.create({
title: "Tomato Land",
description: "Locally farmed tomatoes only",
zipCode: 12345,
items: ["cherry", "heirloom"]
});
})
.then(course => {
testCourse = course;
console.log(`Created course: ${course.title}`);
})
.then(() => {
testSubscriber.courses.push(testCourse);
testSubscriber.save();
})
.then(() => {
return Subscriber.populate(testSubscriber, "courses");
})
.then(subscriber => console.log(subscriber))
.then(() => {
return Subscriber.find({
courses: mongoose.Types.ObjectId(testCourse._id)
});
})
.then(subscriber => console.log(subscriber));