require('dotenv').config({ override: true }); const { Pool } = require('pg'); const xlsx = require('xlsx'); const pool = new Pool({ host: process.env.DB_HOST, port: process.env.DB_PORT, database: process.env.DB_NAME, user: process.env.DB_USER, password: process.env.DB_PASSWORD }); async function importWea() { const wb = xlsx.readFile('./Anlagentypen/Anlagentypen.xlsx'); const sheet = wb.Sheets[wb.SheetNames[0]]; const data = xlsx.utils.sheet_to_json(sheet); try { console.log('Connecting to DB...'); await pool.query('BEGIN'); console.log('Dropping and creating table geodaten.anlagendaten_wea...'); await pool.query(` DROP TABLE IF EXISTS geodaten.anlagendaten_wea; CREATE TABLE geodaten.anlagendaten_wea ( id SERIAL PRIMARY KEY, hersteller VARCHAR(255), anlagentyp VARCHAR(255), nennleistung FLOAT, nabenhoehe FLOAT, rotordurchmesser FLOAT, gesamthoehe FLOAT ); `); console.log(`Importing ${data.length} rows...`); for (const row of data) { // Keys from Excel // "Hersteller", "Anlagentyp", "Nennleistung (MW)", "Nabenhöhe (m)", "Rotordurchmesser (m)", "Gesamthöhe (m)" const hersteller = row['Hersteller'] || ''; const anlagentyp = row['Anlagentyp'] || ''; const nennleistung = parseFloat(row['Nennleistung (MW)'] || 0); const nabenhoehe = parseFloat(row['Nabenhöhe (m)'] || 0); const rotordurchmesser = parseFloat(row['Rotordurchmesser (m)'] || 0); const gesamthoehe = parseFloat(row['Gesamthöhe (m)'] || 0); await pool.query(` INSERT INTO geodaten.anlagendaten_wea (hersteller, anlagentyp, nennleistung, nabenhoehe, rotordurchmesser, gesamthoehe) VALUES ($1, $2, $3, $4, $5, $6) `, [hersteller, anlagentyp, nennleistung, nabenhoehe, rotordurchmesser, gesamthoehe]); } await pool.query('COMMIT'); console.log('Import finished successfully.'); } catch (err) { await pool.query('ROLLBACK'); console.error('Error importing:', err); } finally { await pool.end(); } } importWea();