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:
parent
9bb60e9c24
commit
a85ed6535b
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user