diff --git a/Dockerfile b/Dockerfile index 47b1819..1df2101 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,10 +23,9 @@ COPY --from=build /usr/src/app/sitio/package.json package.real.json RUN sh -c 'echo {\"name\":\"sitio\",\"type\":\"module\",\"dependencies\":$(jq .dependencies < package.real.json)} > package.json' && npm install COPY --from=build /usr/src/app/db-datos node_modules/db-datos COPY --from=build /usr/src/app/sitio/build . -COPY --from=build /usr/src/app/sitio/src/migrate.js . COPY --from=build /usr/src/app/db-datos/drizzle . ENV DB_PATH=/db/db.db EXPOSE 3000 -CMD "node migrate.js && node ." \ No newline at end of file +CMD "node ." \ No newline at end of file diff --git a/db-datos/db.js b/db-datos/db.js index 31f5d17..4686086 100644 --- a/db-datos/db.js +++ b/db-datos/db.js @@ -3,6 +3,13 @@ import Database from "better-sqlite3"; import { drizzle } from "drizzle-orm/better-sqlite3"; import { DB_PATH } from "./drizzle.config.js"; import * as schema from "./schema.js"; +import { migrateDb } from "./migrate.js"; -export const sqlite = new Database(DB_PATH); -export const db = drizzle(sqlite, { schema }); +/** @type {null | import("drizzle-orm/better-sqlite3").BetterSQLite3Database} */ +let db = null; +export function getDb() { + const sqlite = new Database(DB_PATH); + db = drizzle(sqlite, { schema }); + migrateDb(db); + return db; +} diff --git a/db-datos/migrate.js b/db-datos/migrate.js index 7e0ccd5..0419aee 100644 --- a/db-datos/migrate.js +++ b/db-datos/migrate.js @@ -1,21 +1,13 @@ // @ts-check -import Database from "better-sqlite3"; -import { join, dirname } from "node:path"; -import { drizzle } from "drizzle-orm/better-sqlite3"; import { migrate } from "drizzle-orm/better-sqlite3/migrator"; import * as schema from "./schema.js"; -import { DB_PATH } from "./drizzle.config.js"; +import { sql } from "drizzle-orm"; -const url = new URL(import.meta.url); -export function migrateDb() { - const sqlite = new Database(DB_PATH); - const db = drizzle(sqlite, { schema }); - - migrate(db, { migrationsFolder: join(dirname(url.pathname), "drizzle") }); - sqlite.exec(` -pragma journal_mode = WAL; -PRAGMA synchronous = NORMAL; -`); - - sqlite.close(); +/** + * @param {import("drizzle-orm/better-sqlite3").BetterSQLite3Database} db + */ +export function migrateDb(db) { + migrate(db, { migrationsFolder: "node_modules/db-datos/drizzle" }); + db.run(sql`pragma journal_mode = WAL;`); + db.run(sql`PRAGMA synchronous = NORMAL;`); } diff --git a/db-datos/urlHelpers.ts b/db-datos/urlHelpers.ts deleted file mode 100644 index 2ca2d03..0000000 --- a/db-datos/urlHelpers.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { sql } from "drizzle-orm"; -import { db } from "./db.js"; -import { productoUrls } from "./schema.js"; - -export function saveUrls(urls: string[]) { - db.transaction((tx) => { - const now = new Date(); - const insertUrlTra = tx - .insert(productoUrls) - .values({ - url: sql.placeholder("url"), - firstSeen: now, - lastSeen: now, - }) - .onConflictDoUpdate({ - target: productoUrls.url, - set: { lastSeen: now }, - }) - .prepare(); - - for (const href of urls) { - insertUrlTra.run({ url: href }); - } - }); -} diff --git a/sitio/src/lib/server/db.ts b/sitio/src/lib/server/db.ts index 968cc55..1f304d5 100644 --- a/sitio/src/lib/server/db.ts +++ b/sitio/src/lib/server/db.ts @@ -1,2 +1,2 @@ -export { db } from "db-datos/db.js"; +export { getDb } from "db-datos/db.js"; export * as schema from "db-datos/schema.js"; diff --git a/sitio/src/migrate.js b/sitio/src/migrate.js deleted file mode 100644 index bb969c6..0000000 --- a/sitio/src/migrate.js +++ /dev/null @@ -1,2 +0,0 @@ -import { migrateDb } from "db-datos/migrate.js"; -migrateDb(); diff --git a/sitio/src/routes/+layout.server.ts b/sitio/src/routes/+layout.server.ts index c7adbaf..57c2efa 100644 --- a/sitio/src/routes/+layout.server.ts +++ b/sitio/src/routes/+layout.server.ts @@ -1,9 +1,10 @@ import { countDistinct } from "drizzle-orm"; import type { PageServerLoad } from "./$types"; -import { db, schema } from "$lib/server/db"; +import { getDb, schema } from "$lib/server/db"; const { precios } = schema; export const load: PageServerLoad = async () => { + const db = await getDb(); const nProductosR = await db .select({ count: countDistinct(precios.ean), diff --git a/sitio/src/routes/+page.server.ts b/sitio/src/routes/+page.server.ts index d6ec90e..dbb1534 100644 --- a/sitio/src/routes/+page.server.ts +++ b/sitio/src/routes/+page.server.ts @@ -1,11 +1,12 @@ import type { PageData, PageServerLoad } from "./$types"; -import { db, schema } from "$lib/server/db"; +import { getDb, schema } from "$lib/server/db"; const { precios } = schema; import { sql } from "drizzle-orm"; let cache: Promise<{ key: Date; data: { precios: Precios } }> = doQuery(); async function doQuery() { + const db = await getDb(); const q = db .select({ ean: precios.ean, diff --git a/sitio/src/routes/ean/[ean]/+page.server.ts b/sitio/src/routes/ean/[ean]/+page.server.ts index 9f9f9e3..3ec91ad 100644 --- a/sitio/src/routes/ean/[ean]/+page.server.ts +++ b/sitio/src/routes/ean/[ean]/+page.server.ts @@ -1,10 +1,11 @@ import { error } from "@sveltejs/kit"; import { eq, max } from "drizzle-orm"; import type { PageServerLoad } from "./$types"; -import { db, schema } from "$lib/server/db"; +import { getDb, schema } from "$lib/server/db"; const { precios } = schema; export const load: PageServerLoad = async ({ params }) => { + const db = await getDb(); const q = db .select() .from(precios) diff --git a/sitio/src/routes/search/+page.server.ts b/sitio/src/routes/search/+page.server.ts index e0f9f9d..af5db94 100644 --- a/sitio/src/routes/search/+page.server.ts +++ b/sitio/src/routes/search/+page.server.ts @@ -1,9 +1,10 @@ import { error } from "@sveltejs/kit"; import { sql } from "drizzle-orm"; import type { PageServerLoad } from "./$types"; -import { db } from "$lib/server/db"; +import { getDb } from "$lib/server/db"; export const load: PageServerLoad = async ({ url }) => { + const db = await getDb(); const query = url.searchParams.get("q"); let results: null | { ean: string; name: string; imageUrl: string }[] = null; if (query) {