Spaces:
Running
Running
| const mongoose = require('mongoose'); | |
| const slugify = require('slugify'); | |
| const brandSchema = new mongoose.Schema( | |
| { | |
| nameEn: { | |
| type: String, | |
| required: [true, 'A brand must have an English name'], | |
| trim: true, | |
| unique: true, | |
| }, | |
| nameAr: { | |
| type: String, | |
| trim: true, | |
| }, | |
| logo: { | |
| type: String, | |
| trim: true, | |
| }, | |
| id: { | |
| type: Number, | |
| required: [true, 'A brand must have an ID'], | |
| unique: true, | |
| }, | |
| slug: String, | |
| }, | |
| { | |
| timestamps: true, // adds createdAt and updatedAt automatically | |
| }, | |
| ); | |
| // Middleware to generate slug automatically before saving | |
| brandSchema.pre('save', function (next) { | |
| this.slug = slugify(this.nameEn, { lower: true }); | |
| next(); | |
| }); | |
| const Brand = mongoose.model('Brand', brandSchema); | |
| module.exports = Brand; | |