feat: return per-package file match counts from searchPackages

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 16:17:42 +01:00
parent d53e581623
commit 0faacc214b
2 changed files with 15 additions and 6 deletions

View File

@@ -57,6 +57,8 @@ export async function listPackages(options: {
tags: pkg.tags, tags: pkg.tags,
indexedAt: pkg.indexedAt.toISOString(), indexedAt: pkg.indexedAt.toISOString(),
sourceChannel: pkg.sourceChannel, sourceChannel: pkg.sourceChannel,
matchedFileCount: 0,
matchedByContent: false,
})); }));
return { return {
@@ -171,19 +173,22 @@ export async function searchPackages(options: {
const q = options.query; const q = options.query;
if (options.searchIn === "files" || options.searchIn === "both") { if (options.searchIn === "files" || options.searchIn === "both") {
// Search in package files, return parent packages // Get per-package file match counts
const fileMatches = await prisma.packageFile.findMany({ const fileMatches = await prisma.packageFile.groupBy({
by: ["packageId"],
where: { where: {
OR: [ OR: [
{ fileName: { contains: q, mode: "insensitive" } }, { fileName: { contains: q, mode: "insensitive" } },
{ path: { contains: q, mode: "insensitive" } }, { path: { contains: q, mode: "insensitive" } },
], ],
}, },
select: { packageId: true }, _count: { _all: true },
distinct: ["packageId"],
}); });
const packageIds = fileMatches.map((f) => f.packageId); const fileMatchMap = new Map(
fileMatches.map((m) => [m.packageId, m._count._all])
);
const fileMatchedIds = fileMatches.map((f) => f.packageId);
const packageNameIds = const packageNameIds =
options.searchIn === "both" options.searchIn === "both"
@@ -195,7 +200,7 @@ export async function searchPackages(options: {
).map((p) => p.id) ).map((p) => p.id)
: []; : [];
const allIds = [...new Set([...packageIds, ...packageNameIds])]; const allIds = [...new Set([...fileMatchedIds, ...packageNameIds])];
const [items, total] = await Promise.all([ const [items, total] = await Promise.all([
prisma.package.findMany({ prisma.package.findMany({
@@ -234,6 +239,8 @@ export async function searchPackages(options: {
tags: pkg.tags, tags: pkg.tags,
indexedAt: pkg.indexedAt.toISOString(), indexedAt: pkg.indexedAt.toISOString(),
sourceChannel: pkg.sourceChannel, sourceChannel: pkg.sourceChannel,
matchedFileCount: fileMatchMap.get(pkg.id) ?? 0,
matchedByContent: fileMatchMap.has(pkg.id),
})); }));
return { return {

View File

@@ -14,6 +14,8 @@ export interface PackageListItem {
id: string; id: string;
title: string; title: string;
}; };
matchedFileCount: number;
matchedByContent: boolean;
} }
export interface PackageDetail extends PackageListItem { export interface PackageDetail extends PackageListItem {