-
Notifications
You must be signed in to change notification settings - Fork 2
Paginated Lists
Note: This is very much a work in progress. This article describes how lists might work.
Lists are problematic. They're potentially infinite in size, totally ordered, and it's possible to insert or delete parts of the list at any time. Just think about how you'd write that. It ain't comforting.
This proposal suggests an implementation very similar to CouchDB views.
We define a new type:
-
query(...)
denoting a query that returns results of typeBook
. (This needs to be better defined).
And if you write that in the deprecated type language...
database({
types: {
Author: ({ Book, string, query }) => ({
fullName: string,
books: query(Book), // A query that returns a list of type "Book"
}),
Book: ({ string, Author }) => ({
title: string,
author: Author,
}),
},
})
Next, we create an indexing API with an emit(...)
function. Any time an indexed element is created or updated we call the associated indexing function:
database({
indexes: types => new Map([
[
types.Book, // Index all book types.
(book, emit) => {
// Index this book under author ID + book.id.
emit(`${book.author}:${book.id}`, book);
},
],
]),
});
You can emit an index multiple times. The second parameter is the value associated with the new index (book
in the example above).
The product of each index is stored in a B-tree map. That map is generated locally and never synchronized. Each replica will generate its own B-tree.
In the example, if you wanted to locate every book the author has ever written, you'd query ${authorId}:
. The query would iterate over the B-tree starting at ${authorId}:
and work forwards until either it ran out of results or there were too many items to return in a single response. B-trees should be implemented in storage adapters, not Mytosis itself.
Now any book listing that author will automatically show up in the query.
This is rather obvious, but it warrants saying: not all queries can be performed this way, but it shows an interesting model. You can build a query index based on live data. You query the index rather than the data itself.