MCPcopy
hub / github.com/eveningkid/denodb / hasMany

Method hasMany

lib/model.ts:863–892  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 10

formatFieldToDatabaseMethod · 0.95
fieldMethod · 0.80
getComputedPrimaryKeyMethod · 0.80
queryForSchemaMethod · 0.80
getMethod · 0.45
joinMethod · 0.45
whereMethod · 0.45
allMethod · 0.45

Tested by

no test coverage detected