Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fad0d24
feat: add basic Drizzle setup
martakam Oct 3, 2026
eee8907
feat: add basic Drizzle database schema
martakam Oct 3, 2026
901a309
feat: setup commands for local Postgres setup
martakam Oct 3, 2026
98eb942
feat: add seeding script for database
martakam Oct 3, 2026
4e23eb9
docs: add README to backend
martakam Oct 3, 2026
4793d2b
docs: move documentation for frontend
martakam Oct 3, 2026
6153d57
docs: add comment about separate frontend guide in backend guide
martakam Oct 3, 2026
6a02e9c
Merge branch 'feat/basic-graphql-setup' into feat/basic-drizzle-setup
martakam Oct 4, 2026
45e1227
chore: make Prettier ignore db-data
martakam Oct 4, 2026
32ff112
feat: add proper build and dev scripts with esbuild
martakam Oct 4, 2026
7444f2b
refactor: use absolute imports, drop .ts extension
martakam Oct 4, 2026
f5c4434
feat: force no .ts extension in imports
martakam Oct 4, 2026
f3b88a1
fix: insert last batch of books from dataset
martakam Oct 4, 2026
b6940ea
feat: add relations to db object to enable relational queries
martakam Oct 4, 2026
2f8aa76
feat: add resolver for searching books
martakam Oct 4, 2026
ebff158
docs: add note that running db and backend must be done in correct fo…
martakam Oct 7, 2026
b36f8d3
docs: add comment on setting up the dotenv values
martakam Oct 7, 2026
0e3fd63
fix: store NULL values in DB instead of empty strings
martakam Oct 7, 2026
721d29c
fix: do not search for books with empty strings
martakam Oct 7, 2026
7ce08f5
Merge branch 'main' into feat/basic-drizzle-setup
martakam Oct 7, 2026
f189cbc
Merge branch 'main' into feat/basic-drizzle-setup
martakam Oct 7, 2026
8da6599
Merge branch 'main' into feat/basic-drizzle-setup
martakam Oct 8, 2026
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ dist-ssr
# React Router
.react-router/
build/

