From ce781f3336f29511021a4db0f93d55245f1451e0 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 11 Oct 2024 15:46:05 +0200 Subject: [PATCH] refactor(database): :recycle: Use new Drizzle count API --- api/api/v1/markers/index.ts | 76 +++++++++---------------- api/users/:uuid/outbox/index.ts | 22 +++---- packages/database-interface/instance.ts | 11 +--- packages/database-interface/note.ts | 15 ++--- packages/database-interface/user.ts | 12 +--- 5 files changed, 43 insertions(+), 93 deletions(-) diff --git a/api/api/v1/markers/index.ts b/api/api/v1/markers/index.ts index de6b71aa..d5430b9a 100644 --- a/api/api/v1/markers/index.ts +++ b/api/api/v1/markers/index.ts @@ -1,7 +1,7 @@ import { apiRoute, applyConfig, auth, idValidator } from "@/api"; import { createRoute } from "@hono/zod-openapi"; import type { Marker as ApiMarker } from "@versia/client/types"; -import { and, count, eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { z } from "zod"; import { db } from "~/drizzle/db"; import { Markers, RolePermissions } from "~/drizzle/schema"; @@ -140,22 +140,15 @@ export default apiRoute((app) => { ), }); - const totalCount = await db - .select({ - count: count(), - }) - .from(Markers) - .where( - and( - eq(Markers.userId, user.id), - eq(Markers.timeline, "home"), - ), - ); + const totalCount = await db.$count( + Markers, + and(eq(Markers.userId, user.id), eq(Markers.timeline, "home")), + ); if (found?.noteId) { markers.home = { last_read_id: found.noteId, - version: totalCount[0].count, + version: totalCount, updated_at: new Date(found.createdAt).toISOString(), }; } @@ -170,22 +163,18 @@ export default apiRoute((app) => { ), }); - const totalCount = await db - .select({ - count: count(), - }) - .from(Markers) - .where( - and( - eq(Markers.userId, user.id), - eq(Markers.timeline, "notifications"), - ), - ); + const totalCount = await db.$count( + Markers, + and( + eq(Markers.userId, user.id), + eq(Markers.timeline, "notifications"), + ), + ); if (found?.notificationId) { markers.notifications = { last_read_id: found.notificationId, - version: totalCount[0].count, + version: totalCount, updated_at: new Date(found.createdAt).toISOString(), }; } @@ -222,21 +211,14 @@ export default apiRoute((app) => { .returning() )[0]; - const totalCount = await db - .select({ - count: count(), - }) - .from(Markers) - .where( - and( - eq(Markers.userId, user.id), - eq(Markers.timeline, "home"), - ), - ); + const totalCount = await db.$count( + Markers, + and(eq(Markers.userId, user.id), eq(Markers.timeline, "home")), + ); markers.home = { last_read_id: homeId, - version: totalCount[0].count, + version: totalCount, updated_at: new Date(insertedMarker.createdAt).toISOString(), }; } @@ -253,21 +235,17 @@ export default apiRoute((app) => { .returning() )[0]; - const totalCount = await db - .select({ - count: count(), - }) - .from(Markers) - .where( - and( - eq(Markers.userId, user.id), - eq(Markers.timeline, "notifications"), - ), - ); + const totalCount = await db.$count( + Markers, + and( + eq(Markers.userId, user.id), + eq(Markers.timeline, "notifications"), + ), + ); markers.notifications = { last_read_id: notificationsId, - version: totalCount[0].count, + version: totalCount, updated_at: new Date(insertedMarker.createdAt).toISOString(), }; } diff --git a/api/users/:uuid/outbox/index.ts b/api/users/:uuid/outbox/index.ts index 2ac13b0c..58bf16a5 100644 --- a/api/users/:uuid/outbox/index.ts +++ b/api/users/:uuid/outbox/index.ts @@ -4,7 +4,7 @@ import { Collection as CollectionSchema, Note as NoteSchema, } from "@versia/federation/schemas"; -import { and, count, eq, inArray } from "drizzle-orm"; +import { and, eq, inArray } from "drizzle-orm"; import { z } from "zod"; import { db } from "~/drizzle/db"; import { Notes } from "~/drizzle/schema"; @@ -102,19 +102,13 @@ export default apiRoute((app) => NOTES_PER_PAGE * (pageNumber - 1), ); - const totalNotes = ( - await db - .select({ - count: count(), - }) - .from(Notes) - .where( - and( - eq(Notes.authorId, uuid), - inArray(Notes.visibility, ["public", "unlisted"]), - ), - ) - )[0].count; + const totalNotes = await db.$count( + Notes, + and( + eq(Notes.authorId, uuid), + inArray(Notes.visibility, ["public", "unlisted"]), + ), + ); const json = { first: new URL( diff --git a/packages/database-interface/instance.ts b/packages/database-interface/instance.ts index e959385c..da5985a7 100644 --- a/packages/database-interface/instance.ts +++ b/packages/database-interface/instance.ts @@ -10,7 +10,6 @@ import { type InferInsertModel, type InferSelectModel, type SQL, - count, desc, eq, inArray, @@ -340,13 +339,7 @@ export class Instance extends BaseInterface { }); } - static async getCount() { - return ( - await db - .select({ - count: count(), - }) - .from(Instances) - )[0].count; + static getCount(): Promise { + return db.$count(Instances); } } diff --git a/packages/database-interface/note.ts b/packages/database-interface/note.ts index 8f9beaa1..7f12d7c9 100644 --- a/packages/database-interface/note.ts +++ b/packages/database-interface/note.ts @@ -18,7 +18,6 @@ import { type InferInsertModel, type SQL, and, - count, desc, eq, inArray, @@ -349,16 +348,10 @@ export class Note extends BaseInterface { * @returns The number of notes in the database */ static async getCount(): Promise { - return ( - await db - .select({ - count: count(), - }) - .from(Notes) - .where( - sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`, - ) - )[0].count; + return await db.$count( + Notes, + sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`, + ); } /** diff --git a/packages/database-interface/user.ts b/packages/database-interface/user.ts index 6e5831a8..c39f1243 100644 --- a/packages/database-interface/user.ts +++ b/packages/database-interface/user.ts @@ -24,7 +24,6 @@ import { type InferInsertModel, type SQL, and, - count, countDistinct, desc, eq, @@ -327,15 +326,8 @@ export class User extends BaseInterface { ); } - static async getCount() { - return ( - await db - .select({ - count: count(), - }) - .from(Users) - .where(isNull(Users.instanceId)) - )[0].count; + static getCount(): Promise { + return db.$count(Users, isNull(Users.instanceId)); } static async getActiveInPeriod(milliseconds: number) {