返回 AiToEarn
draft-generation-memory.repository.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / draft-generation-memory.repository.ts
1 import type { DraftGenerationMemoryContentType } from '@yikart/aitoearn-ai-shared'
2 import type { UpdateQuery } from 'mongoose'
3 import { Injectable } from '@nestjs/common'
4 import { InjectModel } from '@nestjs/mongoose'
5 import { Model } from 'mongoose'
6 import { DraftGenerationMemory } from '../schemas'
7 import { BaseRepository } from './base.repository'
8
9 @Injectable()
10 export class DraftGenerationMemoryRepository extends BaseRepository<DraftGenerationMemory> {
11 constructor(
12 @InjectModel(DraftGenerationMemory.name)
13 private readonly draftGenerationMemoryModel: Model<DraftGenerationMemory>,
14 ) {
15 super(draftGenerationMemoryModel)
16 }
17
18 async getByUserIdAndContentType(userId: string, contentType: DraftGenerationMemoryContentType) {
19 return await this.findOne({ userId, contentType })
20 }
21
22 async listByUserId(userId: string, contentType?: DraftGenerationMemoryContentType) {
23 return await this.find(
24 { userId, ...(contentType && { contentType }) },
25 { sort: { contentType: 1 } },
26 )
27 }
28
29 async updateByUserIdAndContentType(
30 userId: string,
31 contentType: DraftGenerationMemoryContentType,
32 update: UpdateQuery<DraftGenerationMemory>,
33 ) {
34 return await this.draftGenerationMemoryModel.findOneAndUpdate(
35 { userId, contentType },
36 update,
37 { upsert: true, new: true },
38 ).lean({ virtuals: true }).exec()
39 }
40
41 async deleteItemByUserIdAndItemId(userId: string, itemId: string) {
42 return await this.draftGenerationMemoryModel.findOneAndUpdate(
43 { userId, 'items.id': itemId },
44 { $pull: { items: { id: itemId } } },
45 { new: true },
46 ).lean({ virtuals: true }).exec()
47 }
48 }
49
49 lines TYPESCRIPT