lifetracker/apps/docs/docs/how-i-built-this/03-database.md

57 lines
1.5 KiB
Markdown

---
description: The scary stuff.
---
# Database
The scary part.
This is where my casual fucking around is about to slam face-first into a brick wall.
It's time to:
- Create a database package.
- ~Connect to a postgres database.~ JK, Fuck postgres, I'll use SQLite to keep things easy for now.
I'll worry about formalizing these things later (like, how can I configure these through environment variables, make things multi-user... etc. Not my problem right now. This is single-user and mostly for learning).
## Setting Up Drizzle
Continuing my copy-catting from Hoarder, it looks like Drizzle is the thing to use for database schema type shit in Javascript/Typescript/Node land.
Let's see how that works.
### Create folder
`mkdir packages/db`.
`pnpm init`.
Shamelessly copy code from Hoarder.
### Set up SQLite with Drizzle
```
pnpm add -D drizzle-kit
```
Create `drizzle.config.ts`.
Honestly. just copy the code from the following files:
- drizzle.config.ts
- drizzle.ts
- index.ts
- migrate.ts
- package.json
- schema.ts (PLACEHOLDER SHIT FROM TUTORIAL)
Then, *from the db directory*, run `pnpm drizzle-kit generate` and it'll make the fucking database file, after which, from the workspace project root, `pnpm db:migrate` will do the goddamn migration, after which, `pnpm db:studio` will load Drizzle Studio at some arcane Avahi `local.drizzle.studio` URL and you'll see the fucking table it made.
I hate Javascript development.
But, it's starting to work, goddamn it.
### Load all this crap from the web app
TODO tomorrow.