mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 13:14:06 +01:00
✨ add Guild virtual fields
This commit is contained in:
parent
1def5ff09f
commit
75b690600d
@ -1,5 +1,9 @@
|
|||||||
import { Schema, model, Types, Document } from "mongoose";
|
import { Schema, model, Types, Document } from "mongoose";
|
||||||
import db from "../util/Database";
|
import db from "../util/Database";
|
||||||
|
import { ChannelModel } from "./Channel";
|
||||||
|
import { EmojiModel } from "./Emoji";
|
||||||
|
import { MemberModel } from "./Member";
|
||||||
|
import { RoleModel } from "./Role";
|
||||||
|
|
||||||
export interface GuildDocument extends Document, Guild {
|
export interface GuildDocument extends Document, Guild {
|
||||||
id: bigint;
|
id: bigint;
|
||||||
@ -87,7 +91,43 @@ export const GuildSchema = new Schema({
|
|||||||
widget_enabled: Boolean,
|
widget_enabled: Boolean,
|
||||||
});
|
});
|
||||||
|
|
||||||
// GuildSchema.virtual
|
GuildSchema.virtual("channels", {
|
||||||
|
ref: ChannelModel,
|
||||||
|
localField: "id",
|
||||||
|
foreignField: "guild_id",
|
||||||
|
justOne: false,
|
||||||
|
});
|
||||||
|
GuildSchema.virtual("roles", {
|
||||||
|
ref: RoleModel,
|
||||||
|
localField: "id",
|
||||||
|
foreignField: "guild_id",
|
||||||
|
justOne: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// nested populate is needed for member users: https://gist.github.com/yangsu/5312204
|
||||||
|
GuildSchema.virtual("members", {
|
||||||
|
ref: MemberModel,
|
||||||
|
localField: "id",
|
||||||
|
foreignField: "member_id",
|
||||||
|
justOne: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
GuildSchema.virtual("emojis", {
|
||||||
|
ref: EmojiModel,
|
||||||
|
localField: "id",
|
||||||
|
foreignField: "guild_id",
|
||||||
|
justOne: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
GuildSchema.virtual("joined_at", {
|
||||||
|
ref: MemberModel,
|
||||||
|
localField: "id",
|
||||||
|
foreignField: "guild_id",
|
||||||
|
justOne: true,
|
||||||
|
}).get((member: any, virtual: any, doc: any) => {
|
||||||
|
console.log("get", member, this);
|
||||||
|
return member.joined_at;
|
||||||
|
});
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
export const GuildModel = db.model<GuildDocument>("Guild", GuildSchema, "guilds");
|
export const GuildModel = db.model<GuildDocument>("Guild", GuildSchema, "guilds");
|
||||||
|
Loading…
Reference in New Issue
Block a user