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; /** Folders in MODELS_DIR that no model uses (from the worker's last scan). */ export async function listLocalFolders(): Promise { const d = db(); return d .select() .from(localFolders) .where(notInArray(localFolders.path, d.select({ p: models.localPath }).from(models))) .orderBy(asc(localFolders.path)); } export async function getLocalFolder(path: string): Promise { return db().query.localFolders.findFirst({ where: eq(localFolders.path, path) }); } /** 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")); }