Category Setup
The Category Model
Section titled “The Category Model”First, we need to create the schema and operations for our new collection.
Create a new data handler for categories:
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();⏭ Next: Creating Categories
Section titled “⏭ Next: Creating Categories”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.