返回 AiToEarn
app-config.repository.ts
根目录 / project / aitoearn-backend / libs / mongodb / src / repositories / app-config.repository.ts
1 import { InjectModel } from '@nestjs/mongoose'
2 import { Pagination } from '@yikart/common'
3 import { FilterQuery, Model, RootFilterQuery } from 'mongoose'
4 import { AppConfig } from '../schemas'
5 import { BaseRepository } from './base.repository'
6
7 export interface ListAppConfigParams extends Pagination {
8 appId?: string
9 key?: string
10 enabled?: boolean
11 keyword?: string
12 }
13
14 export class AppConfigRepository extends BaseRepository<AppConfig> {
15 constructor(
16 @InjectModel(AppConfig.name) appConfigModel: Model<AppConfig>,
17 ) {
18 super(appConfigModel)
19 }
20
21 async getByKey(appId: string): Promise<Record<string, any>> {
22 const configs = await this.model.find({
23 appId,
24 enabled: true,
25 }).lean({ virtuals: true }).exec()
26
27 return configs
28 }
29
30 async listHistoryByKey(appId: string, key: string, limit = 10): Promise<AppConfig[]> {
31 return await this.model.find({ appId, key })
32 .sort({ updatedAt: -1 })
33 .limit(limit)
34 .lean({ virtuals: true })
35 .exec()
36 }
37
38 async getEnabledByAppIdAndKey(appId: string, key: string): Promise<AppConfig | null> {
39 return await this.model.findOne({
40 appId,
41 key,
42 enabled: true,
43 }).lean({ virtuals: true }).exec()
44 }
45
46 async updateConfig(
47 appId: string,
48 key: string,
49 value: Record<string, any>,
50 description?: string,
51 metadata?: Record<string, any>,
52 ): Promise<AppConfig> {
53 const updatedConfig = await this.model.findOneAndUpdate(
54 { appId, key },
55 {
56 $set: {
57 value,
58 description,
59 metadata,
60 enabled: true,
61 },
62 },
63 { upsert: true, new: true },
64 ).lean({ virtuals: true }).exec()
65
66 return updatedConfig
67 }
68
69 async batchUpdateConfigs(
70 appId: string,
71 configs: Record<string, any>,
72 ): Promise<{ success: boolean, updatedCount: number }> {
73 const bulkOps = Object.entries(configs).map(([key, value]) => {
74 return {
75 updateOne: {
76 filter: { appId, key },
77 update: {
78 $set: {
79 value,
80 enabled: true,
81 },
82 },
83 upsert: true,
84 },
85 } as const
86 })
87
88 const result = await this.model.bulkWrite(bulkOps)
89 return {
90 success: true,
91 updatedCount: result.modifiedCount + result.upsertedCount,
92 }
93 }
94
95 async deleteByKey(appId: string, key: string): Promise<boolean> {
96 const result = await this.model.deleteOne({ appId, key }).exec()
97 return result.deletedCount > 0
98 }
99
100 async listByAppId(
101 page: {
102 pageNo: number
103 pageSize: number
104 },
105 query: {
106 appId?: string
107 key?: string
108 },
109 ) {
110 const filter: RootFilterQuery<AppConfig> = {
111 ...(query.appId && { appId: query.appId }),
112 ...(query.key && { key: query.key }),
113 }
114 const total = await this.model.countDocuments(filter).exec()
115 const result = await this.model.find(filter).skip((page.pageNo - 1) * page.pageSize).limit(page.pageSize).lean({ virtuals: true }).exec()
116
117 return { total, list: result }
118 }
119
120 async listWithPagination(params: ListAppConfigParams) {
121 const { page, pageSize, appId, key, enabled, keyword } = params
122
123 const filter: FilterQuery<AppConfig> = {}
124 if (appId)
125 filter.appId = appId
126 if (key)
127 filter.key = key
128 if (enabled !== undefined)
129 filter.enabled = enabled
130 if (keyword) {
131 filter.$or = [
132 { key: { $regex: keyword, $options: 'i' } },
133 { description: { $regex: keyword, $options: 'i' } },
134 ]
135 }
136
137 return await this.findWithPagination({
138 page,
139 pageSize,
140 filter,
141 })
142 }
143
144 async listByAppIdWithEnabled(appId: string) {
145 return await this.find({
146 appId,
147 enabled: true,
148 })
149 }
150
151 async listByAppIdAndKey(appId: string, key: string, limit = 10) {
152 return await this.find(
153 { appId, key },
154 { sort: { updatedAt: -1 }, limit },
155 )
156 }
157
158 async upsertByAppIdAndKey(
159 appId: string,
160 key: string,
161 updateData: Partial<AppConfig>,
162 ) {
163 return await this.model.findOneAndUpdate(
164 { appId, key },
165 { $set: updateData },
166 { upsert: true, new: true },
167 ).lean({ virtuals: true }).exec()
168 }
169
170 async createOrUpdateMany(configEntries: Array<{
171 appId: string
172 key: string
173 value: Record<string, any>
174 enabled: boolean
175 }>) {
176 const bulkOps = configEntries.map(entry => ({
177 updateOne: {
178 filter: { appId: entry.appId, key: entry.key },
179 update: {
180 $set: {
181 value: entry.value,
182 enabled: entry.enabled,
183 },
184 },
185 upsert: true,
186 },
187 } as const))
188
189 const result = await this.model.bulkWrite(bulkOps)
190 return result.modifiedCount + result.upsertedCount
191 }
192
193 async deleteByAppIdAndKey(appId: string, key: string) {
194 const result = await this.deleteOne({ appId, key })
195 return result.deletedCount
196 }
197 }
198
198 lines TYPESCRIPT