I forgot to incorporate this into the storage rework and probably broke stuff for a few months without realizing it.

This commit is contained in:
nolan 2021-06-20 20:44:39 -07:00
parent 9bb60e9c24
commit a85ed6535b

View File

@ -1,3 +1,5 @@
const { ObjectId } = require('mongodb');
class MongodbTable { class MongodbTable {
constructor(client, provider, opts = {}) { constructor(client, provider, opts = {}) {
@ -12,6 +14,7 @@ class MongodbTable {
//Data Search //Data Search
find(query, opts = {}) { //opts: { projection: ... } find(query, opts = {}) { //opts: { projection: ... }
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.find(query, opts, async (error, cursor) => { this.collection.find(query, opts, async (error, cursor) => {
@ -22,6 +25,7 @@ class MongodbTable {
} }
findOne(query, opts = {}) { //opts: { projection: ..., sort: ... } findOne(query, opts = {}) { //opts: { projection: ..., sort: ... }
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.findOne(query, opts, async (error, item) => { this.collection.findOne(query, opts, async (error, item) => {
@ -32,6 +36,7 @@ class MongodbTable {
} }
aggregate(query) { aggregate(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.aggregate(query, (error, item) => { this.collection.aggregate(query, (error, item) => {
@ -42,6 +47,7 @@ class MongodbTable {
} }
random(query, amount = 1) { random(query, amount = 1) {
query = this._handleData(query);
if(amount > 100) amount = 100; if(amount > 100) amount = 100;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
@ -55,6 +61,7 @@ class MongodbTable {
//Data Manipulation //Data Manipulation
insertOne(data) { insertOne(data) {
data = this._handleData(data);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.insertOne(data, (error, result) => { this.collection.insertOne(data, (error, result) => {
@ -67,6 +74,7 @@ class MongodbTable {
//NOTE: insertMany? //NOTE: insertMany?
deleteOne(query) { deleteOne(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.deleteOne(query, (error, result) => { this.collection.deleteOne(query, (error, result) => {
@ -77,6 +85,7 @@ class MongodbTable {
} }
deleteMany(query) { deleteMany(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.deleteMany(query, (error, result) => { this.collection.deleteMany(query, (error, result) => {
@ -87,6 +96,7 @@ class MongodbTable {
} }
updateOne(query, data, upsert = true) { updateOne(query, data, upsert = true) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.updateOne(query, { $set: data }, { upsert }, async(error, result) => { this.collection.updateOne(query, { $set: data }, { upsert }, async(error, result) => {
@ -99,6 +109,7 @@ class MongodbTable {
} }
removeProperty(query, data) { removeProperty(query, data) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
@ -114,6 +125,7 @@ class MongodbTable {
} }
push(query, data, upsert = true) { push(query, data, upsert = true) {
query = this._handleData(query);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.')); if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.updateOne(query, { $push: data }, { upsert }, async(error, result) => { 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 //Getters
get collection() { get collection() {