SpaceNinjaServer/src/models/leaderboardModel.ts
AMelonInsideLemon 049f709713 feat(leaderboard): missions & guilds leaderboard (#1338)
Reviewed-on: OpenWF/SpaceNinjaServer#1338
Reviewed-by: Sainan <sainan@calamity.inc>
Co-authored-by: AMelonInsideLemon <166175391+AMelonInsideLemon@users.noreply.github.com>
Co-committed-by: AMelonInsideLemon <166175391+AMelonInsideLemon@users.noreply.github.com>
2025-03-26 14:21:22 -07:00

28 lines
1.1 KiB
TypeScript

import { Document, model, Schema, Types } from "mongoose";
import { ILeaderboardEntryDatabase } from "../types/leaderboardTypes";
const leaderboardEntrySchema = new Schema<ILeaderboardEntryDatabase>(
{
leaderboard: { type: String, required: true },
ownerId: { type: Schema.Types.ObjectId, required: true },
displayName: { type: String, required: true },
score: { type: Number, required: true },
guildId: Schema.Types.ObjectId,
expiry: { type: Date, required: true },
guildTier: Number
},
{ id: false }
);
leaderboardEntrySchema.index({ leaderboard: 1 });
leaderboardEntrySchema.index({ leaderboard: 1, ownerId: 1 }, { unique: true });
leaderboardEntrySchema.index({ expiry: 1 }, { expireAfterSeconds: 0 }); // With this, MongoDB will automatically delete expired entries.
export const Leaderboard = model<ILeaderboardEntryDatabase>("Leaderboard", leaderboardEntrySchema);
// eslint-disable-next-line @typescript-eslint/ban-types
export type TLeaderboardEntryDocument = Document<unknown, {}, ILeaderboardEntryDatabase> & {
_id: Types.ObjectId;
__v: number;
} & ILeaderboardEntryDatabase;