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 10, 2024
1 parent c171307 commit 457db2d
Show file tree
Hide file tree
Showing 2 changed files with 30 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
29 changes: 29 additions & 0 deletions server/migrations/1712732757287-New-Country-Table.ts
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;`);
}
}

0 comments on commit 457db2d

Please sign in to comment.