next/models/challenges/gen.ts

54 lines
1.1 KiB
TypeScript
Raw Normal View History

import mongoose, { Schema, PopulatedDoc, Document, Model } from "mongoose";
import SequenceFactory from "mongoose-sequence";
import { hasMigrated } from "~/lib/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 &&
!mongoose.models.Challenge &&
challengeSchema.plugin(AutoIncrement, { id: "challenges" });
export const Challenge: Model<IChallenge> =
mongoose.models.Challenge ||
mongoose.model("Challenge", challengeSchema, "challenges");