Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The pagination returns wrong totalItems, pageCount, next, previous page links #258

Closed
bluerid opened this issue Mar 23, 2020 · 11 comments
Closed
Assignees

Comments

@bluerid
Copy link

bluerid commented Mar 23, 2020

The pagination returns wrong totalItems, pageCount, next, previous page links even when coupled with a where clause.
When there are 4 rows in my table for user 1, and user 2

return await paginate<PayoutsEntity>(this.repo, options, {
      user_id: 2,
});

This should return totalItems: 2, pageCount: 2, next: /payments?page=1&limit=1, previous: ''

...but this is what i'm getting.
image
Extra optional parameters are not taken into account while the total pages are calculated? Why are total items showing 4 for user_id = 2 when it should be 2 instead.

@ctilley83
Copy link

Yep. I'm having the same issue. I'm asking for 10 items per page but only receiving 5. Depending on the page the item count will be different. Sometimes I'll get back 6 items, sometimes 8, etc. Some of my models work perfectly fine.

When I run the query TypeORM generates directly on the database I get 10 items

@ctilley83
Copy link

Actually, I know why. My issue stems from the fact that I'm joining tables. I'm asking for 10 shipments, but since I'm joining to another table, it's applying the limit to the records in the related table.

image

@ctilley83
Copy link

The problem seems to be solved in master, however when installing from npm, you don't get the latest version. I ended up removing the package from my project and copied all the files from master manually.

@bashleigh
Copy link
Collaborator

Hey guys, sorry I've been really slow recently. Going to be working on the changes in master in the next few days and finishing things off and deploying :) sorry for taking so long!

@bashleigh bashleigh self-assigned this Apr 7, 2020
@ctilley83
Copy link

Hey guys, sorry I've been really slow recently. Going to be working on the changes in master in the next few days and finishing things off and deploying :) sorry for taking so long!

All good. Thanks for creating this package. It saved me a lot of time.

@kauandotnet
Copy link

kauandotnet commented Apr 9, 2020

@bashleigh , same issue here, when join table in many-to-many itemCount and totalItems does not work well... do you have any position? Thanks.

@bashleigh
Copy link
Collaborator

@kauandotnet can you show me your implementation and the query string please if you can?

@kauandotnet
Copy link

kauandotnet commented Apr 11, 2020

@bashleigh

Service:

 async listAll(options: IPaginationOptions): Promise<Pagination<any>> {
    return await paginate(this.repository.findLines(), options);
  }

Repository Query:

  findLines() {
    return this.createQueryBuilder("line")
      .leftJoinAndSelect("line.plans", "plans")
      .leftJoinAndSelect("plans.carrier", "carrier")
      .select(["line.id", "line.number", "line.lineStatus", "line.carrierStatus", "line.reservedBetween", "line.aquisitionDate"])
      .addSelect(["plans.id", "plans.name"])
      .addSelect(["carrier.id", "carrier.name"]);
  }

Line:

@Entity("line")
export class LineEntity extends DefaultEntity {
  @Column({ unique: true })
  number: string;

  @Column({
    type: "enum",
    enum: LineStatus,
    default: LineStatus.ACTIVE
  })
  lineStatus: LineStatus;

  @ManyToMany(type => PlanEntity)
  @JoinTable({
    name: "plan_line",
    joinColumn: {
      name: "lineId",
      referencedColumnName: "id"
    },
    inverseJoinColumn: {
      name: "planId",
      referencedColumnName: "id"
    }
  })
  plans: PlanEntity[];

  @Column({
    type: "enum",
    enum: CarrierStatus,
    default: CarrierStatus.ACTIVE
  })
  carrierStatus: CarrierStatus;

  @Column({ type: "date", nullable: true })
  reservedBetween: string;

  @Column({ type: "date" })
  aquisitionDate: string;
}

Plan:

@Entity("plan")
export class PlanEntity extends DefaultEntity {
  @Column()
  name: string;

  @Column({ nullable: true })
  description: string;

  @ManyToOne(type => ServiceEntity)
  service: ServiceEntity;

  @ManyToOne(
    type => CarrierEntity,
    carrier => carrier.plans
  )
  carrier: CarrierEntity;

  @OneToMany(
    type => OfferEntity,
    offer => offer.plan
  )
  offers: OfferEntity[];

  @Column({ default: 0 })
  activationFee: string;
}

@tranty9597
Copy link

any update on this

@bashleigh
Copy link
Collaborator

@kauandotnet I think using select or left joins has resulted in your error. I quite often hydrate my results if I need to as that's what typeorm does when using joinTables + eager loading. I know it sounds less desired than left joins but it means the results for the pagination are correct! However I would recommend before doing this, making sure your outputted query results in the correct amount of line. So set TYPEORM_LOGGING=true in you envs, take the query, run it against your database and check that your query is resulting in the desired outcome. Other than that I don't think this is an issue with the paginate method? Please let me know if your query results in the correct results and the paginate method doesn't :)

@kauandotnet
Copy link

kauandotnet commented Jun 8, 2020

After you've updated, everything is working now...
Thanks for your time @bashleigh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants