@@ -114,6 +114,7 @@
|
|||||||
"mkdirp": "*",
|
"mkdirp": "*",
|
||||||
"mongoose": "7.0.4",
|
"mongoose": "7.0.4",
|
||||||
"mongoose-aggregate-paginate-v2": "1.0.6",
|
"mongoose-aggregate-paginate-v2": "1.0.6",
|
||||||
|
"mongoose-autopopulate": "1.0.0",
|
||||||
"mongoose-lean-getters": "0.4.0",
|
"mongoose-lean-getters": "0.4.0",
|
||||||
"mongoose-lean-id": "0.5.0",
|
"mongoose-lean-id": "0.5.0",
|
||||||
"mongoose-lean-virtuals": "0.9.1",
|
"mongoose-lean-virtuals": "0.9.1",
|
||||||
|
|||||||
4
paw.paw
4
paw.paw
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:92c5523d93dee059e54c5056dc000fc3162aee6ec2398ffb2a354f3f980efa4c
|
oid sha256:8ebe3bf70ea48751487ce58e435965f0a6a12881b4e261162199ebf6194356c9
|
||||||
size 102593
|
size 105433
|
||||||
|
|||||||
11
pnpm-lock.yaml
generated
11
pnpm-lock.yaml
generated
@@ -176,6 +176,9 @@ importers:
|
|||||||
mongoose-aggregate-paginate-v2:
|
mongoose-aggregate-paginate-v2:
|
||||||
specifier: 1.0.6
|
specifier: 1.0.6
|
||||||
version: 1.0.6
|
version: 1.0.6
|
||||||
|
mongoose-autopopulate:
|
||||||
|
specifier: 1.0.0
|
||||||
|
version: 1.0.0(mongoose@7.0.4)
|
||||||
mongoose-lean-getters:
|
mongoose-lean-getters:
|
||||||
specifier: 0.4.0
|
specifier: 0.4.0
|
||||||
version: 0.4.0(mongoose@7.0.4)
|
version: 0.4.0(mongoose@7.0.4)
|
||||||
@@ -6971,6 +6974,14 @@ packages:
|
|||||||
engines: {node: '>=4.0.0'}
|
engines: {node: '>=4.0.0'}
|
||||||
dev: false
|
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):
|
/mongoose-lean-getters@0.4.0(mongoose@7.0.4):
|
||||||
resolution: {integrity: sha512-4YgzBZYFpXZm4Roz2MKXm3X2yDGR6OkpZx0jka4qTM3S3Gul/KV7SqPlZLauJG1RjU9hyuOvnDXYcji7iZXwcA==}
|
resolution: {integrity: sha512-4YgzBZYFpXZm4Roz2MKXm3X2yDGR6OkpZx0jka4qTM3S3Gul/KV7SqPlZLauJG1RjU9hyuOvnDXYcji7iZXwcA==}
|
||||||
engines: {node: '>= 8'}
|
engines: {node: '>= 8'}
|
||||||
|
|||||||
@@ -156,6 +156,7 @@ export class CommentController {
|
|||||||
limit: size,
|
limit: size,
|
||||||
page,
|
page,
|
||||||
sort: { pin: -1, created: -1 },
|
sort: { pin: -1, created: -1 },
|
||||||
|
populate: 'children',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
import { Query, Types } from 'mongoose'
|
import { Types } from 'mongoose'
|
||||||
|
import autopopulate from 'mongoose-autopopulate'
|
||||||
import { URL } from 'url'
|
import { URL } from 'url'
|
||||||
|
|
||||||
import {
|
import { Ref, modelOptions, plugin, prop } from '@typegoose/typegoose'
|
||||||
DocumentType,
|
|
||||||
Ref,
|
|
||||||
modelOptions,
|
|
||||||
pre,
|
|
||||||
prop,
|
|
||||||
} from '@typegoose/typegoose'
|
|
||||||
import { BeAnObject } from '@typegoose/typegoose/lib/types'
|
|
||||||
|
|
||||||
import { BaseModel } from '~/shared/model/base.model'
|
import { BaseModel } from '~/shared/model/base.model'
|
||||||
import { getAvatar } from '~/utils'
|
import { getAvatar } from '~/utils'
|
||||||
@@ -18,19 +12,6 @@ import { PageModel } from '../page/page.model'
|
|||||||
import { PostModel } from '../post/post.model'
|
import { PostModel } from '../post/post.model'
|
||||||
import { RecentlyModel } from '../recently/recently.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 {
|
export enum CommentRefTypes {
|
||||||
Post = 'Post',
|
Post = 'Post',
|
||||||
Note = 'Note',
|
Note = 'Note',
|
||||||
@@ -44,13 +25,12 @@ export enum CommentState {
|
|||||||
Junk,
|
Junk,
|
||||||
}
|
}
|
||||||
|
|
||||||
@pre<CommentModel>('findOne', autoPopulateSubs)
|
|
||||||
@pre<CommentModel>('find', autoPopulateSubs)
|
|
||||||
@modelOptions({
|
@modelOptions({
|
||||||
options: {
|
options: {
|
||||||
customName: 'Comment',
|
customName: 'Comment',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@plugin(autopopulate)
|
||||||
export class CommentModel extends BaseModel {
|
export class CommentModel extends BaseModel {
|
||||||
@prop({ refPath: 'refType' })
|
@prop({ refPath: 'refType' })
|
||||||
ref: Ref<PostModel | NoteModel | PageModel | RecentlyModel>
|
ref: Ref<PostModel | NoteModel | PageModel | RecentlyModel>
|
||||||
|
|||||||
@@ -228,6 +228,7 @@ export class CommentService {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
sort: { created: -1 },
|
sort: { created: -1 },
|
||||||
|
autopopulate: false,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
ValidateNested,
|
ValidateNested,
|
||||||
} from 'class-validator'
|
} from 'class-validator'
|
||||||
import { Query } from 'mongoose'
|
import { Query } from 'mongoose'
|
||||||
|
import mongooseAutoPopulate from 'mongoose-autopopulate'
|
||||||
|
|
||||||
import { PartialType } from '@nestjs/mapped-types'
|
import { PartialType } from '@nestjs/mapped-types'
|
||||||
import { AutoIncrementID } from '@typegoose/auto-increment'
|
import { AutoIncrementID } from '@typegoose/auto-increment'
|
||||||
@@ -18,7 +19,6 @@ import {
|
|||||||
index,
|
index,
|
||||||
modelOptions,
|
modelOptions,
|
||||||
plugin,
|
plugin,
|
||||||
pre,
|
|
||||||
prop,
|
prop,
|
||||||
} from '@typegoose/typegoose'
|
} from '@typegoose/typegoose'
|
||||||
import { BeAnObject } from '@typegoose/typegoose/lib/types'
|
import { BeAnObject } from '@typegoose/typegoose/lib/types'
|
||||||
@@ -42,8 +42,7 @@ import { NoteMusic } from './models/music.model'
|
|||||||
@index({ text: 'text' })
|
@index({ text: 'text' })
|
||||||
@index({ modified: -1 })
|
@index({ modified: -1 })
|
||||||
@index({ nid: -1 })
|
@index({ nid: -1 })
|
||||||
@pre('findOne', autoPopulateTopic)
|
@plugin(mongooseAutoPopulate)
|
||||||
@pre('find', autoPopulateTopic)
|
|
||||||
export class NoteModel extends WriteBaseModel {
|
export class NoteModel extends WriteBaseModel {
|
||||||
@prop()
|
@prop()
|
||||||
@IsString()
|
@IsString()
|
||||||
@@ -118,6 +117,7 @@ export class NoteModel extends WriteBaseModel {
|
|||||||
foreignField: '_id',
|
foreignField: '_id',
|
||||||
localField: 'topicId',
|
localField: 'topicId',
|
||||||
ref: () => TopicModel,
|
ref: () => TopicModel,
|
||||||
|
autopopulate: true,
|
||||||
})
|
})
|
||||||
topic?: TopicModel
|
topic?: TopicModel
|
||||||
|
|
||||||
|
|||||||
@@ -11,23 +11,21 @@ import {
|
|||||||
Min,
|
Min,
|
||||||
isDateString,
|
isDateString,
|
||||||
} from 'class-validator'
|
} from 'class-validator'
|
||||||
import { Query, Types } from 'mongoose'
|
import { Types } from 'mongoose'
|
||||||
import aggregatePaginate from 'mongoose-aggregate-paginate-v2'
|
import aggregatePaginate from 'mongoose-aggregate-paginate-v2'
|
||||||
|
import mongooseAutoPopulate from 'mongoose-autopopulate'
|
||||||
|
|
||||||
import { UnprocessableEntityException } from '@nestjs/common'
|
import { UnprocessableEntityException } from '@nestjs/common'
|
||||||
import { PartialType } from '@nestjs/mapped-types'
|
import { PartialType } from '@nestjs/mapped-types'
|
||||||
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger'
|
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger'
|
||||||
import {
|
import {
|
||||||
DocumentType,
|
|
||||||
Ref,
|
Ref,
|
||||||
Severity,
|
Severity,
|
||||||
index,
|
index,
|
||||||
modelOptions,
|
modelOptions,
|
||||||
plugin,
|
plugin,
|
||||||
pre,
|
|
||||||
prop,
|
prop,
|
||||||
} from '@typegoose/typegoose'
|
} from '@typegoose/typegoose'
|
||||||
import { BeAnObject } from '@typegoose/typegoose/lib/types'
|
|
||||||
|
|
||||||
import { Paginator } from '~/shared/interface/paginator.interface'
|
import { Paginator } from '~/shared/interface/paginator.interface'
|
||||||
import { CountModel as Count } from '~/shared/model/count.model'
|
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'
|
import { CategoryModel as Category } from '../category/category.model'
|
||||||
|
|
||||||
@plugin(aggregatePaginate)
|
@plugin(aggregatePaginate)
|
||||||
@pre<PostModel>('findOne', autoPopulateRelated)
|
@plugin(mongooseAutoPopulate)
|
||||||
@pre<PostModel>('findOne', autoPopulateCategory)
|
|
||||||
@pre<PostModel>('find', autoPopulateCategory)
|
|
||||||
@index({ slug: 1 })
|
@index({ slug: 1 })
|
||||||
@index({ modified: -1 })
|
@index({ modified: -1 })
|
||||||
@index({ text: 'text' })
|
@index({ text: 'text' })
|
||||||
@@ -65,6 +61,7 @@ export class PostModel extends WriteBaseModel {
|
|||||||
foreignField: '_id',
|
foreignField: '_id',
|
||||||
localField: 'categoryId',
|
localField: 'categoryId',
|
||||||
justOne: true,
|
justOne: true,
|
||||||
|
autopopulate: true,
|
||||||
})
|
})
|
||||||
@ApiHideProperty()
|
@ApiHideProperty()
|
||||||
public category: Ref<Category>
|
public category: Ref<Category>
|
||||||
@@ -127,6 +124,7 @@ export class PostModel extends WriteBaseModel {
|
|||||||
@prop({
|
@prop({
|
||||||
type: Types.ObjectId,
|
type: Types.ObjectId,
|
||||||
ref: () => PostModel,
|
ref: () => PostModel,
|
||||||
|
autopopulate: true,
|
||||||
})
|
})
|
||||||
related?: Partial<PostModel>[]
|
related?: Partial<PostModel>[]
|
||||||
|
|
||||||
@@ -141,41 +139,3 @@ export class PostPaginatorModel {
|
|||||||
data: PostModel[]
|
data: PostModel[]
|
||||||
pagination: Paginator
|
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()
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user