-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata.js
More file actions
96 lines (76 loc) · 4.26 KB
/
Copy pathdata.js
File metadata and controls
96 lines (76 loc) · 4.26 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var MongoClient = require('mongodb').MongoClient,
runStartup = require("./startup.js"),
settings = require('./config.js'),
Guid = require('Guid');
var fullMongoUrl = settings.mongoConfig.serverUrl + settings.mongoConfig.database;
var exports = module.exports = {};
runStartup().then(function(allMovies) {
console.log("After the setup has been complete, we have the following movies:");
console.log(allMovies);
});
MongoClient.connect(fullMongoUrl)
.then(function(db) {
var movieCollection = db.collection("movies");
// Our easiest to follow function!
exports.getAllMovies = function() {
// collection.find() returns a cursor, a special mongoDB object that acts as an interface to the
// results;
// If we call find() without an argument, it will return a cursor to the entire collection
// It may paginate!
// by calling .toArray() on the find cursor, it converts it to the promise that resolves
// an array of all the results
return movieCollection.find().toArray();
};
// we can still update our data module by adding properties here, even though it's inside a callback!
exports.getMovie = function(id) {
if (!id) return Promise.reject("You must provide an ID");
// by calling .toArray() on the function, we convert the Mongo Cursor to a promise where you can
// easily iterate like a normal array
return movieCollection.find({ _id: id }).limit(1).toArray().then(function(listOfMovies) {
if (listOfMovies.length === 0) throw "Could not find movie with id of " + id;
return listOfMovies[0];
});
};
// creating data in MongoDB is very easy, as we can just use a simple insert method
exports.createMovie = function(title, rating) {
if (!title) return Promise.reject("You must provide a title");
if (rating == null || rating === undefined || rating < 0 || rating > 5) return Promise.reject("You have provided an invalid rating");
// Our insertOne method takes a JSON object as its first parameter;
// there are overloads that have more parameters
// You'll notice we name our "id" as "_id"; MongoDB has a few quirks.
// One is that it will index all documents on the _id field,
// Which must be a unique identifier.
// Curiously, it doesn't let you rename it to something more sane.
// Like 'id'.
// Because, you know, reasons.
return movieCollection.insertOne({ _id: Guid.create().toString(), title: title, rating: rating }).then(function(newDoc) {
return newDoc.insertedId;
}).then(function(newId) {
return exports.getMovie(newId);
});
}
// This is just a preview!
// We can find on a few limited operations
exports.getPopularMovies = function() {
// a preview of advanced querying!
return movieCollection.find({ rating: { $gte: 3 } }).toArray();
};
exports.updateMovie = function(id, newTitle, newRating) {
if (!id) return Promise.reject("You must provide an ID");
if (!newTitle) return Promise.reject("You must provide a title");
if (newRating == null || newRating === undefined || newRating < 0 || newRating > 5) return Promise.reject("You have provided an invalid rating");
// our first parameters is a way of describing the document to update;
// our second will be a replacement version of the document;
// next week we will learn how to use mongo atomic updates
return movieCollection.updateOne({ _id: id }, { title: newTitle, rating: newRating }).then(function() {
return exports.getMovie(id);
});
};
exports.deleteMovie = function(id) {
if (!id) return Promise.reject("You must provide an ID");
return movieCollection.deleteOne({ _id: id }).then(function(deletionInfo) {
if (deletionInfo.deletedCount === 0) throw "Could not find the document with this id to delete";
return true;
});
}
});