-
Notifications
You must be signed in to change notification settings - Fork 171
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
Feature: Paging (small breaking change) #64
Conversation
Attempts to properly implement paging using PowerShell's native paging support. Addresses Snow-Shell#19.
I've been doing some additional thinking about this change. I think it's possible to remove the breaking change by adding the # In Get-ServiceNowTable
if ($PSBoundParameters.ContainsKey('Limit')) {
Write-Warning "The -Limit parameter is deprecated, and may be removed in a future release. Use the -First parameter instead."
$Body['sysparm_limit'] = $Limit
}
elseif ($PSCmdlet.PagingParameters.First -ne [uint64]::MaxValue) {
# If the user doesn't provide the -First parameter, it defaults to [uint64]::MaxValue
$Body['sysparm_limit'] = $PSCmdlet.PagingParameters.First
}
else {
# If no paging information was provided, default to the legacy behavior,
# which was to return 10 records
$Body['sysparm_limit'] = 10
}
# In functions that call it
if ($PSBoundParameters.ContainsKey('Limit')) {
$getServiceNowTableSplat.Add('Limit', $Limit)
}
This would allow users to continue using the -Limit parameter for now if necessary, which would make for a more graceful transition since it's no longer a breaking change. Would this be a change you'd like to see in this PR? |
First of... great to see you active again, @replicaJunction ;-) to your PR:
and
|
@lipkau - great to be alive again, and thanks for the feedback. :)
$csv = Import-Csv $csvPath
$csv | select -First 100 | Export-Csv $firstOutputFile -NoTypeInformation
$csv | select -First 100 -Skip 100 | Export-Csv $secondOutputFile -NoTypeInformation
# ...etc
Still looking for feedback above on my comment about adding back the -Limit parameter and avoiding the breaking change. |
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-6#supportspaging
<#
.PARAMETER IncludeTotalCount
Causes an extra output of the total count at the beginning.
Note this is actually a uInt64, but with a custom string representation.
.PARAMETER Skip
Controls how many things will be skipped before starting output.
Defaults to 0.
.PARAMETER First
Indicates how many items to return.
#>
With the full example: # use pagesize of 100 because the payload of each object is small
Invoke-Something -PageSize 100 -First 50 -Skip 25 With a DB of 1000 entries, the cmdlet would: request up to 10 pages (100 entries/page *10 = 1000 entries), will ignore
Yes, keep it in until the next major version. with a |
Okay, I think I see what you're getting at. The confusion comes from the translation from PowerShell to the ServiceNow API, and I think it's only in my example, not in the PR code itself. First off, the PS> 0..100 | select-object -First 5 -Skip 5
5
6
7
8
9
PS> 0..100 | select-object -First 5 -Skip 10
10
11
12
13
14 The same example works with the Get-Numbers function you linked: PS> Get-Numbers -First 5 -Skip 5
Skip Number IncludeTotalCount First
---- ------ ----------------- -----
5 5 False 5
5 6 False 5
5 7 False 5
5 8 False 5
5 9 False 5
PS> Get-Numbers -First 5 -Skip 10
Skip Number IncludeTotalCount First
---- ------ ----------------- -----
10 10 False 5
10 11 False 5
10 12 False 5
10 13 False 5
10 14 False 5
Next, ServiceNow doesn't define a separate "page size" parameter. It only provides two URL parameters:
To your previous example of paging through 1000 entries starting on 25, in ServiceNow, the URL parameters would need to look like this:
To me, these map pretty easily to -First and to -Skip:
So to return all items with this API, the -First parameter stays at a consistent value, and the -Skip parameter is what tells the API to move forward through records. That same logic works with Select-Object when paging through records from another data source (like the CSV example I listed above), and that's the logic used in the implementation of the paging parameters in Get-ServiceNowTable. My example code above is not as well-written, and it does use a PageSize variable as a value for the First parameter. The naming is a bit weird because of that, but the logic is sound - the "page size" for each API request maps to the -First PowerShell parameter, which maps to the sysparm_limit URL parameter. If each page is a consistent size, then the -First parameter should stay consistent as well, and then it could be considered a page size since it determines how many records are returned. To summarize - no, I don't think I'm confusing First and PageSize in the code in this PR, though my example is not really a good one. |
I still think you are. but I also think I don"t know the module. And this is becoming a lot of discussion for a PR. |
@Rick-2CA - If you're still interested in this change, here's what I'm thinking for a plan of action:
How does that sound? |
@replicaJunction - Sounds good. I like the idea of deprecating the limit param. |
Closing for the time being per my previous comment. I'll re-implement this after #67 is accepted. |
Adds support for paging result sets using PowerShell's own paging parameters:
-First
and-Skip
.Addresses #19.
The
-First
parameter is functionally identical to the previous-Limit
parameter, so I have removed that parameter. I have preserved the default behavior - if-First
is not specified, 10 results are returned. However, this will be a breaking change to scripts that provide the-Limit
parameter. Those scripts will just need to have that parameter renamed to-First
instead. I considered leaving-Limit
to avoid this situation, but in my opinion, it's bad design to have redundant parameters, and since the solution is a simple rename (no need to re-think or re-design anything), I don't see it as a huge problem so long as it's documented.The
-Skip
parameter provides the ability to start at an offset. Combining this with-First
allows you to page through multiple result sets.Here is a usage example:
(Edit: fixed a minor issue in the example code.)