mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-22 14:16:19 +00:00
a verrrr
This commit is contained in:
parent
dbbd8e7f3a
commit
d58df3fd04
10 changed files with 27 additions and 52 deletions
|
@ -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 ."
|
||||
CMD "node ."
|
|
@ -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<schema>} */
|
||||
let db = null;
|
||||
export function getDb() {
|
||||
const sqlite = new Database(DB_PATH);
|
||||
db = drizzle(sqlite, { schema });
|
||||
migrateDb(db);
|
||||
return db;
|
||||
}
|
||||
|
|
|
@ -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<schema>} 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;`);
|
||||
}
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
});
|
||||
}
|
|
@ -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";
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
import { migrateDb } from "db-datos/migrate.js";
|
||||
migrateDb();
|
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue