load checks

This commit is contained in:
Erik 2020-04-12 15:37:49 +03:00
parent aae82e2ed5
commit 4a403c2d6d

View File

@ -44,11 +44,11 @@ class MongoDBProvider extends Provider {
find(db, query) { find(db, query) {
if(!this.loaded) throw new Error('MongoDB not connected');
//if(this.manager.debug) this.manager.logger.debug(`Incoming find query for ${db} with parameters ${JSON.stringify(query)}`); //if(this.manager.debug) this.manager.logger.debug(`Incoming find query for ${db} with parameters ${JSON.stringify(query)}`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.loaded) reject(new Error('MongoDB not connected'));
this.db.collection(db).find(query, async (error, cursor) => { this.db.collection(db).find(query, async (error, cursor) => {
if(error) return reject(error); if(error) return reject(error);
@ -71,11 +71,11 @@ class MongoDBProvider extends Provider {
findOne(db, query) { findOne(db, query) {
if(!this.loaded) throw new Error('MongoDB not connected');
//if(this.manager.debug) this.manager.logger.debug(`Incoming findOne query for ${db} with parameters ${JSON.stringify(query)}`); //if(this.manager.debug) this.manager.logger.debug(`Incoming findOne query for ${db} with parameters ${JSON.stringify(query)}`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.loaded) reject(new Error('MongoDB not connected'));
this.db.collection(db).findOne(query, async (error, item) => { this.db.collection(db).findOne(query, async (error, item) => {
if(error) return reject(error); if(error) return reject(error);
@ -99,10 +99,10 @@ class MongoDBProvider extends Provider {
*/ */
updateOne(db, query, data, upsert = false) { updateOne(db, query, data, upsert = false) {
if(!this.loaded) throw new Error('MongoDB not connected');
//if(this.manager.debug) this.manager.logger.debug(`Incoming updateOne query for ${db} with parameters ${JSON.stringify(filter)}`); //if(this.manager.debug) this.manager.logger.debug(`Incoming updateOne query for ${db} with parameters ${JSON.stringify(filter)}`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.loaded) reject(new Error('MongoDB not connected'));
this.db.collection(db).updateOne(query, { $set: data }, { upsert: upsert }, async (error, result) => { this.db.collection(db).updateOne(query, { $set: data }, { upsert: upsert }, async (error, result) => {
@ -130,11 +130,11 @@ class MongoDBProvider extends Provider {
*/ */
push(db, query, data, upsert = false) { push(db, query, data, upsert = false) {
if(!this.loaded) throw new Error('MongoDB not connected');
//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)}`); //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) => { return new Promise((resolve, reject) => {
if(!this.loaded) reject(new Error('MongoDB not connected'));
this.db.collection(db).updateOne(query, { $push: data }, { upsert: upsert }, async (error, result) => { this.db.collection(db).updateOne(query, { $push: data }, { upsert: upsert }, async (error, result) => {
if(error) return reject(error); if(error) return reject(error);
@ -157,13 +157,13 @@ class MongoDBProvider extends Provider {
*/ */
random(db, query = {}, amount = 1) { random(db, query = {}, amount = 1) {
if(!this.loaded) throw new Error('MongoDB not connected');
//if(this.manager.debug) this.manager.logger.debug(`Incoming random query for ${db} with parameters ${JSON.stringify(filter)} and amount ${amount}`); //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; if(amount > 100) amount = 100;
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject)=>{
if(!this.loaded) reject(new Error('MongoDB not connected'));
this.db.collection(db).aggregate([{ $match: query }, { $sample: {size: amount}}], function(err, item) { this.db.collection(db).aggregate([{ $match: query }, { $sample: {size: amount}}], function(err, item) {
if(err) return reject(err); if(err) return reject(err);