25 lines
886 B
TypeScript
25 lines
886 B
TypeScript
import { db } from '../../db/db_index'
|
|
import { keyValStore } from '../../db/schema'
|
|
import { IConstantDbService, Constant, NewConstant } from '../../../../backend/src/trpc/apis/admin-apis/dataAccessors/interfaces/constant-db-service.interface'
|
|
|
|
export class ConstantDbService implements IConstantDbService {
|
|
async getAllConstants(): Promise<Constant[]> {
|
|
return db.select().from(keyValStore)
|
|
}
|
|
|
|
async upsertConstants(constants: { key: string; value: any }[]): Promise<number> {
|
|
await db.transaction(async (tx) => {
|
|
for (const { key, value } of constants) {
|
|
await tx.insert(keyValStore)
|
|
.values({ key, value })
|
|
.onConflictDoUpdate({
|
|
target: keyValStore.key,
|
|
set: { value },
|
|
})
|
|
}
|
|
})
|
|
return constants.length
|
|
}
|
|
}
|
|
|
|
export const constantDbService: IConstantDbService = new ConstantDbService()
|