Skip to content

Commit

Permalink
feat: add option to paginate in desc order
Browse files Browse the repository at this point in the history
  • Loading branch information
JoblersTune committed Oct 23, 2023
1 parent 111a504 commit b880660
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/backend/src/fee/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ async function getFeesPage(
assetId: string,
pagination?: Pagination
): Promise<Fee[]> {
const query = Fee.query(deps.knex).where({ assetId }).getPage(pagination)
const query = Fee.query(deps.knex)
.where({ assetId })
.getPage(pagination, 'desc')

return await query
}
Expand Down
23 changes: 12 additions & 11 deletions packages/backend/src/shared/baseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
* @param pagination Pagination - cursors and limits.
* @returns Model[] An array of Models that form a page.
*/
getPage(pagination?: Pagination): this {
getPage(pagination?: Pagination, sortOrder: 'asc' | 'desc' = 'asc'): this {
const tableName = this.modelClass().tableName
if (
typeof pagination?.before === 'undefined' &&
Expand All @@ -57,33 +57,34 @@ class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
if (first < 0 || first > 100) throw new Error('Pagination index error')
const last = pagination?.last || 20
if (last < 0 || last > 100) throw new Error('Pagination index error')

/**
* Forward pagination
*/
if (typeof pagination?.after === 'string') {
const comparisonOperator = sortOrder === 'asc' ? '>' : '<'
return this.whereRaw(
`("${tableName}"."createdAt", "${tableName}"."id") > (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?)`,
`("${tableName}"."createdAt", "${tableName}"."id") ${comparisonOperator} (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?)`,
[this.modelClass().tableName, pagination.after]
)
.orderBy([
{ column: 'createdAt', order: 'asc' },
{ column: 'id', order: 'asc' }
{ column: 'createdAt', order: sortOrder },
{ column: 'id', order: sortOrder }
])
.limit(first)
}

/**
* Backward pagination
*/
if (typeof pagination?.before === 'string') {
const comparisonOperator = sortOrder === 'asc' ? '<' : '>'
const order = sortOrder === 'asc' ? 'desc' : 'asc'
return this.whereRaw(
`("${tableName}"."createdAt", "${tableName}"."id") < (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?)`,
`("${tableName}"."createdAt", "${tableName}"."id") ${comparisonOperator} (select "${tableName}"."createdAt" :: TIMESTAMP, "${tableName}"."id" from ?? where "${tableName}"."id" = ?)`,
[this.modelClass().tableName, pagination.before]
)
.orderBy([
{ column: 'createdAt', order: 'desc' },
{ column: 'id', order: 'desc' }
{ column: 'createdAt', order: order },
{ column: 'id', order: order }
])
.limit(last)
.runAfter((models) => {
Expand All @@ -94,8 +95,8 @@ class PaginationQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<
}

return this.orderBy([
{ column: 'createdAt', order: 'asc' },
{ column: 'id', order: 'asc' }
{ column: 'createdAt', order: sortOrder },
{ column: 'id', order: sortOrder }
]).limit(first)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/webhook/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ async function getWebhookEventsPage(
query.whereIn('type', filter.type.in)
}

return await query.getPage(pagination)
return await query.getPage(pagination, 'desc')
}

0 comments on commit b880660

Please sign in to comment.