-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockRepository.js
31 lines (30 loc) · 1013 Bytes
/
stockRepository.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
var mongoClient = require('mongodb').MongoClient;
var url = process.env.MONGOLAB_URI || 'mongodb://localhost:27017/book_inventory_stock';
var collectionPromise = mongoClient.connect(url)
.then(function(db) {
console.log("Connected correctly to server");
return db.collection('books_drawski');
});
module.exports = {
findAll: function() {
return collectionPromise
.then(function(collection) {
return collection.find({}).toArray();
});
},
stockUp: function (isbn, count) {
return collectionPromise
.then(function(collection) {
return collection.updateOne({isbn: isbn}, {
isbn: isbn,
count: count
}, {upsert: true});
});
},
getBook: function(isbn) {
return collectionPromise
.then(function(collection) {
return collection.find({isbn: isbn}).limit(1).next();
});
}
};