Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Paginate function not working with Postgres #8

Open
shradhaN opened this issue Jul 2, 2020 · 2 comments
Open

Paginate function not working with Postgres #8

shradhaN opened this issue Jul 2, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@shradhaN
Copy link

shradhaN commented Jul 2, 2020

We are using a node backend application with Postgres as database.

The paginate is failing method is failing in @leapfrogtechnology/db-model@1.0.0-beta.2

The model

import BaseModel from '../baseModel';
import Table from '../common/enums/table.enum';

/**
* User Model
*/
class User extends BaseModel {
 public static table: string = Table.USERS;
}

export default User;

Dao layer method call

export function fetchPaginatedUser(): Promise<any> {
  const params = {
    maxRows: 10,
    currentPage: 1,
    totalCountQuery: 'SELECT count(*) as count from users'
  }
  return User.paginate('SELECT * from users', params);
}

Expectation:

To get 10 rows of records.

Error message

  knex:client acquired connection from pool: __knexUid2 +2s
  knex:query SELECT count(*) as count from users undefined +2s
  knex:bindings [] undefined +2s
  knex:client releasing connection to pool: __knexUid2 +2ms
[ 2020-07-02T11:55:54.273Z ] - [ error ] Error: TypeError: Cannot read property 'count' of undefined

The error seems to have originated from
totalCount = totalRecords[0].count; line in paginator.ts

the output of totalRecords is in the following format for postgres:

row : { 
          count: '99'
       }

@kabirbaidhya @mesaugat @cham11ng Any suggestion on how to get it working?

@mesaugat
Copy link
Member

mesaugat commented Jul 2, 2020

@shradhaN Unfortunately, we have not tested our package with Postgres or MySQL. There might be a few of these cases lying around.

Please feel free to send a PR with the fix and we'll happily merge it. 👉 https://github.com/leapfrogtechnology/js-util/blob/%40leapfrogtechnology/db-model%401.0.0-beta.2/packages/db-model/src/utils/paginator.ts

@kabirbaidhya kabirbaidhya added the bug Something isn't working label Jul 2, 2020
@kabirbaidhya
Copy link
Contributor

Any suggestion on how to get it working?

We have to find out. It would be great if you could do a PR. 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants