import { Relations } from "./relations.js" ; import { Connector } from "../connection/connector.js" ; import { ModelInterface , Relation } from "../models/model.interface.js" ; export class HasMany extends Relations { constructor ( public db , public connector , protected parentModel , public childRelation ) { super ( ) ; } param results public async fetch ( results ) { let model = this . getRelationModel ( this . childRelation ) ; model = this . filteredModel ( model , this . childRelation ) ; const values = results . map ( result => result [ this . getLocalKey ( this . parentModel , this . childRelation ) ] ) ; model . whereIndexIn ( this . childRelation . foreignKey , values ) ; const relationResults = await model . all ( ) ; return this . bindResults ( results , relationResults , this . childRelation ) ; } param parentResults param relationResults param relation public bindResults ( parentResults , relationResults , relation ) { const localKey = this . getLocalKey ( this . parentModel , this . childRelation ) ; parentResults . forEach ( parentResult => { parentResult [ this . getAttributeName ( this . parentModel , relation ) ] = relationResults . filter ( ( relationResult ) => relationResult [ relation . foreignKey ] === parentResult [ localKey ] ) ; } ) ; return Promise . resolve ( parentResults ) ; } }