From b0d8c02787b4d3fe1ef1d9a38bc23c84c8658327 Mon Sep 17 00:00:00 2001 From: Admin Date: Sat, 4 Apr 2026 23:47:23 +0500 Subject: [PATCH] fix: add created field to chapters_idx to fix recentlyUpdatedBooks 400 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chapters_idx was missing created/updated columns (never defined in the PocketBase schema), causing PocketBase to return 400 for any query sorted by -created. recentlyUpdatedBooks() uses this sort. - Add created date field to chapters_idx schema in pb-init-v3.sh (also added via add_field for existing installations) - Add idx_chapters_idx_created index for sort performance - Set created timestamp on first insert in upsertChapterIdx so new chapters are immediately sortable; existing records retain empty created and will sort to the back (acceptable — only affects home page recency) --- backend/internal/storage/store.go | 9 ++++++++- scripts/pb-init-v3.sh | 10 +++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/backend/internal/storage/store.go b/backend/internal/storage/store.go index 4a1fc9b..588071e 100644 --- a/backend/internal/storage/store.go +++ b/backend/internal/storage/store.go @@ -130,7 +130,14 @@ func (s *Store) upsertChapterIdx(ctx context.Context, slug string, ref domain.Ch return err } if len(items) == 0 { - postErr := s.pb.post(ctx, "/api/collections/chapters_idx/records", payload, nil) + // Set created timestamp on first insert so recentlyUpdatedBooks can sort by it. + insertPayload := map[string]any{ + "slug": slug, + "number": ref.Number, + "title": ref.Title, + "created": time.Now().UTC().Format(time.RFC3339), + } + postErr := s.pb.post(ctx, "/api/collections/chapters_idx/records", insertPayload, nil) if postErr == nil { return nil } diff --git a/scripts/pb-init-v3.sh b/scripts/pb-init-v3.sh index ca932f4..888d233 100755 --- a/scripts/pb-init-v3.sh +++ b/scripts/pb-init-v3.sh @@ -149,9 +149,10 @@ create "books" '{ create "chapters_idx" '{ "name":"chapters_idx","type":"base","fields":[ - {"name":"slug", "type":"text", "required":true}, - {"name":"number","type":"number", "required":true}, - {"name":"title", "type":"text"} + {"name":"slug", "type":"text", "required":true}, + {"name":"number", "type":"number", "required":true}, + {"name":"title", "type":"text"}, + {"name":"created", "type":"date"} ]}' create "ranking" '{ @@ -326,9 +327,12 @@ add_field "app_users" "polar_customer_id" "text" add_field "app_users" "polar_subscription_id" "text" add_field "user_library" "shelf" "text" add_field "user_sessions" "device_fingerprint" "text" +add_field "chapters_idx" "created" "date" # ── 6. Indexes ──────────────────────────────────────────────────────────────── add_index "chapters_idx" "idx_chapters_idx_slug_number" \ "CREATE UNIQUE INDEX idx_chapters_idx_slug_number ON chapters_idx (slug, number)" +add_index "chapters_idx" "idx_chapters_idx_created" \ + "CREATE INDEX idx_chapters_idx_created ON chapters_idx (created)" log "done"