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