From 977157e0cbf2f01ea5d3f987047b6e8b411502ed Mon Sep 17 00:00:00 2001 From: Disura Randunu Date: Sun, 22 Sep 2024 18:51:02 +0530 Subject: [PATCH] Countrties seeding modified --- src/scripts/seed-db-countries.ts | 39 -------------------------------- src/scripts/seed-db.ts | 38 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 39 deletions(-) delete mode 100644 src/scripts/seed-db-countries.ts diff --git a/src/scripts/seed-db-countries.ts b/src/scripts/seed-db-countries.ts deleted file mode 100644 index 997a185..0000000 --- a/src/scripts/seed-db-countries.ts +++ /dev/null @@ -1,39 +0,0 @@ -import path from 'path' -import fs from 'fs' -import { dataSource } from '../configs/dbConfig' -import { Country } from '../entities/country.entity' - -// Countries file must be in the same directory as the seeding file in build directory. -const countriesDataFileName = 'countries.json' -const countriesFilePath = path.join(__dirname, countriesDataFileName) - -export const seedCountries = async (): Promise => { - const countriesData: Record = JSON.parse( - fs.readFileSync(countriesFilePath, 'utf-8') - ) - - await dataSource.initialize() - const countryRepository = dataSource.getRepository(Country) - - for (const [code, name] of Object.entries(countriesData)) { - const existingCountry = await countryRepository.findOne({ - where: { code } - }) - - if (!existingCountry) { - await countryRepository.save(new Country(code, name)) - console.log(`Inserted: ${name}`) - } else { - console.log(`Country already exists: ${name}`) - } - } - await dataSource.destroy() -} - -seedCountries() - .then(() => { - console.log('Countries Seeding completed') - }) - .catch((err) => { - console.error('Countries Seeding Error', err) - }) diff --git a/src/scripts/seed-db.ts b/src/scripts/seed-db.ts index aa14901..91ce4ce 100644 --- a/src/scripts/seed-db.ts +++ b/src/scripts/seed-db.ts @@ -1,10 +1,14 @@ import { faker } from '@faker-js/faker' +import path from 'path' +import fs from 'fs' import { dataSource } from '../configs/dbConfig' import Category from '../entities/category.entity' import Email from '../entities/email.entity' import Mentee from '../entities/mentee.entity' import Mentor from '../entities/mentor.entity' import Profile from '../entities/profile.entity' +import Country from '../entities/country.entity' + import { EmailStatusTypes, MenteeApplicationStatus, @@ -163,6 +167,32 @@ const createMentor = (category: Category, profile: Profile): Mentor => { } as unknown as Mentor } +export const seedCountries = async (): Promise => { + // Countries data file must be in the same directory as the seeding file in build directory. + const countriesDataFilePath = path.join(__dirname, 'countries.json') + + const countriesData: Record = JSON.parse( + fs.readFileSync(countriesDataFilePath, 'utf-8') + ) + + await dataSource.initialize() + const countryRepository = dataSource.getRepository(Country) + + for (const [code, name] of Object.entries(countriesData)) { + const existingCountry = await countryRepository.findOne({ + where: { code } + }) + + if (!existingCountry) { + await countryRepository.save(new Country(code, name)) + console.log(`Inserted: ${name}`) + } else { + console.log(`Country already exists: ${name}`) + } + } + await dataSource.destroy() +} + seedDatabaseService() .then((res) => { console.log(res) @@ -170,3 +200,11 @@ seedDatabaseService() .catch((err) => { console.error(err) }) + +seedCountries() + .then(() => { + console.log('Countries Seeding completed') + }) + .catch((err) => { + console.error('Countries Seeding Error', err) + })