159 lines
4.9 KiB
TypeScript
159 lines
4.9 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;
|
|
}
|
|
|
|
async function importBusinesses() {
|
|
const filePath = './data/businesses.json'
|
|
const data: string = readFileSync(filePath, 'utf8');
|
|
const jsonData: BusinessListing[]|any = JSON.parse(data); // Erwartet ein Array von Objekten
|
|
await pool.query('drop table if exists businesses');
|
|
await pool.query(`CREATE TABLE businesses (
|
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
created TIMESTAMP,
|
|
updated TIMESTAMP,
|
|
visits INTEGER,
|
|
last_visit TIMESTAMP,
|
|
data jsonb
|
|
);`);
|
|
for (const listing of jsonData) {
|
|
const created = listing.created
|
|
delete listing.created;
|
|
delete listing.id;
|
|
delete listing.temporary
|
|
const json_values = [
|
|
created, new Date(), 0, null, listing
|
|
]
|
|
|
|
await pool.query('INSERT INTO businesses (created, updated, visits, last_visit, data) VALUES ($1,$2,$3,$4,$5)', json_values);
|
|
}
|
|
console.log('All data imported successfully.');
|
|
}
|
|
|
|
async function importUser() {
|
|
const filePath = './data/broker.json'
|
|
const data: string = readFileSync(filePath, 'utf8');
|
|
const jsonData: any[] = JSON.parse(data); // Erwartet ein Array von Objekten
|
|
await pool.query('drop table if exists users');
|
|
await pool.query(`CREATE TABLE users (
|
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
created TIMESTAMP,
|
|
updated TIMESTAMP,
|
|
visits INTEGER,
|
|
last_visit TIMESTAMP,
|
|
data jsonb
|
|
);`);
|
|
for (const user of jsonData) {
|
|
delete user.id;
|
|
user.hasCompanyLogo=false;
|
|
user.hasProfile=false;
|
|
const json_values = [
|
|
getRandomDateLastYear(), new Date(), 0, null, user
|
|
]
|
|
|
|
await pool.query('INSERT INTO users (created, updated, visits, last_visit, data) VALUES ($1,$2,$3,$4,$5)', json_values);
|
|
}
|
|
console.log('All data imported successfully.');
|
|
}
|
|
|
|
async function importCommercials() {
|
|
const filePath = './data/commercials.json'
|
|
const data: string = readFileSync(filePath, 'utf8');
|
|
const jsonData: any[]|any = JSON.parse(data); // Erwartet ein Array von Objekten
|
|
await pool.query('drop table if exists commercials');
|
|
await pool.query(`CREATE TABLE commercials (
|
|
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
|
created TIMESTAMP,
|
|
updated TIMESTAMP,
|
|
visits INTEGER,
|
|
last_visit TIMESTAMP,
|
|
data jsonb
|
|
);`);
|
|
for (const commercial of jsonData) {
|
|
commercial.hasImages=false;
|
|
commercial.imagePath=commercial.id;
|
|
delete commercial.id;
|
|
delete commercial.temporary;
|
|
const json_values = [
|
|
getRandomDateLastYear(), new Date(), 0, null, commercial
|
|
]
|
|
|
|
await pool.query('INSERT INTO commercials (created, updated, visits, last_visit, data) VALUES ($1,$2,$3,$4,$5)', json_values);
|
|
}
|
|
console.log('All data imported successfully.');
|
|
}
|
|
|
|
function idUpdate(jsonData) {
|
|
const out: BusinessListing[] = []
|
|
for (const listing of jsonData) {
|
|
|
|
const uuid = uuidv4();
|
|
listing.id = uuid;
|
|
out.push(listing);
|
|
}
|
|
writeJsonSync('./data/businesses_.json', out);
|
|
console.log('All data updated sucessfully.');
|
|
}
|
|
|
|
function getRandomDateLastYear(): Date {
|
|
const today = new Date();
|
|
const lastYear = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
|
|
|
|
// Generiere eine zufällige Zahl zwischen 0 und der Anzahl der Millisekunden in einem Jahr
|
|
const randomTime = Math.random() * (today.getTime() - lastYear.getTime());
|
|
|
|
// Erstelle ein neues Datum basierend auf dieser zufälligen Zeit
|
|
const randomDate = new Date(lastYear.getTime() + randomTime);
|
|
return randomDate;
|
|
}
|
|
|
|
// Passen Sie den Dateipfad an Ihre spezifischen Bedürfnisse an
|
|
try {
|
|
await importBusinesses();
|
|
await importUser();
|
|
await importCommercials();
|
|
} catch (err) {
|
|
console.error('Error importing data:', err.message);
|
|
} finally {
|
|
// Schließen der Verbindung zum Pool
|
|
await pool.end();
|
|
}
|
|
|