Skip to content

Category Setup

First, we need to create the schema and operations for our new collection.

Create a new data handler for categories:

data/categories.js
const mongoose = require("mongoose");
const categorySchema = new mongoose.Schema({
slug: { type: String, required: true, unique: true },
name: { type: String, required: true },
description: { type: String }, // Optional context for the category
});
const Category = mongoose.model("Category", categorySchema);
class CategoryOps {
async getAllCategories() {
// To be implemented
}
async getCategoryById(id) {
// To be implemented
}
async createCategory(formData) {
// To be implemented
}
async updateCategoryById(id, updates) {
// To be implemented
}
async deleteCategoryById(id) {
// To be implemented
}
}
module.exports = new CategoryOps();

We have our model, but no way to interact with it yet. Next, let’s build the operations and views necessary to create our first categories.