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

No way to modify totalResults and itemsPerPage in the ListResponse result #64

Open
anthwal opened this issue Dec 30, 2024 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@anthwal
Copy link

anthwal commented Dec 30, 2024

I am using the ListResponse class to return a paginated response for User resources, which are retrieved from my application's database.
Is there a way to modify the itemsPerPage and totalResults properties of the ListResponse?
I am passing the itemsPerPage value from the params to the ListResponse to my SQL query which should correctly return the specified number of records from the database but the totalResults is meant to be a number showing count of the total records which satisfy the given conditions.

In my case the ListResponse returns the records which my database query returns.

new ListResponse(
  resultRows,
  {
    sortBy: sortBy,
    sortOrder: sortOrder,
    startIndex: pgStart,
    itemsPerPage: limit,
  }
)

// response
{
  schemas: [ 'urn:ietf:params:scim:api:messages:2.0:ListResponse' ],
  totalResults: 1,
  Resources: [
    {
      id: 1,
      email: '[email protected]',
    }
  ],
  startIndex: 1,
  itemsPerPage: 20
}
    

here the totalResults is showing the length of the Resources property of the ListResponse, but I am not able to find a way to change it to the calculated value of 8.
And the itemsPerPage value in the result is always 20, I provided the itemsPerPage param value as 1.

@sleelin sleelin self-assigned this Jan 8, 2025
@sleelin sleelin added question Further information is requested documentation Improvements or additions to documentation labels Jan 8, 2025
@sleelin
Copy link
Collaborator

sleelin commented Jan 8, 2025

Hi @anthwal, thanks for reaching out!
Apologies for the confusion, I've realised it's not explicitly documented anywhere that the totalResults property is set using the length property of the array passed in to the ListResponse constructor, and can be modified by manipulating the length property of said array (by taking advantage of JavaScript's sparse array functionality, this does not cause the results to be filled with empty values). Using your example, this would look something like the following:

const totalResults = someDatabaseCountMethod({sortBy, sortOrder});
const resultsRow = someDatabaseSelectMethod({sortBy, sortOrder, pgStart, limit});

new ListResponse(
    Object.assign(resultRows, {length: totalResults}),
    {
        sortBy, sortOrder,
        startIndex: pgStart,
        itemsPerPage: limit
    }
);

Regarding the itemsPerPage property, it will fall back to the default value of 20 when both the itemsPerPage and count parameters are undefined - could you please confirm that the limit variable you assign to itemsPerPage is your expected value of 1?

I'm also curious as to what your use case is for using the ListResponse class directly, as opposed to the SCIMMY.Resources.User class, which automates the parameter parsing and instantiation of list responses on read?

Thanks,
Sam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Development

No branches or pull requests

2 participants