---
lang: en
title: 'API docs: repository.createhasonerepositoryfactory'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/repository
permalink: /doc/en/lb4/apidocs.repository.createhasonerepositoryfactory.html
---

<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@loopback/repository](./repository.md) &gt; [createHasOneRepositoryFactory](./repository.createhasonerepositoryfactory.md)

## createHasOneRepositoryFactory() function

Enforces a constraint on a repository based on a relationship contract between models. For example, if a Customer model is related to an Address model via a HasOne relation, then, the relational repository returned by the factory function would be constrained by a Customer model instance's id(s).

If the target model is polymorphic, i.e. stored within different repositories, supply the targetRepositoryGetter with a dictionary in the form of {<!-- -->\[typeName: string\]: repositoryGetter<!-- -->}

**Signature:**

```typescript
export declare function createHasOneRepositoryFactory<Target extends Entity, TargetID, ForeignKeyType>(relationMetadata: HasOneDefinition, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>> | {
    [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>;
}): HasOneRepositoryFactory<Target, ForeignKeyType>;
```

## Parameters

|  Parameter | Type | Description |
|  --- | --- | --- |
|  relationMetadata | [HasOneDefinition](./repository.hasonedefinition.md) | The relation metadata used to describe the relationship and determine how to apply the constraint. |
|  targetRepositoryGetter | Getter&lt;[EntityCrudRepository](./repository.entitycrudrepository.md)<!-- -->&lt;Target, TargetID&gt;&gt; \| { \[repoType: string\]: Getter&lt;[EntityCrudRepository](./repository.entitycrudrepository.md)<!-- -->&lt;Target, TargetID&gt;&gt;; } | The repository or a dictionary of classname - repository, which represents the target model of a relation attached to a datasource. For the dictionary, the key is the class name of the concrete class the the polymorphic model. |

**Returns:**

[HasOneRepositoryFactory](./repository.hasonerepositoryfactory.md)<!-- -->&lt;Target, ForeignKeyType&gt;

The factory function which accepts a foreign key value to constrain the given target repository


