keyserver/src/dao/mongo.js

126 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-05-25 14:13:49 +00:00
/**
* Mailvelope - secure email with OpenPGP encryption for Webmail
* Copyright (C) 2016 Mailvelope GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
const MongoClient = require('mongodb').MongoClient;
/**
* A simple wrapper around the official MongoDB client.
*/
class Mongo {
/**
2016-06-08 12:01:30 +00:00
* Initializes the database client by connecting to the MongoDB.
* @param {String} uri The mongodb uri
* @param {String} user The databse user
* @param {String} pass The database user's password
* @yield {undefined}
2016-05-25 14:13:49 +00:00
*/
2016-06-08 12:01:30 +00:00
*init(options) {
let uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri;
this._db = yield MongoClient.connect(uri);
2016-05-25 14:13:49 +00:00
}
/**
* Cleanup by closing the connection to the database.
* @yield {undefined}
2016-05-25 14:13:49 +00:00
*/
disconnect() {
return this._db.close();
}
/**
* Inserts a single document.
* @param {Object} document Inserts a single document
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The operation result
2016-05-25 14:13:49 +00:00
*/
create(document, type) {
let col = this._db.collection(type);
2016-05-25 14:13:49 +00:00
return col.insertOne(document);
}
/**
* Inserts a list of documents.
* @param {Array} documents Inserts a list of documents
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The operation result
*/
batch(documents, type) {
let col = this._db.collection(type);
return col.insertMany(documents);
}
2016-05-25 14:13:49 +00:00
/**
* Update a single document.
* @param {Object} query The query e.g. { _id:'0' }
* @param {Object} diff The attributes to change/set e.g. { foo:'bar' }
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The operation result
2016-05-25 14:13:49 +00:00
*/
update(query, diff, type) {
let col = this._db.collection(type);
2016-05-25 14:13:49 +00:00
return col.updateOne(query, { $set:diff });
}
/**
* Read a single document.
* @param {Object} query The query e.g. { _id:'0' }
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The document object
2016-05-25 14:13:49 +00:00
*/
get(query, type) {
let col = this._db.collection(type);
2016-05-25 14:13:49 +00:00
return col.findOne(query);
}
/**
* Read multiple documents at once.
* @param {Object} query The query e.g. { foo:'bar' }
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Array} An array of document objects
2016-05-25 14:13:49 +00:00
*/
list(query, type) {
let col = this._db.collection(type);
2016-05-25 14:13:49 +00:00
return col.find(query).toArray();
}
/**
* Delete all documents matching a query.
* @param {Object} query The query e.g. { _id:'0' }
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The operation result
2016-05-25 14:13:49 +00:00
*/
remove(query, type) {
let col = this._db.collection(type);
return col.deleteMany(query);
2016-05-25 14:13:49 +00:00
}
/**
* Clear all documents of a collection.
* @param {String} type The collection to use e.g. 'publickey'
* @yield {Object} The operation result
2016-05-25 14:13:49 +00:00
*/
clear(type) {
let col = this._db.collection(type);
2016-05-25 14:13:49 +00:00
return col.deleteMany({});
}
}
module.exports = Mongo;