Skip to content

Commit 2be56ab

Browse files
cursoragentjherr
andcommitted
fix(examples): voting resolves a post by id, then by locator
#38 narrows get's locator to the locator index (the slug, or the id of a document without one), so get({ locator: 'p1' }) no longer finds a post whose slug is '/first'. Try the id first; this works before and after #38. Co-authored-by: jherr <jherr@pobox.com>
1 parent d951055 commit 2be56ab

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎.claude/skills/voting/SKILL.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ export const castVote = createServerFn({ method: 'POST' })
209209
```
210210

211211
The worked example reads the post with
212-
`getAs(publicContext(), { collection: 'post', locator: postId })`, which
213-
matches the id or the slug, so links using either form work.
212+
`getAs(publicContext(), { collection: 'post', id: postId })`, then
213+
`{ locator: postId }` (the slug) when no post has that id, so links using
214+
either form work.
214215

215216
**Atomicity gap (report it, don't hide it).** `commitDocument` and
216217
`deleteDocument` each run in their own storage transaction, so the vote write

‎examples/blog-voting/src/voting/votes.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ const VOTES = systemContext('voting: votes and tallies are server-owned')
6060

6161
/** A published post by id or slug: voters only ever see published posts. */
6262
async function resolvePost(postId: string): Promise<{ id: string; doc: Doc }> {
63-
const post = await getAs(publicContext(), {
64-
collection: 'post',
65-
locator: postId,
66-
populate: false,
67-
})
63+
const ctx = publicContext()
64+
const post =
65+
(await getAs(ctx, { collection: 'post', id: postId, populate: false })) ??
66+
(await getAs(ctx, {
67+
collection: 'post',
68+
locator: postId,
69+
populate: false,
70+
}))
6871
if (post) return { id: post.id, doc: post.doc }
6972
throw new Error(`Post not found: ${postId}`)
7073
}

0 commit comments

Comments
 (0)