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) { if (!analyticsSettings.enableWidgetData) {
return; 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", { const response = await umamiInstance.track("server-widget-data", {
countWidgets: widgetCount, countWidgets: widgetCount,
@@ -52,7 +52,7 @@ const sendUserDataAsync = async (umamiInstance: Umami, analyticsSettings: typeof
if (!analyticsSettings.enableUserData) { if (!analyticsSettings.enableUserData) {
return; 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", { const response = await umamiInstance.track("server-user-data", {
countUsers: userCount, countUsers: userCount,

View File

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

View File

@@ -2,7 +2,6 @@ import type { AnySQLiteTable } from "drizzle-orm/sqlite-core";
import { isProviderEnabled } from "@homarr/auth/server"; import { isProviderEnabled } from "@homarr/auth/server";
import type { Database } from "@homarr/db"; import type { Database } from "@homarr/db";
import { count } from "@homarr/db";
import { apps, boards, groups, integrations, invites, users } from "@homarr/db/schema"; import { apps, boards, groups, integrations, invites, users } from "@homarr/db/schema";
import { createTRPCRouter, publicProcedure } from "../trpc"; import { createTRPCRouter, publicProcedure } from "../trpc";
@@ -28,5 +27,5 @@ const getCountForTableAsync = async (db: Database, table: AnySQLiteTable, canVie
return 0; 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 { icons } from "@homarr/db/schema";
import { validation } from "@homarr/validation"; 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 { TRPCError } from "@trpc/server";
import { z } from "zod"; 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 { getServerSettingByKeyAsync } from "@homarr/db/queries";
import { searchEngines, users } from "@homarr/db/schema"; import { searchEngines, users } from "@homarr/db/schema";
import { integrationCreator } from "@homarr/integrations"; import { integrationCreator } from "@homarr/integrations";
@@ -13,12 +13,7 @@ import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publ
export const searchEngineRouter = createTRPCRouter({ export const searchEngineRouter = createTRPCRouter({
getPaginated: protectedProcedure.input(validation.common.paginated).query(async ({ input, ctx }) => { getPaginated: protectedProcedure.input(validation.common.paginated).query(async ({ input, ctx }) => {
const whereQuery = input.search ? like(searchEngines.name, `%${input.search.trim()}%`) : undefined; const whereQuery = input.search ? like(searchEngines.name, `%${input.search.trim()}%`) : undefined;
const searchEngineCount = await ctx.db const searchEngineCount = await ctx.db.$count(searchEngines, whereQuery);
.select({
count: sql<number>`count(*)`,
})
.from(searchEngines)
.where(whereQuery);
const dbSearachEngines = await ctx.db.query.searchEngines.findMany({ const dbSearachEngines = await ctx.db.query.searchEngines.findMany({
limit: input.pageSize, limit: input.pageSize,
@@ -28,7 +23,7 @@ export const searchEngineRouter = createTRPCRouter({
return { return {
items: dbSearachEngines, items: dbSearachEngines,
totalCount: searchEngineCount[0]?.count ?? 0, totalCount: searchEngineCount,
}; };
}), }),
getSelectable: protectedProcedure getSelectable: protectedProcedure