import { Getter , inject } from '@loopback/core' ; import { DefaultCrudRepository , HasOneRepositoryFactory , juggler , repository , } from '@loopback/repository' ; import { UserServiceBindings } from '../keys' ; import { User , UserCredentials , UserRelations } from '../models' ; import { UserCredentialsRepository } from './user-credentials.repository' ; export class UserRepository extends DefaultCrudRepository < User , typeof User . prototype . id , UserRelations > { public readonly userCredentials : HasOneRepositoryFactory < UserCredentials , typeof User . prototype . id > ; constructor ( @ inject ( `datasources.${ UserServiceBindings . DATASOURCE_NAME }` ) dataSource , @ repository . getter ( 'UserCredentialsRepository' ) protected userCredentialsRepositoryGetter ) { super ( User , dataSource ) ; this . userCredentials = this . createHasOneRepositoryFactoryFor ( 'userCredentials' , userCredentialsRepositoryGetter ) ; this . registerInclusionResolver ( 'userCredentials' , this . userCredentials . inclusionResolver ) ; } async findCredentials ( userId ) { try { return await this . userCredentials ( userId ) . get ( ) ; } catch ( err ) { if ( err . code === 'ENTITY_NOT_FOUND' ) { return undefined ; } throw err ; } } }