summary refs log tree commit diff
path: root/dist/util/Database.js
diff options
context:
space:
mode:
Diffstat (limited to 'dist/util/Database.js')
-rw-r--r--dist/util/Database.js99
1 files changed, 52 insertions, 47 deletions
diff --git a/dist/util/Database.js b/dist/util/Database.js

index 01b1b57c..6f8d284d 100644 --- a/dist/util/Database.js +++ b/dist/util/Database.js
@@ -17,58 +17,63 @@ class MongooseCache extends events_1.default { this.collection = collection; this.pipeline = pipeline; this.opts = opts; + this.init = async () => { + this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); + this.stream.on("change", this.change); + this.stream.on("close", this.destroy); + this.stream.on("error", console.error); + if (!this.opts.onlyEvents) { + const arr = await this.collection.aggregate(this.pipeline).toArray(); + this.data = arr.length ? arr[0] : arr; + } + }; + this.convertResult = (obj) => { + if (obj instanceof mongodb_1.Long) + return BigInt(obj.toString()); + if (typeof obj === "object") { + Object.keys(obj).forEach((key) => { + obj[key] = this.convertResult(obj[key]); + }); + } + return obj; + }; this.change = (doc) => { - // @ts-ignore - if (doc.fullDocument) { + try { // @ts-ignore - if (!this.opts.onlyEvents) - this.data = doc.fullDocument; + 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": + return this.emit("delete", doc.documentKey._id.toHexString()); + case "insert": + return this.emit("insert", doc.fullDocument); + case "update": + case "replace": + return this.emit("change", doc.fullDocument); + case "invalidate": + return this.destroy(); + default: + return; + } } - switch (doc.operationType) { - case "dropDatabase": - return this.destroy(); - case "drop": - return this.destroy(); - case "delete": - return this.emit("delete", doc.documentKey._id.toHexString()); - case "insert": - return this.emit("insert", doc.fullDocument); - case "update": - case "replace": - return this.emit("change", doc.fullDocument); - case "invalidate": - return this.destroy(); - default: - return; + catch (error) { + this.emit("error", error); } }; - } - async init() { - this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); - this.stream.on("change", this.change); - this.stream.on("close", this.destroy); - this.stream.on("error", console.error); - if (!this.opts.onlyEvents) { - const arr = await this.collection.aggregate(this.pipeline).toArray(); - this.data = arr.length ? arr[0] : arr; - } - } - convertResult(obj) { - if (obj instanceof mongodb_1.Long) - return BigInt(obj.toString()); - if (typeof obj === "object") { - Object.keys(obj).forEach((key) => { - obj[key] = this.convertResult(obj[key]); - }); - } - return obj; - } - destroy() { - this.stream.off("change", this.change); - this.emit("close"); - if (this.stream.isClosed()) - return; - return this.stream.close(); + this.destroy = () => { + this.stream?.off("change", this.change); + this.emit("close"); + if (this.stream.isClosed()) + return; + return this.stream.close(); + }; } } exports.MongooseCache = MongooseCache;