Skip to content

Commit 10abed8

Browse files
author
Benny Burkert
committed
feat: WIP prepare db migration
1 parent 4a131c7 commit 10abed8

7 files changed

Lines changed: 172 additions & 11 deletions

File tree

backend/package-lock.json

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/src/controllers/prompts/dtos/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsArray, IsDefined, IsEnum, IsOptional, IsString } from 'class-validator';
3-
import { VisibilityType } from 'src/domain/prompt/interfaces';
3+
import { VisibilityType } from 'src/domain/prompt';
44

55
export class CreatePromptCategoryDto {
66
@ApiProperty({

backend/src/domain/database/entities/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export * from './setting';
1111
export * from './usage';
1212
export * from './user';
1313
export * from './user-group';
14+
export * from './prompt';
15+
export * from './prompt-category';

backend/src/domain/database/entities/prompt-category.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
1+
import { Column, Entity, PrimaryColumn } from 'typeorm';
2+
import { VisibilityType } from '../../prompt';
23
import { schema } from '../typeorm.helper';
3-
import { VisibilityType } from 'src/domain/prompt/interfaces';
44

55
@Entity({ name: 'prompt-categories', schema })
66
export class PromptCategoryEntity {
7-
@PrimaryGeneratedColumn()
8-
id!: string;
9-
10-
@Column({ nullable: true })
7+
@PrimaryColumn()
118
label!: string;
129

1310
@Column()

backend/src/domain/database/entities/prompt.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
1+
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';
2+
import { VisibilityType } from '../../prompt';
23
import { schema } from '../typeorm.helper';
3-
import { VisibilityType } from 'src/domain/prompt/interfaces';
44
import { PromptCategoryEntity } from './prompt-category';
55

66
@Entity({ name: 'prompts', schema })
@@ -17,8 +17,9 @@ export class PromptEntity {
1717
@Column({ nullable: false })
1818
content!: string;
1919

20-
@OneToMany(() => PromptEntity, (prompt) => prompt.categories)
21-
categories?: [string];
20+
@ManyToMany(() => PromptCategoryEntity)
21+
@JoinTable()
22+
categories?: PromptCategoryEntity[];
2223

2324
@Column()
2425
visibility!: VisibilityType;

backend/src/domain/prompt/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './interfaces';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class AddPrompts1757081968480 implements MigrationInterface {
4+
name = 'AddPrompts1757081968480';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "company_chat"."prompt-categories" ("label" character varying NOT NULL, "description" character varying NOT NULL, "creationDate" TIMESTAMP NOT NULL, "visibility" character varying NOT NULL, CONSTRAINT "PK_fcceccb98fc892cc5b3e40c1097" PRIMARY KEY ("label"))`,
9+
);
10+
await queryRunner.query(
11+
`CREATE TABLE "company_chat"."prompts" ("id" SERIAL NOT NULL, "title" character varying, "description" character varying, "content" character varying NOT NULL, "visibility" character varying NOT NULL, "raiting" integer, CONSTRAINT "PK_21f33798862975179e40b216a1d" PRIMARY KEY ("id"))`,
12+
);
13+
await queryRunner.query(
14+
`CREATE TABLE "company_chat"."prompts_categories_prompt-categories" ("promptsId" integer NOT NULL, "promptCategoriesLabel" character varying NOT NULL, CONSTRAINT "PK_645256b9fc1fe950d6f6e7c3003" PRIMARY KEY ("promptsId", "promptCategoriesLabel"))`,
15+
);
16+
await queryRunner.query(
17+
`CREATE INDEX "IDX_b38fb3b439a6eea0016e1d5906" ON "company_chat"."prompts_categories_prompt-categories" ("promptsId") `,
18+
);
19+
await queryRunner.query(
20+
`CREATE INDEX "IDX_6515aea8d350337de0804256c9" ON "company_chat"."prompts_categories_prompt-categories" ("promptCategoriesLabel") `,
21+
);
22+
await queryRunner.query(
23+
`ALTER TABLE "company_chat"."prompts_categories_prompt-categories" ADD CONSTRAINT "FK_b38fb3b439a6eea0016e1d5906a" FOREIGN KEY ("promptsId") REFERENCES "company_chat"."prompts"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
24+
);
25+
await queryRunner.query(
26+
`ALTER TABLE "company_chat"."prompts_categories_prompt-categories" ADD CONSTRAINT "FK_6515aea8d350337de0804256c98" FOREIGN KEY ("promptCategoriesLabel") REFERENCES "company_chat"."prompt-categories"("label") ON DELETE CASCADE ON UPDATE CASCADE`,
27+
);
28+
}
29+
30+
public async down(queryRunner: QueryRunner): Promise<void> {
31+
await queryRunner.query(
32+
`ALTER TABLE "company_chat"."prompts_categories_prompt-categories" DROP CONSTRAINT "FK_6515aea8d350337de0804256c98"`,
33+
);
34+
await queryRunner.query(
35+
`ALTER TABLE "company_chat"."prompts_categories_prompt-categories" DROP CONSTRAINT "FK_b38fb3b439a6eea0016e1d5906a"`,
36+
);
37+
await queryRunner.query(`DROP INDEX "company_chat"."IDX_6515aea8d350337de0804256c9"`);
38+
await queryRunner.query(`DROP INDEX "company_chat"."IDX_b38fb3b439a6eea0016e1d5906"`);
39+
await queryRunner.query(`DROP TABLE "company_chat"."prompts_categories_prompt-categories"`);
40+
await queryRunner.query(`DROP TABLE "company_chat"."prompts"`);
41+
await queryRunner.query(`DROP TABLE "company_chat"."prompt-categories"`);
42+
}
43+
}

0 commit comments

Comments
 (0)