Skip to content

Commit 10cb525

Browse files
ordering documentation
1 parent dd6ba75 commit 10cb525

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
## **Understanding Ordering in Magic IndexedDB**
2+
3+
One of the most critical design considerations of **Magic IndexedDB** is how **query ordering** works — or more accurately, how **you should not expect the returned results to be in the order you might assume**.
4+
5+
### **The Key Concept: Intent-Based Ordering vs. Return Ordering**
6+
7+
Magic IndexedDB is **not a traditional in-memory LINQ provider**, nor is it a SQL engine. It’s the world’s first **true LINQ-to-IndexedDB translation layer** — meaning it interprets your queries as _intent_, decomposes and optimizes them into efficient IndexedDB calls, and then reassembles the data according to your query semantics.
8+
9+
Here’s what that means in practice:
10+
11+
- **Your ordering intent is always honored.**
12+
If you write `.OrderByDescending(x => x.Created).Take(2).Skip(4)`, that exact _logic_ will be applied during query translation.
13+
The filtering, ordering, skipping, and taking all impact which items are retrieved.
14+
15+
- **But the order in which items are _returned_ is not guaranteed.**
16+
Due to the highly optimized and multi-query nature of Magic IndexedDB, results are often **streamed** or **yielded** back as they’re resolved. This eliminates the need to collect all data into memory first — which is a massive performance gain — but also means the final result **will not necessarily be in a sorted order when returned**.
17+
18+
19+
---
20+
21+
### **Why This Happens: Performance & Yield-First Design**
22+
23+
Magic IndexedDB was designed around **performance-first streaming**:
24+
25+
- Queries are split and optimized across indexes, cursors, and even compound key paths.
26+
- This allows data to be **yielded early** — meaning results start returning as soon as possible, with minimal memory overhead.
27+
- To support this, Magic IndexedDB does **not re-sort results in-memory before returning them**.
28+
29+
That would require full buffering, which defeats the purpose of streaming and would introduce latency and memory consumption — especially for large datasets.
30+
31+
---
32+
33+
### **What About `.ToList()` or Non-Yielded Queries?**
34+
35+
Some query methods **don’t use the streaming system** under the hood — they’ll buffer the results, and as a result, may return in the correct order.
36+
37+
But to keep things consistent, Magic IndexedDB **does not differentiate** based on the method called. Whether you use `.ToListAsync()` or `.AsAsyncEnumerable()`, **you should always assume that result ordering is not preserved**.
38+
39+
This simplifies expectations and behavior across the entire system.
40+
41+
---
42+
43+
### **What Should I Do If I Need Ordered Results?**
44+
45+
**Just sort the result in-memory after retrieval.**
46+
You’re guaranteed that the **correct items** were retrieved (based on your ordering, skip, take, etc.) — they just might not be in order.
47+
48+
Example:
49+
50+
```csharp
51+
var result = await db.Items
52+
.Where(x => x.Type == "Important")
53+
.OrderByDescending(x => x.Created)
54+
.Take(10)
55+
.ToListAsync();
56+
57+
// Reapply ordering in-memory (if needed)
58+
var sorted = result.OrderByDescending(x => x.Created).ToList();
59+
```
60+
61+
---
62+
63+
### **Can I Build a Wrapper That Orders for Me?**
64+
65+
Yes!
66+
One of the strengths of Magic IndexedDB’s **universal query format** is that it **preserves ordering intent**.
67+
68+
So wrapper libraries or platforms **can apply the expected order in-memory** based on the translated query. That’s entirely valid — but it's outside the scope of the core Magic IndexedDB system.
69+
70+
The **core system will always prioritize performance, streaming, and early return** — which is why ordering on return is intentionally **not supported**.
71+
72+
---
73+
74+
### **Final Thoughts**
75+
76+
This is one of the few **major differences** between Magic IndexedDB and a traditional LINQ-to-SQL engine. But it’s also what enables its **high-performance, low-memory footprint design** — perfect for modern web apps.
77+
78+
Ordering is honored in logic — but not in output. And once you understand this design principle, you’ll find that Magic IndexedDB gives you **complete control**, with **massive performance advantages**, and zero guesswork.

0 commit comments

Comments
 (0)