You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1006 B
32 lines
1006 B
import { EntityRepository, FindOneOptions, Repository } from "typeorm"; |
|
|
|
import { User } from "../entities"; |
|
|
|
export interface FindUserOptions { |
|
devices: boolean; |
|
} |
|
|
|
function applyDefaultOptions(options?: Partial<FindUserOptions>): FindOneOptions<User> { |
|
const opts: FindUserOptions = { devices: false, ...options }; |
|
const relations = [opts.devices && "devices"] |
|
.filter(Boolean) as string[]; |
|
return { relations }; |
|
} |
|
|
|
@EntityRepository(User) |
|
export class UserRepository extends Repository<User> { |
|
findAll(options?: Partial<FindUserOptions>) { |
|
const opts = applyDefaultOptions(options); |
|
return super.find(opts); |
|
} |
|
|
|
findById(id: number, options?: Partial<FindUserOptions>) { |
|
const opts = applyDefaultOptions(options); |
|
return super.findOne(id, opts); |
|
} |
|
|
|
findByUsername(username: string, options?: Partial<FindUserOptions>) { |
|
const opts = applyDefaultOptions(options); |
|
return this.findOne({ username }, opts); |
|
} |
|
}
|
|
|