Files
app-admin/app/config/actions.ts
2026-03-24 15:46:27 +03:00

49 lines
1.2 KiB
TypeScript

"use server";
import { db } from "@/db";
import { remoteConfig } from "@/db/schema";
import { eq, and } from "drizzle-orm";
/** Fetch all active config entries for a given app */
export async function getAppConfigs(appId: number) {
return await db.query.remoteConfig.findMany({
where: eq(remoteConfig.appId, appId),
orderBy: (rc, { asc }) => [asc(rc.configKey)],
});
}
/**
* Insert or update a config key for an app.
* If the key already exists for this appId, it is overwritten.
*/
export async function upsertConfig(
appId: number,
key: string,
value: unknown
) {
const existing = await db.query.remoteConfig.findFirst({
where: and(
eq(remoteConfig.appId, appId),
eq(remoteConfig.configKey, key)
),
});
if (existing) {
await db
.update(remoteConfig)
.set({ configValue: value, updatedAt: new Date() })
.where(eq(remoteConfig.id, existing.id));
} else {
await db.insert(remoteConfig).values({
appId,
configKey: key,
configValue: value,
});
}
}
/** Delete a config entry by its primary key */
export async function deleteConfig(id: number) {
await db.delete(remoteConfig).where(eq(remoteConfig.id, id));
}