logologo
البدء
الدليل
التطوير
الإضافات
API
English
简体中文
日本語
한국어
Deutsch
Français
Español
Português
Русский
Italiano
Türkçe
Українська
Tiếng Việt
Bahasa Indonesia
ไทย
Polski
Nederlands
Čeština
العربية
עברית
हिन्दी
Svenska
البدء
الدليل
التطوير
الإضافات
API
logologo
API Overview

@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
المستودع (Repository)

shared

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

@nocobase/data-source-manager

DataSourceManager
DataSource (تجريدي)
ICollectionManager
ICollection
IField
IModel
IRepository

@nocobase/flow-engine

مدير مصدر البيانات
سياق سير العمل
FlowEngine
FlowModel
مصدر سير العمل

@nocobase/logger

المسجل

@nocobase/server

AppCommand
التطبيق
AuditManager
السياق
الهجرة
إضافة

@nocobase/sdk

Auth
التخزين

@nocobase/telemetry

المقياس
Telemetry
التتبع
Previous PageHasManyRepository
Next Pageالمستودع (Repository)
إشعار الترجمة بالذكاء الاصطناعي

تمت ترجمة هذه الوثائق تلقائيًا بواسطة الذكاء الاصطناعي.

#HasOneRepository

#نظرة عامة

HasOneRepository هو المستودع (repository) الخاص بالارتباطات من نوع 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'));

#توابع الفئة (Class Methods)

#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: كائن المعاملة (transaction). إذا لم يتم تمرير معاملة، فسيقوم التابع بإنشاء معاملة داخلية تلقائيًا.

مثال

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

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

#destroy()

حذف الكائن المرتبط

التوقيع

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

التفاصيل

  • transaction: كائن المعاملة (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: كائن المعاملة (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