Skip to content

Commit

Permalink
feat(specification): persistence layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Aug 10, 2021
1 parent 52f991d commit 6070ccf
Show file tree
Hide file tree
Showing 9 changed files with 592 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

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

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "specification" ("id" uuid NOT NULL, "scenario_id" uuid NOT NULL, "draft" boolean NOT NULL, CONSTRAINT "PK_01b2d90197e187e3187b2d888be" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "specification_feature" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "specification_feature_config_id" uuid NOT NULL, "feature_id" uuid NOT NULL, "calculated" boolean NOT NULL, CONSTRAINT "PK_fe6096ea6f25997b62b8a5c5f99" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TYPE "specification_feature_config_operation_enum" AS ENUM('split', 'stratification', 'copy')`,
);
await queryRunner.query(
`CREATE TABLE "specification_feature_config" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "specification_id" uuid NOT NULL, "base_feature_id" uuid NOT NULL, "against_feature_id" uuid, "operation" "specification_feature_config_operation_enum" NOT NULL, "features_determined" boolean NOT NULL, CONSTRAINT "PK_df1e25cf3972f83b1ff5bcb0337" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "specification" ADD CONSTRAINT "FK_2cecab1b81e5fa64f2fdbbaec76" FOREIGN KEY ("scenario_id") REFERENCES "scenarios"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "specification_feature" ADD CONSTRAINT "FK_ba3687896f22d3f57b3b570368e" FOREIGN KEY ("specification_feature_config_id") REFERENCES "specification_feature_config"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "specification_feature_config" ADD CONSTRAINT "FK_1f1f9830fbe8a94ffd5c86d0bca" FOREIGN KEY ("specification_id") REFERENCES "specification"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "specification_feature_config" DROP CONSTRAINT "FK_1f1f9830fbe8a94ffd5c86d0bca"`,
);
await queryRunner.query(
`ALTER TABLE "specification_feature" DROP CONSTRAINT "FK_ba3687896f22d3f57b3b570368e"`,
);
await queryRunner.query(
`ALTER TABLE "specification" DROP CONSTRAINT "FK_2cecab1b81e5fa64f2fdbbaec76"`,
);
await queryRunner.query(`DROP TABLE "specification_feature_config"`);
await queryRunner.query(
`DROP TYPE "specification_feature_config_operation_enum"`,
);
await queryRunner.query(`DROP TABLE "specification_feature"`);
await queryRunner.query(`DROP TABLE "specification"`);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { DbSpecificationRepository } from './specification.repository';
import { SpecificationApiEntity } from './specification.api.entity';
import { SpecificationFeatureConfigApiEntity } from './specification-feature-config.api.entity';
import { SpecificationFeatureApiEntity } from './specification-feature.api.entity';

import { SpecificationRepository } from '../application/specification.repository';

@Module({
imports: [
TypeOrmModule.forFeature([
SpecificationApiEntity,
SpecificationFeatureConfigApiEntity,
SpecificationFeatureApiEntity,
]),
],
providers: [
{
provide: SpecificationRepository,
useClass: DbSpecificationRepository,
},
],
exports: [SpecificationRepository],
})
export class SpecificationAdaptersModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { SpecificationApiEntity } from './specification.api.entity';
import { SpecificationOperation } from '../domain';
import { SpecificationFeatureApiEntity } from '@marxan-api/modules/specification/adapters/specification-feature.api.entity';

@Entity(`specification_feature_config`)
export class SpecificationFeatureConfigApiEntity {
@PrimaryGeneratedColumn(`uuid`)
id!: string;

@ManyToOne(() => SpecificationApiEntity, {
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'specification_id',
referencedColumnName: 'id',
})
specification?: SpecificationApiEntity;

@Column({
type: `uuid`,
name: `specification_id`,
})
specificationId!: string;

@OneToMany(
() => SpecificationFeatureApiEntity,
(specificationFeature) => specificationFeature.specificationFeatureConfig,
{
cascade: true,
eager: true,
},
)
features?: SpecificationFeatureApiEntity[];

@Column({
type: `uuid`,
name: `base_feature_id`,
})
baseFeatureId!: string;

@Column({
type: `uuid`,
name: `against_feature_id`,
nullable: true,
})
againstFeatureId?: string | null;

@Column({
type: `enum`,
enum: SpecificationOperation,
})
operation!: SpecificationOperation;

@Column({
type: `boolean`,
name: `features_determined`,
})
featuresDetermined!: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { SpecificationFeatureConfigApiEntity } from './specification-feature-config.api.entity';

@Entity(`specification_feature`)
export class SpecificationFeatureApiEntity {
@PrimaryGeneratedColumn(`uuid`)
id!: string;

@ManyToOne(() => SpecificationFeatureConfigApiEntity, {
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'specification_feature_config_id',
referencedColumnName: 'id',
})
specificationFeatureConfig?: SpecificationFeatureConfigApiEntity;

@Column({
type: `uuid`,
name: `specification_feature_config_id`,
})
specificationFeatureConfigId!: string;

@Column({
type: `uuid`,
name: `feature_id`,
})
featureId!: string;

@Column({
type: `boolean`,
})
calculated!: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryColumn,
} from 'typeorm';
import { Scenario } from '@marxan-api/modules/scenarios/scenario.api.entity';
import { SpecificationFeatureConfigApiEntity } from './specification-feature-config.api.entity';

@Entity(`specification`)
export class SpecificationApiEntity {
@PrimaryColumn({
type: `uuid`,
})
id!: string;

@OneToMany(
() => SpecificationFeatureConfigApiEntity,
(specificationFeaturesConfig) => specificationFeaturesConfig.specification,
{
cascade: true,
eager: true,
},
)
specificationFeaturesConfiguration?: SpecificationFeatureConfigApiEntity[];

@ManyToOne(() => Scenario, {
onDelete: 'CASCADE',
})
@JoinColumn({
name: 'scenario_id',
referencedColumnName: 'id',
})
scenario?: Scenario;

@Column({
type: `uuid`,
name: `scenario_id`,
})
scenarioId!: string;

@Column({
type: `boolean`,
})
draft!: boolean;
}
Loading

0 comments on commit 6070ccf

Please sign in to comment.