nocobase/docs/en-US/api/database/relation-repository/has-one-repository.md

185 lines
3.6 KiB
Markdown
Raw Normal View History

# HasOneRepository
2022-11-13 15:00:59 +00:00
2023-01-26 14:47:12 +00:00
## Overview
`HasOneRepository` is the associated repository of type `HasOne`.
```typescript
const User = db.collection({
name: 'users',
fields: [
{ type: 'hasOne', name: 'profile' },
{ type: 'string', name: 'name' },
],
});
const Profile = db.collection({
name: 'profiles',
fields: [{ type: 'string', name: 'avatar' }],
});
const user = await User.repository.create({
values: { name: 'u1' },
});
2023-01-26 14:47:12 +00:00
// Get the associated repository
2022-11-13 15:00:59 +00:00
const userProfileRepository = User.repository.relation('profile').of(user.get('id'));
2023-01-26 14:47:12 +00:00
// Or to initialize directly
2022-11-13 15:00:59 +00:00
new HasOneRepository(User, 'profile', user.get('id'));
```
2023-02-05 04:35:24 +00:00
## Class Methods
### `find()`
2023-01-26 14:52:04 +00:00
Find associated objects.
2023-01-26 14:47:12 +00:00
**Signature**
* `async find(options?: SingleRelationFindOption): Promise<Model<any> | null>`
2023-01-26 14:47:12 +00:00
**Type**
```typescript
interface SingleRelationFindOption extends Transactionable {
fields?: Fields;
except?: Except;
appends?: Appends;
filter?: Filter;
}
```
2023-01-26 14:47:12 +00:00
**Detailed Information**
2023-01-26 14:47:12 +00:00
Query parameters are the same as [`Repository.find()`](../repository.md#find).
2023-01-26 14:47:12 +00:00
**Example**
```typescript
const profile = await UserProfileRepository.find();
2023-01-26 14:47:12 +00:00
// Return null if the associated object does not exist
```
### `create()`
2023-01-26 14:52:04 +00:00
Create associated objects.
2023-01-26 14:47:12 +00:00
**Signature**
* `async create(options?: CreateOptions): Promise<Model>`
<embed src="../shared/create-options.md"></embed>
2023-01-26 14:47:12 +00:00
**Example**
```typescript
const profile = await UserProfileRepository.create({
values: { avatar: 'avatar1' },
});
console.log(profile.toJSON());
/*
{
id: 1,
avatar: 'avatar1',
userId: 1,
updatedAt: 2022-09-24T13:59:40.025Z,
createdAt: 2022-09-24T13:59:40.025Z
}
*/
```
### `update()`
2023-01-26 14:52:04 +00:00
Update associated objects.
2023-01-26 14:47:12 +00:00
**Signature**
* `async update(options: UpdateOptions): Promise<Model>`
<embed src="../shared/update-options.md"></embed>
2023-01-26 14:47:12 +00:00
**Example**
```typescript
const profile = await UserProfileRepository.update({
values: { avatar: 'avatar2' },
});
profile.get('avatar'); // 'avatar2'
```
### `remove()`
2023-01-26 14:52:04 +00:00
Remove associated objects. Only to unassociate, not to delete the associated object.
2023-01-26 14:47:12 +00:00
**Signature**
* `async remove(options?: Transactionable): Promise<void>`
2023-01-26 14:47:12 +00:00
**Detailed Information**
2023-01-26 14:47:12 +00:00
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
2023-01-26 14:47:12 +00:00
**Example**
```typescript
await UserProfileRepository.remove();
await UserProfileRepository.find() == null; // true
await Profile.repository.count() === 1; // true
```
### `destroy()`
2023-01-26 14:52:04 +00:00
Delete associated objects.
2023-01-26 14:47:12 +00:00
**Signature**
* `async destroy(options?: Transactionable): Promise<Boolean>`
2023-01-26 14:47:12 +00:00
**Detailed Information**
2023-01-26 14:47:12 +00:00
* `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
2023-01-26 14:47:12 +00:00
**Example**
```typescript
await UserProfileRepository.destroy();
await UserProfileRepository.find() == null; // true
await Profile.repository.count() === 0; // true
```
### `set()`
2023-01-26 14:52:04 +00:00
Set associated objects.
2023-01-26 14:47:12 +00:00
**Signature**
* `async set(options: TargetKey | SetOption): Promise<void>`
2023-01-26 14:47:12 +00:00
**Type**
```typescript
interface SetOption extends Transactionable {
tk?: TargetKey;
}
````
2023-01-26 14:47:12 +00:00
**Detailed Information**
2023-01-26 14:47:12 +00:00
* tk: Set the targetKey of the associated object.
* transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
2023-01-26 14:47:12 +00:00
**Example**
```typescript
const newProfile = await Profile.repository.create({
values: { avatar: 'avatar2' },
});
await UserProfileRepository.set(newProfile.get('id'));
(await UserProfileRepository.find()).get('id') === newProfile.get('id'); // true
```