next/models/challenges/gen.ts
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ a8e113e969
refactor(db/models): remove model existence guards
this way, plugins are registered if `hasMigrated` is true. we've already enabled automatically overwriting models, therefore, the guard's condition will always be false
2023-12-29 16:36:26 -05:00

52 lines
1.0 KiB
TypeScript

import mongoose, { Schema, PopulatedDoc, Document, Model } from "mongoose";
import SequenceFactory from "mongoose-sequence";
import { hasMigrated } from "@dbconfig";
const AutoIncrement = SequenceFactory(mongoose);
export interface IChallenge {
_id: number;
name: string;
description: string;
deadline: Date;
active: boolean;
color: string;
allowMultiple: boolean;
}
const challengeSchema = new mongoose.Schema<IChallenge>({
_id: {
type: Number,
},
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
deadline: {
type: Date,
required: true,
},
active: {
type: Boolean,
default: true,
},
color: {
type: String,
required: true,
default: `#${Math.floor(Math.random() * 16777215).toString(16)}`,
},
allowMultiple: {
type: Boolean,
default: true,
},
});
hasMigrated && challengeSchema.plugin(AutoIncrement, { id: "challenges" });
export const Challenge: Model<IChallenge> =
// mongoose.models.Challenge ||
mongoose.model("Challenge", challengeSchema, "challenges");