import "server-only"; import { asc, eq, notInArray } from "drizzle-orm"; import { db } from "@/lib/db/client"; import { localFolders, models, workerState } from "@/lib/db/schema"; export type LocalFolder = typeof localFolders.$inferSelect; export type LocalFolderSummary = Pick; /** * Folders in MODELS_DIR that no model uses (from the worker's last scan), without their file lists: * those can hold thousands of entries and are only needed once you click Import (getLocalFolder). */ export async function listLocalFolders(): Promise { const d = db(); return d .select({ path: localFolders.path, commit: localFolders.commit, fileCount: localFolders.fileCount, totalBytes: localFolders.totalBytes }) .from(localFolders) .where(notInArray(localFolders.path, d.select({ p: models.localPath }).from(models))) .orderBy(asc(localFolders.path)); } /** One folder with its file list. */ export async function getLocalFolder(path: string): Promise { return db().query.localFolders.findFirst({ where: eq(localFolders.path, path) }); } export async function localFolderExists(path: string): Promise { const row = await db().query.localFolders.findFirst({ columns: { path: true }, where: eq(localFolders.path, path) }); return !!row; } /** Ask the worker to scan MODELS_DIR now (it picks this up with its next heartbeat). */ export async function requestFolderScan(): Promise { await db().update(workerState).set({ scanRequestedAt: new Date() }).where(eq(workerState.id, "worker")); }