-- 0006_views.sql — per-user view counts. -- -- "Most Viewed" is a per-user notion, not a household one: what I re-read is -- not what you re-read. Counted when an item is actually opened in a reader, -- not when its detail page is fetched, so the number means "consumed". -- -- Only the current view/play count and the last time are stored; a full event -- log would grow without bound for a feature this simple. CREATE TABLE item_views ( user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, item_id INTEGER NOT NULL REFERENCES media_items(id) ON DELETE CASCADE, views INTEGER NOT NULL DEFAULT 0, last_viewed_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), PRIMARY KEY (user_id, item_id) ); CREATE INDEX idx_item_views_user ON item_views(user_id, views DESC);