fix: autopopulate

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2023-04-24 23:38:33 +08:00
parent 5580aae3ca
commit dd0ca23885
8 changed files with 28 additions and 74 deletions

View File

@@ -114,6 +114,7 @@
"mkdirp": "*",
"mongoose": "7.0.4",
"mongoose-aggregate-paginate-v2": "1.0.6",
"mongoose-autopopulate": "1.0.0",
"mongoose-lean-getters": "0.4.0",
"mongoose-lean-id": "0.5.0",
"mongoose-lean-virtuals": "0.9.1",

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:92c5523d93dee059e54c5056dc000fc3162aee6ec2398ffb2a354f3f980efa4c
size 102593
oid sha256:8ebe3bf70ea48751487ce58e435965f0a6a12881b4e261162199ebf6194356c9
size 105433

11
pnpm-lock.yaml generated
View File

@@ -176,6 +176,9 @@ importers:
mongoose-aggregate-paginate-v2:
specifier: 1.0.6
version: 1.0.6
mongoose-autopopulate:
specifier: 1.0.0
version: 1.0.0(mongoose@7.0.4)
mongoose-lean-getters:
specifier: 0.4.0
version: 0.4.0(mongoose@7.0.4)
@@ -6971,6 +6974,14 @@ packages:
engines: {node: '>=4.0.0'}
dev: false
/mongoose-autopopulate@1.0.0(mongoose@7.0.4):
resolution: {integrity: sha512-VWsGb4VqSFqWgS6a7zFTOuUr2+XArrThtX7fjUlzIMPowVxvuS1Zeiif7D0DgXIETLNc+B/0r84kV5A2B7w2UQ==}
peerDependencies:
mongoose: 6.x || 7.x
dependencies:
mongoose: 7.0.4
dev: false
/mongoose-lean-getters@0.4.0(mongoose@7.0.4):
resolution: {integrity: sha512-4YgzBZYFpXZm4Roz2MKXm3X2yDGR6OkpZx0jka4qTM3S3Gul/KV7SqPlZLauJG1RjU9hyuOvnDXYcji7iZXwcA==}
engines: {node: '>= 8'}

View File

@@ -156,6 +156,7 @@ export class CommentController {
limit: size,
page,
sort: { pin: -1, created: -1 },
populate: 'children',
},
)

View File

@@ -1,14 +1,8 @@
import { Query, Types } from 'mongoose'
import { Types } from 'mongoose'
import autopopulate from 'mongoose-autopopulate'
import { URL } from 'url'
import {
DocumentType,
Ref,
modelOptions,
pre,
prop,
} from '@typegoose/typegoose'
import { BeAnObject } from '@typegoose/typegoose/lib/types'
import { Ref, modelOptions, plugin, prop } from '@typegoose/typegoose'
import { BaseModel } from '~/shared/model/base.model'
import { getAvatar } from '~/utils'
@@ -18,19 +12,6 @@ import { PageModel } from '../page/page.model'
import { PostModel } from '../post/post.model'
import { RecentlyModel } from '../recently/recently.model'
function autoPopulateSubs(
this: Query<
any,
DocumentType<CommentModel, BeAnObject>,
{},
DocumentType<CommentModel, BeAnObject>
>,
next: () => void,
) {
this.populate({ options: { sort: { created: -1 } }, path: 'children' })
next()
}
export enum CommentRefTypes {
Post = 'Post',
Note = 'Note',
@@ -44,13 +25,12 @@ export enum CommentState {
Junk,
}
@pre<CommentModel>('findOne', autoPopulateSubs)
@pre<CommentModel>('find', autoPopulateSubs)
@modelOptions({
options: {
customName: 'Comment',
},
})
@plugin(autopopulate)
export class CommentModel extends BaseModel {
@prop({ refPath: 'refType' })
ref: Ref<PostModel | NoteModel | PageModel | RecentlyModel>

View File

@@ -228,6 +228,7 @@ export class CommentService {
},
],
sort: { created: -1 },
autopopulate: false,
},
)

View File

@@ -9,6 +9,7 @@ import {
ValidateNested,
} from 'class-validator'
import { Query } from 'mongoose'
import mongooseAutoPopulate from 'mongoose-autopopulate'
import { PartialType } from '@nestjs/mapped-types'
import { AutoIncrementID } from '@typegoose/auto-increment'
@@ -18,7 +19,6 @@ import {
index,
modelOptions,
plugin,
pre,
prop,
} from '@typegoose/typegoose'
import { BeAnObject } from '@typegoose/typegoose/lib/types'
@@ -42,8 +42,7 @@ import { NoteMusic } from './models/music.model'
@index({ text: 'text' })
@index({ modified: -1 })
@index({ nid: -1 })
@pre('findOne', autoPopulateTopic)
@pre('find', autoPopulateTopic)
@plugin(mongooseAutoPopulate)
export class NoteModel extends WriteBaseModel {
@prop()
@IsString()
@@ -118,6 +117,7 @@ export class NoteModel extends WriteBaseModel {
foreignField: '_id',
localField: 'topicId',
ref: () => TopicModel,
autopopulate: true,
})
topic?: TopicModel

View File

@@ -11,23 +11,21 @@ import {
Min,
isDateString,
} from 'class-validator'
import { Query, Types } from 'mongoose'
import { Types } from 'mongoose'
import aggregatePaginate from 'mongoose-aggregate-paginate-v2'
import mongooseAutoPopulate from 'mongoose-autopopulate'
import { UnprocessableEntityException } from '@nestjs/common'
import { PartialType } from '@nestjs/mapped-types'
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger'
import {
DocumentType,
Ref,
Severity,
index,
modelOptions,
plugin,
pre,
prop,
} from '@typegoose/typegoose'
import { BeAnObject } from '@typegoose/typegoose/lib/types'
import { Paginator } from '~/shared/interface/paginator.interface'
import { CountModel as Count } from '~/shared/model/count.model'
@@ -36,9 +34,7 @@ import { WriteBaseModel } from '~/shared/model/write-base.model'
import { CategoryModel as Category } from '../category/category.model'
@plugin(aggregatePaginate)
@pre<PostModel>('findOne', autoPopulateRelated)
@pre<PostModel>('findOne', autoPopulateCategory)
@pre<PostModel>('find', autoPopulateCategory)
@plugin(mongooseAutoPopulate)
@index({ slug: 1 })
@index({ modified: -1 })
@index({ text: 'text' })
@@ -65,6 +61,7 @@ export class PostModel extends WriteBaseModel {
foreignField: '_id',
localField: 'categoryId',
justOne: true,
autopopulate: true,
})
@ApiHideProperty()
public category: Ref<Category>
@@ -127,6 +124,7 @@ export class PostModel extends WriteBaseModel {
@prop({
type: Types.ObjectId,
ref: () => PostModel,
autopopulate: true,
})
related?: Partial<PostModel>[]
@@ -141,41 +139,3 @@ export class PostPaginatorModel {
data: PostModel[]
pagination: Paginator
}
function autoPopulateCategory(
this: Query<
any,
DocumentType<PostModel, BeAnObject>,
{},
DocumentType<PostModel, BeAnObject>
>,
next: () => void,
) {
this.populate({ path: 'category' })
next()
}
function autoPopulateRelated(
this: Query<
any,
DocumentType<PostModel, BeAnObject>,
{},
DocumentType<PostModel, BeAnObject>
>,
next: () => void,
) {
this.populate({
path: 'related',
select: [
'slug',
'title',
'summary',
'created',
'categoryId',
'modified',
'_id',
'id',
],
})
next()
}