From d3cd6964528e61f6c79e12207b1bc718ffb8464b Mon Sep 17 00:00:00 2001 From: Lea Tveteraas Date: Wed, 7 Oct 2026 15:27:32 +0200 Subject: [PATCH] oppgave/Importere-land-vinerregioner-produsenter-#14 --- backend/scripts/import.ts | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/backend/scripts/import.ts b/backend/scripts/import.ts index 9675f0d..e8b9d77 100644 --- a/backend/scripts/import.ts +++ b/backend/scripts/import.ts @@ -1,5 +1,6 @@ import { createReadStream } from "node:fs"; import { parse } from "csv-parse"; +import { pool } from "../src/db.js"; const WINES_FILE = "data/XWines_Test_100_wines.csv"; @@ -8,10 +9,38 @@ async function main() { let count = 0; for await (const row of parser) { - if (count === 0) console.log(row); + await pool.query( + `INSERT INTO countries (code, name) VALUES ($1, $2) + ON CONFLICT (code) DO NOTHING`, + [row.Code, row.Country] + ); + + await pool.query( + `INSERT INTO regions (id, name, country_code) VALUES ($1, $2, $3) + ON CONFLICT (id) DO NOTHING`, + [row.RegionID, row.RegionName, row.Code] + ); + + await pool.query( + `INSERT INTO wineries (id, name, website) VALUES ($1, $2, $3) + ON CONFLICT (id) DO NOTHING`, + [row.WineryID, row.WineryName, row.Website || null] + ); + + await pool.query( + `INSERT INTO wines + (id, name, type, elaborate, abv, body, acidity, winery_id, region_id, country_code) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + ON CONFLICT (id) DO NOTHING`, + [row.WineID, row.WineName, row.Type, row.Elaborate, row.ABV, + row.Body, row.Acidity, row.WineryID, row.RegionID, row.Code] + ); + count++; } - console.log(`Leste ${count} rader`); + + console.log(`Importerte ${count} viner`); + await pool.end(); } main(); \ No newline at end of file