kysely-paginate icon indicating copy to clipboard operation
kysely-paginate copied to clipboard

Cursor tiebreaker

Open Fedaz8 opened this issue 1 year ago • 4 comments

I've noticed that id isn't working as a tiebreaker for duplicate updatedAt values using the following code:

I know that my ids are all unique and I also know that I do have duplicate updatedAt values in my db

let qb: SelectQueryBuilder<Database, "user" | "userRelationship", any> = db
      .selectFrom("user")
      .select([
        "user.id",
        "user.username",
        "user.createdAt",
        "user.updatedAt",
      ])
      .where("user.id", "!=", context.user.id)

    qb = qb.orderBy("user.updatedAt", "desc")
    qb = qb.orderBy("user.id", "desc")

    const page = await executeWithCursorPagination({
      qb,
      opts: {
        after: after,
        cursorPerRow: true,
        perPage: first,
        fields: [
          { key: "updatedAt", expression: "user.updatedAt", direction: "desc" },
          { key: "id", expression: "user.id", direction: "desc" },
        ],
        parseCursor: (cursor) => {
          const parsed = {
            updatedAt: new Date(cursor.updatedAt),
            id: parseInt(cursor.id, 10),
          }

          console.log("!!parsed", parsed)

          return parsed
        },
      },
    })

    const result = {
      edges: page.rows.map((row) => {
        return {
          cursor: row.$cursor,
          node: { ...row },
        }
      }),
      pageInfo: {
        startCursor: page.startCursor,
        endCursor: page.endCursor,
        hasNextPage: page.hasNextPage,
        hasPrevPage: page.hasPrevPage,
      },
    }

    return result

Fedaz8 avatar Jan 22 '24 19:01 Fedaz8