import { Injectable } from '@nestjs/common' import { HttpService } from '../services/http' import { BaseRepository } from './base.repository' const GET = ` query config($key: String!) { config(where: {key: {_eq: $key}}) { value } } ` const GET_BY_PREFIX = ` query config($prefix: String!) { config(where: {key: {_like: $prefix}}) { key value } } ` const SET = ` mutation setConfig($key: String!, $value: String!) { insert_config_one(object: {key: $key, value: $value}, on_conflict: {constraint: config_pkey, update_columns: value}) { value } } ` @Injectable() export class ConfigRepository extends BaseRepository { constructor(protected readonly httpService: HttpService) { super(httpService, 'config') } async get(key: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET, variables: { key } }, }) return data?.config?.[0]?.value || null } async getByPrefix(prefix: string): Promise<{ key: string; value: string }[]> { const { data } = await this.fetchGraphQL({ body: { query: GET_BY_PREFIX, variables: { prefix: `${prefix}_%` } }, }) return data?.config || null } async set(key: string, value: string): Promise { await this.fetchGraphQL({ body: { query: SET, variables: { key, value } }, }) } }