Review request

Habit tracker for my students, does this Next.js + Supabase setup look sane?

Solved · 162 viewsasked by rosa_m

Repo or live app

git.example.com/rosam/streaks ↗

I teach and built this so my students can track daily practice streaks. Cursor and I got the data model to a point where it works, but I copy-pasted a lot of Supabase examples and I'm not sure the table structure makes sense long-term.

Comment

2 answers

Marked as helpful by the asker
mira_dev

The schema is fine for the size you're at. One thing that will bite you: streaks has a current_streak column you update from the client on every check-in. That will drift out of sync the moment two devices check in close together.

Compute it instead with a Postgres function that counts consecutive days from checkins, called from a trigger after insert. Slightly more setup, never wrong.

Comment
I'll calculate the streak in a view instead of storing it. Is that the right direction? rosa_m
Yes. Derive it from the check-ins and there's nothing left to drift. mira_dev
hannah_reyes

Before you write that view, decide what a "day" is. checkins.created_at is a timestamptz stored in UTC, so a student who practises at 00:30 in Amsterdam checks in on the previous UTC day. Streaks then break for exactly the students who practise late, and those reports are miserable to debug.

Derive the day in the student's time zone inside the view:

(c.created_at at time zone p.timezone)::date as day

and count consecutive distinct days on that, so two check-ins on the same evening don't count as two days.

Comment
All my students are in one time zone. Can I just hardcode Europe/Amsterdam? rosa_m
For one class, yes. Keep it in one place in the view so it's a one-line change if that ever stops being true. hannah_reyes
Hardcoded in the view. The 'my streak broke at midnight' complaints from two students are gone. rosa_m