All files / models Category.ts

0% Statements 0/7
0% Branches 0/2
100% Functions 0/0
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                 
import mongoose, { Schema, Document, Model } from "mongoose";
 
export interface ICategory extends Document {
  slug: string;
  name: string;
  description?: string;
  order?: number;
  createdAt: Date;
  updatedAt: Date;
}
 
const CategorySchema = new Schema<ICategory>(
  {
    slug: {
      type: String,
      required: [true, "Slug is required"],
      unique: true,
      lowercase: true,
      trim: true,
      match: [/^[a-z0-9-]+$/, "Slug must be URL-safe (lowercase, numbers, and hyphens only)"],
    },
    name: {
      type: String,
      required: [true, "Name is required"],
      trim: true,
    },
    description: {
      type: String,
      trim: true,
    },
    order: {
      type: Number,
      default: 0,
    },
  },
  {
    timestamps: true,
  }
);
 
// Indexes
CategorySchema.index({ slug: 1 }, { unique: true });
CategorySchema.index({ order: 1 });
 
const Category: Model<ICategory> =
  mongoose.models.Category || mongoose.model<ICategory>("Category", CategorySchema);
 
export default Category;