objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Nested relation childs count

Open abdusamadtv opened this issue 6 years ago • 9 comments

I have a base query variable which is growing according to request params

let query = Client.query()
  .withGraphFetched(
    '[documents.[files], loans(onlyActiveLoansByMfo).[documents.[files]]]',
  )
  .modifiers({
    onlyActiveLoansByMfo(builder) {
      builder.whereIn('filialCode', mfos).andWhere('condition', '1')
    },
  })

like this

if (user.mfoAccesses.length !== 0 && mfos.length !== allMfos.length) {
  // Hasn't access to all mfos
  query = query.whereExists(
    Client.relatedQuery('accounts')
      .whereIn('filialCode', mfos)
      .andWhere('number', 'like', '202%')
      .andWhere(function() {
        this.where('status', 'M').orWhere('status', 'O')
      }),
  )

  const ClientDocument = require('~root/models/clientDocument')

  if (params.withStatistic) {
    statistic.clientDocumentsTotal = await ClientDocument.query().whereIn('clientCode', mfos)
    .andWhere('condition', '1').count()
    
    statistic.loanDocumentFilesTotal = await Loan.relatedQuery('documents').whereIn('filialCode', mfos)
    .andWhere('condition', '1').whereExists().count()
  }
}

at the end of this code I have unfinished queries where I want to get all clients (of above query) documents total amount and total amount of documents with files.

my relations schema: client has many documents and loans, documents and loans has many to many files relationship

abdusamadtv avatar Jan 13 '20 13:01 abdusamadtv

Cool, looks good.

koskimas avatar Jan 13 '20 13:01 koskimas

Cool, looks good.

@koskimas can you help me with a query? I need to count client and loan documents total and also them which have files. clients also have hasMany loans relation

abdusamadtv avatar Jan 14 '20 03:01 abdusamadtv

I don't understand what you are after. I can't help if you can't explain the problem better.

koskimas avatar Jan 15 '20 11:01 koskimas

I have clients query

Client.query().whereExists(
    Client.relatedQuery('accounts')
      .whereIn('filialCode', mfos)
      .andWhere('number', 'like', '202%')
      .andWhere(function() {
        this.where('status', 'M').orWhere('status', 'O')
      })

I want to get these clients related documents without and with files total count. Not separately for each client, but the total. Same for loans which also have documents relation

abdusamadtv avatar Jan 16 '20 04:01 abdusamadtv

I still don't know if I understood, but how about this?

const clientsQuery = Client.query().whereExists(
    Client.relatedQuery('accounts')
      .whereIn('filialCode', mfos)
      .andWhere('number', 'like', '202%')
      .andWhere(function() {
        this.where('status', 'M').orWhere('status', 'O')
      })
    )

statistic.loanDocumentFilesTotal = await Client.relatedQuery('documents').for(clientsQuery).count()

koskimas avatar Jan 17 '20 11:01 koskimas

@koskimas oh, yes, that's exactly what is needed

abdusamadtv avatar Jan 20 '20 10:01 abdusamadtv

@koskimas Now I have this, but there's a problem with performance when I'm adding this condition to relatedQuery

.whereExists(ClientLoanDocument.relatedQuery('files'))

In the console you can see that performance degrades about 10 times

image

abdusamadtv avatar Jan 20 '20 12:01 abdusamadtv

@koskimas same thing here when I'm trying to delete documents through loansQuery with condition.

is it possible to reduce the query completion time?

image

abdusamadtv avatar Jan 22 '20 12:01 abdusamadtv

You probably don't have the needed indexes.

koskimas avatar Jan 22 '20 14:01 koskimas