1- import { eq , asc , and , count } from 'drizzle-orm' ;
1+ import { eq , asc , and , count , isNull } from 'drizzle-orm' ;
22import { getSchema } from '../db/index' ;
33import type { AnyDatabase } from '../db' ;
44import type { FastifyBaseLogger } from 'fastify' ;
@@ -23,6 +23,16 @@ export interface FeaturedCategory {
2323 featured_server_count : number ;
2424}
2525
26+ export interface CategoryWithServerCount {
27+ id : string ;
28+ name : string ;
29+ description : string | null ;
30+ icon : string | null ;
31+ sort_order : number ;
32+ server_count : number ;
33+ created_at : Date ;
34+ }
35+
2636export interface CreateMcpCategoryRequest {
2737 name : string ;
2838 description ?: string ;
@@ -219,4 +229,53 @@ export class McpCategoriesService {
219229 featured_server_count : Number ( row . featured_server_count )
220230 } ) ) ;
221231 }
232+
233+ async getAllCategoriesWithServerCount ( ) : Promise < CategoryWithServerCount [ ] > {
234+ this . logger . debug ( {
235+ operation : 'get_all_categories_with_server_count'
236+ } , 'Getting all categories with global server count' ) ;
237+
238+ const results = await this . db
239+ . select ( {
240+ id : this . mcpCategories . id ,
241+ name : this . mcpCategories . name ,
242+ description : this . mcpCategories . description ,
243+ icon : this . mcpCategories . icon ,
244+ sort_order : this . mcpCategories . sort_order ,
245+ created_at : this . mcpCategories . created_at ,
246+ server_count : count ( this . mcpServers . id )
247+ } )
248+ . from ( this . mcpCategories )
249+ . leftJoin (
250+ this . mcpServers ,
251+ and (
252+ eq ( this . mcpServers . category_id , this . mcpCategories . id ) ,
253+ isNull ( this . mcpServers . owner_team_id ) // Only global servers
254+ )
255+ )
256+ . groupBy (
257+ this . mcpCategories . id ,
258+ this . mcpCategories . name ,
259+ this . mcpCategories . description ,
260+ this . mcpCategories . icon ,
261+ this . mcpCategories . sort_order ,
262+ this . mcpCategories . created_at
263+ )
264+ . orderBy ( asc ( this . mcpCategories . sort_order ) , asc ( this . mcpCategories . name ) ) ;
265+
266+ this . logger . info ( {
267+ operation : 'get_all_categories_with_server_count' ,
268+ categoriesFound : results . length
269+ } , 'Retrieved all categories with global server count' ) ;
270+
271+ return results . map ( row => ( {
272+ id : row . id ,
273+ name : row . name ,
274+ description : row . description ,
275+ icon : row . icon ,
276+ sort_order : row . sort_order ,
277+ server_count : Number ( row . server_count ) ,
278+ created_at : row . created_at
279+ } ) ) ;
280+ }
222281}
0 commit comments