From ca6dbafaf19fe400a3cd1c55fb7c280d14941306 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 21 Jan 2025 11:25:26 +0100 Subject: [PATCH] extend api table by id --- src/entity/user/api.ts | 5 ++++- src/entity/user/api_permission.ts | 2 +- src/migrations/1737453096674-addApiTokens.ts | 16 +++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/entity/user/api.ts b/src/entity/user/api.ts index a3ead90..36279fd 100644 --- a/src/entity/user/api.ts +++ b/src/entity/user/api.ts @@ -2,7 +2,10 @@ import { Column, CreateDateColumn, Entity, PrimaryColumn } from "typeorm"; @Entity() export class api { - @PrimaryColumn({ type: "varchar", length: 255 }) + @PrimaryColumn({ generated: "increment", type: "int" }) + id: number; + + @Column({ type: "varchar", length: 255, unique: true, select: false }) token: string; @Column({ type: "varchar", length: 255 }) diff --git a/src/entity/user/api_permission.ts b/src/entity/user/api_permission.ts index 4eb546d..1b031b4 100644 --- a/src/entity/user/api_permission.ts +++ b/src/entity/user/api_permission.ts @@ -5,7 +5,7 @@ import { api } from "./api"; @Entity() export class apiPermission { @PrimaryColumn({ type: "int" }) - apiToken: number; + apiId: number; @PrimaryColumn({ type: "varchar", length: 255 }) permission: PermissionString; diff --git a/src/migrations/1737453096674-addApiTokens.ts b/src/migrations/1737453096674-addApiTokens.ts index 182f77f..d547892 100644 --- a/src/migrations/1737453096674-addApiTokens.ts +++ b/src/migrations/1737453096674-addApiTokens.ts @@ -11,11 +11,12 @@ export class AddApiTokens1737453096674 implements MigrationInterface { new Table({ name: "api", columns: [ - { name: "token", type: "varchar", length: "255", isPrimary: true, isNullable: false }, + { name: "id", type: variableType_int, isPrimary: true, isNullable: false }, + { name: "token", type: "varchar", length: "255", isUnique: true, isNullable: false }, { name: "title", type: "varchar", length: "255", isNullable: false }, { name: "createdAt", type: "datetime", default: "CURRENT_TIMESTAMP(6)", isNullable: false }, - { name: "lastUsage", type: "datetime", isNullable: true }, - { name: "expiry", type: "datetime", isNullable: true }, + { name: "lastUsage", type: "datetime", isNullable: true, default: null }, + { name: "expiry", type: "datetime", isNullable: true, default: null }, ], }), true @@ -25,17 +26,18 @@ export class AddApiTokens1737453096674 implements MigrationInterface { new Table({ name: "api_permission", columns: [ - { name: "apiToken", type: "varchar", length: "255", isPrimary: true, isNullable: false }, + { name: "apiId", type: variableType_int, isPrimary: true, isNullable: false }, { name: "permission", type: "varchar", length: "255", isPrimary: true, isNullable: false }, ], - }) + }), + true ); await queryRunner.createForeignKey( "api_permission", new TableForeignKey({ - columnNames: ["apiToken"], - referencedColumnNames: ["token"], + columnNames: ["apiId"], + referencedColumnNames: ["id"], referencedTableName: "api", onDelete: "CASCADE", onUpdate: "RESTRICT",