-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.ts
More file actions
60 lines (49 loc) · 1.46 KB
/
queries.ts
File metadata and controls
60 lines (49 loc) · 1.46 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import "server-only"; // Ensure this file is only run on the server
import { and, eq, isNull } 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 async function getRootFolderForUser(ownerId: string) {
const folders = await db
.select()
.from(folderSchema)
.where(and(eq(folderSchema.ownerId, ownerId), isNull(folderSchema.parent)));
return folders[0];
}
export function getAllFolders(folderId: number) {
return db
.select()
.from(folderSchema)
.where(eq(folderSchema.parent, folderId))
.orderBy(folderSchema.name);
}
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))
.orderBy(fileSchema.name);
}