Issue tracker with server and local filters
An issue tracker with 40 issues in two projects. Pick a project and a status tab, and the db sends them to the server.
Pick an assignee or type a search, and nothing is sent: the db filters the rows it already holds. The Query plan
panel prints explain() for the current list, so you can see which conditions went to the server and which did not.
Click an issue to open it. The header shows at once from the list row, and the description arrives with the full issue. Change its status, assignee or title: the list and the panel change together, and turn on Make writes fail to see both revert. Click Simulate a teammate, or open the page in a second tab and edit there, to watch issues move between tabs without a refetch.
npx tiged BetterTyped/hyper-fetch/examples/db/issue-tracker hyper-fetch-db-issue-tracker && cd hyper-fetch-db-issue-tracker && npm install && npm run dev- Server filters and local filters in one query: the loader declares
projectIdandstatus, so those are sent. The assignee is not declared, so the db filters it on the rows it holds. explain()returns the query plan: the loader, the conditions sent, the conditions run locally, whether held rows answered the query, and the socket topics that keep it live.- A join:
.join("assignee", db.user, ...)adds each issue's assignee. The assignees of the whole list load in oneGET /users?ids=...request. - List and detail: two collections for two shapes, linked so an edit of the full issue changes the list row too.
- Optimistic writes that update the list and the detail together, and revert both when the request fails.
- Realtime sync: socket events move issues in and out of filtered lists live, in every open tab.
How it works
Collections and loaders
db.tsdefines three collections.issueSummaryholds list rows,issueholds full issues with adescription, anduserholds the team.- The list loader maps
projectIdto the URL withrequired: true, andstatusto?status=. A query without aprojectIddoes not compile. issuehas abyKeyloader forGET /issues/:issueId.userhas two loaders on the same request: a list loader for the whole team, and abyKeysloader that reads several users by id in one request.createDb(..., { links: [["issue", "issueSummary"]] })says both collections hold the same records. Writes go throughissue, and the link carries them to the list row.
The list query
IssueList.tsxbuilds one query from the filters:eqonprojectId,eqonstatusunless the tab is "All", andeqonassigneeIdwhen an assignee is picked. Conditions in a top-levelandthat the loader declares are sent, the rest run locally.- Open "All", then "Done". The plan says the query was answered without a request: the loaded "All" list already holds
every issue of the project, so the db filters it. The server log shows no new
GET. - The text search is a plain
filterover the live result in React. The db operators compare whole values, so a substring match runs on the rows the query returned, also with no request. explain()reads the mounted query, soIssueListcalls it in an effect afteruseLiveQuerysubscribed, and again when the rows change.
The detail panel
IssueDetail.tsxreadsdb.issueSummary.findOne(id).local(). It only reads what the db holds, so the title, status and assignee show the moment you click.db.issue.findOne(id)loads the full issue through thebyKeyloader. The panel shows "Loading description..." until it arrives, and keeps the controls disabled, because edits go throughissue.db.issue.update(id, changes)is an optimistic write. A status change moves the row out of the current tab at once. It resolves with{ success }and never throws. When the server answers 500, the db has already reverted the panel and the list.
Realtime
- The server publishes the whole issue on
projects/:projectId/issues/savedafter every change. The sync'swherefills:projectIdfrom the query, so each list listens to its own project only. - On
issueSummarythe sync hastype: "insert": each event is a complete row that creates or replaces the stored one. A list only holds the rows of its filter, so an issue moved into "Todo" elsewhere has to arrive whole to join it. Anupdateevent only changes rows the db already holds. - On
issuethe same topic is anupdatesync. It changes the open issue and ignores the rest. broadcast-adapter.tscarries socket frames over aBroadcastChannel, andserver.tskeeps the issues inlocalStorage. Two tabs share one fake backend, and each hears the other's changes. With a real backend you pass no adapter and get WebSockets.
- Lists with filters where some run on the server and some do not, such as a status in the URL and a search on screen.
- Screens with a list and a detail view whose endpoints return different fields.
- Apps where other people change the same records, such as trackers, boards and admin panels.
Related
You split one query into conditions the server runs and conditions the db runs, and read the split with explain().
You joined assignees in one batched request, showed a detail header from the list row, saved edits optimistically
through a link, and kept every filtered list live from one socket topic.