src/database/schemas.ts (view raw)
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import {
type HydratedDocument,
type InferSchemaType,
Schema,
model,
} from "mongoose";
import config from "../config";
const uniqueProjects = Object.keys(config.data.projects) as [
string,
...string[],
];
const CounterSchema = new Schema({
_id: String,
sequence_value: { type: Number, default: 0 },
});
const GuildSchema = new Schema(
{
guild_id: { type: String, required: true, unique: true },
suggestion_forum: String,
bug_channel: String,
commands_channel: String,
highlights_channel: String,
manager_roles: [String],
report_message: { type: String, default: null },
report_channel: { type: String, default: null },
},
{ timestamps: true },
);
const UserSchema = new Schema(
{
user_id: { type: String, required: true },
guild_id: { type: String, required: true, ref: "Guild" },
cat_points: { type: Number, default: 0 },
/** @deprecated */
has_reported: { type: Boolean, default: false },
last_report: { type: Date, default: null },
},
{ timestamps: true },
);
const BugSchema = new Schema(
{
bug_id: { type: Number, required: true, unique: true },
user_id: { type: String, required: true, ref: "User" },
status: { type: String, enum: ["open", "closed"], default: "open" },
title: { type: String, required: true },
/** @deprecated */
project: { type: String, enum: uniqueProjects, required: false },
projects: [{ type: String, enum: uniqueProjects, required: false }],
description: { type: String, required: true },
sent: { type: Boolean, default: false },
message_id: String,
thread_id: String,
},
{ timestamps: true },
);
const MediaSchema = new Schema(
{
media_type: { type: String, enum: ["image", "video"], required: true },
data: { type: Buffer, required: true },
user_id: { type: String, required: true, ref: "User" },
bug_id: { type: String, ref: "Bug" },
extension: { type: String, required: true },
},
{ timestamps: true },
);
// compound indexes for better perf
UserSchema.index({ user_id: 1, guild_id: 1 }, { unique: true });
// types
export type GuildType = HydratedDocument<InferSchemaType<typeof GuildSchema>>;
export type UserType = HydratedDocument<InferSchemaType<typeof UserSchema>>;
export type BugType = HydratedDocument<InferSchemaType<typeof BugSchema>>;
export type MediaType = HydratedDocument<InferSchemaType<typeof MediaSchema>>;
// models
export const GuildModel = model<GuildType>("Guild", GuildSchema);
export const UserModel = model<UserType>("User", UserSchema);
export const BugModel = model<BugType>("Bug", BugSchema);
export const MediaModel = model<MediaType>("Media", MediaSchema);
export const CounterModel = model("Counter", CounterSchema);
// funcs
export async function getNextBugId(): Promise<number> {
const counter = await CounterModel.findByIdAndUpdate(
"bug_id",
{ $inc: { sequence_value: 1 } },
{ new: true, upsert: true },
);
return counter.sequence_value;
}
|