summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-06-27 23:11:04 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-06-27 23:11:04 +0200
commit7efdbe4027e39c6cc2571b4eba8ba174dd177962 (patch)
tree2ec63a4b4836b220318cb066857e0d7c61e9bd2e /src/util
parent1.3.19 (diff)
downloadserver-7efdbe4027e39c6cc2571b4eba8ba174dd177962.tar.xz
:bug: fix mongoose cache
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Config.ts2
-rw-r--r--src/util/Database.ts29
2 files changed, 23 insertions, 8 deletions
diff --git a/src/util/Config.ts b/src/util/Config.ts
index 30d216a4..bd8559a8 100644
--- a/src/util/Config.ts
+++ b/src/util/Config.ts
@@ -4,7 +4,7 @@ import db, { MongooseCache } from "./Database";
 import { Snowflake } from "./Snowflake";
 import crypto from "crypto";
 
-var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false });
+var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false, array: false });
 
 export default {
 	init: async function init(defaultOpts: any = DefaultOptions) {
diff --git a/src/util/Database.ts b/src/util/Database.ts
index 3a0f0157..1b77629f 100644
--- a/src/util/Database.ts
+++ b/src/util/Database.ts
@@ -52,9 +52,11 @@ export class MongooseCache extends EventEmitter {
 		public pipeline: Array<Record<string, unknown>>,
 		public opts: {
 			onlyEvents: boolean;
+			array?: boolean;
 		}
 	) {
 		super();
+		if (this.opts.array == null) this.opts.array = true;
 	}
 
 	init = async () => {
@@ -67,7 +69,8 @@ export class MongooseCache extends EventEmitter {
 
 		if (!this.opts.onlyEvents) {
 			const arr = await this.collection.aggregate(this.pipeline).toArray();
-			this.data = arr.length ? arr[0] : arr;
+			if (this.opts.array) this.data = arr || [];
+			else this.data = arr?.[0];
 		}
 	};
 
@@ -90,23 +93,34 @@ export class MongooseCache extends EventEmitter {
 
 	change = (doc: ChangeEvent) => {
 		try {
-			// @ts-ignore
-			if (doc.fullDocument) {
-				// @ts-ignore
-				if (!this.opts.onlyEvents) this.data = doc.fullDocument;
-			}
-
 			switch (doc.operationType) {
 				case "dropDatabase":
 					return this.destroy();
 				case "drop":
 					return this.destroy();
 				case "delete":
+					if (!this.opts.onlyEvents) {
+						if (this.opts.array) {
+							this.data = this.data.filter((x: any) => doc.documentKey?._id?.equals(x._id));
+						} else this.data = null;
+					}
 					return this.emit("delete", doc.documentKey._id.toHexString());
 				case "insert":
+					if (!this.opts.onlyEvents) {
+						if (this.opts.array) this.data.push(doc.fullDocument);
+						else this.data = doc.fullDocument;
+					}
 					return this.emit("insert", doc.fullDocument);
 				case "update":
 				case "replace":
+					if (!this.opts.onlyEvents) {
+						if (this.opts.array) {
+							const i = this.data.findIndex((x: any) => doc.fullDocument?._id?.equals(x._id));
+							if (i == -1) this.data.push(doc.fullDocument);
+							else this.data[i] = doc.fullDocument;
+						} else this.data = doc.fullDocument;
+					}
+
 					return this.emit("change", doc.fullDocument);
 				case "invalidate":
 					return this.destroy();
@@ -119,6 +133,7 @@ export class MongooseCache extends EventEmitter {
 	};
 
 	destroy = () => {
+		this.data = null;
 		this.stream?.off("change", this.change);
 		this.emit("close");