refactor: top level dir + moved route

create a root directory entry in table for each user and move file browser under /directories/$id
This commit is contained in:
2025-09-19 23:01:44 +00:00
parent 9fbd5e678a
commit 4812fcc0a2
15 changed files with 1047 additions and 25 deletions

View File

@@ -19,6 +19,22 @@ type File = {
export type DirectoryItem = Directory | File
export type DirectoryItemKind = DirectoryItem["kind"]
export async function fetchRoot(ctx: AuthenticatedQueryCtx) {
return await ctx.db
.query("directories")
.withIndex("byParentId", (q) =>
q.eq("userId", ctx.user._id).eq("parentId", undefined),
)
.first()
}
export async function fetch(
ctx: AuthenticatedQueryCtx,
{ directoryId }: { directoryId: Id<"directories"> },
) {
return await ctx.db.get(directoryId)
}
export async function fetchContent(
ctx: AuthenticatedQueryCtx,
{
@@ -76,17 +92,14 @@ export async function fetchContent(
export async function create(
ctx: AuthenticatedMutationCtx,
{ name, parentId }: { name: string; parentId?: Id<"directories"> },
{ name, parentId }: { name: string; parentId: Id<"directories"> },
): Promise<Id<"directories">> {
let parentDir: Doc<"directories"> | null = null
if (parentId) {
parentDir = await ctx.db.get(parentId)
if (!parentDir) {
throw Err.create(
Err.Code.DirectoryNotFound,
`Parent directory ${parentId} not found`,
)
}
const parentDir = await ctx.db.get(parentId)
if (!parentDir) {
throw Err.create(
Err.Code.DirectoryNotFound,
`Parent directory ${parentId} not found`,
)
}
const existing = await ctx.db

View File

@@ -1,4 +1,3 @@
import type { Id } from "../_generated/dataModel"
import type { MutationCtx, QueryCtx } from "../_generated/server"
import type { AuthenticatedMutationCtx } from "../functions"
import * as Err from "./error"
@@ -40,7 +39,17 @@ export async function userOrThrow(ctx: QueryCtx | MutationCtx) {
}
export async function register(ctx: AuthenticatedMutationCtx) {
await ctx.db.insert("users", {
jwtSubject: ctx.identity.subject,
})
const now = new Date().toISOString()
await Promise.all([
ctx.db.insert("users", {
jwtSubject: ctx.identity.subject,
}),
ctx.db.insert("directories", {
name: "",
path: "",
userId: ctx.user._id,
createdAt: now,
updatedAt: now,
}),
])
}