📝 Document the PocketBase and SignalDB stack

The README now describes the PocketBase binary, the per-user settings,
the admin UI, the sign-in page and the import-legacy command. PLAN.md
records the decisions of the migration and marks the sections that
describe the old backend as superseded.
This commit is contained in:
2026-11-30 17:43:13 +00:00
parent 74ebd2b38f
commit 6a95b885b8
2 changed files with 120 additions and 85 deletions
+19
View File
@@ -297,6 +297,25 @@ Switched from online-first/offline-fallback to fully offline-first.
- `+layout.svelte`: removed offline banner (no longer meaningful; behaviour is identical online or offline). - `+layout.svelte`: removed offline banner (no longer meaningful; behaviour is identical online or offline).
- `online.ts`: deleted (unused). - `online.ts`: deleted (unused).
### M12 — PocketBase + SignalDB ✅
The backend is now an embedded PocketBase (v0.40.6), which replaces `chi`, the hand-rolled SQLite store, the token auth and the sync log. On the client, SignalDB replaces Dexie and the outbox; collections persist in IndexedDB (one database per user) and sync with the server.
Decisions:
- Typed PocketBase collections instead of opaque payload blobs (the server computes closed days and weeks).
- Open sign-up with owner scoped rules.
- Clients are the only writers of entries, settings and balance adjustments.
- Day and week closing stay server routes.
- All deletes are soft.
- `settings.timezone` is the source of truth for day keys instead of the `TZ` environment variable.
- 15 character PocketBase ids.
- `entries.end_time` 0 means running on the server and `null` in the client.
- Stale running entries are stopped at the end of their day by the client and by a server cron.
- `wotra import-legacy` imports the old SQLite database (`--email` of an existing account, optional `--tz`, `--dry-run`; replaces all data of that user).
The Stack, Project Layout, Database Schema, API Surface, Auth, Sync Protocol and Offline Strategy sections above describe the pre-M12 backend and are superseded where they conflict.
### M11 — Future ### M11 — Future
CSV/JSON export, monthly summary view. CSV/JSON export, monthly summary view.
+82 -66
View File
@@ -1,6 +1,6 @@
# Wotra — Working Time Tracker # Wotra — Working Time Tracker
A self-hosted working time tracker with a Go backend and a Svelte PWA frontend. Offline-capable, single-binary deploy. A self-hosted working time tracker with a single Go binary that embeds PocketBase and serves a Svelte PWA frontend. Offline-first, single-binary deploy.
## Features ## Features
@@ -8,81 +8,87 @@ A self-hosted working time tracker with a Go backend and a Svelte PWA frontend.
- **Close days**: merges all entries (worked_ms = sum of durations, breaks excluded). - **Close days**: merges all entries (worked_ms = sum of durations, breaks excluded).
- **Mark days** as holiday, vacation, or sick (auto-credits expected daily hours). - **Mark days** as holiday, vacation, or sick (auto-credits expected daily hours).
- **Close weeks**: computes overtime/undertime against a frozen snapshot of your expected hours. - **Close weeks**: computes overtime/undertime against a frozen snapshot of your expected hours.
- **Settings history**: change hours/week with an `effective_from` date — past closed weeks are unaffected. - **Settings history**: change hours/week, workdays and timezone with an `effective_from` date; past closed weeks are unaffected.
- **PWA + offline**: works without a network connection; syncs via IndexedDB outbox when back online. - **Accounts**: email + password sign-up on the login page, optional OAuth2 providers.
- **PocketBase admin UI** at `/_/`: manage users, collections and OAuth2.
- **Offline-first PWA**: SignalDB collections persist in IndexedDB (one database per user) and sync with the server, so the app keeps working without a network connection.
- **CSV export**: entries, days, weeks. - **CSV export**: entries, days, weeks.
- **Midnight guard**: running entries are automatically stopped at 23:59:59 if they cross midnight. - **Midnight guard**: stale running entries are automatically stopped at the end of their day (by the client and a server cron).
## Quick Start ## Quick Start
Tool versions are declared in `mise.toml`. Install [mise](https://mise.jdx.dev) and run `mise install` once to get the correct Go and Node versions. Tool versions are declared in `mise.toml`. Install [mise](https://mise.jdx.dev) and run `mise install` once to get the correct Go and Node versions.
**Note**: Dependencies are automatically installed when needed. If you want to manually trigger dependency installation, run `mise deps`.
### Development ### Development
```bash ```bash
# Start both servers (Go API + Vite dev server) # Start PocketBase API and Vite dev server
mise run dev mise run dev
# Go API: http://localhost:8080 # PocketBase API: http://localhost:8080 (admin UI: /_/)
# Vite UI: http://localhost:5173 (with /api proxy) # Vite UI: http://localhost:5173 (with /api proxy)
``` ```
**Note**: Dependencies are automatically installed when needed. If you want to manually trigger dependency installation, run `mise deps`. Open `http://localhost:5173` for the UI and `http://localhost:8080` for the API. The PocketBase admin UI is at `http://localhost:8080/_/`. Create an account on the sign-in page.
`AUTH_TOKEN` defaults to `devtoken` in `mise.toml`. Override it by setting the variable in your environment before running.
Open `http://localhost:5173`, go to **Settings**, enter your token.
### Production (single binary) ### Production (single binary)
```bash The frontend build must run before `go build`, because the binary embeds `web/build/`.
mise run build # builds web/ then embeds it in the Go binary
AUTH_TOKEN=mysecrettoken ./wotra ```bash
mise run build # builds web/ (npm run build) then the Go binary
./wotra serve --http=0.0.0.0:8080 --dir=./pb_data
# open http://localhost:8080 # open http://localhost:8080
``` ```
On first run, create the first superuser via the admin UI at `http://localhost:8080/_/`, or with:
```bash
./wotra superuser create <email> <password>
```
### Container
```bash
podman build -t wotra .
podman run -p 8080:8080 -v wotra-data:/pb_data wotra
```
### All tasks ### All tasks
| Task | Description | | Task | Description |
|---------------|--------------------------------------------------| |----------------------|------------------------------------------------------|
| `mise run build` | Build production binary (web + Go) | | `mise run build` | Build production binary (web + Go, sequential) |
| `mise run build:web` | Build Svelte frontend only | | `mise run build:web` | Build Svelte frontend only |
| `mise run build:go` | Build Go binary only | | `mise run build:go` | Build Go binary only |
| `mise run dev` | Start API + UI dev servers concurrently | | `mise run dev` | Start PocketBase API + Vite dev servers concurrently |
| `mise run dev:api` | Start Go API server only | | `mise run dev:api` | Start PocketBase dev server only |
| `mise run dev:ui` | Start Vite dev server only | | `mise run dev:ui` | Start Vite dev server only |
| `mise run test` | Run all Go tests | | `mise run test` | Run all Go tests |
| `mise run test:web` | Run frontend Vitest unit tests |
| `mise run test:all` | Run Go tests and frontend tests |
| `mise run check` | Lint and typecheck (go vet, gofmt, svelte-check) |
| `mise deps` | Install/update project dependencies | | `mise deps` | Install/update project dependencies |
| `mise run clean` | Remove build artifacts | | `mise run clean` | Remove build artifacts (recreates `web/build/.gitkeep`) |
## Configuration (environment variables) ## PocketBase Admin UI and OAuth2
| Variable | Default | Description | The PocketBase admin UI lives at `/_/` on the server host. There you manage collections and users. Accounts live in the `users` collection (email + password); sign-up happens on the login page. OAuth2 providers are optional and configured in the admin UI (users collection, Options, OAuth2), with the redirect URL `https://<host>/api/oauth2-redirect`.
|-------------|-------------|----------------------------------------------|
| `AUTH_TOKEN` | **required** | Bearer token for API access | Hours per week, workdays and timezone are per-user settings inside the app; the timezone setting is what day keys are computed with. New users get one default settings row (40 h, Monday to Friday, UTC, effective 2000-01-01).
| `PORT` | `8080` | HTTP port |
| `DB_PATH` | `wotra.db` | SQLite database file path |
| `TZ` | `UTC` | Timezone for day/week key calculation |
## API Reference ## API Reference
All API endpoints require `Authorization: Bearer <token>` except `/healthz`. All routes require an authenticated session except `/healthz` and `/api/health` (PocketBase health endpoint).
### Entries Entries, settings and balance adjustments are written by clients through the sync push (`POST /api/sync/push`), not through REST endpoints.
| Method | Path | Description |
|--------|-----------------------------|--------------------------|
| POST | `/api/entries/start` | Start tracking |
| POST | `/api/entries/{id}/stop` | Stop a specific entry |
| GET | `/api/entries?from=&to=` | List entries by date |
| PUT | `/api/entries/{id}` | Edit start/end/note |
| DELETE | `/api/entries/{id}` | Soft-delete an entry |
### Days ### Days
| Method | Path | Description | | Method | Path | Description |
|--------|-------------------------------|--------------------------------------| |--------|---------------------------|-----------------------------------|
| GET | `/api/days?from=&to=` | List closed days |
| POST | `/api/days/{day}/close` | Close a day (merge entries) | | POST | `/api/days/{day}/close` | Close a day (merge entries) |
| POST | `/api/days/{day}/mark` | Mark as holiday/vacation/sick | | POST | `/api/days/{day}/mark` | Mark as holiday/vacation/sick |
| DELETE | `/api/days/{day}/close` | Reopen a closed day | | DELETE | `/api/days/{day}/close` | Reopen a closed day |
@@ -90,25 +96,14 @@ All API endpoints require `Authorization: Bearer <token>` except `/healthz`.
### Weeks ### Weeks
| Method | Path | Description | | Method | Path | Description |
|--------|-------------------------------|--------------------------------------| |--------|----------------------------|-----------------------------------|
| GET | `/api/weeks?from=&to=` | List closed weeks |
| POST | `/api/weeks/{week}/close` | Close a week (compute overtime) | | POST | `/api/weeks/{week}/close` | Close a week (compute overtime) |
| DELETE | `/api/weeks/{week}/close` | Reopen a closed week | | DELETE | `/api/weeks/{week}/close` | Reopen a closed week |
Week key format: `YYYY-Www` (e.g. `2024-W03`).
### Settings
| Method | Path | Description |
|--------|-------------------------|------------------------------------|
| GET | `/api/settings` | Current effective settings |
| PUT | `/api/settings` | Add a new settings version |
| GET | `/api/settings/history` | All settings history |
### Export ### Export
| Method | Path | Description | | Method | Path | Description |
|--------|------------------------------|--------------------------| |--------|-----------------------------|--------------------------|
| GET | `/api/export/entries.csv` | Export entries as CSV | | GET | `/api/export/entries.csv` | Export entries as CSV |
| GET | `/api/export/days.csv` | Export days as CSV | | GET | `/api/export/days.csv` | Export days as CSV |
| GET | `/api/export/weeks.csv` | Export weeks as CSV | | GET | `/api/export/weeks.csv` | Export weeks as CSV |
@@ -116,33 +111,54 @@ Week key format: `YYYY-Www` (e.g. `2024-W03`).
### Sync ### Sync
| Method | Path | Description | | Method | Path | Description |
|--------|-------------------|-------------------------------------| |--------|-----------------------|----------------------------------------------------------|
| POST | `/api/sync/pull` | Pull changes since a version | | POST | `/api/sync/push` | Batch push of client changes (entries, settings, balance adjustments) |
| POST | `/api/sync/push` | Push local changes (advisory) |
Pulls use the PocketBase `updated` cursor; pushes are batched. Entries, settings and balance adjustments are written exclusively through this endpoint.
### Health ### Health
| Method | Path | Description | | Method | Path | Description |
|--------|-------------|--------------------------| |--------|----------------|----------------------------------------|
| GET | `/healthz` | Unauthenticated health | | GET | `/healthz` | Unauthenticated wotra health endpoint |
| GET | `/api/health` | PocketBase health endpoint |
## Migrating from the legacy version
If you ran a pre-PocketBase version of wotra, import your old SQLite database:
1. Back up the legacy database: `cp wotra.db wotra.db.bak`.
2. Build and start the new version, then create your account in the app (sign-up on the login page). The account must already exist before importing.
3. Stop the server.
4. Run the import with the same `--dir` as the server:
```bash
./wotra import-legacy --db wotra.db --email <your account email> [--tz <IANA zone>] [--dry-run]
```
The import replaces all data of that user. `--tz` overrides the timezone stored in the imported settings rows; day keys are imported unchanged. Use `--dry-run` to preview first.
## Architecture ## Architecture
``` ```
┌─────────────────────────┐ ┌──────────────────────────┐ ┌────────────────────────────┐ ┌───────────────────────────────────┐
│ Svelte PWA (client) │ ◄─────► │ Go service (API) │ │ Svelte PWA (client) │ │ Single Go binary (server) │
│ - IndexedDB (Dexie) │ HTTPS │ - REST/JSON endpoints │ │ - SignalDB collections │ HTTPS │ - Embedded PocketBase │
│ - Service Worker │ JSON │ - Business logic │ │ persisted in IndexedDB │ JSON + │ - Records API │
│ - Sync outbox │ │ - SQLite (modernc) │ │ - Service Worker │ SSE │ - Auth (users + OAuth2) │
└─────────────────────────┘ └──────────────────────────┘ │ - One database per user │ ◄───────► │ - Realtime (SSE) │
│ │ │ - Close day/week business logic │
│ │ │ - SQLite storage │
└────────────────────────────┘ └───────────────────────────────────┘
``` ```
Collections: `entries`, `closed_days`, `closed_weeks`, `settings_history`, `balance_adjustments` (all owner scoped).
See [PLAN.md](PLAN.md) for the full design document. See [PLAN.md](PLAN.md) for the full design document.
## Development Notes ## Development Notes
- **Go module**: `github.com/wotra/wotra` - **Day boundary**: entries must not cross midnight (in the user's timezone setting); stale running entries are stopped at the end of their day by the client and by a server cron.
- **SQLite driver**: `modernc.org/sqlite` (pure Go, no CGO) - **Settings history**: `effective_from` lets you change hours/week, workdays and timezone without rewriting past closed weeks.
- **Day boundary**: entries must not cross midnight; the server auto-stops any running entry at 23:59:59 local time.
- **Settings history**: `effective_from` lets you change hours/week without rewriting past closed weeks.
- **Overtime**: stored as a signed `delta_ms` on each `closed_weeks` row. Frozen at close time. - **Overtime**: stored as a signed `delta_ms` on each `closed_weeks` row. Frozen at close time.
- **Sync model**: SignalDB collections sync with the server; ids are client-generated 15 character PocketBase ids, conflicts resolve last-write-wins by `client_updated_at`, deletes are soft. Pulls walk the PocketBase `updated` cursor; pushes go through `POST /api/sync/push`.