Chapter 5

Remembering Everything: The Database

Understand how data is stored, organized, and retrieved.

Why Do We Need a Database?

When you create a habit, write a todo, or track a project, that data needs to live somewhere permanent. If we only stored it in the browser's memory, everything would vanish when you close the tab. A database is a specialized program that stores data on a server so it persists across sessions, devices, and time.

The Spreadsheet Analogy

Think of a database like a collection of spreadsheets

Each table is like a sheet — Habit, Todo, Project. Each row is one entry (one specific habit). Each column is a property (title, color, frequency). The key difference: databases enforce structure (every habit MUST have a title) and handle millions of rows efficiently.

Prisma: Speaking to the Database in JavaScript

Instead of writing raw SQL queries, StudyTracker uses Prisma ORM — a tool that lets you interact with the database using JavaScript/TypeScript methods. prisma.habit.findMany() becomes SELECT * FROM Habit. This gives you type safety, autocompletion, and no risk of SQL injection.

ORM (Object-Relational Mapping)

A layer that translates between your programming language's objects and the database's tables. Instead of SELECT * FROM habits WHERE userId = '123', you write prisma.habit.findMany({ where: { userId: '123' } }).

The Schema: Your Data Blueprint

The Prisma schema (in prisma/schema.prisma) defines every table, column, type, and relationship in your database. When you change the schema and run prisma migrate, Prisma automatically generates the SQL to update your database structure.

How Tables Connect

Data in different tables is linked through foreign keys. A HabitLog has a habitId field that points to a specific Habit. This means one habit can have many logs — a one-to-many relationship. Click the tables below to explore StudyTracker's data model:

habithabitlog(has many)
objectivekeyresult(has many)

Click a table to see details. StudyTracker has 13 database tables in total.

StudyTracker has 13 database tables across its 9 modules. The most complex relationships are in the OKR module: an Objective has many KeyResult entries, and each KeyResult has many CheckIn records — a three-level hierarchy.

Test Your Knowledge

Question 1 of 3Score: 0

What is an ORM?