import { EntityRepository, FindOneOptions, Repository } from "typeorm"; import { User } from "../entities"; export interface FindUserOptions { devices: boolean; } function applyDefaultOptions(options?: Partial): FindOneOptions { const opts: FindUserOptions = { devices: false, ...options }; const relations = [opts.devices && "devices"] .filter(Boolean) as string[]; return { relations }; } @EntityRepository(User) export class UserRepository extends Repository { findAll(options?: Partial) { const opts = applyDefaultOptions(options); return super.find(opts); } findById(id: number, options?: Partial) { const opts = applyDefaultOptions(options); return super.findOne(id, opts); } findByUsername(username: string, options?: Partial) { const opts = applyDefaultOptions(options); return this.findOne({ username }, opts); } }