-
Notifications
You must be signed in to change notification settings - Fork 11
/
post.model.ts
129 lines (118 loc) · 2.8 KB
/
post.model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* @FilePath: /nx-core/apps/page-service/src/model/post.model.ts
* @author: Wibus
* @Date: 2022-09-18 15:02:05
* @LastEditors: Wibus
* @LastEditTime: 2022-09-24 07:59:35
* Coding With IU
*/
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import {
index,
modelOptions,
pre,
prop,
Ref,
Severity,
plugin,
DocumentType,
} from '@typegoose/typegoose';
import { BeAnObject } from '@typegoose/typegoose/lib/types';
import {
IsString,
IsOptional,
IsMongoId,
IsBoolean,
ArrayUnique,
} from 'class-validator';
import { Query } from 'mongoose';
import { CountMixed, WriteBaseModel } from '~/shared/model/base.model';
import { CategoryModel } from './category.model';
import aggregatePaginate from 'mongoose-aggregate-paginate-v2';
@plugin(aggregatePaginate)
@pre<PostModel>('findOne', autoPopulateCategory)
@pre<PostModel>('find', autoPopulateCategory)
@index({ slug: 1 })
@index({ modified: -1 })
@index({ text: 'text' })
@modelOptions({ options: { customName: 'Post', allowMixed: Severity.ALLOW } })
export class PostModel extends WriteBaseModel {
@prop({ trim: true, unique: true })
@IsString()
@IsOptional()
@ApiProperty({ description: '文章路径' })
slug?: string;
@prop({ trim: true })
@IsString()
@ApiProperty({ description: '文章摘要' })
@IsOptional()
summary?: string;
@prop({ ref: () => CategoryModel, required: true })
@IsMongoId()
@ApiProperty({ example: '5eb2c62a613a5ab0642f1f7a' })
@ApiProperty({ description: '文章分类' })
categoryId: Ref<CategoryModel>;
@prop({
ref: () => CategoryModel,
foreignField: '_id',
localField: 'categoryId',
justOne: true,
})
@ApiHideProperty()
public category?: Ref<CategoryModel>;
@prop({ default: true })
@IsBoolean()
@IsOptional()
@ApiProperty({ description: '文章署名' })
copyright?: boolean;
@prop({
type: CountMixed,
default: {
read: 0,
like: 0,
},
_id: false, // 关闭_id
})
@IsOptional()
@ApiProperty({ description: '文章阅读 & 喜欢数' })
count?: CountMixed;
@prop({
type: String,
})
@IsString({ each: true })
@IsOptional()
@ArrayUnique()
@ApiProperty({ description: '文章标签' })
tags?: string[];
@prop({
type: Date,
default: Date.now,
})
@IsString()
@IsOptional()
@ApiProperty({ description: '文章创建时间' })
created?: Date;
@prop({
type: Date,
})
@IsString()
@IsOptional()
@ApiProperty({ description: '文章修改时间' })
modified?: Date;
// @prop()
// @IsOptional()
// @ApiProperty({ description: "文章作者" })
// username?: string;
}
function autoPopulateCategory(
this: Query<
any,
DocumentType<PostModel, BeAnObject>,
{},
DocumentType<PostModel, BeAnObject>
>,
next: () => void,
) {
this.populate({ path: 'category' });
next();
}