From ea27bed45428e37b7597e8afe1bcdcc1fe01f0d3 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 16 Apr 2024 19:05:28 +0100 Subject: [PATCH 1/4] Create users entity --- src/database/entities/app/User.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/database/entities/app/User.ts diff --git a/src/database/entities/app/User.ts b/src/database/entities/app/User.ts new file mode 100644 index 0000000..31d5ea1 --- /dev/null +++ b/src/database/entities/app/User.ts @@ -0,0 +1,31 @@ +import { Column, Entity } from "typeorm"; +import AppBaseEntity from "../../../contracts/AppBaseEntity"; +import AppDataSource from "../../dataSources/appDataSource"; + +@Entity() +export default class User extends AppBaseEntity { + constructor(userId: string, currency: number) { + super(); + + this.UserId = userId; + this.Currency = currency; + } + + @Column() + UserId: string; + + @Column() + Currency: number; + + public UpdateCurrency(currency: number) { + this.Currency = currency; + } + + public static async FetchOneByUserId(userId: string): Promise { + const repository = AppDataSource.getRepository(User); + + const single = await repository.findOne({ where: { UserId: userId }}); + + return single; + } +} \ No newline at end of file -- 2.43.4 From d608e1c7528f6056b3d7939c846cfd371720efc7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 16 Apr 2024 19:05:39 +0100 Subject: [PATCH 2/4] Create migration script --- .../0.6/1713289062969-user/Up/01-table/User.sql | 8 ++++++++ .../migrations/app/0.6/1713289062969-user.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 database/0.6/1713289062969-user/Up/01-table/User.sql create mode 100644 src/database/migrations/app/0.6/1713289062969-user.ts diff --git a/database/0.6/1713289062969-user/Up/01-table/User.sql b/database/0.6/1713289062969-user/Up/01-table/User.sql new file mode 100644 index 0000000..aa7c36b --- /dev/null +++ b/database/0.6/1713289062969-user/Up/01-table/User.sql @@ -0,0 +1,8 @@ +CREATE TABLE `user` ( + `Id` varchar(255) NOT NULL, + `WhenCreated` datetime NOT NULL, + `WhenUpdated` datetime NOT NULL, + `UserId` varchar(255) NOT NULL, + `Currency` int NOT NULL, + PRIMARY KEY (`Id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file diff --git a/src/database/migrations/app/0.6/1713289062969-user.ts b/src/database/migrations/app/0.6/1713289062969-user.ts new file mode 100644 index 0000000..9604db8 --- /dev/null +++ b/src/database/migrations/app/0.6/1713289062969-user.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import MigrationHelper from "../../../../helpers/MigrationHelper"; + +export class User1713289062969 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + MigrationHelper.Up("1713289062969-user", "0.6", [ + "01-table/User", + ], queryRunner); + } + + public async down(queryRunner: QueryRunner): Promise { + } + +} -- 2.43.4 From 6c2233790ec8a7b55675aa203e63e4d94a2cd58c Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 16 Apr 2024 19:06:43 +0100 Subject: [PATCH 3/4] Fix linting issues --- src/database/entities/app/User.ts | 4 ++-- src/database/migrations/app/0.6/1713289062969-user.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/database/entities/app/User.ts b/src/database/entities/app/User.ts index 31d5ea1..26d1303 100644 --- a/src/database/entities/app/User.ts +++ b/src/database/entities/app/User.ts @@ -12,10 +12,10 @@ export default class User extends AppBaseEntity { } @Column() - UserId: string; + UserId: string; @Column() - Currency: number; + Currency: number; public UpdateCurrency(currency: number) { this.Currency = currency; diff --git a/src/database/migrations/app/0.6/1713289062969-user.ts b/src/database/migrations/app/0.6/1713289062969-user.ts index 9604db8..151db88 100644 --- a/src/database/migrations/app/0.6/1713289062969-user.ts +++ b/src/database/migrations/app/0.6/1713289062969-user.ts @@ -9,7 +9,7 @@ export class User1713289062969 implements MigrationInterface { ], queryRunner); } - public async down(queryRunner: QueryRunner): Promise { + public async down(): Promise { } } -- 2.43.4 From d7412cb71a202b43ba6d706c76e91dde1446c459 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 25 Apr 2024 17:33:17 +0100 Subject: [PATCH 4/4] Update database to use id of User table as user id --- .../0.6/1713289062969-user/Up/01-table/User.sql | 1 - src/database/entities/app/User.ts | 14 +------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/database/0.6/1713289062969-user/Up/01-table/User.sql b/database/0.6/1713289062969-user/Up/01-table/User.sql index aa7c36b..86a5d36 100644 --- a/database/0.6/1713289062969-user/Up/01-table/User.sql +++ b/database/0.6/1713289062969-user/Up/01-table/User.sql @@ -2,7 +2,6 @@ CREATE TABLE `user` ( `Id` varchar(255) NOT NULL, `WhenCreated` datetime NOT NULL, `WhenUpdated` datetime NOT NULL, - `UserId` varchar(255) NOT NULL, `Currency` int NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file diff --git a/src/database/entities/app/User.ts b/src/database/entities/app/User.ts index 26d1303..c954aea 100644 --- a/src/database/entities/app/User.ts +++ b/src/database/entities/app/User.ts @@ -1,31 +1,19 @@ import { Column, Entity } from "typeorm"; import AppBaseEntity from "../../../contracts/AppBaseEntity"; -import AppDataSource from "../../dataSources/appDataSource"; @Entity() export default class User extends AppBaseEntity { constructor(userId: string, currency: number) { super(); - this.UserId = userId; + this.Id = userId; this.Currency = currency; } - @Column() - UserId: string; - @Column() Currency: number; public UpdateCurrency(currency: number) { this.Currency = currency; } - - public static async FetchOneByUserId(userId: string): Promise { - const repository = AppDataSource.getRepository(User); - - const single = await repository.findOne({ where: { UserId: userId }}); - - return single; - } } \ No newline at end of file -- 2.43.4