From 4a403c2d6d6ef0552fe772e6c6be6703801a8566 Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 12 Apr 2020 15:37:49 +0300 Subject: [PATCH] load checks --- storage/providers/Mongodb.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/storage/providers/Mongodb.js b/storage/providers/Mongodb.js index 0ed9456..a17aae2 100644 --- a/storage/providers/Mongodb.js +++ b/storage/providers/Mongodb.js @@ -44,11 +44,11 @@ class MongoDBProvider extends Provider { 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)}`); return new Promise((resolve, reject) => { + if(!this.loaded) reject(new Error('MongoDB not connected')); + this.db.collection(db).find(query, async (error, cursor) => { if(error) return reject(error); @@ -71,11 +71,11 @@ class MongoDBProvider extends Provider { 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)}`); return new Promise((resolve, reject) => { + if(!this.loaded) reject(new Error('MongoDB not connected')); + this.db.collection(db).findOne(query, async (error, item) => { if(error) return reject(error); @@ -99,10 +99,10 @@ class MongoDBProvider extends Provider { */ 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)}`); 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) => { @@ -130,11 +130,11 @@ class MongoDBProvider extends Provider { */ 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)}`); 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) => { if(error) return reject(error); @@ -157,13 +157,13 @@ class MongoDBProvider extends Provider { */ 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(amount > 100) amount = 100; 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) { if(err) return reject(err);