diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-04-07 03:02:13 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-04-07 03:02:13 +0200 |
commit | f48127f8a4881afc6dfb9b066b4eb357f1ef6c63 (patch) | |
tree | 2fcd6f0d13b54ceb9490902767beab6d013cd480 /src/util | |
parent | remove dist (diff) | |
download | server-f48127f8a4881afc6dfb9b066b4eb357f1ef6c63.tar.xz |
:bug: fix lean not working with virtuals
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/Database.ts | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/util/Database.ts b/src/util/Database.ts index 883c8dcd..e17eeccb 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -2,6 +2,7 @@ import "./MongoBigInt"; import mongoose, { Collection, Connection } from "mongoose"; import { ChangeStream, ChangeEvent, Long } from "mongodb"; import EventEmitter from "events"; +import { Document } from "mongoose"; const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPreference=secondaryPreferred"; console.log(`[DB] connect: ${uri}`); @@ -10,6 +11,14 @@ const connection = mongoose.createConnection(uri, { autoIndex: true, useNewUrlPa export default <Connection>connection; +function transform<T extends Document>(document: T) { + return document.toObject({ virtuals: true }); +} + +export function toObject<T extends Document>(document: T | T[]) { + return Array.isArray(document) ? document.map((x) => transform<T>(x)) : transform(document); +} + export interface MongooseCache { on(event: "delete", listener: (id: string) => void): this; on(event: "change", listener: (data: any) => void): this; |