0

I'm developing a account management system using loopback4

I have defined following relationships on my models properly.

Owner hasMany Transaction
Transaction belongsTo owner

Transaction belongsTo Bank

I want to find all transactions belongs to one owner and include Bank details in response object. This is what I try to do

// OwnerRepository
// owner.repository.ts

const transList = this.transactionRepository.find({ include: [Bank] })

But I couldn't figure out how include filter works. All relations are working fine. I'm using mongodb for database.

Please explain me how to use the include filter properly.

thanks

Salitha
  • 1,022
  • 1
  • 12
  • 32

2 Answers2

0

its seems not be implemented yet https://github.com/strongloop/loopback-next/issues/1889 https://github.com/strongloop/loopback-next/issues/1352

You have to do the filter by your self: Example:

async find( @param.query.object('filter', getFilterSchemaFor(Cuenta)) filter?: Filter<Cuenta>): Promise<any[]> {

     let cuenta = await this.cuentaRepository.find(filter);
     console.log(cuenta);
     return this.populateDuenio(cuenta);
}

async populateDuenio(cuentas: Cuenta[]) :Promise<any[]>{
    let cuentasPopulate = [];
    for (let i:number = 0; i < cuentas.length; i++) {
       let persona = await this.cuentaRepository.persona(cuentas[i].id);
       cuentasPopulate.push( {
          ...cuentas[i],
          personaId:{
          ...persona
        }
     })
 }
   return cuentasPopulate;
}
0

You can overwrite the where method property of the filter object and use the inq parameter to filter.

Basically something like this:

filter.where = {id: { inq: objectAsPerId }};
and then
const transList = this.transactionRepository.find(filter);

which will basically override the where property as per your requirement. and in this scenario, if you have also passed some other filter like limit, offset they will still exist in the filter.

Hopefully, this helps.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Yash Rahurikar
  • 186
  • 1
  • 10