galactic-bot/storage/providers/Mongodb.js

186 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-04-11 21:16:37 +02:00
const Provider = require('../Provider.js');
2020-04-08 16:27:34 +02:00
const { MongoClient } = require('mongodb');
2020-04-08 18:08:46 +02:00
class MongoDBProvider extends Provider {
2020-04-08 16:27:34 +02:00
constructor(manager, config) {
super(manager, {
name: 'mongodb',
config
});
2020-04-08 16:27:34 +02:00
}
async init() {
try {
this.connection = await MongoClient.connect(this.config.host+this.config.database, { useUnifiedTopology: true });
this.manager.db = await this.connection.db(this.config.database);
2020-04-08 16:27:34 +02:00
this.db = this.manager.db;
2020-04-14 17:05:27 +02:00
this.manager.logger.write('info', `Provider ${this.name} connected.`);
this._initialized = true;
2020-04-08 16:27:34 +02:00
} catch(err) {
this.manager.logger.write('error', `Provider ${this.name} failed to connect: ${err}`);
2020-04-08 16:27:34 +02:00
}
}
_query(query) {
2020-04-14 17:05:27 +02:00
/* Query object structure
{
2020-04-14 17:05:27 +02:00
type: '', -- The function to use from this class, ex. findOne
collection: '', -- Which collection to query from
query: { }, -- Actual search query
data: { } -- If the operation is to update or insert something, this is what to insert
}
*/
if(!this[query.type]) return { error: true, message: `Invalid query type, got '${query.type}'` };
2020-04-14 17:05:27 +02:00
if(!query.collection) return { error: true, message: `You must specify a collection to query!` };
return this[query.type](query);
}
2020-04-08 16:27:34 +02:00
/**
* Find and return the first match
* @memberof Database
*/
find({ collection, query }) {
2020-04-08 16:27:34 +02:00
//if(this.manager.debug) this.manager.logger.debug(`Incoming find query for ${db} with parameters ${JSON.stringify(query)}`);
return new Promise((resolve, reject) => {
if(!this._initialized) reject(new Error('MongoDB not connected'));
2020-04-12 14:37:49 +02:00
this.db.collection(collection).find(query, async (error, cursor) => {
2020-04-08 16:27:34 +02:00
if(error) return reject(error);
return resolve(await cursor.toArray());
});
});
}
/**
* Find and return the first match
*
* @param {String} db The collection in which the data is to be updated
* @param {Object} query The filter that is used to find the data
* @returns {Object} An object containing the queried data
* @memberof Database
*/
findOne({ collection, query }) {
2020-04-08 16:27:34 +02:00
//if(this.manager.debug) this.manager.logger.debug(`Incoming findOne query for ${db} with parameters ${JSON.stringify(query)}`);
return new Promise((resolve, reject) => {
if(!this._initialized) reject(new Error('MongoDB not connected'));
2020-04-12 14:37:49 +02:00
this.db.collection(collection).findOne(query, async (error, item) => {
2020-04-08 16:27:34 +02:00
if(error) return reject(error);
return resolve(item);
});
});
}
/**
* Update the first filter match.
*
* @param {String} db The collection in which the data is to be updated
* @param {Object} filter The filter that is used to find the data
* @param {Object} data The updated data
* @param {Boolean} [upsert=false] Create a new entry if no match is found
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
* @memberof Database
*/
updateOne({ collection, query, data, upsert = false }) {
2020-04-08 16:27:34 +02:00
2020-04-08 18:08:46 +02:00
//if(this.manager.debug) this.manager.logger.debug(`Incoming updateOne query for ${db} with parameters ${JSON.stringify(filter)}`);
2020-04-08 16:27:34 +02:00
return new Promise((resolve, reject) => {
2020-04-12 14:37:49 +02:00
if(!this._initialized) reject(new Error('MongoDB not connected'));
2020-04-08 16:27:34 +02:00
this.db.collection(collection).updateOne(query, { $set: data }, { upsert: upsert }, async (error, result) => {
2020-04-08 16:27:34 +02:00
if(error) return reject(error);
else {
//return resolve(result)
let { matchedCount, upsertedCount, modifiedCount } = result;
return resolve({ matched: matchedCount, upserted: upsertedCount, modified: modifiedCount });
}
});
});
}
/**
* Push data to an array
*
* @param {string} db The collection to query
* @param {object} filter The filter to find the document to update
* @param {object} data The data to be pushed
* @param {boolean} [upsert=false] Create a new entry if no match is found
* @returns
* @memberof Database
*/
push({ collection, query, data, upsert = false }) {
2020-04-08 16:27:34 +02:00
//if(this.manager.debug) this.manager.logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${JSON.stringify(filter)} and data ${JSON.stringify(data)}`);
return new Promise((resolve, reject) => {
if(!this._initialized) reject(new Error('MongoDB not connected'));
2020-04-12 14:37:49 +02:00
this.db.collection(collection).updateOne(query, { $push: data }, { upsert: upsert }, async (error, result) => {
2020-04-08 16:27:34 +02:00
if(error) return reject(error);
else return resolve(result);
});
});
}
/**
* Find a random element from a database
*
* @param {string} db The collection to query
* @param {object} [filter={}] The filtering object to narrow down the sample pool
* @param {number} [amount=1] Amount of items to return
* @returns {object}
* @memberof Database
*/
random({ collection, query = {}, amount = 1 }) {
2020-04-08 16:27:34 +02:00
//if(this.manager.debug) this.manager.logger.debug(`Incoming random query for ${db} with parameters ${JSON.stringify(filter)} and amount ${amount}`);
if(amount > 100) amount = 100;
return new Promise((resolve, reject)=>{
if(!this._initialized) reject(new Error('MongoDB not connected'));
2020-04-12 14:37:49 +02:00
this.db.collection(collection).aggregate([{ $match: query }, { $sample: {size: amount}}], function(err, item) {
2020-04-08 16:27:34 +02:00
if(err) return reject(err);
resolve(item);
});
});
}
}
2020-04-08 18:08:46 +02:00
module.exports = MongoDBProvider;