arreglar busqueda para queries extrañas

This commit is contained in:
Cat /dev/Nulo 2024-01-04 18:47:24 -03:00
parent f0798e8620
commit e890d5f63b
3 changed files with 7 additions and 9 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -39,6 +39,6 @@
"chart.js": "^4.4.1", "chart.js": "^4.4.1",
"chartjs-adapter-dayjs-4": "^1.0.4", "chartjs-adapter-dayjs-4": "^1.0.4",
"dayjs": "^1.11.10", "dayjs": "^1.11.10",
"drizzle-orm": "=0.29.1" "drizzle-orm": "^0.29.1"
} }
} }

View file

@ -1,19 +1,17 @@
import { error } from "@sveltejs/kit"; import { error } from "@sveltejs/kit";
import { eq, max, sql } from "drizzle-orm"; import { sql } from "drizzle-orm";
import type { PageServerLoad } from "./$types"; import type { PageServerLoad } from "./$types";
import { db, schema } from "$lib/server/db"; import { db } from "$lib/server/db";
const { precios } = schema;
export const load: PageServerLoad = async ({ url }) => { export const load: PageServerLoad = async ({ url }) => {
const query = url.searchParams.get("q"); const query = url.searchParams.get("q");
let results: null | { ean: string; name: string; imageUrl: string }[] = null; let results: null | { ean: string; name: string; imageUrl: string }[] = null;
if (query) { if (query) {
results = db.all( const sqlQuery = sql`select p.ean, p.name, p.image_url as imageUrl from precios_fts f
sql`select p.ean, p.name, p.image_url as imageUrl from precios_fts f
join precios p on p.ean = f.ean join precios p on p.ean = f.ean
where f.name match ${query} where f.name match ${`"${query}"`}
group by p.ean;`, group by p.ean;`;
); results = db.all(sqlQuery);
} }
return { query, results }; return { query, results };