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