chore: seeder for city and province
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import { Logger } from "@nestjs/common";
|
||||
import { DataSource } from "typeorm";
|
||||
|
||||
import { City } from "../../src/modules/address/entities/city.entity";
|
||||
import { Province } from "../../src/modules/address/entities/province.entity";
|
||||
import provincesData from "../data/json/ostan.json";
|
||||
import citiesData from "../data/json/shahr.json";
|
||||
|
||||
export const seedCityAndProvince = async (dataSource: DataSource, logger: Logger) => {
|
||||
try {
|
||||
const provinceRepo = dataSource.getRepository(Province);
|
||||
const cityRepo = dataSource.getRepository(City);
|
||||
|
||||
for (const province of provincesData) {
|
||||
const newProvince = provinceRepo.create({ name: province.name });
|
||||
await provinceRepo.save(newProvince);
|
||||
}
|
||||
|
||||
for (const city of citiesData) {
|
||||
const province = await provinceRepo.findOne({ where: { id: city.ostan } });
|
||||
if (province) {
|
||||
const newCity = cityRepo.create({ name: city.name, province });
|
||||
await cityRepo.save(newCity);
|
||||
}
|
||||
}
|
||||
|
||||
logger.log("City and Province seeding completed");
|
||||
} catch (error) {
|
||||
logger.error("Error in seeding City and Province", error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user