PrismaP
Prisma17mo ago
3 replies
bockster6669

Search text with Prisma

Hello, guys! I am trying to create a search functionality with prisma and want to ask which is the better way: making it with the search prop or making it with contains prop. Now I am using search and it looks like this

function formatSearchQuery(query: string | null): string | undefined {
  if (!query) return undefined;
  return query.split(' ').join(' &');
}

export async function GET(req: NextRequest) {
  const searchParams = req.nextUrl.searchParams;
  let searchInput: string | null = searchParams.get('search');
  const formattedQuery = formatSearchQuery(searchInput);

  try {
    const posts = await PostRepo.findMany({
      where: {
        title: {
          search: formattedQuery,
        },
      },
      include: {
        author: true,
        tags: true,
        comments: {
          include: {
            author: true,
          },
        },
      },
    });
Was this page helpful?