Next.js Discord

Discord Forum

Vercel Postgres search query with spaces?

Unanswered
Netherland Dwarf posted this in #help-forum
Open in Discord
Netherland DwarfOP
I am very new to this and trying to figure out how best searches querys can be handled. Right now any single word search query works but when I have two words it will not work.

import { sql } from '@vercel/postgres';

import { ProductFormat } from "../server/ProductFormat"

export async function pullProducts(page = 1, search = 'Two Words') {
  const pageSize = 24
  const pageOffset = (page - 1) * 24
  
  const result = await sql`
    SELECT * FROM table
    WHERE title LIKE ${`%${search}%`}
    ORDER BY title ASC
    LIMIT ${pageSize} OFFSET ${pageOffset}
  `;
  
  const totalProducts = await sql`
    SELECT COUNT(*) FROM table
    WHERE title LIKE ${`%${search}%`}
  `;

  const productsTyped = result.rows as ProductFormat[];

  return {
    products: productsTyped,
    totalProducts: parseInt(totalProducts.rows[0].count, 10)
  };
}

2 Replies