Files
news-editor/server/src/models/news.model.ts
T

60 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Schema, model, type HydratedDocument, type Types } from 'mongoose';
export type NewsStatus = 'draft' | 'scheduled' | 'published';
export interface INews {
title: string;
content: string;
status: NewsStatus;
author: Types.ObjectId;
scheduledAt: Date | null;
publishedAt: Date | null;
createdAt: Date;
updatedAt: Date;
}
export type NewsDocument = HydratedDocument<INews>;
const newsSchema = new Schema<INews>(
{
title: {
type: String,
required: [true, 'Название новости обязательно'],
trim: true,
maxlength: [200, 'Название не должно превышать 200 символов'],
},
content: {
type: String,
default: '',
},
status: {
type: String,
enum: ['draft', 'scheduled', 'published'],
default: 'draft',
},
author: {
type: Schema.Types.ObjectId,
ref: 'User',
required: [true, 'Автор новости обязателен'],
},
scheduledAt: {
type: Date,
default: null,
},
publishedAt: {
type: Date,
default: null,
},
},
{
timestamps: true,
},
);
// Индекс для scheduler: поиск scheduled новостей с прошлым scheduledAt
newsSchema.index({ status: 1, scheduledAt: 1 });
const News = model<INews>('News', newsSchema);
export default News;