-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoStockRepository.js
40 lines (37 loc) · 1.13 KB
/
mongoStockRepository.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
var MongoClient = require('mongodb').MongoClient;
var url = process.env.MONGOLAB_URI || 'mongodb://localhost:27017/books_inventory_db';
var collectionPromise = MongoClient.
connect(url).
then(function (db) {
return db.collection('books');
});
module.exports = {
stockUp: function (isbn, count) {
console.log("audun er best books")
return collectionPromise.
then(function (collection) {
collection.updateOne({isbn: isbn}, {
isbn: isbn,
count: count
}, {upsert: true});
});
},
findAll: function () {
return collectionPromise.
then(function (collection) {
return collection.
find({}).
toArray();
});
},
getCount: function (isbn) {
return collectionPromise.then(function (collection) {
return collection.find({"isbn": isbn}).limit(1).next();
}).then(function (result) {
if (result) {
return result.count;
}
return null;
});
}
};