refactor: directory path handling

intsead of storing path as field in directories table, it is derived on demand, because it makes moving directories a heck lot eaiser

Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
2025-09-20 23:23:28 +00:00
parent cd2c10fbed
commit 0f5b1f79ff
8 changed files with 94 additions and 56 deletions

View File

@@ -18,6 +18,7 @@ import type * as functions from "../functions.js";
import type * as model_directories from "../model/directories.js";
import type * as model_error from "../model/error.js";
import type * as model_files from "../model/files.js";
import type * as model_filesystem from "../model/filesystem.js";
import type * as model_user from "../model/user.js";
import type * as users from "../users.js";
@@ -35,6 +36,7 @@ declare const fullApi: ApiFromModules<{
"model/directories": typeof model_directories;
"model/error": typeof model_error;
"model/files": typeof model_files;
"model/filesystem": typeof model_filesystem;
"model/user": typeof model_user;
users: typeof users;
}>;

View File

@@ -54,10 +54,9 @@ export const fetchDirectory = authenticatedQuery({
export const fetchDirectoryContent = authenticatedQuery({
args: {
directoryId: v.optional(v.id("directories")),
path: v.optional(v.string()),
},
handler: async (ctx, { directoryId, path }): Promise<DirectoryItem[]> => {
return await Directories.fetchContent(ctx, { directoryId, path })
handler: async (ctx, { directoryId }): Promise<DirectoryItem[]> => {
return await Directories.fetchContent(ctx, { directoryId })
},
})

View File

@@ -1,10 +1,10 @@
import type { Doc, Id } from "@fileone/convex/_generated/dataModel"
import { joinPath, PATH_SEPARATOR } from "@fileone/path"
import type {
AuthenticatedMutationCtx,
AuthenticatedQueryCtx,
} from "../functions"
import * as Err from "./error"
import type { FilePath, ReverseFilePath } from "./filesystem"
type Directory = {
kind: "directory"
@@ -19,6 +19,8 @@ type File = {
export type DirectoryItem = Directory | File
export type DirectoryItemKind = DirectoryItem["kind"]
export type DirectoryInfo = Doc<"directories"> & { path: FilePath }
export async function fetchRoot(ctx: AuthenticatedQueryCtx) {
return await ctx.db
.query("directories")
@@ -31,30 +33,36 @@ export async function fetchRoot(ctx: AuthenticatedQueryCtx) {
export async function fetch(
ctx: AuthenticatedQueryCtx,
{ directoryId }: { directoryId: Id<"directories"> },
) {
return await ctx.db.get(directoryId)
): Promise<DirectoryInfo> {
const directory = await ctx.db.get(directoryId)
if (!directory) {
throw Err.create(
Err.Code.DirectoryNotFound,
`Directory ${directoryId} not found`,
)
}
const path: ReverseFilePath = [{ id: directoryId, name: directory.name }]
let parentDirId = directory.parentId
while (parentDirId) {
const parentDir = await ctx.db.get(parentDirId)
if (parentDir) {
path.push({ id: parentDir._id, name: parentDir.name })
parentDirId = parentDir.parentId
} else {
throw Err.create(Err.Code.Internal)
}
}
return { ...directory, path: path.reverse() as FilePath }
}
export async function fetchContent(
ctx: AuthenticatedQueryCtx,
{
path,
directoryId,
}: { path?: string; directoryId?: Id<"directories"> } = {},
{ directoryId }: { directoryId?: Id<"directories"> } = {},
): Promise<DirectoryItem[]> {
let dirId: Id<"directories"> | undefined
if (path) {
dirId = await ctx.db
.query("directories")
.withIndex("byPath", (q) =>
q
.eq("userId", ctx.user._id)
.eq("path", path)
.eq("deletedAt", undefined),
)
.first()
.then((dir) => dir?._id)
} else if (directoryId) {
if (directoryId) {
dirId = directoryId
}
@@ -127,7 +135,6 @@ export async function create(
userId: ctx.user._id,
createdAt: now,
updatedAt: now,
path: parentDir ? joinPath(parentDir.path, name) : joinPath("", name),
})
}

View File

@@ -0,0 +1,17 @@
import type { Id } from "../_generated/dataModel"
export type DirectoryPathComponent = {
id: Id<"directories">
name: string
}
export type FilePathComponent = {
id: Id<"files">
name: string
}
export type PathComponent = FilePathComponent | DirectoryPathComponent
export type FilePath = [...DirectoryPathComponent[], PathComponent]
export type ReverseFilePath = [PathComponent, ...DirectoryPathComponent[]]

View File

@@ -46,7 +46,6 @@ export async function register(ctx: AuthenticatedMutationCtx) {
}),
ctx.db.insert("directories", {
name: "",
path: "",
userId: ctx.user._id,
createdAt: now,
updatedAt: now,

View File

@@ -27,7 +27,6 @@ const schema = defineSchema({
]),
directories: defineTable({
name: v.string(),
path: v.string(),
userId: v.id("users"),
parentId: v.optional(v.id("directories")),
createdAt: v.string(),
@@ -41,8 +40,7 @@ const schema = defineSchema({
"parentId",
"name",
"deletedAt",
])
.index("byPath", ["userId", "path", "deletedAt"]),
]),
})
export default schema