Skip to content

Commit

Permalink
Mentor Country Relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Disura-Randunu committed Sep 29, 2024
1 parent 400b8fd commit 32a63c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/entities/country.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Column, Entity } from 'typeorm'
import { Column, Entity, OneToMany } from 'typeorm'
import BaseEntity from './baseEntity'
import Mentor from './mentor.entity'

@Entity()
export class Country extends BaseEntity {
Expand All @@ -9,6 +10,9 @@ export class Country extends BaseEntity {
@Column()
name: string

@OneToMany(() => Mentor, (mentor) => mentor.country)
mentors?: Mentor[]

constructor(code: string, name: string) {
super()
this.code = code
Expand Down
9 changes: 8 additions & 1 deletion src/entities/mentor.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Mentee from './mentee.entity'
import Category from './category.entity'
import { MentorApplicationStatus } from '../enums'
import BaseEntity from './baseEntity'
import { Country } from './country.entity'

@Entity('mentor')
class Mentor extends BaseEntity {
Expand All @@ -28,6 +29,10 @@ class Mentor extends BaseEntity {
@JoinColumn()
profile: Profile

@ManyToOne(() => Country, (country) => country.mentors)
@JoinColumn()
country: Country

@OneToMany(() => Mentee, (mentee) => mentee.mentor)
mentees?: Mentee[]

Expand All @@ -36,14 +41,16 @@ class Mentor extends BaseEntity {
category: Category,
application: Record<string, unknown>,
availability: boolean,
profile: Profile
profile: Profile,
country: Country
) {
super()
this.state = state
this.category = category
this.application = application
this.availability = availability
this.profile = profile
this.country = country
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/migrations/1726930041488-UpdateMentorTableWithCountry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from 'typeorm'

export class UpdateMentorTableWithCountry1726930041488
implements MigrationInterface
{
name = 'UpdateMentorTableWithCountry1726930041488'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "mentor" ADD "countryUuid" uuid`)
await queryRunner.query(
`ALTER TABLE "mentor" ADD CONSTRAINT "FK_3302c22eb1636f239d605eb61c3" FOREIGN KEY ("countryUuid") REFERENCES "country"("uuid") ON DELETE NO ACTION ON UPDATE NO ACTION`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "mentor" DROP CONSTRAINT "FK_3302c22eb1636f239d605eb61c3"`
)
await queryRunner.query(`ALTER TABLE "mentor" DROP COLUMN "countryUuid"`)
}
}

0 comments on commit 32a63c9

Please sign in to comment.