# Lakebed Examples

Use these capsules as patterns for app-building agents. Each example is a complete Lakebed app with the same file layout your generated app should use:

```txt
server/index.ts
client/index.tsx
shared/
```

## What To Copy

- Put schema, queries, and mutations in `server/index.ts`.
- Keep validation helpers and shared types in `shared/`.
- Export `App` from `client/index.tsx`.
- Declare Lakebed user references with `userId()`. Read the caller's identity with `ctx.auth.requireIdentity()` and filter user-owned rows by its `userId`.
- Use `isGuest` and `isSignedIn` to distinguish protected guests from accounts. Use `requireSignedIn()` for account-only operations.
- Set `auth: { requireSignIn: true }` to block all app data access before sign-in.
- Declare indexes for filtered queries and use `withIndex`, `order`, and an async terminal such as `collect` or `take`.
- Await database reads and writes in server handlers.
- Create one `createClient<typeof app>()` and use `client.useQuery` and `client.useMutation` rather than inventing a client API. `client.useQuery` returns `undefined` until the first result arrives.
- Style with Tailwind classes in JSX.

Both checked-in examples use database API v1. For an older capsule that still uses `where`, `orderBy`, `limit`, `all`,
or synchronous database calls, follow the [Database API v1 migration guide](https://docs.lakebed.dev/database-migration/index.md).

## Examples

- [`todo`](https://docs.lakebed.dev/examples/todo/index.md): per-user rows, ownership checks, checkbox mutation, clear-completed mutation.
- [`guestbook`](https://docs.lakebed.dev/examples/guestbook/index.md): shared feed, author metadata from auth, bounded text validation.

Both examples use `userId()` fields, so guest references transfer on Google sign-in. Todo lists combine with an existing account's todos. Guestbook entries remain shared, and their author references follow the account. Stored author names and pictures are snapshots and do not change during transfer.

Run the checked-in todo example:

```sh
npx lakebed dev examples/todo
```

Guest access uses protected browser sessions by default. For local tests only, open separate tabs with named guest overrides:

```txt
http://localhost:3000/?lakebed_guest=alice
http://localhost:3000/?lakebed_guest=bob
```

Named overrides cannot upgrade to accounts. Test sign-in and transfer with the default browser session instead. See the [auth guide](https://docs.lakebed.dev/auth/index.md) for session limits and merge hooks.
