From 0289313cd6b9611511e16643a3232e40fc8f4226 Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 28 Dec 2023 00:44:53 -0300 Subject: [PATCH] WIP: barra de busqueda MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lamentablemente drizzle-kit no está aplicando los triggers en la migracion, sino se puede subir --- db-datos/drizzle/0004_left_wolfsbane.sql | 17 ++++ db-datos/drizzle/meta/0004_snapshot.json | 101 +++++++++++++++++++++++ db-datos/drizzle/meta/_journal.json | 7 ++ sitio/src/routes/+layout.server.ts | 14 ++++ sitio/src/routes/+layout.svelte | 40 ++++++++- sitio/src/routes/search/+page.server.ts | 18 ++++ sitio/src/routes/search/+page.svelte | 19 +++++ 7 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 db-datos/drizzle/0004_left_wolfsbane.sql create mode 100644 db-datos/drizzle/meta/0004_snapshot.json create mode 100644 sitio/src/routes/+layout.server.ts create mode 100644 sitio/src/routes/search/+page.server.ts create mode 100644 sitio/src/routes/search/+page.svelte diff --git a/db-datos/drizzle/0004_left_wolfsbane.sql b/db-datos/drizzle/0004_left_wolfsbane.sql new file mode 100644 index 0000000..9fb754b --- /dev/null +++ b/db-datos/drizzle/0004_left_wolfsbane.sql @@ -0,0 +1,17 @@ +-- Custom SQL migration file, put you code below! -- +create virtual table precios_fts using fts5(ean, url, name, content=precios, content_rowid=id); + +insert into precios_fts(rowid,ean,url,name) select id,ean,url,name from precios; + +-- https://sqlite.org/fts5.html#external_content_and_contentless_tables +-- Triggers to keep the FTS index up to date. +CREATE TRIGGER precios_fts_ai AFTER INSERT ON precios BEGIN + INSERT INTO precios_fts(rowid, ean, url, name) VALUES (new.id, new.ean, new.url, new.name); +END; +CREATE TRIGGER precios_fts_ad AFTER DELETE ON precios BEGIN + INSERT INTO precios_fts(precios_fts, rowid, ean, url, name) VALUES('delete', old.id, old.ean, old.url, old.name); +END; +CREATE TRIGGER precios_fts_au AFTER UPDATE ON precios BEGIN + INSERT INTO precios_fts(precios_fts, rowid, ean, url, name) VALUES('delete', old.id, old.ean, old.url, old.name); + INSERT INTO precios_fts(rowid, ean, url, name) VALUES (new.id, new.ean, new.url, new.name); +END; diff --git a/db-datos/drizzle/meta/0004_snapshot.json b/db-datos/drizzle/meta/0004_snapshot.json new file mode 100644 index 0000000..52e3adf --- /dev/null +++ b/db-datos/drizzle/meta/0004_snapshot.json @@ -0,0 +1,101 @@ +{ + "id": "bf90a1cd-ae6a-4dba-a1aa-79f14a11d958", + "prevId": "e1217fdb-6f54-44c5-a04b-c5aebf202102", + "version": "5", + "dialect": "sqlite", + "tables": { + "precios": { + "name": "precios", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "ean": { + "name": "ean", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "fetched_at": { + "name": "fetched_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "precio_centavos": { + "name": "precio_centavos", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "in_stock": { + "name": "in_stock", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "warc_record_id": { + "name": "warc_record_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "parser_version": { + "name": "parser_version", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "image_url": { + "name": "image_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "precios_ean_idx": { + "name": "precios_ean_idx", + "columns": [ + "ean" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/db-datos/drizzle/meta/_journal.json b/db-datos/drizzle/meta/_journal.json index 754b339..32e5cc7 100644 --- a/db-datos/drizzle/meta/_journal.json +++ b/db-datos/drizzle/meta/_journal.json @@ -29,6 +29,13 @@ "when": 1703521964385, "tag": "0003_abandoned_landau", "breakpoints": true + }, + { + "idx": 4, + "version": "5", + "when": 1703726748364, + "tag": "0004_left_wolfsbane", + "breakpoints": true } ] } \ No newline at end of file diff --git a/sitio/src/routes/+layout.server.ts b/sitio/src/routes/+layout.server.ts new file mode 100644 index 0000000..96831b2 --- /dev/null +++ b/sitio/src/routes/+layout.server.ts @@ -0,0 +1,14 @@ +import { count, countDistinct, eq, max, sql } from "drizzle-orm"; +import type { PageServerLoad } from "./$types"; +import { db, schema } from "$lib/server/db"; +const { precios } = schema; + +export const load: PageServerLoad = async () => { + const nProductosR = await db + .select({ + count: countDistinct(precios.ean), + }) + .from(precios); + const nProductos = nProductosR[0].count; + return { nProductos }; +}; diff --git a/sitio/src/routes/+layout.svelte b/sitio/src/routes/+layout.svelte index bde3404..23a27e9 100644 --- a/sitio/src/routes/+layout.svelte +++ b/sitio/src/routes/+layout.svelte @@ -1,5 +1,43 @@ - + +
+
+ + +
+
+ diff --git a/sitio/src/routes/search/+page.server.ts b/sitio/src/routes/search/+page.server.ts new file mode 100644 index 0000000..81bfcd5 --- /dev/null +++ b/sitio/src/routes/search/+page.server.ts @@ -0,0 +1,18 @@ +import { error } from "@sveltejs/kit"; +import { eq, max, sql } from "drizzle-orm"; +import type { PageServerLoad } from "./$types"; +import { db, schema } from "$lib/server/db"; +const { precios } = schema; + +export const load: PageServerLoad = async ({ url }) => { + const query = url.searchParams.get("q"); + let results: null | { ean: string; name: string }[] = null; + if (query) { + results = db.all( + sql`select ean, name from precios_fts where name match ${query};`, + ); + console.debug(results); + } + + return { query, results }; +}; diff --git a/sitio/src/routes/search/+page.svelte b/sitio/src/routes/search/+page.svelte new file mode 100644 index 0000000..918228f --- /dev/null +++ b/sitio/src/routes/search/+page.svelte @@ -0,0 +1,19 @@ + + +{#if data.results} + +{:else} + Probá buscando algo. +{/if}