Skip to content

Commit

Permalink
add new table country + migration and seed all country
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ryo committed Apr 12, 2024
1 parent 4a25fa6 commit f0f76b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/entities/country.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Country {
public id: number;

@Column({ type: 'text', nullable: false })
public code: string;
public isoCode: string;

@Column({ type: 'text', nullable: false })
public name: string;
Expand Down
27 changes: 27 additions & 0 deletions server/migrations/1712732757287-New-Country-Table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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
);`);
// 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;`);
}
}

0 comments on commit f0f76b2

Please sign in to comment.