55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const artistSchema = new mongoose.Schema({
|
|
name: {
|
|
type: String,
|
|
required: true,
|
|
trim: true,
|
|
index: true
|
|
},
|
|
// Нормализованное имя для поиска (lowercase, no spaces)
|
|
normalizedName: {
|
|
type: String,
|
|
required: true,
|
|
lowercase: true,
|
|
index: true
|
|
},
|
|
description: {
|
|
type: String,
|
|
default: ''
|
|
},
|
|
coverImage: {
|
|
type: String,
|
|
default: null
|
|
},
|
|
// Статистика
|
|
stats: {
|
|
albums: { type: Number, default: 0 },
|
|
tracks: { type: Number, default: 0 },
|
|
plays: { type: Number, default: 0 }
|
|
},
|
|
// Кто добавил исполнителя
|
|
addedBy: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
ref: 'User',
|
|
required: true
|
|
}
|
|
}, {
|
|
timestamps: true
|
|
});
|
|
|
|
// Индексы для поиска
|
|
artistSchema.index({ name: 'text', normalizedName: 'text' });
|
|
artistSchema.index({ createdAt: -1 });
|
|
|
|
// Хук для автоматического создания normalizedName
|
|
artistSchema.pre('save', function(next) {
|
|
if (this.isModified('name')) {
|
|
this.normalizedName = this.name.toLowerCase().replace(/\s+/g, '');
|
|
}
|
|
next();
|
|
});
|
|
|
|
module.exports = mongoose.model('Artist', artistSchema);
|
|
|