refactor: replace drizzle count with $count (#2471)

This commit is contained in:
Meier Lukas
2025-03-02 10:54:44 +01:00
committed by GitHub
parent 9eb76634d0
commit d66610b324
5 changed files with 11 additions and 22 deletions

View File

@@ -36,7 +36,7 @@ const sendWidgetDataAsync = async (umamiInstance: Umami, analyticsSettings: type
if (!analyticsSettings.enableWidgetData) {
return;
}
const widgetCount = (await db.select({ count: count(items.id) }).from(items))[0]?.count ?? 0;
const widgetCount = await db.$count(items);
const response = await umamiInstance.track("server-widget-data", {
countWidgets: widgetCount,
@@ -52,7 +52,7 @@ const sendUserDataAsync = async (umamiInstance: Umami, analyticsSettings: typeof
if (!analyticsSettings.enableUserData) {
return;
}
const userCount = (await db.select({ count: count(users.id) }).from(users))[0]?.count ?? 0;
const userCount = await db.$count(users);
const response = await umamiInstance.track("server-user-data", {
countUsers: userCount,

View File

@@ -2,7 +2,7 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import type { Database } from "@homarr/db";
import { and, createId, eq, handleTransactionsAsync, like, not, sql } from "@homarr/db";
import { and, createId, eq, handleTransactionsAsync, like, not } from "@homarr/db";
import { getMaxGroupPositionAsync } from "@homarr/db/queries";
import { groupMembers, groupPermissions, groups } from "@homarr/db/schema";
import { everyoneGroup } from "@homarr/definitions";
@@ -42,12 +42,7 @@ export const groupRouter = createTRPCRouter({
.input(validation.common.paginated)
.query(async ({ input, ctx }) => {
const whereQuery = input.search ? like(groups.name, `%${input.search.trim()}%`) : undefined;
const groupCount = await ctx.db
.select({
count: sql<number>`count(*)`,
})
.from(groups)
.where(whereQuery);
const groupCount = await ctx.db.$count(groups, whereQuery);
const dbGroups = await ctx.db.query.groups.findMany({
with: {
@@ -74,7 +69,7 @@ export const groupRouter = createTRPCRouter({
...group,
members: group.members.map((member) => member.user),
})),
totalCount: groupCount[0]?.count ?? 0,
totalCount: groupCount,
};
}),
getById: permissionRequiredProcedure

View File

@@ -2,7 +2,6 @@ import type { AnySQLiteTable } from "drizzle-orm/sqlite-core";
import { isProviderEnabled } from "@homarr/auth/server";
import type { Database } from "@homarr/db";
import { count } from "@homarr/db";
import { apps, boards, groups, integrations, invites, users } from "@homarr/db/schema";
import { createTRPCRouter, publicProcedure } from "../trpc";
@@ -28,5 +27,5 @@ const getCountForTableAsync = async (db: Database, table: AnySQLiteTable, canVie
return 0;
}
return (await db.select({ count: count() }).from(table))[0]?.count ?? 0;
return await db.$count(table);
};

View File

@@ -1,4 +1,4 @@
import { and, count, like } from "@homarr/db";
import { and, like } from "@homarr/db";
import { icons } from "@homarr/db/schema";
import { validation } from "@homarr/validation";
@@ -24,7 +24,7 @@ export const iconsRouter = createTRPCRouter({
},
},
}),
countIcons: (await ctx.db.select({ count: count() }).from(icons))[0]?.count ?? 0,
countIcons: await ctx.db.$count(icons),
};
}),
});

View File

@@ -1,7 +1,7 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { asc, createId, eq, like, sql } from "@homarr/db";
import { asc, createId, eq, like } from "@homarr/db";
import { getServerSettingByKeyAsync } from "@homarr/db/queries";
import { searchEngines, users } from "@homarr/db/schema";
import { integrationCreator } from "@homarr/integrations";
@@ -13,12 +13,7 @@ import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publ
export const searchEngineRouter = createTRPCRouter({
getPaginated: protectedProcedure.input(validation.common.paginated).query(async ({ input, ctx }) => {
const whereQuery = input.search ? like(searchEngines.name, `%${input.search.trim()}%`) : undefined;
const searchEngineCount = await ctx.db
.select({
count: sql<number>`count(*)`,
})
.from(searchEngines)
.where(whereQuery);
const searchEngineCount = await ctx.db.$count(searchEngines, whereQuery);
const dbSearachEngines = await ctx.db.query.searchEngines.findMany({
limit: input.pageSize,
@@ -28,7 +23,7 @@ export const searchEngineRouter = createTRPCRouter({
return {
items: dbSearachEngines,
totalCount: searchEngineCount[0]?.count ?? 0,
totalCount: searchEngineCount,
};
}),
getSelectable: protectedProcedure