db-data/
book-data.csv
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Visit our project here: [Readr](http://it2810-37.idi.ntnu.no/project2/)
> [!NOTE]
> The project is not deployed yet.
## Contributing and getting started
## Getting started

In order to get started with the project, check the [Contributing guidelines](./CONTRIBUTING.md).
In order to get started with the project, check the [README.md for the frontend](./frontend/README.md) and the [README.md for the backend](./backend/README.md).

## Testing

Expand Down
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=postgresql://postgres@localhost:5433/readr
1 change: 1 addition & 0 deletions backend/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pnpm-lock.yaml
*.generated.*
db-data
105 changes: 105 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,110 @@
# Readr - backend

## Getting started

This guide assumes you have set up `pnpm` and have a running frontend app.
If not, please follow the frontend guide in the [README.md for the frontend](../frontend/README.md) first.

### Installing PostgreSQL

As we do not use Docker, a local installation of PostgreSQL will be required.

#### Linux

Install Postgres using your package manager:

```sh
apt install postgresql # For Debian-based distributions
dnf install postgresql-server # For Red Hat-based distributions
```

Make sure the PostgreSQL version is 18.

#### Windows

Download and install PostgreSQL from the official [download page](https://www.postgresql.org/download/).

After installation, attempt to run these commands:

```sh
postgres --version
initdb --version
```

If both succeed, you can proceed with the instructions.

Otherwise, Postgres and its utilities need to be added to your `PATH`.

If you need help, you can use [this guide](https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/).
The most likely path that you need to add is `C:\Program Files\PostgreSQL\18\bin`, however, if you chose a different installation folder, you need to adjust this path.

#### MacOS

The simplest way to install Postgres on MacOS is to use [homebrew](https://brew.sh/):

```sh
brew install postgresql@18
```

### Setting the environment variables

Copy the [.env.example](./.env.example) and replace the `DATABASE_URL` if needed.

### Downloading the book dataset

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

The book dataset is available on Kaggle:
[Comprehensive Literary Greats Dataset](https://www.kaggle.com/datasets/thedevastator/comprehensive-literary-greats-dataset)

The easiest way to download it is using cURL:

```sh
curl -L -o comprehensive-literary-greats-dataset.zip https://www.kaggle.com/api/v1/datasets/download/thedevastator/comprehensive-literary-greats-dataset
```

After downloading:

- Extract the `.csv` file
- Rename it to `book-data.csv`
- Move it to [./src/db/seed](./src/db/seed/)

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

### Setting up the database

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

```sh
cd backend
cp .env.example .env
```

The default values work fine for local development.

### Using the database

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

We have multiple commands defined in [package.json](./package.json) which will make our lives easier.

The command `pnpm db:setup` does multiple steps automatically:

- Deletes any previously running database using `pnpm db:delete`
- Initializes the database using `pnpm db:init`
- Starts the database using `pnpm db:start`
- Creates the SQL database itself using `pnpm db:create`
- Pushes the newest version of our SQL schema using `pnpm db:push`
- Seeds the database with our dataset using `pnpm db:seed`

> [!CAUTION]
> `pnpm db:setup` and `pnpm db:delete` are destructive commands!
> They delete the local database running from the `db-data` folder.
The database files will be placed in a folder named `db-data`.

You can also use the `pnpm db:studio` command in order to inspect the database live.

## GraphQL automatically generated types

Whenever you change the GraphQL schema in the folder [./src/schema](./src/schema/), please run the following command:
Expand Down
1 change: 0 additions & 1 deletion backend/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const config: CodegenConfig = {
schema: "**/schema.graphql",
generates: {
"src/schema": defineConfig({
importExtension: ".ts",
typesPluginsConfig: { useTypeImports: true },
}),
},
Expand Down
11 changes: 11 additions & 0 deletions backend/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "dotenv/config";
import { defineConfig } from "drizzle-kit";

export default defineConfig({
out: "./drizzle",
schema: "./src/db/schema.ts",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
31 changes: 27 additions & 4 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@
"pnpm": ">=12.4.2"
},
"description": "",
"main": "dist/src/index.js",
"main": "dist/index.js",
"scripts": {
"prestart": "pnpm exec graphql-codegen",
"compile": "tsc",
"start": "pnpm compile && node ./dist/src/index.js"
"typecheck": "tsc",
"dev": "tsx src/index.ts",
"build": "esbuild src/index.ts --bundle --platform=node --format=esm --target=node24 --packages=external --outfile=dist/index.js",
"start": "pnpm build && node dist/index.js",
"db:init": "initdb -D db-data -U postgres -E UTF8 --auth=trust",
"db:start": "pg_ctl -D db-data -l ./db-data/db-data.log -w -o \"-p 5433 -c listen_addresses=127.0.0.1\" start",
"db:create": "createdb -h 127.0.0.1 -p 5433 -U postgres readr",
"db:stop": "pg_ctl -D db-data stop",
"db:status": "pg_ctl -D db-data status",
"db:psql": "psql -h 127.0.0.1 -p 5433 -U postgres readr",
"db:delete": "node -e \"require('child_process').spawnSync('pg_ctl',['-D','db-data','stop'],{stdio:'inherit'});require('fs').rmSync('db-data',{recursive:true,force:true})\"",
"db:push": "drizzle-kit push",
"db:generate": "drizzle-kit generate",
"db:migrate": "drizzle-kit migrate",
"db:studio": "drizzle-kit studio",
"db:seed": "tsx src/db/seed/index.ts",
"db:setup": "pnpm db:delete && pnpm run db:init && pnpm run db:start && pnpm run db:create && pnpm run db:push && pnpm run db:seed"
},
"keywords": [],
"author": "",
Expand All @@ -30,16 +45,24 @@
"@as-integrations/express5": "^1.1.2",
"@graphql-tools/merge": "^9.2.6",
"cors": "^2.8.6",
"csv-parse": "^7.0.3",
"dotenv": "^18.0.5",
"drizzle-orm": "1.0.0-rc.4",
"express": "^5.2.1",
"graphql": "^17.0.2"
"graphql": "^17.0.2",
"pg": "^8.23.1"
},
"devDependencies": {
"@eddeee888/gcg-typescript-resolver-files": "^0.19.0",
"@graphql-codegen/cli": "^7.4.3",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/node": "^26.6.4",
"@types/pg": "^8.23.1",
"drizzle-kit": "1.0.0-rc.4",
"esbuild": "0.28.2",
"prettier": "3.9.9",
"tsx": "^4.23.15",
"typescript": "^7.0.2"
}
}
Loading