37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class SettingsService {
|
|
constructor(private prisma: PrismaService) {}
|
|
|
|
async getAll(): Promise<Record<string, string>> {
|
|
const settings = await this.prisma.setting.findMany();
|
|
return Object.fromEntries(settings.map((s) => [s.key, s.value]));
|
|
}
|
|
|
|
async get(key: string): Promise<string | null> {
|
|
const setting = await this.prisma.setting.findUnique({ where: { key } });
|
|
return setting?.value ?? null;
|
|
}
|
|
|
|
async set(key: string, value: string) {
|
|
return this.prisma.setting.upsert({
|
|
where: { key },
|
|
update: { value },
|
|
create: { key, value },
|
|
});
|
|
}
|
|
|
|
async setMany(settings: Record<string, string>) {
|
|
const ops = Object.entries(settings).map(([key, value]) =>
|
|
this.prisma.setting.upsert({ where: { key }, update: { value }, create: { key, value } }),
|
|
);
|
|
return this.prisma.$transaction(ops);
|
|
}
|
|
|
|
async remove(key: string) {
|
|
return this.prisma.setting.delete({ where: { key } }).catch(() => null);
|
|
}
|
|
}
|