From be15e50d6accf5e8810083ceb115bd2ebef0847f Mon Sep 17 00:00:00 2001 From: Navy Date: Fri, 17 Apr 2020 23:18:08 +0300 Subject: [PATCH] remove and insertOne operations --- storage/providers/Mongodb.js | 40 ++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/storage/providers/Mongodb.js b/storage/providers/Mongodb.js index 6eab85f..572c68a 100644 --- a/storage/providers/Mongodb.js +++ b/storage/providers/Mongodb.js @@ -26,7 +26,8 @@ class MongoDBProvider extends Provider { } - _query(query) { + _query(request) { + /* Query object structure { type: '', -- The function to use from this class, ex. findOne @@ -36,13 +37,44 @@ class MongoDBProvider extends Provider { } */ - if(!this[query.type]) return { error: true, message: `Invalid query type, got '${query.type}'` }; - if(!query.collection) return { error: true, message: `You must specify a collection to query!` }; - return this[query.type](query); + if (!this._initialized) return { error: true, message: 'MongoDB not connected' }; + if (!this[request.type]) return { error: true, message: `Invalid request type, got '${request.type}'` }; + if (!request.collection) return { error: true, message: `You must specify a collection to query!` }; + return this[request.type](request); } + remove({ collection, query }) { + + return new Promise((resolve, reject) => { + + this.db.collection(collection).remove(query, (err, result) => { + + if (err) reject(err); + resolve(result); + + }); + + }); + + } + + insertOne({ collection, data }) { + + return new Promise((resolve, reject) => { + + this.db.collection(collection).insertOne(data, (err, result) => { + + if(err) reject(err); + resolve(result); + + }); + + }); + + } + /** * Find and return the first match * @memberof Database