Skip to content

Commit

Permalink
fix: cast page and limit to numbers, since paginate receives input fr…
Browse files Browse the repository at this point in the history
…om the HTTP request
  • Loading branch information
thetutlage committed Jun 3, 2020
1 parent 5ebbc9e commit bd38a5d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 36 deletions.
8 changes: 1 addition & 7 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,13 +1259,7 @@ export abstract class Chainable extends Macroable implements ChainableContract {
* A shorthand for applying offset and limit based upon
* the current page
*/
public forPage (page: number, perPage?: number): this {
/**
* Cast to number
*/
page = Number(page)
perPage = Number(perPage)

public forPage (page: number, perPage: number): this {
/**
* Calculate offset from current page and per page values
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil
* Paginate through rows inside a given table
*/
public async paginate (page: number, perPage: number = 20) {
/**
* Cast to number
*/
page = Number(page)
perPage = Number(perPage)

const countQuery = this.clone().clearOrder().clearLimit().clearOffset().clearSelect().count('* as total')
const aggregates = await countQuery.exec()

Expand Down
6 changes: 6 additions & 0 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
* Paginate through rows inside a given table
*/
public async paginate (page: number, perPage: number = 20) {
/**
* Cast to number
*/
page = Number(page)
perPage = Number(perPage)

const countQuery = this.clone().clearOrder().clearLimit().clearOffset().clearSelect().count('* as total')

/**
Expand Down
16 changes: 2 additions & 14 deletions src/Orm/Relations/HasManyThrough/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { HasManyThroughQueryBuilderContract } from '@ioc:Adonis/Lucid/Relations'
import { HasManyThrough } from './index'
import { getValue, unique } from '../../../utils'
import { BaseQueryBuilder } from '../Base/QueryBuilder'
import { SimplePaginator } from '../../../Database/Paginator/SimplePaginator'

/**
* Extends the model query builder for executing queries in scope
Expand Down Expand Up @@ -100,18 +99,6 @@ export class HasManyThroughQueryBuilder extends BaseQueryBuilder implements HasM
})
}

/**
* Executes the pagination query for the relationship
*/
private async paginateRelated (page: number, perPage: number) {
const countQuery = this.clone().clearOrder().clearLimit().clearOffset().clearSelect().count('* as total')
const aggregateQuery = await countQuery.exec()
const total = this.hasGroupBy ? aggregateQuery.length : aggregateQuery[0].total

const results = total > 0 ? await this.forPage(page, perPage).exec() : []
return new SimplePaginator(results, total, perPage, page)
}

/**
* Profiler data for HasManyThrough relationship
*/
Expand Down Expand Up @@ -226,7 +213,8 @@ export class HasManyThroughQueryBuilder extends BaseQueryBuilder implements HasM
if (this.isEagerQuery) {
throw new Error(`Cannot paginate relationship "${this.relation.relationName}" during preload`)
}
return this.paginateRelated(page, perPage)

return super.paginate(page, perPage)
}

/**
Expand Down
17 changes: 2 additions & 15 deletions src/Orm/Relations/ManyToMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { ManyToManyQueryBuilderContract } from '@ioc:Adonis/Lucid/Relations'
import { ManyToMany } from './index'
import { getValue, unique } from '../../../utils'
import { BaseQueryBuilder } from '../Base/QueryBuilder'
import { SimplePaginator } from '../../../Database/Paginator/SimplePaginator'

/**
* Extends the model query builder for executing queries in scope
Expand Down Expand Up @@ -134,19 +133,6 @@ export class ManyToManyQueryBuilder extends BaseQueryBuilder implements ManyToMa
})
}

/**
* Executes the pagination query for the relationship
*/
private async paginateRelated (page: number, perPage: number) {
const countQuery = this.clone().clearOrder().clearLimit().clearOffset().clearSelect().count('* as total')

const aggregateQuery = await countQuery.exec()
const total = this.hasGroupBy ? aggregateQuery.length : aggregateQuery[0].total

const results = total > 0 ? await this.forPage(page, perPage).exec() : []
return new SimplePaginator(results, total, perPage, page)
}

/**
* Select keys from the related table
*/
Expand Down Expand Up @@ -404,7 +390,8 @@ export class ManyToManyQueryBuilder extends BaseQueryBuilder implements ManyToMa
if (this.isEagerQuery) {
throw new Error(`Cannot paginate relationship "${this.relation.relationName}" during preload`)
}
return this.paginateRelated(page, perPage)

return super.paginate(page, perPage)
}

/**
Expand Down

0 comments on commit bd38a5d

Please sign in to comment.