logologo
スタート
マニュアル
開発
プラグイン
API
ホーム
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
スタート
マニュアル
開発
プラグイン
API
ホーム
logologo
API概要

@nocobase/auth

AuthManager
Auth
BaseAuth

@nocobase/cache

CacheManager
キャッシュ

@nocobase/cli

NocoBase CLI
グローバル環境変数

@nocobase/client

アプリケーション
プラグイン

@nocobase/database

コレクション
フィールド

interfaces

BaseInterface
フィルター演算子

RelationRepository

BelongsToManyRepository
belongs-to-repository
HasManyRepository
HasOneRepository
リポジトリ

shared

create-options
destroy-options
find-one
find-options
transaction
update-options

@nocobase/data-source-manager

DataSourceManager
DataSource (抽象)
ICollectionManager
ICollection
IField
IModel
IRepository

@nocobase/flow-engine

データソースマネージャー
フローコンテキスト
FlowEngine
フローモデル
フローリソース

@nocobase/logger

ロガー

@nocobase/server

AppCommand
アプリケーション
AuditManager
コンテキスト
マイグレーション
プラグイン

@nocobase/sdk

Auth
ストレージ
Previous PageHasManyRepository
Next Pageリポジトリ
TIP

このドキュメントはAIによって翻訳されました。不正確な情報については、英語版をご参照ください

#HasOneRepository

#概要

HasOneRepository は、HasOne タイプの関連リポジトリです。

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' },
});

// 関連リポジトリを取得します
const userProfileRepository = User.repository
  .relation('profile')
  .of(user.get('id'));

// 直接初期化することも可能です
new HasOneRepository(User, 'profile', user.get('id'));

#クラスメソッド

#find()

関連オブジェクトを検索します。

シグネチャ

  • async find(options?: SingleRelationFindOption): Promise<Model<any> | null>

型

interface SingleRelationFindOption extends Transactionable {
  fields?: Fields;
  except?: Except;
  appends?: Appends;
  filter?: Filter;
}

詳細

クエリパラメータは Repository.find() と同じです。

例

const profile = await UserProfileRepository.find();
// 関連オブジェクトが存在しない場合は、null を返します

#create()

関連オブジェクトを作成します。

シグネチャ

  • async create(options?: CreateOptions): Promise<Model>

例

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()

関連オブジェクトを更新します。

シグネチャ

  • async update(options: UpdateOptions): Promise<Model>

例

const profile = await UserProfileRepository.update({
  values: { avatar: 'avatar2' },
});

profile.get('avatar'); // 'avatar2'

#remove()

関連オブジェクトを削除します。これは関連付けを解除するだけで、関連オブジェクト自体は削除しません。

シグネチャ

  • async remove(options?: Transactionable): Promise<void>

詳細

  • transaction: トランザクションオブジェクトです。トランザクションパラメータが渡されない場合、このメソッドは自動的に内部トランザクションを作成します。

例

await UserProfileRepository.remove();
(await UserProfileRepository.find()) == null; // true

(await Profile.repository.count()) === 1; // true

#destroy()

関連オブジェクトを削除します。

シグネチャ

  • async destroy(options?: Transactionable): Promise<Boolean>

詳細

  • transaction: トランザクションオブジェクトです。トランザクションパラメータが渡されない場合、このメソッドは自動的に内部トランザクションを作成します。

例

await UserProfileRepository.destroy();
(await UserProfileRepository.find()) == null; // true
(await Profile.repository.count()) === 0; // true

#set()

関連オブジェクトを設定します。

シグネチャ

  • async set(options: TargetKey | SetOption): Promise<void>

型

interface SetOption extends Transactionable {
  tk?: TargetKey;
}

詳細

  • tk: 設定する関連オブジェクトの targetKey です。
  • transaction: トランザクションオブジェクトです。トランザクションパラメータが渡されない場合、このメソッドは自動的に内部トランザクションを作成します。

例

const newProfile = await Profile.repository.create({
  values: { avatar: 'avatar2' },
});

await UserProfileRepository.set(newProfile.get('id'));

(await UserProfileRepository.find()).get('id') === newProfile.get('id'); // true