Repository navigation
docs(postgrest): document that order() defaults to descending - #1637
Conversation
Co-Authored-By: Claude Opus 5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe ChangesOrder documentation
Estimated code review effort: 1 (Trivial) | ~2 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
spydon
left a comment
There was a problem hiding this comment.
Thanks, we'll change the default of this in v3 (the default was changed in the js SDK a few years ago)
Closes #1638 ## What kind of change does this PR introduce? Breaking change. ## What is the current behavior? `order()` defaults `ascending` to `false` in two places, so omitting the parameter sorts descending: - `PostgrestTransformBuilder.order()` - `SupabaseStreamBuilder.order()` `.order('created_at')` therefore returns newest rows first, which disagrees with SQL's `ORDER BY` (defaults to `ASC`) and with `postgrest-js` (`{ ascending = true }`). The failure is silent: the query succeeds, the row count and types are right, only the order is wrong. The default was never chosen for Dart. `postgrest-dart` `7a8f8b7` (2020-09-10) ported the first `order()` from the pre-TypeScript `postgrest-js`, which then had `order(columnName, ascending = false, nullsFirst = false)`. `postgrest-js` `2960d09` moved to `{ ascending = true }` ten days later and Dart never followed. #1637 documented the current behavior in the meantime. Two examples in this repository were already wrong because of it, which is the clearest evidence that the default surprises people: - `examples/realtime_room/lib/room_repository.dart` says "SELECT the existing messages once, oldest first" and returned them newest first. - `examples/database_crud/lib/tasks_repository.dart` says "ordered alphabetically by name" and returned projects in reverse alphabetical order. Both are now correct without touching their query code. ## What is the new behavior? - `PostgrestTransformBuilder.order()`: `bool ascending = true`. - `SupabaseStreamBuilder.order()`: `bool ascending = true`, so `.stream()` and `.select()` agree on what an unqualified `.order()` means. - Doc comments on both state the default and drop the note about diverging from SQL and `postgrest-js`, since there is no longer a divergence. `nullsFirst` is unchanged at `false`. That already matched `postgrest-js` and PostgREST. ## Migration `MIGRATION.md` gets an `order() now sorts ascending by default` section under v2 to v3, covering both call sites, why the analyzer will not catch it, and how to keep the old behavior: ```dart await supabase.from('messages').select().order('created_at', ascending: false); ``` The section also points out that `ascending: false` already means descending on v2, so consumers can add it before upgrading and leave this change out of the upgrade itself. ## Tests - New `packages/postgrest/test/order_default_test.dart` asserts the generated `order` query parameter without a backend: the default, explicit `ascending: false`, `nullsFirst: true`, a referenced table, and that chained `order()` calls keep their own direction. - `transforms_test.dart` gains `order defaults to ascending` and `order descending` against the local stack. - `mock_test.dart` gains `order defaults to ascending` for the stream builder, and the mock server now answers `order=id.asc.nullslast`. - Tests that were exercising the old implicit default (multi-column order, order with filters, referenced-table order, embedded order, stream order with limit) now pass `ascending: false` explicitly, so their coverage of descending order is preserved rather than silently reinterpreted. `melos analyze` and `melos format` are clean. `packages/postgrest` (196 tests) and `packages/supabase` (136 tests) pass in full against a local Supabase stack. ## Additional context Part of the v3 umbrella (#1278). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Database queries and real-time streams now default to ascending order. * Descending order remains available with `ascending: false`. * Ordering continues to support null placement, referenced data, multiple columns, and filters. * **Documentation** * Updated ordering guidance, examples, and migration notes to reflect the new default. * **Tests** * Expanded coverage for default, ascending, descending, nested, referenced-table, filtered, and stream ordering. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
What kind of change does this PR introduce?
Documentation.
What is the current behavior?
order()declaresbool ascending = false, so a call that omits the parameterreturns rows in descending order. The doc comment never states this. It only
says:
Both examples in the doc comment then pass
ascending: falseexplicitly, whichsuggests the opposite — that the parameter has to be passed to get descending
order, and that omitting it yields ascending.
This is easy to get wrong, because the default is inverted relative to two things
a developer is likely to expect:
ORDER BY, which defaults toASCpostgrest-js, whereorder()destructures{ ascending = true }The failure is silent: the query succeeds and returns data, just in the wrong
order. In my case it survived unit tests and only surfaced when a historical
backfill read a series inverted.
There is also a small rendering bug in the same doc comment — the first code
fence is closed with four backticks instead of three, which breaks the block in
generated dartdoc.
What is the new behavior?
ascendingandnullsFirstexplicitly, and notes the divergence from SQL and
postgrest-js.ascending, showing the actual default; a secondexample shows how to request ascending order.
Doc comment only — no behavioural change. Changing the default itself would be
breaking and is deliberately not proposed here.
Additional context
Happy to open a follow-up for the equivalent JSDoc in
postgrest-js, whichlikewise does not state its default. Let me know if that would be useful.
Summary by CodeRabbit