Find associated values for the given model for one-to-many and many-to-many relationships. * * class Airport { * static flights() { * return this.hasMany(Flight); * } * } * * Airport.where("id", "1").flights();
(
this: T,
model: ModelSchema,
)
| 861 | * Airport.where("id", "1").flights(); |
| 862 | */ |
| 863 | static hasMany<T extends ModelSchema>( |
| 864 | this: T, |
| 865 | model: ModelSchema, |
| 866 | ): Promise<Model | Model[]> { |
| 867 | const currentWhereValue = this._findCurrentQueryWhereClause(); |
| 868 | |
| 869 | if (model.name in this.pivot) { |
| 870 | const pivot = this.pivot[model.name]; |
| 871 | const pivotField = this.formatFieldToDatabase( |
| 872 | pivot._pivotsFields[this.name], |
| 873 | ) as string; |
| 874 | const pivotOtherModel = pivot._pivotsModels[model.name]; |
| 875 | const pivotOtherModelField = pivotOtherModel.formatFieldToDatabase( |
| 876 | pivot._pivotsFields[model.name], |
| 877 | ) as string; |
| 878 | |
| 879 | return pivot |
| 880 | .where(pivot.field(pivotField), currentWhereValue) |
| 881 | .join( |
| 882 | pivotOtherModel, |
| 883 | pivotOtherModel.field(pivotOtherModel.getComputedPrimaryKey()), |
| 884 | pivot.field(pivotOtherModelField), |
| 885 | ) |
| 886 | .get(); |
| 887 | } |
| 888 | |
| 889 | const foreignKeyName = this._findModelForeignKeyField(model); |
| 890 | this._currentQuery = this._queryBuilder.queryForSchema(this); |
| 891 | return model.where(foreignKeyName, currentWhereValue).all(); |
| 892 | } |
| 893 | |
| 894 | /** Find associated values for the given model for one-to-one and one-to-many relationships. */ |
| 895 | static async hasOne<T extends ModelSchema>(this: T, model: ModelSchema) { |
nothing calls this directly
no test coverage detected