samoulla-backend / models /brandModel.js
Samoulla Sync Bot
Auto-deploy Samoulla Backend: 8574a71f0fc617aeb1ce9b5e35dac24c5319a12a
59c49c1
raw
history blame contribute delete
834 Bytes
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;