86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import pkg from 'pg';
|
|
const { Pool } = pkg;
|
|
import fsextra from 'fs-extra';
|
|
const { fstat, readFileSync, writeJsonSync } = fsextra;
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
// PostgreSQL Verbindungskonfiguration
|
|
const pool = new Pool({
|
|
user: 'bizmatch',
|
|
host: 'localhost',
|
|
database: 'bizmatch',
|
|
password: 'xieng7Seih',
|
|
port: 5432,
|
|
});
|
|
|
|
// Typdefinition für das JSON-Objekt
|
|
interface BusinessListing {
|
|
userId?: string;
|
|
listingsCategory: string;
|
|
title: string;
|
|
description: string;
|
|
type: string;
|
|
state: string;
|
|
city: string;
|
|
id: string;
|
|
price: number;
|
|
salesRevenue: number;
|
|
leasedLocation: boolean;
|
|
established: number;
|
|
employees: number;
|
|
reasonForSale: string;
|
|
supportAndTraining: string;
|
|
cashFlow: number;
|
|
brokerLicencing: string;
|
|
internalListingNumber: number;
|
|
realEstateIncluded: boolean;
|
|
franchiseResale: boolean;
|
|
draft: boolean;
|
|
internals: string;
|
|
created: Date;
|
|
}
|
|
|
|
// Funktion zum Einlesen und Importieren von JSON-Daten
|
|
async function importJsonData(filePath: string): Promise<void> {
|
|
try {
|
|
const data: string = readFileSync(filePath, 'utf8');
|
|
const jsonData: BusinessListing[] = JSON.parse(data); // Erwartet ein Array von Objekten
|
|
const out: BusinessListing[] =[]
|
|
// Daten für jedes Listing in die Datenbank einfügen
|
|
for (const listing of jsonData) {
|
|
// const uuid = uuidv4();
|
|
// listing.id=uuid;
|
|
const values = [
|
|
listing.userId, listing.listingsCategory, listing.title, listing.description,
|
|
listing.type, listing.state, listing.city, listing.id, listing.price, listing.salesRevenue,
|
|
listing.leasedLocation, listing.established, listing.employees,
|
|
listing.reasonForSale, listing.supportAndTraining, listing.cashFlow, listing.brokerLicencing,
|
|
listing.internalListingNumber, listing.realEstateIncluded, listing.franchiseResale,
|
|
listing.draft, listing.internals, listing.created, new Date(), 0, null
|
|
];
|
|
const json_values = [
|
|
listing.id, listing
|
|
]
|
|
|
|
await pool.query(`INSERT INTO businesses
|
|
(user_id, listings_category, title, description, type, state, city, id, price, sales_revenue, leased_location,
|
|
established, employees, reason_for_sale, support_and_training, cash_flow, broker_licencing, internal_listing_number,
|
|
real_estate_included, franchise_resale, draft, internals, created, updated, visits, last_visit)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26)`, values);
|
|
|
|
await pool.query('INSERT INTO businesses_json (id, data) VALUES ($1,$2)', json_values);
|
|
|
|
// out.push(listing);
|
|
}
|
|
writeJsonSync('./data/businesses_.json',out);
|
|
console.log('All data imported successfully.');
|
|
} catch (err) {
|
|
console.error('Error importing data:', err.message);
|
|
} finally {
|
|
// Schließen der Verbindung zum Pool
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
// Passen Sie den Dateipfad an Ihre spezifischen Bedürfnisse an
|
|
importJsonData('./data/businesses_.json');
|