mirror of
https://github.com/spacebarchat/server.git
synced 2024-09-22 02:31:36 +02:00
🐛 fix mongoose cache
This commit is contained in:
parent
e8c3b57f00
commit
7efdbe4027
@ -4,7 +4,7 @@ import db, { MongooseCache } from "./Database";
|
|||||||
import { Snowflake } from "./Snowflake";
|
import { Snowflake } from "./Snowflake";
|
||||||
import crypto from "crypto";
|
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 {
|
export default {
|
||||||
init: async function init(defaultOpts: any = DefaultOptions) {
|
init: async function init(defaultOpts: any = DefaultOptions) {
|
||||||
|
@ -52,9 +52,11 @@ export class MongooseCache extends EventEmitter {
|
|||||||
public pipeline: Array<Record<string, unknown>>,
|
public pipeline: Array<Record<string, unknown>>,
|
||||||
public opts: {
|
public opts: {
|
||||||
onlyEvents: boolean;
|
onlyEvents: boolean;
|
||||||
|
array?: boolean;
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
|
if (this.opts.array == null) this.opts.array = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
init = async () => {
|
init = async () => {
|
||||||
@ -67,7 +69,8 @@ export class MongooseCache extends EventEmitter {
|
|||||||
|
|
||||||
if (!this.opts.onlyEvents) {
|
if (!this.opts.onlyEvents) {
|
||||||
const arr = await this.collection.aggregate(this.pipeline).toArray();
|
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) => {
|
change = (doc: ChangeEvent) => {
|
||||||
try {
|
try {
|
||||||
// @ts-ignore
|
|
||||||
if (doc.fullDocument) {
|
|
||||||
// @ts-ignore
|
|
||||||
if (!this.opts.onlyEvents) this.data = doc.fullDocument;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (doc.operationType) {
|
switch (doc.operationType) {
|
||||||
case "dropDatabase":
|
case "dropDatabase":
|
||||||
return this.destroy();
|
return this.destroy();
|
||||||
case "drop":
|
case "drop":
|
||||||
return this.destroy();
|
return this.destroy();
|
||||||
case "delete":
|
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());
|
return this.emit("delete", doc.documentKey._id.toHexString());
|
||||||
case "insert":
|
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);
|
return this.emit("insert", doc.fullDocument);
|
||||||
case "update":
|
case "update":
|
||||||
case "replace":
|
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);
|
return this.emit("change", doc.fullDocument);
|
||||||
case "invalidate":
|
case "invalidate":
|
||||||
return this.destroy();
|
return this.destroy();
|
||||||
@ -119,6 +133,7 @@ export class MongooseCache extends EventEmitter {
|
|||||||
};
|
};
|
||||||
|
|
||||||
destroy = () => {
|
destroy = () => {
|
||||||
|
this.data = null;
|
||||||
this.stream?.off("change", this.change);
|
this.stream?.off("change", this.change);
|
||||||
this.emit("close");
|
this.emit("close");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user