From 14dbd7056f8b99f6f3cfa73fd7b75d86e0ae9ccc Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Fri, 1 Oct 2021 20:14:34 +0200 Subject: [PATCH] User model --- backend/src/typeorm/entity/User.ts | 21 --------------------- backend/src/typeorm/repository/User.ts | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 backend/src/typeorm/repository/User.ts diff --git a/backend/src/typeorm/entity/User.ts b/backend/src/typeorm/entity/User.ts index 84a40a5cd..ad76bd4a3 100644 --- a/backend/src/typeorm/entity/User.ts +++ b/backend/src/typeorm/entity/User.ts @@ -28,25 +28,4 @@ export class User extends BaseEntity { @Column() disabled: boolean - - // Moriz: I am voting for the data mapper implementation. - // see: https://typeorm.io/#/active-record-data-mapper/what-is-the-data-mapper-pattern - // We should discuss this ASAP - static findByPubkeyHex(pubkeyHex: string): Promise { - return this.createQueryBuilder('user') - .where('hex(user.pubkey) = :pubkeyHex', { pubkeyHex }) - .getOneOrFail() - } - - static async getUsersIndiced(userIds: number[]): Promise { - const users = await this.createQueryBuilder('user') - .select(['user.id', 'user.firstName', 'user.lastName', 'user.email']) - .where('user.id IN (:...users)', { users: userIds }) - .getMany() - const usersIndiced: User[] = [] - users.forEach((value) => { - usersIndiced[value.id] = value - }) - return usersIndiced - } } diff --git a/backend/src/typeorm/repository/User.ts b/backend/src/typeorm/repository/User.ts new file mode 100644 index 000000000..2a2299a4c --- /dev/null +++ b/backend/src/typeorm/repository/User.ts @@ -0,0 +1,23 @@ +import { EntityRepository, Repository } from 'typeorm' +import { User } from '../entity/User' + +@EntityRepository(User) +export class UserRepository extends Repository { + async findByPubkeyHex(pubkeyHex: string): Promise { + return this.createQueryBuilder('user') + .where('hex(user.pubkey) = :pubkeyHex', { pubkeyHex }) + .getOneOrFail() + } + + async getUsersIndiced(userIds: number[]): Promise { + const users = await this.createQueryBuilder('user') + .select(['user.id', 'user.firstName', 'user.lastName', 'user.email']) + .where('user.id IN (:...users)', { users: userIds }) + .getMany() + const usersIndiced: User[] = [] + users.forEach((value) => { + usersIndiced[value.id] = value + }) + return usersIndiced + } +}