-
-
Notifications
You must be signed in to change notification settings - Fork 226
feat: add support for sorting in list v2 endpoint #749
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,107 @@ | ||
CREATE OR REPLACE FUNCTION storage.search_v2 ( | ||
prefix text, | ||
bucket_name text, | ||
limits int DEFAULT 100, | ||
levels int DEFAULT 1, | ||
start_after text DEFAULT '', | ||
sort_order text DEFAULT 'asc', | ||
sort_column text DEFAULT 'name', | ||
sort_column_after text DEFAULT '' | ||
) RETURNS TABLE ( | ||
key text, | ||
name text, | ||
id uuid, | ||
updated_at timestamptz, | ||
created_at timestamptz, | ||
last_accessed_at timestamptz, | ||
metadata jsonb | ||
) | ||
SECURITY INVOKER | ||
AS $func$ | ||
DECLARE | ||
sort_col text; | ||
sort_ord text; | ||
cursor_op text; | ||
cursor_expr text; | ||
sort_expr text; | ||
BEGIN | ||
-- Validate sort_order | ||
sort_ord := lower(sort_order); | ||
IF sort_ord NOT IN ('asc', 'desc') THEN | ||
sort_ord := 'asc'; | ||
END IF; | ||
|
||
-- Determine cursor comparison operator | ||
IF sort_ord = 'asc' THEN | ||
cursor_op := '>'; | ||
ELSE | ||
cursor_op := '<'; | ||
END IF; | ||
|
||
sort_col := lower(sort_column); | ||
-- Validate sort column | ||
IF sort_col IN ('updated_at', 'created_at') THEN | ||
cursor_expr := format( | ||
'($5 = '''' OR ROW(date_trunc(''milliseconds'', %I), name COLLATE "C") %s ROW(COALESCE(NULLIF($6, '''')::timestamptz, ''epoch''::timestamptz), $5))', | ||
sort_col, cursor_op | ||
); | ||
sort_expr := format( | ||
'COALESCE(date_trunc(''milliseconds'', %I), ''epoch''::timestamptz) %s, name COLLATE "C" %s', | ||
sort_col, sort_ord, sort_ord | ||
); | ||
ELSE | ||
cursor_expr := format('($5 = '''' OR name COLLATE "C" %s $5)', cursor_op); | ||
sort_expr := format('name COLLATE "C" %s', sort_ord); | ||
END IF; | ||
|
||
RETURN QUERY EXECUTE format( | ||
$sql$ | ||
SELECT * FROM ( | ||
( | ||
SELECT | ||
split_part(name, '/', $4) AS key, | ||
name, | ||
NULL::uuid AS id, | ||
updated_at, | ||
created_at, | ||
NULL::timestamptz AS last_accessed_at, | ||
NULL::jsonb AS metadata | ||
FROM storage.prefixes | ||
WHERE name COLLATE "C" LIKE $1 || '%%' | ||
AND bucket_id = $2 | ||
AND level = $4 | ||
AND %s | ||
ORDER BY %s | ||
LIMIT $3 | ||
) | ||
UNION ALL | ||
( | ||
SELECT | ||
split_part(name, '/', $4) AS key, | ||
name, | ||
id, | ||
updated_at, | ||
created_at, | ||
last_accessed_at, | ||
metadata | ||
FROM storage.objects | ||
WHERE name COLLATE "C" LIKE $1 || '%%' | ||
AND bucket_id = $2 | ||
AND level = $4 | ||
AND %s | ||
ORDER BY %s | ||
LIMIT $3 | ||
) | ||
) obj | ||
ORDER BY %s | ||
LIMIT $3 | ||
$sql$, | ||
cursor_expr, -- prefixes WHERE | ||
sort_expr, -- prefixes ORDER BY | ||
cursor_expr, -- objects WHERE | ||
sort_expr, -- objects ORDER BY | ||
sort_expr -- final ORDER BY | ||
) | ||
USING prefix, bucket_name, limits, levels, start_after, sort_column_after; | ||
END; | ||
$func$ LANGUAGE plpgsql STABLE; |
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
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
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.