Skip to content

fix!: make order() default to ascending - #1658

Merged
spydon merged 1 commit into
mainfrom
breaking/postgrest-order-ascending-default
Aug 6, 2026
Merged

spydon merged 1 commit into
mainfrom
breaking/postgrest-order-ascending-default

Conversation

@spydon

@spydon spydon commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

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:

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).

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.

@spydon
spydon requested a review from a team as a code owner August 6, 2026 09:00
@coderabbitai

coderabbitai Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 24b405f7-d224-49cb-bdd3-da8f4f0923db

📥 Commits

Reviewing files that changed from the base of the PR and between aa1c149 and ed13a52.

📒 Files selected for processing (8)
  • MIGRATION.md
  • examples/database_crud/lib/tasks_repository.dart
  • packages/postgrest/lib/src/postgrest_transform_builder.dart
  • packages/postgrest/test/order_default_test.dart
  • packages/postgrest/test/resource_embedding_test.dart
  • packages/postgrest/test/transforms_test.dart
  • packages/supabase/lib/src/supabase_stream_builder.dart
  • packages/supabase/test/mock_test.dart
🚧 Files skipped from review as they are similar to previous changes (6)
  • packages/postgrest/test/resource_embedding_test.dart
  • examples/database_crud/lib/tasks_repository.dart
  • packages/supabase/lib/src/supabase_stream_builder.dart
  • packages/postgrest/lib/src/postgrest_transform_builder.dart
  • packages/supabase/test/mock_test.dart
  • packages/postgrest/test/transforms_test.dart

📝 Walkthrough

Walkthrough

The PostgREST and Supabase stream order methods now default to ascending order. Documentation, migration guidance, and tests cover explicit descending order and related ordering options. The CRUD example now orders tasks by priority and newest creation time.

Changes

Ascending order defaults

Layer / File(s) Summary
Order API defaults and documentation
packages/postgrest/lib/src/postgrest_transform_builder.dart, packages/supabase/lib/src/supabase_stream_builder.dart, MIGRATION.md
Both order methods now default to ascending order. Documentation and migration guidance show explicit descending usage.
PostgREST ordering validation
packages/postgrest/test/order_default_test.dart, packages/postgrest/test/transforms_test.dart, packages/postgrest/test/resource_embedding_test.dart
Tests cover default ascending order, explicit directions, null handling, referenced tables, chained orders, and complete result sequences.
Stream and example ordering updates
packages/supabase/test/mock_test.dart, examples/database_crud/lib/tasks_repository.dart
Stream tests cover default ascending and explicit descending order. The CRUD example orders tasks by priority and descending creation time.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: tr00d

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the breaking change to the default order direction.
Linked Issues check ✅ Passed The changes implement issue #1638 by updating both order() defaults, documentation, migration guidance, and test coverage.
Out of Scope Changes check ✅ Passed All changes directly support the requested order() default change and its required documentation and test updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch breaking/postgrest-order-ascending-default

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@spydon spydon changed the title feat!: make order() default to ascending fix!: make order() default to ascending Aug 6, 2026
@spydon
spydon force-pushed the breaking/postgrest-order-ascending-default branch from fc1afc6 to b99c800 Compare August 6, 2026 09:07
`PostgrestTransformBuilder.order()` and `SupabaseStreamBuilder.order()`
defaulted `ascending` to `false`, so `.order('created_at')` returned rows in
descending order. That was inherited from the pre-TypeScript postgrest-js in
2020 and never revisited after postgrest-js moved to `ascending = true`, which
left Dart disagreeing with both SQL's `ORDER BY` and every other client
library.

Both defaults are now `true`. Callers that want descending order pass
`ascending: false` explicitly.

Tests that were exercising the old implicit default now request descending
explicitly, and the new default is covered by an offline query parameter test,
a postgrest integration test and a stream test.

`MIGRATION.md` gets a v2 to v3 section for it, since the change is silent and
needs checking at every call site rather than being caught by the analyzer.

BREAKING CHANGE: `order()` now sorts ascending when `ascending` is omitted, in
both postgrest and the supabase stream builder. Calls that relied on the old
default silently reverse their result order and have to pass
`ascending: false`.
@spydon
spydon force-pushed the breaking/postgrest-order-ascending-default branch from b99c800 to ed13a52 Compare August 6, 2026 09:30
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@QuintinWillison QuintinWillison left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@spydon
spydon merged commit e06f230 into main Aug 6, 2026
52 of 54 checks passed
@spydon
spydon deleted the breaking/postgrest-order-ascending-default branch August 6, 2026 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3: flip order() default to ascending: true

3 participants