Data model
The nine D1 tables, their relationships and how deletes work.
The schema in migrations/0001_init.sql has nine tables. Every id is an INTEGER PRIMARY KEY AUTOINCREMENT.
Tables and relationships#
| Table | Purpose | References |
|---|---|---|
users | API accounts that log in and receive tokens | |
customers | People who write reviews; separate table from users | |
recipe_classifications | Categories such as Meat & Veggis | |
recipes | Recipes with nutrition (a JSON text column) | recipe_classifications |
ingredients | Ingredient lines with unit and count | recipes |
weekly_menus | One row per week_number | |
weekly_recipe_map | Mapping of a recipe into a week | weekly_menus, recipes |
recipe_reviews | A customer scores a recipe | recipes, customers |
weekly_menu_reviews | A customer scores a week | weekly_menus, customers |
Uniqueness rules beyond the primary keys:
| Table | Unique on |
|---|---|
users, customers | public_id, name |
recipes | recipe_name |
recipe_classifications | name |
ingredients | ingredient_name + recipe_id |
weekly_menus | week_number |
recipe_reviews | recipe_id + customer_id |
weekly_menu_reviews | menu_id + customer_id |
Review scores carry a CHECK (score BETWEEN 1 AND 5) constraint, so an out-of-range score fails the insert even if a route missed it.
Deletes#
Two kinds of delete exist:
- Soft delete for recipes, classifications, reviews and menus: the row stays and
is_publishbecomes 0. List endpoints filter onis_publish = 1; reads by id still find the row. - Hard delete for ingredients: the row goes.
Deleting is refused with 400 when other rows depend on the resource: a recipe that appears in a Mapping, or a classification that recipes reference. Customers soft delete through is_active instead of is_publish.
Derived values#
A recipe response carries has_links (true when a mapping references it) and its full ingredient list. A weekly menu response carries recipes (its mappings) and score, the mean of its menu reviews, or 0 with no reviews. Both are computed per request, not stored.
Nutrition#
The 2021 Postgres schema stored nutrition as an hstore. D1 has no hstore, so nutrition is a JSON text column. Routes parse it on read and stringify it on write, so the API keeps accepting and returning a JSON object.