|
1 | | -import { count, desc, eq, sql, sum } from "drizzle-orm"; |
| 1 | +import { count, desc, eq } from "drizzle-orm"; |
2 | 2 |
|
3 | 3 | import { db } from "../db/db"; |
4 | | -import { InsertLink, SelectLink, linksTable } from "../db/schema"; |
| 4 | +import { InsertLink, SelectLink, linksTable, redirectsTable } from "../db/schema"; |
5 | 5 | import { generateCode } from "./utils"; |
6 | 6 |
|
7 | | -export async function codeAlreadyUsed(code: SelectLink["code"]): Promise<boolean> { |
8 | | - const link = await db.select().from(linksTable).where(eq(linksTable.code, code)); |
9 | | - return link.length > 0; |
| 7 | +async function codeAlreadyUsed(code: SelectLink["code"]): Promise<boolean> { |
| 8 | + const result = await db |
| 9 | + .select({ id: linksTable.id }) |
| 10 | + .from(linksTable) |
| 11 | + .where(eq(linksTable.code, code)) |
| 12 | + .get(); |
| 13 | + return result !== undefined; |
10 | 14 | } |
11 | 15 |
|
12 | 16 | export async function validateCode(code: string | undefined): Promise<string> { |
@@ -34,30 +38,34 @@ export async function editLink(code: SelectLink["code"], data: InsertLink): Prom |
34 | 38 | return await db.update(linksTable).set(data).where(eq(linksTable.code, code)).returning(); |
35 | 39 | } |
36 | 40 |
|
37 | | -export async function incrementRedirects(code: SelectLink["code"]): Promise<void> { |
38 | | - await db |
39 | | - .update(linksTable) |
40 | | - .set({ redirects: sql`${linksTable.redirects} + 1` }) |
41 | | - .where(eq(linksTable.code, code)); |
| 41 | +export async function deleteLink(code: SelectLink["code"]): Promise<boolean> { |
| 42 | + const result = await db |
| 43 | + .delete(linksTable) |
| 44 | + .where(eq(linksTable.code, code)) |
| 45 | + .returning({ id: linksTable.id }); |
| 46 | + return result.length > 0; |
42 | 47 | } |
43 | 48 |
|
44 | | -export async function deleteLink(code: SelectLink["code"]): Promise<void> { |
45 | | - await db.delete(linksTable).where(eq(linksTable.code, code)); |
46 | | -} |
47 | | - |
48 | | -export async function deleteAllLinks(): Promise<void> { |
49 | | - await db.delete(linksTable); |
50 | | -} |
51 | 49 |
|
52 | | -export async function getAllLinks(): Promise<SelectLink[]> { |
53 | | - return db.select().from(linksTable).orderBy(desc(linksTable.id)); |
| 50 | +export async function getAllLinks() { |
| 51 | + return db |
| 52 | + .select({ |
| 53 | + id: linksTable.id, |
| 54 | + code: linksTable.code, |
| 55 | + url: linksTable.url, |
| 56 | + redirects: count(redirectsTable.id), |
| 57 | + createdAt: linksTable.createdAt, |
| 58 | + }) |
| 59 | + .from(linksTable) |
| 60 | + .leftJoin(redirectsTable, eq(redirectsTable.linkId, linksTable.id)) |
| 61 | + .groupBy(linksTable.id) |
| 62 | + .orderBy(desc(linksTable.id)); |
54 | 63 | } |
55 | 64 |
|
56 | 65 | export async function getLinkStats(): Promise<{ totalLinks: number; totalRedirects: number }> { |
57 | | - const totalLinks = await db.select({ count: count() }).from(linksTable); |
58 | | - if (totalLinks[0].count === 0) return { totalLinks: 0, totalRedirects: 0 }; |
59 | | - const totalRedirects = await db |
60 | | - .select({ sum: sum(linksTable.redirects).mapWith(Number) }) |
61 | | - .from(linksTable); |
62 | | - return { totalLinks: totalLinks[0].count, totalRedirects: totalRedirects[0].sum }; |
| 66 | + const [[{ totalLinks }], [{ totalRedirects }]] = await Promise.all([ |
| 67 | + db.select({ totalLinks: count() }).from(linksTable), |
| 68 | + db.select({ totalRedirects: count() }).from(redirectsTable), |
| 69 | + ]); |
| 70 | + return { totalLinks, totalRedirects }; |
63 | 71 | } |
0 commit comments