i399exmongo/dao.js

38 lines
740 B
JavaScript
Raw Permalink Normal View History

2017-05-03 15:17:25 +03:00
'use strict';
const mongodb = require('mongodb');
const ObjectID = require('mongodb').ObjectID;
2017-05-11 11:22:47 +03:00
const COLLECTION = 'contacts';
2017-05-03 15:17:25 +03:00
class Dao {
connect(url) {
return mongodb.MongoClient.connect(url)
.then(db => this.db = db);
}
findAll() {
return this.db.collection(COLLECTION).find().toArray();
}
2017-05-11 11:22:47 +03:00
findById(id) {
id = new ObjectID(id);
return this.db.collection(COLLECTION).findOne({_id:id});
}
update(id, data) {
id = new ObjectID(id);
data._id = id
return this.db.collection(COLLECTION)
.updateOne({_id:id}, data);
}
2017-05-03 15:17:25 +03:00
close() {
if (this.db) {
this.db.close();
}
}
}
module.exports = Dao;