-
-
Notifications
You must be signed in to change notification settings - Fork 72
Add support for bucket pagination and sorting #1087
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
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
Storage/src/commonMain/kotlin/io/github/jan/supabase/storage/BucketFilter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.github.jan.supabase.storage | ||
|
|
||
| import io.ktor.http.parameters | ||
|
|
||
| /** | ||
| * A filter builder for [Storage.listBuckets] | ||
| */ | ||
| class BucketFilter { | ||
|
|
||
| /** | ||
| * The maximum number of buckets to return. If null, no limit is applied. | ||
| */ | ||
| var limit: Int? = null | ||
|
|
||
| /** | ||
| * The number of buckets to skip before returning results. Useful for pagination. | ||
| */ | ||
| var offset: Int? = null | ||
|
|
||
| /** | ||
| * A search query to filter buckets by name. If null, no search filter is applied. | ||
| */ | ||
| var search: String? = null | ||
|
|
||
| /** | ||
| * The sort order for the results. Can be [SortOrder.ASC] (ascending) or [SortOrder.DESC] (descending). | ||
| * If null, the default sort order from the API is used. | ||
| */ | ||
| private var sortOrder: SortOrder? = null | ||
|
|
||
| /** | ||
| * The column to sort the results by. If null, the default sort column from the API is used. | ||
| */ | ||
| private var sortColumn: SortColumn? = null | ||
|
|
||
| /** | ||
| * Sets the sorting criteria for the bucket list results | ||
| * @param column The column to sort by | ||
| * @param order The sort order (ascending or descending) | ||
| */ | ||
| fun sortBy(column: SortColumn, order: SortOrder) { | ||
| sortColumn = column | ||
| sortOrder = order | ||
| } | ||
|
|
||
| fun build() = parameters { | ||
| limit?.let { set("limit", it.toString()) } | ||
| offset?.let { set("offset", it.toString()) } | ||
| search?.let { set("search", it) } | ||
| sortOrder?.let { set("sortOrder", it.name.lowercase()) } | ||
| sortColumn?.let { set("sortColumn", it.name.lowercase()) } | ||
| } | ||
|
|
||
| /** | ||
| * Represents the available columns for sorting bucket results. | ||
| */ | ||
| enum class SortColumn { | ||
| /** Sort by bucket ID */ | ||
| ID, | ||
|
|
||
| /** Sort by bucket name */ | ||
| NAME, | ||
|
|
||
| /** Sort by bucket creation timestamp */ | ||
| CREATED_AT, | ||
|
|
||
| /** Sort by bucket last updated timestamp */ | ||
| UPDATED_AT | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Storage/src/commonMain/kotlin/io/github/jan/supabase/storage/SortOrder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package io.github.jan.supabase.storage | ||
|
|
||
| /** | ||
| * Represents the sort order for query results. | ||
| */ | ||
| enum class SortOrder { | ||
| /** | ||
| * Ascending order | ||
| */ | ||
| ASC, | ||
|
|
||
| /** | ||
| * Descending order | ||
| */ | ||
| DESC | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import io.github.jan.supabase.storage.BucketFilter | ||
| import io.github.jan.supabase.storage.SortOrder | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNull | ||
|
|
||
| class BucketFilterTest { | ||
|
|
||
| @Test | ||
| fun testBucketFilterWithAllParameters() { | ||
| val filter = BucketFilter().apply { | ||
| limit = 10 | ||
| offset = 5 | ||
| search = "test" | ||
| sortBy(BucketFilter.SortColumn.NAME, SortOrder.ASC) | ||
| } | ||
| val params = filter.build() | ||
| assertEquals("10", params["limit"]) | ||
| assertEquals("5", params["offset"]) | ||
| assertEquals("test", params["search"]) | ||
| assertEquals("asc", params["sortOrder"]) | ||
| assertEquals("name", params["sortColumn"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testBucketFilterEmpty() { | ||
| val filter = BucketFilter() | ||
| val params = filter.build() | ||
| assertNull(params["limit"]) | ||
| assertNull(params["offset"]) | ||
| assertNull(params["search"]) | ||
| assertNull(params["sortOrder"]) | ||
| assertNull(params["sortColumn"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testBucketFilterIndividualParameters() { | ||
| // Test limit only | ||
| var filter = BucketFilter().apply { limit = 20 } | ||
| var params = filter.build() | ||
| assertEquals("20", params["limit"]) | ||
| assertNull(params["offset"]) | ||
|
|
||
| // Test offset only | ||
| filter = BucketFilter().apply { offset = 15 } | ||
| params = filter.build() | ||
| assertEquals("15", params["offset"]) | ||
| assertNull(params["limit"]) | ||
|
|
||
| // Test search only | ||
| filter = BucketFilter().apply { search = "my-bucket" } | ||
| params = filter.build() | ||
| assertEquals("my-bucket", params["search"]) | ||
| assertNull(params["limit"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testBucketFilterSortColumns() { | ||
| // Test all sort columns with both orders | ||
| val columns = listOf( | ||
| BucketFilter.SortColumn.ID to "id", | ||
| BucketFilter.SortColumn.NAME to "name", | ||
| BucketFilter.SortColumn.CREATED_AT to "created_at", | ||
| BucketFilter.SortColumn.UPDATED_AT to "updated_at" | ||
| ) | ||
|
|
||
| for ((column, expectedName) in columns) { | ||
| // Test ascending | ||
| var filter = BucketFilter().apply { sortBy(column, SortOrder.ASC) } | ||
| var params = filter.build() | ||
| assertEquals(expectedName, params["sortColumn"]) | ||
| assertEquals("asc", params["sortOrder"]) | ||
|
|
||
| // Test descending | ||
| filter = BucketFilter().apply { sortBy(column, SortOrder.DESC) } | ||
| params = filter.build() | ||
| assertEquals(expectedName, params["sortColumn"]) | ||
| assertEquals("desc", params["sortOrder"]) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testBucketFilterEdgeCases() { | ||
| // Zero values | ||
| var filter = BucketFilter().apply { | ||
| limit = 0 | ||
| offset = 0 | ||
| } | ||
| var params = filter.build() | ||
| assertEquals("0", params["limit"]) | ||
| assertEquals("0", params["offset"]) | ||
|
|
||
| // Empty search string | ||
| filter = BucketFilter().apply { search = "" } | ||
| params = filter.build() | ||
| assertEquals("", params["search"]) | ||
|
|
||
| // Special characters in search | ||
| filter = BucketFilter().apply { search = "test-bucket_123" } | ||
| params = filter.build() | ||
| assertEquals("test-bucket_123", params["search"]) | ||
|
|
||
| // Large numbers | ||
| filter = BucketFilter().apply { | ||
| limit = 1000 | ||
| offset = 5000 | ||
| } | ||
| params = filter.build() | ||
| assertEquals("1000", params["limit"]) | ||
| assertEquals("5000", params["offset"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testBucketFilterCombinations() { | ||
| // Limit and offset | ||
| var filter = BucketFilter().apply { | ||
| limit = 25 | ||
| offset = 50 | ||
| } | ||
| var params = filter.build() | ||
| assertEquals("25", params["limit"]) | ||
| assertEquals("50", params["offset"]) | ||
| assertNull(params["search"]) | ||
|
|
||
| // Search and sort | ||
| filter = BucketFilter().apply { | ||
| search = "images" | ||
| sortBy(BucketFilter.SortColumn.UPDATED_AT, SortOrder.ASC) | ||
| } | ||
| params = filter.build() | ||
| assertEquals("images", params["search"]) | ||
| assertEquals("updated_at", params["sortColumn"]) | ||
| assertEquals("asc", params["sortOrder"]) | ||
|
|
||
| // Pagination with sort | ||
| filter = BucketFilter().apply { | ||
| limit = 10 | ||
| offset = 30 | ||
| sortBy(BucketFilter.SortColumn.NAME, SortOrder.ASC) | ||
| } | ||
| params = filter.build() | ||
| assertEquals("10", params["limit"]) | ||
| assertEquals("30", params["offset"]) | ||
| assertEquals("name", params["sortColumn"]) | ||
| assertEquals("asc", params["sortOrder"]) | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.