Update has-one-repository.md

This commit is contained in:
Pearl C 2023-01-26 22:47:12 +08:00 committed by GitHub
parent dd2a092f2d
commit e447c5e2ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,8 @@
# HasOneRepository # HasOneRepository
## 概览
`HasOneRepository``HasOne` 类型的关联 Repository。 ## Overview
`HasOneRepository` is the associated repository of type `HasOne`.
```typescript ```typescript
const User = db.collection({ const User = db.collection({
@ -21,28 +22,24 @@ const user = await User.repository.create({
values: { name: 'u1' }, values: { name: 'u1' },
}); });
// Get the associated repository
// 获取到关联 Repository
const userProfileRepository = User.repository.relation('profile').of(user.get('id')); const userProfileRepository = User.repository.relation('profile').of(user.get('id'));
// 也可直接初始化 // Or to initialize directly
new HasOneRepository(User, 'profile', user.get('id')); new HasOneRepository(User, 'profile', user.get('id'));
``` ```
## 类方法 ## Class Method
### `find()` ### `find()`
查找关联对象 Find associated object.
**签名** **Signature**
* `async find(options?: SingleRelationFindOption): Promise<Model<any> | null>` * `async find(options?: SingleRelationFindOption): Promise<Model<any> | null>`
**类型** **Type**
```typescript ```typescript
interface SingleRelationFindOption extends Transactionable { interface SingleRelationFindOption extends Transactionable {
@ -53,27 +50,28 @@ interface SingleRelationFindOption extends Transactionable {
} }
``` ```
**详细信息** **Detailed Information**
查询参数与 [`Repository.find()`](../repository.md#find) 一致。 Query parameters are the same as [`Repository.find()`](../repository.md#find).
**示例** **Example**
```typescript ```typescript
const profile = await UserProfileRepository.find(); const profile = await UserProfileRepository.find();
// 关联对象不存在时,返回 null // Return null if the associated object does not exist
``` ```
### `create()` ### `create()`
创建关联对象
**签名** Create associated object.
**Signature**
* `async create(options?: CreateOptions): Promise<Model>` * `async create(options?: CreateOptions): Promise<Model>`
<embed src="../shared/create-options.md"></embed> <embed src="../shared/create-options.md"></embed>
**示例** **Example**
```typescript ```typescript
const profile = await UserProfileRepository.create({ const profile = await UserProfileRepository.create({
@ -95,16 +93,15 @@ console.log(profile.toJSON());
### `update()` ### `update()`
更新关联对象 Update associated object.
**签名** **Signature**
* `async update(options: UpdateOptions): Promise<Model>` * `async update(options: UpdateOptions): Promise<Model>`
<embed src="../shared/update-options.md"></embed> <embed src="../shared/update-options.md"></embed>
**Example**
**示例**
```typescript ```typescript
const profile = await UserProfileRepository.update({ const profile = await UserProfileRepository.update({
@ -116,17 +113,17 @@ profile.get('avatar'); // 'avatar2'
### `remove()` ### `remove()`
移除关联对象,仅解除关联关系,不删除关联对象 Remove associated object. Only to unassociate, not to delete the associated object.
**签名** **Signature**
* `async remove(options?: Transactionable): Promise<void>` * `async remove(options?: Transactionable): Promise<void>`
**详细信息** **Detailed Information**
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。 * `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**示例** **Example**
```typescript ```typescript
await UserProfileRepository.remove(); await UserProfileRepository.remove();
@ -137,18 +134,18 @@ await Profile.repository.count() === 1; // true
### `destroy()` ### `destroy()`
删除关联对象 Delete associated object.
**签名** **Signature**
* `async destroy(options?: Transactionable): Promise<Boolean>` * `async destroy(options?: Transactionable): Promise<Boolean>`
**详细信息** **Detailed Information**
* `transaction`: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。 * `transaction`: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**示例** **Example**
```typescript ```typescript
await UserProfileRepository.destroy(); await UserProfileRepository.destroy();
@ -158,25 +155,25 @@ await Profile.repository.count() === 0; // true
### `set()` ### `set()`
设置关联对象 Set associated object.
**签名** **Signature**
* `async set(options: TargetKey | SetOption): Promise<void>` * `async set(options: TargetKey | SetOption): Promise<void>`
**类型** **Type**
```typescript ```typescript
interface SetOption extends Transactionable { interface SetOption extends Transactionable {
tk?: TargetKey; tk?: TargetKey;
} }
```` ````
**详细信息** **Detailed Information**
* tk: 设置关联对象的 targetKey * tk: Set the targetKey of the associated object.
* transaction: 事务对象。如果没有传入事务参数,该方法会自动创建一个内部事务。 * transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.
**示例** **Example**
```typescript ```typescript
const newProfile = await Profile.repository.create({ const newProfile = await Profile.repository.create({