Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"js/ts.preferences.importModuleSpecifier": "non-relative"
"js/ts.preferences.importModuleSpecifier": "non-relative",
"cSpell.words": [
"configtest",
"enconf",
"enmod",
"Goodreads",
"ntnu",
"Readr",
"usermod"
]
}
28 changes: 21 additions & 7 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
# Readr - backend
# Readr - backend <!-- omit in toc -->

- [Getting started](#getting-started)
- [Installing PostgreSQL](#installing-postgresql)
- [Linux](#linux)
- [Windows](#windows)
- [MacOS](#macos)
- [Set the environment variables](#set-the-environment-variables)
- [Download the dataset](#download-the-dataset)
- [Set up the database](#set-up-the-database)
- [Use the database](#use-the-database)
- [After changing the database schema](#after-changing-the-database-schema)
- [Run the backend](#run-the-backend)
- [Testing the API](#testing-the-api)
- [GraphQL types](#graphql-types)

## Getting started

Expand Down Expand Up @@ -46,7 +60,7 @@ The simplest way to install Postgres on MacOS is to use [homebrew](https://brew.
brew install postgresql@18
```

### Setting the environment variables
### Set the environment variables

Start by copying the [.env.example](./.env.example) to `.env`:

Expand All @@ -64,7 +78,7 @@ Proceed to do the following:
- Create and insert the Discord client ID and client secret:
- This can be done by creating an application in [Discord's Developer Portal](https://discord.com/developers/applications)

### Downloading the book dataset
### Download the dataset

The book dataset is quite large (~70 MB), so we prefer to not keep it inside the Git repository.

Expand All @@ -85,7 +99,7 @@ After downloading:

Now you're ready to insert the data to the database!

### Setting up the database
### Set up the database

Copy the `.env.example` file and create your own, private and gitignored version:

Expand All @@ -96,7 +110,7 @@ cp .env.example .env

The default values work fine for local development.

### Using the database
### Use the database

When running the database and backend, please go into the backend folder using `cd backend`.

Expand Down Expand Up @@ -137,7 +151,7 @@ pnpm db:generate --name [short_migration_name]
> If you need to test changes locally, use `pnpm db:setup` to start a completely new database immediately,
> or use `pnpm db:push` to add changes to a database that is running already.
## Running the backend along with the frontend
## Run the backend

Make sure the frontend is running in another terminal window or background process.

Expand Down Expand Up @@ -185,7 +199,7 @@ that users must be logged in, and only see their own favorites).
> The test database is very small, so the tests do not check or verify the performance of our database.
> This is rather done by careful planning of indexes and checking the results of `EXPLAIN` queries to see if indexes and efficient strategies are used.
## GraphQL automatically generated types
## GraphQL types

Whenever you change the GraphQL schema in the folder [./src/schema](./src/schema/), please run the following command:

Expand Down
13 changes: 9 additions & 4 deletions docs/accessibility.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Accessibility
# Accessibility <!-- omit in toc -->

- [What we do well](#what-we-do-well)
- [Pointer cursor](#pointer-cursor)
- [Automatic testing](#automatic-testing)
- [Limitations and future work](#limitations-and-future-work)

## What we do well

Expand All @@ -18,13 +23,13 @@

- **Responsive layout.** The book grid goes from four columns to one as the screen gets narrower, and the book page stacks vertically on small screens, so the app can be used on phones.

## Pointer cursor on interactive elements
## Pointer cursor

Tailwind CSS v4 changed buttons to use `cursor: default` instead of `cursor: pointer`. We have deliberately chosen to keep the pointer cursor, which differs from the shadcn defaults. We have added cursor-pointer to the relevant shadcn class names.

A pointer cursor is a visual signal that an element can be clicked. Using it consistently on every button and dropdown item makes it easier to see what is interactive and avoids some elements looking like they cannot be clicked. Consistent cursors make the interface more predictable, which fits the WCAG principle that components should behave in a consistent way. Because the cursor only helps people who use a mouse, it is only one part of our accessibility work, and we also follow the other accessibility requirements described in this document.

## Automatic testing using Playwright
## Automatic testing

We use [Playwright](https://playwright.dev/) to perform automatic accessibility checks.

Expand All @@ -44,7 +49,7 @@ We're glad to share that we pass the automatic Playwright checks for all of the
Testing all of the WCAG requirements is difficult, and Playwright only tests for a subset of these.
We have rectified multiple issues highlighted by Playwright.

## Limitations/Future work
## Limitations and future work

This section is meant as an overview of potential improvements with regard to accessibility, and also shows that we are aware of the downsides of our implementation. The table has a column for the status at delivery 3, and the same table will be used to show what work has been done from delivery 3 to 4.

Expand Down
15 changes: 12 additions & 3 deletions docs/ai-documentation.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Use of AI in this project
# Use of AI in this project <!-- omit from toc -->

- [Overview](#overview)
- [How we keep humans in the loop](#how-we-keep-humans-in-the-loop)
- [Learning and AI](#learning-and-ai)
- [Code quality](#code-quality)
- [LLM restrictions](#llm-restrictions)
- [Examples of rejecting AI advice](#examples-of-rejecting-ai-advice)

## Overview

We have used Claude (through Claude Code) as a tool during development. In short, it was used for:

Expand Down Expand Up @@ -29,15 +38,15 @@ Furthermore, if used correctly, AI can be used to help explain concepts when you

No matter whether the code was written manually or using help from LLMs, all issues and pull requests must have a proper description, as required by our [CONTRIBUTING.md](../CONTRIBUTING.md) file. This forces all group members to explain their thought process, reasoning for their code, and makes it obvious if someone does not understand the code at all. Fortunately, we have not had such issues in our group, and practically all pull requests have had coherent descriptions which are easy to understand.

## Code generated by AI is quality-checked in the same strict way as all other code
## Code quality

All code that LLMs (and humans) write should be checked by ESLint, Prettier and our Vitest tests and E2E tests before pushing to git.
Furthermore, we have checks in CI (GitHub Actions) which run all of these, excluding the E2E tests (see the [project choices and reasoning documentation](./choices-and-reasoning.md) for further details).
These checks confirm whether the code passes these strict requirements, and if it does not, it will not get merged. It's that simple.

Some things cannot be checked automatically, such as some of the practices described in our [CONTRIBUTING.md](../CONTRIBUTING.md) file, so after the automatic checks, we still perform human review on all pull requests. In order for a pull request to be merged, all conversations must be resolved, and it needs at least one explicit approving review.

## LLMs are unpredictable - and their access is restricted based on that
## LLM restrictions

The LLMs used by our group do not have access to run any write operations using git. This means no automatic committing, pushing or otherwise adding code that has not been quality checked.
Furthermore, the LLMs only have access to the fewest tools they need to get the job done. That means reading and editing only the files they need, and performing only the terminal commands that are needed.
Expand Down
48 changes: 34 additions & 14 deletions docs/choices-and-reasoning.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# Readr - project choices and reasoning
# Project choices and reasoning <!-- omit in toc -->

- [Overview](#overview)
- [PostgreSQL as the database](#postgresql-as-the-database)
- [Drizzle ORM](#drizzle-orm)
- [Drizzle vs Prisma](#drizzle-vs-prisma)
- [GraphQL Codegen](#graphql-codegen)
- [Authentication (better-auth)](#authentication-better-auth)
- [Search state in the URL](#search-state-in-the-url)
- [Pagination](#pagination)
- [Missing ratings](#missing-ratings)
- [Sorting](#sorting)
- [Rating sort filter](#rating-sort-filter)
- [Author sorting](#author-sorting)
- [Genre selection](#genre-selection)
- [UI components: shadcn/ui on top of Base UI](#ui-components-shadcnui-on-top-of-base-ui)
- [Filter design](#filter-design)
- [E2E testing](#e2e-testing)
- [Specification changes](#specification-changes)

## Overview

Some parts of the stack were given by the course: React with TypeScript and Vite, a GraphQL backend, and our own database running directly on the VM without Docker. The choices below are the ones we made ourselves.

Expand All @@ -9,13 +29,13 @@ We ended up doing some things differently - this is described at the end of this

Our data can be modeled using relations: books belong to many genres, and users have favorites that point to books. PostgreSQL handles this well with foreign keys and joins, and supports case-insensitive search with `ILIKE`. It is also one of the most used databases in real production applications, comfortably handling hundreds of thousands to millions of records (i.e. SQL rows in the tables).

## Drizzle ORM and migrations
## Drizzle ORM

We define the database schema in TypeScript with [Drizzle](https://orm.drizzle.team/), which gives us type-checked queries in the resolvers in the Apollo Server backend.

### The (seemingly) difficult choice: Drizzle ORM vs Prisma ORM
### Drizzle vs Prisma

Drizzle is not yet fully released - v1 is a release candidate. Therefore, some changes could still happen, but they are less likely compared to a beta version.
Drizzle ORM is not yet fully released - v1 is a release candidate. Therefore, some changes could still happen, but they are less likely compared to a beta version.
There is also another ORM that's popular in the Node.js community, namely Prisma ORM.

We have decided to go with Drizzle ORM. Let's explain why.
Expand All @@ -40,23 +60,23 @@ In general, Drizzle ORM has worked great for Readr.

We generate TypeScript types from our GraphQL schema for both the backend resolvers and the frontend queries. If the schema changes, type errors show us every place that needs to be updated.

## better-auth for user accounts
## Authentication (better-auth)

better-auth stores users and sessions in our own PostgreSQL database through its Drizzle adapter, and gives us email/password login as well as GitHub and Discord login with little code. We added user accounts because favorites (and later reviews) are user-generated data that should be stored on our server. Our first version stored favorites in `localStorage`, which meant they only existed in one browser. With accounts, the data belongs to a user and follows them across devices.

## The URL holds the search state
## Search state in the URL

The search text, filters, sorting and page number are stored in the URL (`?q=...&genres=...&sort=...&page=...`) instead of in component state. This means a search can be shared as a link, survives a page reload, and the browser's back button works as expected. Together with Apollo Client's cache for data from the server, this covers our need for state management without a separate global store.

## Pagination with page numbers
## Pagination

Search results are fetched 12 at a time with `limit` and `offset`, together with the total number of matches (`totalCount`). Filtering, sorting and counting happen in the database, so the client never loads more than one page, no matter how large the dataset is. This lets us show page numbers and the number of results, and any page can be opened directly from the URL (`?page=40`), for example from a shared link.

We chose offset pagination based on how we expect the app to be used. Most users look at the first few pages of results, and change their search or filters if they don't find what they are looking for. The cost of `offset` only grows with how far out you go: to show page 600, the database has to step past all the rows on the pages before it. Since browsing that deep is very rare, we think page numbers are worth more to our users than what we would gain from keyset (cursor) pagination, which keeps every page equally fast but only lets you move to the next or previous page. The pagination in the interface shows the first and last page and the pages around the current one, so an arbitrary page can only be opened from the URL. With page numbers, the URL stays short and readable, and we can show which page you are on out of how many.

Keeping the first pages fast depends on database indexes. We have added indexes on the columns we sort by (number of ratings, rating, title, pages and publish year), on the language and book format filters, and trigram indexes for the search on title and author (see `backend/src/db/schema/books.ts`). If the dataset grows much larger, the total count could be capped or estimated instead of counting every match.

## Books without ratings are stored as NULL
## Missing ratings

The dataset uses a rating of 0.0 for books nobody has rated. We store these as `NULL` instead, so they aren't treated as the worst-rated books, and they are placed last when sorting by rating.

Expand All @@ -66,33 +86,33 @@ The search results can be sorted by relevance (the default), rating, title, publ

Relevance shows the books with the most ratings first, so well-known books come before books that few people have rated. When a search has no exact matches and we show books with similar titles or authors instead, the closest match comes first.

### Sorting by highest rating adds a minimum number of ratings
### Rating sort filter

When the user chooses "Rating: high to low", we also add the filter "10,000+ ratings", unless the user has already chosen a minimum number of ratings. A toast tells the user that the filter was added, and that it can be changed in the filters.

Without a minimum, this sort order is not very useful. More than 800 books in our dataset have a rating of 5.0, and the one with the most ratings has only 36, so about the first 70 pages would be books that almost nobody has rated. A single 5-star review from one of our users on a book without ratings would also place it among them. With the minimum, about 12,600 of our roughly 52,000 books are left, and it is the same limit as we use for the top-rated books on the home page.

We discussed whether a filter that is chosen for the user is more annoying than useful (#117). We kept it because it is easy to notice and easy to remove: the toast explains it, the filter button shows how many filters are active, and the minimum can be set back to "Any". We also never replace a minimum that the user has chosen. The filter stays when the user switches to another sort order, since filters and sorting are independent everywhere else.

### No sorting by author
### Author sorting

We do not offer sorting by author (#100). The author field in the dataset is free text and can hold several names, like "Author One, Author Two". We have no guarantee that the authors of a book are always listed in the same order, or whether a name is written as "First Last" or "Last, First", so sorting on this field would often give an order that looks random. Authors can be found with the search field instead, which searches both titles and authors.

## Selecting several genres shows books in any of them
## Genre selection

When several genres are selected, we show books that belong to at least one of them (OR), not all of them (AND). This is because it widens the search instead of quickly ending up with no results.

## UI components: shadcn/ui on top of Base UI

Base UI provides accessible components (keyboard navigation, focus handling and ARIA attributes), and shadcn/ui gives us styled versions whose code lives in our repository, so we can adjust them to our needs. We use Tailwind CSS for styling, which replaced our CSS module files.

## Design of book filters
## Filter design

The filters were first placed in a popover, with several of the filters being dropdowns inside it. This meant opening one menu right on top of another which was awkward to use and harder for keyboard and screen reader users to navigate. We tried a drawer which reduced the problem, but the dropdowns still opened inside it, so the double layer remained for both mobile and desktop users.

The filters are now always visible in a panel on the left side of the books on desktop, so there is no menu to open and no dropdown on top of another menu. On smaller screens, the same filters are shown in a sheet that slides in from the side. We chose a sheet over a drawer because it is a more natural fit for a menu coming in from the side, and because swipe-to-close is not needed and could close the menu by accident while the user is still choosing filters.

## Playwright E2E testing
## E2E testing

We use [Playwright](https://playwright.dev/) to run end-to-end (E2E) tests.

Expand All @@ -115,7 +135,7 @@ How many workers a machine can handle differs, so one worker gives the same resu
We do not use CI for E2E tests. Our VM only has 1 CPU core available and the tests take a very long time. They also slow down our app, which is running on the same server.
Instead, we use CI for other tasks, such as linting and formatting.

## Changes/not implemented yet from our specification
## Specification changes

- **Filters:** We filter on genres, language, book format, number of pages and a minimum number of ratings. The specification also mentioned author, publisher, place and person. Authors are covered by the search field, which searches both titles and authors, and we have not implemented filters for publisher, place and person. The dataset has columns for these: `publisher`, `setting` (places) and `characters` (persons). `publisher` is filled in for 93% of the books, but `setting` and `characters` only for 22% and 26%, so filters on those two would leave out most of the books.
- **Login:** Instead of Google login, we support email/password and GitHub and Discord login. Google was rejected since it required https connection, which we do not provide.
Expand Down
Loading