-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.ts
More file actions
46 lines (37 loc) · 1.15 KB
/
queries.ts
File metadata and controls
46 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import "server-only"; // Ensure this file is only run on the server
import { eq } from "drizzle-orm";
import { db } from "~/server/db";
import {
files_table as fileSchema,
folders_table as folderSchema,
} from "~/server/db/schema";
export async function getAllParentsForFolder(folderId: number) {
const parents = [];
let currentId: number | null = folderId;
while (currentId != null) {
const folders = await db
.selectDistinct()
.from(folderSchema)
.where(eq(folderSchema.id, currentId));
if (!folders[0]) throw new Error("parent folder not found");
parents.unshift(folders[0]);
currentId = folders[0]?.parent; // parent can be null
}
return parents;
}
export function getAllFolders(folderId: number) {
return db
.select()
.from(folderSchema)
.where(eq(folderSchema.parent, folderId));
}
export async function getFolderById(folderId: number) {
const folders = await db
.select()
.from(folderSchema)
.where(eq(folderSchema.id, folderId));
return folders[0];
}
export function getAllFiles(folderId: number) {
return db.select().from(fileSchema).where(eq(fileSchema.parent, folderId));
}