nocobase/docs/en-US/api/database/relation-repository/has-one-repository.md
2023-01-26 23:01:51 +08:00

3.6 KiB

HasOneRepository

Overview

HasOneRepository is the associated repository of type 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' },
});

// Get the associated repository
const userProfileRepository = User.repository.relation('profile').of(user.get('id'));

// Or to initialize directly
new HasOneRepository(User, 'profile', user.get('id'));

Class Method

find()

Find associated objects.

Signature

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

Type

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

Detailed Information

Query parameters are the same as Repository.find().

Example

const profile = await UserProfileRepository.find();
// Return null if the associated object does not exist

create()

Create associated objects.

Signature

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

Example

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

Update associated objects.

Signature

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

Example

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

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

remove()

Remove associated objects. Only to unassociate, not to delete the associated object.

Signature

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

Detailed Information

  • transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.

Example

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

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

destroy()

Delete associated objects.

Signature

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

Detailed Information

  • transaction: Transaction object. If no transaction parameter is passed, the method will automatically create an internal transaction.

Example

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

set()

Set associated objects.

Signature

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

Type

interface SetOption extends Transactionable {
  tk?: TargetKey;
}

Detailed Information

  • 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.

Example

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

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

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