Skip to content

Commit

Permalink
fix: Error with order on count query (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet authored Jan 29, 2025
1 parent 039c67d commit ee45108
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/adapters/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
FROM friendship_actions fa2
WHERE fa2.friendship_id = fa.friendship_id
)
ORDER BY fa.timestamp DESC
`)

if (limit) {
baseQuery.append(SQL` LIMIT ${limit}`)
}
if (!onlyCount) {
baseQuery.append(SQL` ORDER BY fa.timestamp DESC`)

if (limit) {
baseQuery.append(SQL` LIMIT ${limit}`)
}

if (offset) {
baseQuery.append(SQL` OFFSET ${offset}`)
if (offset) {
baseQuery.append(SQL` OFFSET ${offset}`)
}
}

return baseQuery
Expand Down
10 changes: 10 additions & 0 deletions test/unit/adapters/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ describe('db', () => {
const result = await dbComponent.getReceivedFriendshipRequestsCount('0x456')

expect(result).toBe(mockCount)
expect(mockPg.query).not.toHaveBeenCalledWith(
expect.objectContaining({
text: expect.stringContaining('ORDER BY fa.timestamp DESC')
})
)
})
})

Expand Down Expand Up @@ -336,6 +341,11 @@ describe('db', () => {
const result = await dbComponent.getSentFriendshipRequestsCount('0x123')

expect(result).toBe(mockCount)
expect(mockPg.query).not.toHaveBeenCalledWith(
expect.objectContaining({
text: expect.stringContaining('ORDER BY fa.timestamp DESC')
})
)
})
})

Expand Down

0 comments on commit ee45108

Please sign in to comment.