app-template/packages/models/src/app-settings.ts

39 lines
1.5 KiB
TypeScript

import { sql } from "drizzle-orm";
import { integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core";
import { users } from "./users.js";
/**
* 应用级配置(跨用户共享)。敏感值存密文。
* 唯一键 `key`;由管理员经 API 写入,服务端启动/读取时解密。
*/
export const appSettings = sqliteTable(
"app_settings",
{
id: text("id")
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
key: text("key").notNull(),
/** 明文值;敏感项应写 encryptSecret() 密文 */
value: text("value").notNull().default(""),
/** 由谁改的(审计) */
updatedBy: text("updated_by").references(() => users.id, { onDelete: "set null" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.default(sql`(unixepoch())`),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.default(sql`(unixepoch())`),
},
(t) => [uniqueIndex("app_settings_key_uq").on(t.key)],
);
export type AppSettingRow = typeof appSettings.$inferSelect;
export type NewAppSettingRow = typeof appSettings.$inferInsert;
/** 开放弹幕网络配置键 */
export const APP_SETTING_OPEN_DANMAKU_APP_ID = "open_danmaku_app_id";
export const APP_SETTING_OPEN_DANMAKU_APP_SECRET = "open_danmaku_app_secret";
export const APP_SETTING_DANDANPLAY_TOKEN = "dandanplay_token";
export const APP_SETTING_DANDANPLAY_SCREEN_NAME = "dandanplay_screen_name";