From a85ed6535b832822a060a2e5426b5060e6681062 Mon Sep 17 00:00:00 2001 From: nolan Date: Sun, 20 Jun 2021 20:44:39 -0700 Subject: [PATCH] I forgot to incorporate this into the storage rework and probably broke stuff for a few months without realizing it. --- structure/storage/interfaces/MongodbTable.js | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/structure/storage/interfaces/MongodbTable.js b/structure/storage/interfaces/MongodbTable.js index a0da0f2..9972ebf 100644 --- a/structure/storage/interfaces/MongodbTable.js +++ b/structure/storage/interfaces/MongodbTable.js @@ -1,3 +1,5 @@ +const { ObjectId } = require('mongodb'); + class MongodbTable { constructor(client, provider, opts = {}) { @@ -12,6 +14,7 @@ class MongodbTable { //Data Search find(query, opts = {}) { //opts: { projection: ... } + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.find(query, opts, async (error, cursor) => { @@ -22,6 +25,7 @@ class MongodbTable { } findOne(query, opts = {}) { //opts: { projection: ..., sort: ... } + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.findOne(query, opts, async (error, item) => { @@ -32,6 +36,7 @@ class MongodbTable { } aggregate(query) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.aggregate(query, (error, item) => { @@ -42,6 +47,7 @@ class MongodbTable { } random(query, amount = 1) { + query = this._handleData(query); if(amount > 100) amount = 100; return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); @@ -55,6 +61,7 @@ class MongodbTable { //Data Manipulation insertOne(data) { + data = this._handleData(data); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.insertOne(data, (error, result) => { @@ -67,6 +74,7 @@ class MongodbTable { //NOTE: insertMany? deleteOne(query) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.deleteOne(query, (error, result) => { @@ -77,6 +85,7 @@ class MongodbTable { } deleteMany(query) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.deleteMany(query, (error, result) => { @@ -87,6 +96,7 @@ class MongodbTable { } updateOne(query, data, upsert = true) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.updateOne(query, { $set: data }, { upsert }, async(error, result) => { @@ -99,6 +109,7 @@ class MongodbTable { } removeProperty(query, data) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); @@ -114,6 +125,7 @@ class MongodbTable { } push(query, data, upsert = true) { + query = this._handleData(query); return new Promise((resolve, reject) => { if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); this.collection.updateOne(query, { $push: data }, { upsert }, async(error, result) => { @@ -153,6 +165,20 @@ class MongodbTable { }); } + //Lazy Function + _handleData(data) { //Convert data._id to Mongo ObjectIds (gets converted to plaintext through shard communication) + if(data._id) { + if(typeof data._id === 'string') data._id = ObjectId(data._id); + else if(typeof data._id === 'object') data._id = { + $in: Object.values(data._id)[0].map((id) => { + return ObjectId(id); + }) + }; + } + return data; + } + + //Getters get collection() {