Build series · Technical design

Designing the database.

RepGarden’s database has 45 tables, but six decisions shaped almost all of them: what gets copied, what gets stored, how a day is recorded, who owns a row and which rules the database keeps itself. Here they are, with the other way each could have gone.

By Ahmed Gadir7 min read

The map

RepGarden has two sides. A personal trainer, Benji in our examples, builds workouts and programmes on a website and gives them to his clients. A client, Sam, opens the phone app, does the workout and logs every set, and each set grows a vegetable. The database has to hold both sides and the line between them.

Most of its 45 tables hang off nine. Benji’s library is three of them: exercises, workouts made of exercises, and programmes made of workouts. When Benji gives a programme to Sam, it becomes a plan, Sam’s own calendar of planned workouts. What Sam actually does lands in logged sets, each one logged against the workout it was planned in, and a clip of a set hangs off that set. Benji and Sam are joined by a link, which matters more than it looks.

The nine tables at the centre of RepGarden’s databaseTrainer and client are joined by a link with a start and an end. The trainer builds a library: programmes made of workouts, workouts that use exercises. A programme is copied into a plan for one client, which holds planned workouts released a week at a time; each planned workout points at its exercises rather than copying them. The client logs sets, each logged against the planned workout, and a clip films one logged set.linkedbuildsmade ofusescopiedforweeklypointsatloggedagainstfilmsTrainerClientProgrammeWorkoutExercisePlanPlannedworkoutLogged setClipBenji’s librarySam’s plan and what he did
Nine of the 45 tables. Benji’s library on the left, Sam’s copy of it in the middle, and what Sam actually did on the right. Goals, weigh-ins, check-ins, replies, pauses, the game’s ledgers and payments hang off these.

The other 36 tables are the things around the edges: goals, weigh-ins, check-ins, Benji’s replies to clips, pauses, the XP and coins the game pays, payments and accounts. They follow the same six decisions.

A plan is a copy

When Benji gives his Push, Pull and Legs programme to Sam, the database could do two things. It could keep a live link, so that Sam’s calendar reads the programme every time. Or it could copy the programme onto Sam’s calendar as Sam’s own rows. A live link sounds tidier, with one source of truth and an edit that reaches everyone.

It breaks the first time Benji edits the programme. Say Sam benched 100 kg for five last Monday, which was exactly the target. Next week Benji raises Push to 105 kg. With a live link, last Monday’s target is now 105 kg too, and Sam’s set has turned from on target to short of it after he did it. With a copy, last Monday stays as it was.

A live link against a copy, when the trainer edits the programmeBenji raises Push from 100 kg to 105 kg. With a live link, Sam’s workout last Monday now shows a 105 kg target, so the 100 kg for five he lifted is short of target after the fact. With a copy, which RepGarden uses, last Monday keeps its 100 kg target and the set stays on target.Benji raises Pushfrom 100 kg to 105 kgA live linkSam, last Monday: bench pressTarget105 kg, changedSam lifted100 kg × 5Short of target, after the factA copy (what RepGarden does)Sam, last Monday: bench pressTarget100 kg, as setSam lifted100 kg × 5On target, as it was
The same edit, two designs. A live link rewrites history; a copy leaves last week as it was, and lets Benji set next week’s weight for Sam alone.

So a plan is a copy, made when the programme is given. That also leaves room for what trainers actually do: Benji sets next week’s bench weight for Sam alone, which a shared template has nowhere to keep. When Benji improves a programme and wants a client to have it, the website gives him a button for that: give Sam the new version.

Two details make the copy work. The exercise itself is not copied: the plan points at it, so when Benji films a better demonstration clip, every client sees it. And the whole programme is copied at once but released a week at a time, because the game runs on weeks: a delivery of seeds on Monday and a harvest on Sunday night. Benji can see and adjust the weeks ahead; Sam’s phone gets one week when it arrives.

Store what happened, work out the rest

A logged set is a fact: this weight, this many reps, on this day. Almost everything else the app shows follows from facts like it. The set’s grade, whether it was a personal best, whether a workout was done on its day, early, late or missed, how far a goal has come, a coin balance: each is worked out by one rule every time a screen asks.

The alternative is to store them, with a “was a best” mark on the set and running totals on the client. Reads get simpler, and every correction becomes a hunt. If Sam fixes a typo and his 100 kg was really 90, a stored mark leaves a gold plant he didn’t earn, and something has to remember to repair it. Worked out, the gold simply moves to wherever the best now is.

There are deliberate exceptions, and they come from the game. XP and coins are promises that never go down, so they are kept as ledgers: a table that only ever gains rows, one per thing earned or spent. A correction never takes XP back, and the database refuses an edit or a delete on those tables at all.

A workout never moves

The first designs let Sam move a workout to another free day in its week. In the database, that is the phone editing the trainer’s plan, and the day the workout was meant to be is lost.

