2026-09-13 16:18:10 +08:00
|
|
|
/**
|
|
|
|
|
* The unified async DB interface. Routes depend only on this — never on
|
|
|
|
|
* `better-sqlite3` or `pg` directly — so the backend can switch between SQLite
|
|
|
|
|
* (`deployMode=local`) and Postgres (`deployMode=k8s`) without touching callers.
|
|
|
|
|
* @module dshs/db/adapter
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
BusinessPlugin,
|
|
|
|
|
CredentialKey,
|
2026-09-14 00:00:15 +08:00
|
|
|
CredentialKeyMeta,
|
|
|
|
|
CredentialLandingRow,
|
2026-09-13 16:18:10 +08:00
|
|
|
CreateSessionInput,
|
|
|
|
|
CreateUserInput,
|
|
|
|
|
Domain,
|
|
|
|
|
DshInstance,
|
|
|
|
|
DshInstanceRole,
|
|
|
|
|
DshInstanceStatus,
|
|
|
|
|
PublicUser,
|
|
|
|
|
SessionRow,
|
|
|
|
|
SessionUser,
|
|
|
|
|
UpsertBusinessPluginInput,
|
|
|
|
|
UpsertDshInstanceInput,
|
|
|
|
|
User,
|
|
|
|
|
UserRole,
|
|
|
|
|
Workspace,
|
|
|
|
|
} from './types.js'
|
|
|
|
|
|
|
|
|
|
export interface DbAdapter {
|
|
|
|
|
// users
|
|
|
|
|
createUser(input: CreateUserInput): Promise<User>
|
|
|
|
|
findUserByUsername(username: string): Promise<User | undefined>
|
|
|
|
|
findUserBySlug(slug: string): Promise<User | undefined>
|
|
|
|
|
findUserById(id: string): Promise<User | undefined>
|
|
|
|
|
listPublicUsers(): Promise<PublicUser[]>
|
|
|
|
|
countAdmins(): Promise<number>
|
|
|
|
|
setUserRole(id: string, role: UserRole, approvedBy?: string): Promise<boolean>
|
|
|
|
|
/** Assign a Linux uid to a user (used by the legacy backfill). */
|
|
|
|
|
setUserUid(userId: string, uid: number): Promise<void>
|
|
|
|
|
/** Ids of users whose uid column is still null (legacy rows awaiting backfill). */
|
|
|
|
|
listUsersWithoutUid(): Promise<string[]>
|
|
|
|
|
/**
|
|
|
|
|
* Permanently delete a user and every owned row (credential_vault / domains /
|
|
|
|
|
* sessions / dsh_instances / audit_log / folder_plugins / workspaces), inside
|
|
|
|
|
* one transaction. Returns false when no such user exists.
|
|
|
|
|
*/
|
|
|
|
|
deleteUser(userId: string): Promise<boolean>
|
|
|
|
|
// sessions
|
|
|
|
|
createSession(input: CreateSessionInput): Promise<void>
|
|
|
|
|
findSession(tokenHash: string): Promise<SessionRow | undefined>
|
|
|
|
|
deleteSession(tokenHash: string): Promise<void>
|
|
|
|
|
deleteUserSessions(userId: string): Promise<void>
|
|
|
|
|
findSessionWithUser(tokenHash: string): Promise<SessionUser | undefined>
|
|
|
|
|
/** Whether the user has any session that has not yet expired (idle reap). */
|
|
|
|
|
hasActiveSession(userId: string): Promise<boolean>
|
|
|
|
|
// audit
|
|
|
|
|
audit(actor: string | null, action: string, detail?: string | null): Promise<void>
|
|
|
|
|
// workspaces / plugins
|
|
|
|
|
findWorkspaceByPath(userId: string, relPath: string): Promise<Workspace | undefined>
|
|
|
|
|
getOrCreateWorkspace(userId: string, relPath: string): Promise<Workspace>
|
|
|
|
|
setFolderPlugins(workspaceId: string, selections: ReadonlyArray<{ id: string; enabled: boolean }>): Promise<void>
|
|
|
|
|
getEnabledPluginIds(workspaceId: string): Promise<string[]>
|
|
|
|
|
// business plugins (系统外插件候选池)
|
|
|
|
|
listBusinessPlugins(): Promise<BusinessPlugin[]>
|
|
|
|
|
findBusinessPlugin(id: string): Promise<BusinessPlugin | undefined>
|
|
|
|
|
upsertBusinessPlugin(input: UpsertBusinessPluginInput): Promise<BusinessPlugin>
|
|
|
|
|
deleteBusinessPlugin(id: string): Promise<boolean>
|
|
|
|
|
// domains
|
|
|
|
|
findDomainByUser(userId: string): Promise<Domain | undefined>
|
|
|
|
|
findDomainById(id: string): Promise<Domain | undefined>
|
|
|
|
|
listDomains(): Promise<Domain[]>
|
|
|
|
|
upsertDomain(userId: string, domain: string, nginxConfig: string): Promise<Domain>
|
|
|
|
|
setDomainVerified(id: string, verified: boolean): Promise<boolean>
|
|
|
|
|
// credential vault
|
|
|
|
|
listCredentialKeys(userId: string): Promise<CredentialKey[]>
|
2026-09-14 00:00:15 +08:00
|
|
|
/** 档案 86:该用户**全部已启用**的条目(spawn 时按它们写实例配置)。 */
|
|
|
|
|
listEnabledCredentialKeys(userId: string): Promise<CredentialKey[]>
|
|
|
|
|
/** 档案 87:同上 + **encrypted ref** —— **仅供 `server.ts` 的落地层**,绝不经 API 返回。 */
|
|
|
|
|
listCredentialLandingRows(userId: string): Promise<CredentialLandingRow[]>
|
2026-09-13 16:18:10 +08:00
|
|
|
getEnabledCredentialKeyRef(userId: string): Promise<string | null>
|
2026-09-14 00:00:15 +08:00
|
|
|
setCredentialKey(
|
|
|
|
|
userId: string,
|
|
|
|
|
name: string,
|
|
|
|
|
encryptedRef: string,
|
|
|
|
|
meta?: CredentialKeyMeta,
|
|
|
|
|
): Promise<CredentialKey>
|
2026-09-13 16:18:10 +08:00
|
|
|
selectCredentialKey(userId: string, id: string): Promise<boolean>
|
2026-09-14 00:00:15 +08:00
|
|
|
/** 档案 86:开/关**单个**条目(不动其它条目 —— 用户口径:可同时启用多个)。 */
|
|
|
|
|
toggleCredentialKey(userId: string, id: string, enabled: boolean): Promise<boolean>
|
|
|
|
|
/** 档案 86:用户是否使用「平台共享模型」(admin 配的那把)—— 用户侧偏好。 */
|
|
|
|
|
getSharedModelEnabled(userId: string): Promise<boolean>
|
|
|
|
|
setSharedModelEnabled(userId: string, enabled: boolean): Promise<boolean>
|
2026-09-13 16:18:10 +08:00
|
|
|
deleteCredentialKey(userId: string, id: string): Promise<boolean>
|
|
|
|
|
// instances (desired state the k8s controller reconciles against — docs/k8s.md §5.7)
|
|
|
|
|
upsertInstance(input: UpsertDshInstanceInput): Promise<void>
|
|
|
|
|
findInstance(id: string): Promise<DshInstance | undefined>
|
|
|
|
|
findUserInstance(userId: string, role: DshInstanceRole): Promise<DshInstance | undefined>
|
|
|
|
|
listInstancesByRole(role: DshInstanceRole): Promise<DshInstance[]>
|
|
|
|
|
/** Record a state transition; `exitCode`/`lastError` also stamp `last_exit`. */
|
|
|
|
|
setInstanceStatus(
|
|
|
|
|
id: string,
|
|
|
|
|
status: DshInstanceStatus,
|
|
|
|
|
outcome?: { exitCode?: number; lastError?: string },
|
|
|
|
|
): Promise<boolean>
|
|
|
|
|
deleteInstance(id: string): Promise<boolean>
|
|
|
|
|
deleteUserInstances(userId: string): Promise<void>
|
|
|
|
|
// lifecycle
|
|
|
|
|
close(): Promise<void>
|
|
|
|
|
}
|