User model

This commit is contained in:
Ulf Gebhardt 2021-10-01 20:14:34 +02:00
parent 9ff3632021
commit 14dbd7056f
No known key found for this signature in database
GPG Key ID: 81308EFE29ABFEBD
2 changed files with 23 additions and 21 deletions

View File

@ -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<User> {
return this.createQueryBuilder('user')
.where('hex(user.pubkey) = :pubkeyHex', { pubkeyHex })
.getOneOrFail()
}
static async getUsersIndiced(userIds: number[]): Promise<User[]> {
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
}
}

View File

@ -0,0 +1,23 @@
import { EntityRepository, Repository } from 'typeorm'
import { User } from '../entity/User'
@EntityRepository(User)
export class UserRepository extends Repository<User> {
async findByPubkeyHex(pubkeyHex: string): Promise<User> {
return this.createQueryBuilder('user')
.where('hex(user.pubkey) = :pubkeyHex', { pubkeyHex })
.getOneOrFail()
}
async getUsersIndiced(userIds: number[]): Promise<User[]> {
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
}
}