69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
import 'dotenv/config';
|
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
import { readFileSync } from 'fs';
|
|
import { Pool } from 'pg';
|
|
import { BusinessListingService } from 'src/listings/business-listing.service';
|
|
import { CommercialPropertyService } from 'src/listings/commercial-property.service';
|
|
import { UserService } from 'src/user/user.service';
|
|
import winston from 'winston';
|
|
import { BusinessListing, CommercialPropertyListing, User } from '../models/db.model';
|
|
import * as schema from './schema';
|
|
|
|
(async () => {
|
|
const connectionString = process.env.DATABASE_URL;
|
|
const client = new Pool({ connectionString });
|
|
const db = drizzle(client, { schema, logger: true });
|
|
const logger = winston.createLogger({
|
|
transports: [new winston.transports.Console()],
|
|
});
|
|
const commService = new CommercialPropertyService(null, db);
|
|
const businessService = new BusinessListingService(null, db);
|
|
const userService = new UserService(null, db, null, null);
|
|
|
|
//Delete Content
|
|
await db.delete(schema.commercials);
|
|
await db.delete(schema.businesses);
|
|
await db.delete(schema.users);
|
|
|
|
let filePath = `./data/users_export.json`;
|
|
let data: string = readFileSync(filePath, 'utf8');
|
|
const usersData: User[] = JSON.parse(data); // Erwartet ein Array von Objekten
|
|
for (let index = 0; index < usersData.length; index++) {
|
|
const user = usersData[index];
|
|
delete user.id;
|
|
const u = await userService.saveUser(user, false);
|
|
logger.info(`user_${index} inserted`);
|
|
}
|
|
|
|
//Corporate Listings
|
|
filePath = `./data/commercials_export.json`;
|
|
data = readFileSync(filePath, 'utf8');
|
|
const commercialJsonData = JSON.parse(data) as CommercialPropertyListing[]; // Erwartet ein Array von Objekten
|
|
for (let index = 0; index < commercialJsonData.length; index++) {
|
|
const commercial = commercialJsonData[index];
|
|
delete commercial.id;
|
|
const result = await commService.createListing(commercial);
|
|
}
|
|
|
|
//Business Listings
|
|
filePath = `./data/businesses_export.json`;
|
|
data = readFileSync(filePath, 'utf8');
|
|
const businessJsonData = JSON.parse(data) as BusinessListing[]; // Erwartet ein Array von Objekten
|
|
for (let index = 0; index < businessJsonData.length; index++) {
|
|
const business = businessJsonData[index];
|
|
delete business.id;
|
|
await businessService.createListing(business);
|
|
}
|
|
|
|
//End
|
|
await client.end();
|
|
})();
|
|
function getRandomItem<T>(arr: T[]): T {
|
|
if (arr.length === 0) {
|
|
throw new Error('The array is empty.');
|
|
}
|
|
|
|
const randomIndex = Math.floor(Math.random() * arr.length);
|
|
return arr[randomIndex];
|
|
}
|