Skip to content

Commit

Permalink
fix: resources relationships (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Dec 10, 2024
1 parent 0780512 commit 98edd5a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/entities/resource-user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ export class ResourceUserEntity extends BaseBloomEntity {
@Column({ type: 'date', nullable: true })
completedAt: Date;

@Column()
resourceId: string;
@ManyToOne(() => ResourceEntity, (resourceEntity) => resourceEntity.resourceUser, {
onDelete: 'CASCADE',
})
@JoinTable({ name: 'resource', joinColumn: { name: 'resourceId' } })
resource: ResourceEntity;

@Column()
userId: string;
@ManyToOne(() => UserEntity, (userEntity) => userEntity.resourceUser, { onDelete: 'CASCADE' })
@JoinTable({ name: 'user', joinColumn: { name: 'userId' } })
user: UserEntity;
Expand Down
39 changes: 39 additions & 0 deletions src/migrations/1733850090811-bloom-backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class BloomBackend1733850090811 implements MigrationInterface {
name = 'BloomBackend1733850090811';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "resource_user" DROP CONSTRAINT "FK_774b61a463074cee88e57685925"`,
);
await queryRunner.query(
`ALTER TABLE "resource_user" DROP CONSTRAINT "FK_ea89e3c7f0126d7e9d02308c2ca"`,
);
await queryRunner.query(`ALTER TABLE "resource_user" ALTER COLUMN "resourceId" SET NOT NULL`);
await queryRunner.query(`ALTER TABLE "resource_user" ALTER COLUMN "userId" SET NOT NULL`);
await queryRunner.query(
`ALTER TABLE "resource_user" ADD CONSTRAINT "FK_774b61a463074cee88e57685925" FOREIGN KEY ("resourceId") REFERENCES "resource"("resourceId") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "resource_user" ADD CONSTRAINT "FK_ea89e3c7f0126d7e9d02308c2ca" FOREIGN KEY ("userId") REFERENCES "user"("userId") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "resource_user" DROP CONSTRAINT "FK_ea89e3c7f0126d7e9d02308c2ca"`,
);
await queryRunner.query(
`ALTER TABLE "resource_user" DROP CONSTRAINT "FK_774b61a463074cee88e57685925"`,
);
await queryRunner.query(`ALTER TABLE "resource_user" ALTER COLUMN "userId" DROP NOT NULL`);
await queryRunner.query(`ALTER TABLE "resource_user" ALTER COLUMN "resourceId" DROP NOT NULL`);
await queryRunner.query(
`ALTER TABLE "resource_user" ADD CONSTRAINT "FK_ea89e3c7f0126d7e9d02308c2ca" FOREIGN KEY ("userId") REFERENCES "user"("userId") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "resource_user" ADD CONSTRAINT "FK_774b61a463074cee88e57685925" FOREIGN KEY ("resourceId") REFERENCES "resource"("resourceId") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}
}
2 changes: 2 additions & 0 deletions src/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { BloomBackend1718728423454 } from './migrations/1718728423454-bloom-back
import { BloomBackend1719668310816 } from './migrations/1719668310816-bloom-backend';
import { BloomBackend1722295564731 } from './migrations/1722295564731-bloom-backend';
import { BloomBackend1733160378757 } from './migrations/1733160378757-bloom-backend';
import { BloomBackend1733850090811 } from './migrations/1733850090811-bloom-backend';

config();
const configService = new ConfigService();
Expand Down Expand Up @@ -128,6 +129,7 @@ export const dataSourceOptions = {
BloomBackend1719668310816,
BloomBackend1722295564731,
BloomBackend1733160378757,
BloomBackend1733850090811,
],
subscribers: [],
ssl: isProduction || isStaging,
Expand Down

0 comments on commit 98edd5a

Please sign in to comment.