Skip to content

Commit

Permalink
fix: pass preloads, sideloader values to a group limit query
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 25, 2021
1 parent 9296214 commit 9162b64
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 20 deletions.
5 changes: 5 additions & 0 deletions adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ declare module '@ioc:Adonis/Lucid/Model' {
ExcutableQueryBuilderContract<Result[]> {
model: Model

/**
* Define a custom preloader for the current query
*/
usePreloader(preloader: PreloaderContract<LucidRow>): this

/**
* Whether or not the query is a child query generated for `.where`
* callbacks
Expand Down
20 changes: 15 additions & 5 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ import {
ModelObject,
ModelAdapterOptions,
ModelQueryBuilderContract,
LucidRow,
} from '@ioc:Adonis/Lucid/Model'

import { DBQueryCallback } from '@ioc:Adonis/Lucid/DatabaseQueryBuilder'
import { RelationQueryBuilderContract, RelationshipsContract } from '@ioc:Adonis/Lucid/Relations'
import {
PreloaderContract,
RelationshipsContract,
RelationQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Relations'

import {
DialectContract,
Expand Down Expand Up @@ -66,12 +71,12 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
/**
* Sideloaded attributes that will be passed to the model instances
*/
private sideloaded: ModelObject = {}
protected sideloaded: ModelObject = {}

/**
* A copy of defined preloads on the model instance
*/
private preloader = new Preloader(this.model)
protected preloader: PreloaderContract<LucidRow> = new Preloader(this.model)

/**
* Required by macroable
Expand All @@ -95,13 +100,13 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
* Custom data someone want to send to the profiler and the
* query event
*/
private customReporterData: any
protected customReporterData: any

/**
* Control whether to debug the query or not. The initial
* value is inherited from the query client
*/
private debugQueries: boolean = this.client.debug
protected debugQueries: boolean = this.client.debug

/**
* Self join counter, increments with every "withCount"
Expand Down Expand Up @@ -367,6 +372,11 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
return this
}

public usePreloader(preloader: PreloaderContract<LucidRow>) {
this.preloader = preloader
return this
}

/**
* Set sideloaded properties to be passed to the model instance
*/
Expand Down
12 changes: 7 additions & 5 deletions src/Orm/Relations/HasMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ export class HasManyQueryBuilder
'adonis_temp'
)

return this.relation
.relatedModel()
.query()
.from(this)
.where(rowName, '<=', this.groupConstraints.limit!)
const groupQuery = this.relation.relatedModel().query()
groupQuery.usePreloader(this.preloader)
groupQuery.sideload(this.sideloaded)
groupQuery.debug(this.debugQueries)
this.customReporterData && groupQuery.reporterData(this.customReporterData)

return groupQuery.from(this).where(rowName, '<=', this.groupConstraints.limit!)
}
}
12 changes: 7 additions & 5 deletions src/Orm/Relations/HasManyThrough/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ export class HasManyThroughQueryBuilder
'adonis_temp'
)

return this.relation
.relatedModel()
.query()
.from(this)
.where(rowName, '<=', this.groupConstraints.limit!)
const groupQuery = this.relation.relatedModel().query()
groupQuery.usePreloader(this.preloader)
groupQuery.sideload(this.sideloaded)
groupQuery.debug(this.debugQueries)
this.customReporterData && groupQuery.reporterData(this.customReporterData)

return groupQuery.from(this).where(rowName, '<=', this.groupConstraints.limit!)
}
}
12 changes: 7 additions & 5 deletions src/Orm/Relations/ManyToMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,12 @@ export class ManyToManyQueryBuilder
'adonis_temp'
)

return this.relation
.relatedModel()
.query()
.from(this)
.where(rowName, '<=', this.groupConstraints.limit!)
const groupQuery = this.relation.relatedModel().query()
groupQuery.usePreloader(this.preloader)
groupQuery.sideload(this.sideloaded)
groupQuery.debug(this.debugQueries)
this.customReporterData && groupQuery.reporterData(this.customReporterData)

return groupQuery.from(this).where(rowName, '<=', this.groupConstraints.limit!)
}
}
Loading

0 comments on commit 9162b64

Please sign in to comment.