The rule now is that a workout has two dates: the day it was planned for, which is Benji’s, and the day it was done, which is Sam’s. Sam’s phone only ever writes what happened. Whether a workout was early, late, still open or missed is worked out from those two dates and the day of the next planned workout.

A workout keeps two dates: the day planned and the day doneA week from Monday to Sunday. Push was planned for Monday and done on Monday. Pull was planned for Wednesday and done on Thursday, a day late, which still counts. Legs was planned for Friday and never done; it stayed open until the week closed on Sunday night and is now missed. A ring marks the planned day, which is the trainer’s, and a filled dot the day done, which is the client’s.MTWTFSSPushdone on its dayPulldone a day lateLegsmissed, closed Sunday nightPlanned day, Benji’sDay done, Sam’s
The ring is the day Benji planned and the dot is the day Sam did it. Sam’s phone only ever writes the dot, so nothing it does can move Benji’s plan.

That one rule settled a run of questions that had been waiting. Sam can do the next planned workout early, within the same week. A missed workout can still be done until the next planned workout’s day arrives, and it closes for good with its week on Sunday night. A late workout counts, and Benji sees it on the day it was planned, marked with the day it was done, never as a miss.

The phone also makes the ids for workouts and sets itself. Sam can log a whole workout in a gym with no signal, and when the sets go up later, a set sent twice is still one row.

The simple way to say who Sam’s trainer is would be a trainer column on Sam’s row. It can only ever say “now”. It can’t say that Sam trained with Benji until 3 November, and it ties Sam’s data to Benji in a way that gets messy the day Sam leaves.

Instead, the link between a trainer and a client is a row of its own, with a start and an end. Benji’s right to see anything of Sam’s is exactly that open link, and nothing else. Everything Sam logs carries Sam’s id and no trainer’s, so it is Sam’s. When the link ends, because Sam leaves, Benji removes him or either of them deletes their account, one row changes and nothing is taken from Sam. Benji keeps a name and two dates for 30 days, so his list can say Sam left.

The trainer–client link has a start and an endA timeline. While the link between Benji and Sam is open, from the day Sam joins to the day he leaves, Benji can see Sam’s training. When it ends, Benji keeps only a name and two dates, for 30 days. Underneath, Sam’s sets, weigh-ins and clips run the whole length: they are Sam’s before, during and after.Benji can see Sam’s trainingSam joinsSam leavesthen a name and two dates,for 30 daysSam’s sets, weigh-ins and clipsSam’s before, during and after
Ending the link changes one row. Benji’s view of Sam stops; Sam’s own records don’t move.

One trainer per client is a rule over that table, too: there can only ever be one open link for a client, and the database itself enforces it.

One library for everyone

RepGarden ships with its own exercises, workouts and programmes, and every trainer adds their own. They could live in separate tables. Then every place that points at “an exercise” needs two columns, one of them always empty, and every list of exercises is two reads stitched together.

So they share the same three tables, and a trainer column says whose a row is, left empty for RepGarden’s own. When Benji wants his own clip on one of RepGarden’s exercises, that is a small row beside the exercise, not a copy of it.

Rules the database keeps itself

Every rule has to live somewhere, and the choice matters. A rule kept by the database holds whatever code talks to it, including a bug written next year. A rule kept by the server’s code can be richer, but every route has to remember it. RepGarden splits them like this:

  • Kept by the database: one open link per client; an exercise someone has logged can’t be deleted, only retired, so no history ever points at nothing; the XP and coin tables only gain rows; and deleting an account removes that person’s rows.
  • Kept by the server: a trainer reads a client’s rows only while their link is open, in one check every request passes through; a trainer changes only their own exercises, workouts and programmes; and the client app simply has no operation for payments, prices or other clients, so there is nothing to protect.

How the decisions were made

The database came after the screens. Every screen of the phone app and the website had been designed first, so the model could be drawn from what the screens actually show and do. Claude drew the whole thing as one web page: every table, what points at what, who may read each one, example rows with Benji’s and Sam’s data, and ten worked examples, such as Sam doing one workout a day late, missing one for good and doing one early, with the rows each step reads, adds or changes. The page is built from a single file, and it refuses to build if a line points at a table that doesn’t exist.

Every choice it made, and every question it couldn’t answer from the designs, went onto a page of decisions for me, written in plain English with the options and a recommendation. There were 49 for the database alone. Most I took as recommended. Some I didn’t: a fully paused week now pushes the rest of a plan back by a week, trainers can pause a client’s plan too, and pounds are in the first version alongside kilograms.

Where things stand

None of this is built. The model doesn’t depend on which database product runs it, and that choice comes in the technical design, next. Passwords, card details and the like are not in it: sign-in and payment are handled by outside services, and the database keeps only their ids.

RepGarden has its own website with a waiting list, at repgarden.app.