implement platform config
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common'
|
||||
|
||||
// import { ConfigConstant } from 'src/common/constant/config.constant';
|
||||
import { PrismaService } from 'src/Prisma/prisma.service'
|
||||
import { ConfigConstants } from './appconfig.constant'
|
||||
import { Config } from '@prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class AppConfigService implements OnModuleInit {
|
||||
@@ -9,21 +10,49 @@ export class AppConfigService implements OnModuleInit {
|
||||
|
||||
async onModuleInit() {
|
||||
// get each config from database, if not exist, create default config
|
||||
// const configs = await this.prisma.config.findMany();
|
||||
// if (configs.length === 0) {
|
||||
// await this.prisma.config.createMany({
|
||||
// data: Object.values(ConfigConstant).map((config) => ({
|
||||
// ...config,
|
||||
// })),
|
||||
// });
|
||||
// }
|
||||
// // regenerate missing config
|
||||
// for (const config of Object.values(ConfigConstant)) {
|
||||
// if (!configs.find((c) => c.key === config.key)) {
|
||||
// await this.prisma.config.create({
|
||||
// data: config,
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
const configs = await this.prisma.config.findMany()
|
||||
if (configs.length === 0) {
|
||||
Object.entries(ConfigConstants).forEach(async ([_key, value]) => {
|
||||
await this.prisma.config.create({
|
||||
data: {
|
||||
name: value.name,
|
||||
key: value.key,
|
||||
value: value.value,
|
||||
visible: value.visible,
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async getConfig(key: string) {
|
||||
return await this.prisma.config.findUnique({
|
||||
where: { key },
|
||||
})
|
||||
}
|
||||
|
||||
async getVisibleConfigs() {
|
||||
return await this.prisma.config.findMany({
|
||||
where: { visible: true },
|
||||
})
|
||||
}
|
||||
|
||||
async updateConfig(key: string, value: string) {
|
||||
return await this.prisma.config.update({
|
||||
where: { key },
|
||||
data: { value },
|
||||
})
|
||||
}
|
||||
|
||||
async updateVisibleConfigs(configs: Config[]) {
|
||||
return await this.prisma.config.updateMany({
|
||||
data: configs,
|
||||
})
|
||||
}
|
||||
|
||||
async deleteConfig(key: string) {
|
||||
return await this.prisma.config.delete({
|
||||
where: { key },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user