logologo
开始
手册
开发
插件
API
English
简体中文
开始
手册
开发
插件
API
English
简体中文
logologo
API Overview

@nocobase/auth

AuthManager
Auth
BaseAuth

@nocobase/cache

CacheManager
Cache

@nocobase/cli

NocoBase CLI
全局环境变量

@nocobase/client

Application
Plugin

@nocobase/database

Collection
Field

interfaces

BaseInterface
Filter Operators

RelationRepository

BelongsToManyRepository
belongs-to-repository
HasManyRepository
HasOneRepository
Repository

shared

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

@nocobase/data-source-manager

DataSourceManager
DataSource (abstract)
ICollectionManager
ICollection
IField
IModel
IRepository

@nocobase/flow-engine

DataSourceManager
FlowContext
FlowEngine
FlowModel
FlowResource

@nocobase/logger

Logger

@nocobase/server

AppCommand
Application
AuditManager
Context
Migration
Plugin

@nocobase/sdk

Auth
Storage
Previous PageHasManyRepository
Next PageRepository

#HasOneRepository

#概览

HasOneRepository 为 HasOne 类型的关联 Repository。

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

// 获取到关联 Repository
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