Store events and config in SQLite

Replace the JSON-file event and config stores with a SQLite database
(modernc.org/sqlite, pure-Go so the static build keeps CGO_ENABLED=0).
Last-write-wins now rides on the upsert's WHERE clause rather than a
Go-side map compare; the sync protocol and HTTP handlers are unchanged.

On first start the server auto-imports any legacy events.json/config.json
sitting in the data dir, renaming them to *.imported. The -data flag now
points at puppy.db; photos still live on the filesystem alongside it.
This commit is contained in:
Alexander Heldt
2026-07-09 16:56:35 +00:00
parent da692d84da
commit 9207aaa4aa
6 changed files with 281 additions and 129 deletions
+10 -4
View File
@@ -15,6 +15,10 @@ source-of-truth and sync between devices.
client POSTs its full event list to `/api/events/sync`. The server merges
it with its own copy using last-write-wins on `updatedAt` and returns the
merged set.
- The server keeps its copy in a SQLite database (`puppy.db`); events and the
shared profile are separate tables, and last-write-wins is enforced by the
upsert itself. On first start it auto-imports any legacy `events.json` /
`config.json` sitting alongside it, renaming them to `*.imported`.
- Service worker bypasses cache for `/api/*` so writes always hit the server
when online; static assets are still cached for offline use.
- The puppy's name and birthday are a shared profile stored on the host
@@ -35,7 +39,8 @@ puppy-tracker/
├── module.nix # systemd unit, StateDirectory, hardening
├── server/
│ ├── go.mod
── main.go # JSON-file store, LWW sync, static file serving
── go.sum
│ └── main.go # SQLite store, LWW sync, static file serving
└── src/ # the web app
├── index.html
├── app.js
@@ -52,7 +57,7 @@ nix run # http://localhost:8080, data in $XDG_DATA_HOME/pu
PUPPY_ADDR=:9000 nix run # custom port
# Hot-iterate (data in /tmp):
nix develop -c sh -c 'cd server && go run . -static ../src -data /tmp/puppy-events.json'
nix develop -c sh -c 'cd server && go run . -static ../src -data /tmp/puppy.db'
```
## Use it on NixOS
@@ -81,8 +86,9 @@ In your system flake:
}
```
The server runs as a `DynamicUser` systemd unit. Data is stored at
`/var/lib/puppy-tracker/events.json` via `StateDirectory`.
The server runs as a `DynamicUser` systemd unit. Data is stored in a SQLite
database at `/var/lib/puppy-tracker/puppy.db` via `StateDirectory` (with photos
alongside it under `photos/`).
## Notes