summary refs log tree commit diff
path: root/src/models/Template.ts
blob: ef82c343cfa4a5c0e032faa9d07204a072c73170 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
	Schema,
	model,
	Types,
	Document
} from "mongoose";
import db from "../util/Database";
import {
	PublicUser,
	User,
	UserModel,
	PublicUserProjection
} from "./User";
import {
	Guild, GuildModel
} from "./Guild";

export interface Template extends Document {
	id: string;
	code: string;
	name: string;
	description ? : string;
	usage_count ? : number;
	creator_id: string;
	creator: User;
	created_at: Date;
	updated_at: Date;
	source_guild_id: String;
	serialized_source_guild: Guild;
}

export const TemplateSchema = new Schema({
	id: String,
	code: String,
	name: String,
	description: String,
	usage_count: Number,
	creator_id: String,
	created_at: Date,
	updated_at: Date,
	source_guild_id: String,
});

TemplateSchema.virtual("creator", {
	ref: UserModel,
	localField: "creator_id",
	foreignField: "id",
	justOne: false,
	autopopulate: {
		select: PublicUserProjection
	},
});

TemplateSchema.virtual("serialized_source_guild", {
	ref: GuildModel,
	localField: "source_guild_id",
	foreignField: "id",
	justOne: false,
	autopopulate: true,
});

// @ts-ignore
export const TemplateModel = db.model < Template > ("Template", TemplateSchema, "templates");