指南 參考 原始碼
public class | 來源

HasMany

繼承

Association → HasMany

一對多關聯

在以下的 API 參考中,將關聯的名稱加入方法中,例如 User.hasMany(Project),則 getter 會是 user.getProjects()。如果關聯有別名,則改用別名,例如 User.hasMany(Project, { as: 'jobs' }),則會是 user.getJobs()

請參閱

方法摘要

公用方法
public

async add(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise

將一個或多個目標列與 this 關聯。

public

async count(instance: Model, options: object): Promise<number>

計算目前與此關聯的所有項目,可使用可選的 where 子句。

public

async create(sourceInstance: Model, values: object, options: object): Promise

建立相關模型的新實例,並將其與此關聯。

public

async get(instances: Model | Array<Model>, options: object): Promise<Array<Model>>

使用可選的 where 子句,取得目前與此關聯的所有項目。

public

async has(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise

檢查一個或多個列是否與 this 關聯。

public

async remove(sourceInstance: Model, targetInstances: Model | Model[] | string | string[] | number | number[], options: object): Promise

取消關聯一個或多個目標列。

public

async set(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise

通過傳遞持久化實例或其主鍵的陣列來設定關聯的模型。

繼承摘要

來自類別 Association
public

關聯的類型。

public
public

公用方法

public async add(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise 來源

將一個或多個目標列與 this 關聯。此方法接受 Model / 字串 / 數字來關聯單列,或接受 Model / 字串 / 數字的混合陣列來關聯多列。

參數

名稱類型屬性描述
sourceInstance Model

來源實例

targetInstances Model | Model[] | string[] | string | number[] | number
  • 選用

單一實例或主鍵,或是持久化實例或主鍵的混合陣列

options object
  • 選用

傳遞給 target.update 的選項。

回傳

Promise

public async count(instance: Model, options: object): Promise<number> 來源

計算目前與此關聯的所有項目,可使用可選的 where 子句。

參數

名稱類型屬性描述
實例 Model

來源實例

options object
  • 選用

尋找與計數選項

options.where object
  • 選用

用於限制關聯模型的可選 where 子句

options.scope string | boolean
  • 選用

在相關模型上套用作用域,或傳遞 false 來移除其預設作用域

回傳

Promise<number>

public async create(sourceInstance: Model, values: object, options: object): Promise 來源

建立相關模型的新實例,並將其與此關聯。

參數

名稱類型屬性描述
sourceInstance Model

來源實例

數值 object
  • 選用

目標模型實例的數值

options object
  • 選用

傳遞給 target.create 的選項

回傳

Promise

public async get(instances: Model | Array<Model>, options: object): Promise<Array<Model>> 來源

使用可選的 where 子句,取得目前與此關聯的所有項目。

參數

名稱類型屬性描述
實例 Model | Array<Model>

來源實例

options object
  • 選用

尋找選項

options.where object
  • 選用

用於限制關聯模型的可選 where 子句

options.scope string | boolean
  • 選用

在相關模型上套用作用域,或傳遞 false 來移除其預設作用域

options.schema 字串
  • 選用

在相關模型上套用架構

回傳

Promise<Array<Model>>

請參閱

public async has(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise source

檢查一個或多個列是否與 this 關聯。

參數

名稱類型屬性描述
sourceInstance Model

來源實例

targetInstances Model | Model[] | string[] | string | number[] | number
  • 選用

可以是實例的陣列或它們的主鍵。

options object
  • 選用

傳遞給 getAssociations 的選項。

回傳

Promise

public async remove(sourceInstance: Model, targetInstances: Model | Model[] | string | string[] | number | number[], options: object): Promise source

取消關聯一個或多個目標列。

參數

名稱類型屬性描述
sourceInstance Model

要取消關聯的實例。

targetInstances Model | Model[] | string | string[] | number | number[]
  • 選用

可以是實例或其主鍵,或實例和主鍵的混合陣列。

options object
  • 選用

傳遞給 target.update 的選項。

回傳

Promise

public async set(sourceInstance: Model, targetInstances: Model | Model[] | string[] | string | number[] | number, options: object): Promise source

通過傳遞持久化實例或其主鍵的陣列來設定關聯的模型。不在傳遞陣列中的所有內容都將取消關聯。

參數

名稱類型屬性描述
sourceInstance Model

要與新實例關聯的源實例。

targetInstances Model | Model[] | string[] | string | number[] | number
  • 選用

要與此關聯的持久化實例或實例主鍵的陣列。傳遞 nullundefined 以移除所有關聯。

options object
  • 選用

傳遞給 target.findAllupdate 的選項。

options.validate object
  • 選用

為聯結模型執行驗證。

回傳

Promise