-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new table country + migration and seed all country
- Loading branch information
Showing
2 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
import { countries } from '../utils/iso-3166-countries-french'; | ||
|
||
export class NewCountryTable1712732757287 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE IF NOT EXISTS country ( | ||
id int PRIMARY KEY AUTO_INCREMENT, | ||
isoCode varchar(3) UNIQUE NOT NULL, | ||
name varchar(255) UNIQUE NOT NULL | ||
);`); | ||
console.warn('Country table already exist'); | ||
console.warn('Seeding...'); | ||
// seed country in db | ||
for (const country of countries) { | ||
// try catch here to prevent if country is already set in db for some reason | ||
try { | ||
console.warn(country); | ||
await queryRunner.query(`INSERT INTO country (isoCode, name) VALUES ("${country.isoCode}", "${country.name}");`); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE IF EXISTS country;`); | ||
} | ||
} |