Remove events schema; Create days schema; Add some dev tooling; Document above
This commit is contained in:
parent
850149ebcb
commit
9602e55f04
@ -50,7 +50,3 @@ Then, *from the db directory*, run `pnpm drizzle-kit generate` and it'll make th
|
||||
I hate Javascript development.
|
||||
|
||||
But, it's starting to work, goddamn it.
|
||||
|
||||
### Load all this crap from the web app
|
||||
|
||||
TODO tomorrow.
|
||||
|
||||
83
apps/docs/docs/how-i-built-this/04-babys-first-schema.md
Normal file
83
apps/docs/docs/how-i-built-this/04-babys-first-schema.md
Normal file
@ -0,0 +1,83 @@
|
||||
---
|
||||
description: Time for some actual functionality, believe it or not.
|
||||
---
|
||||
|
||||
# Baby's First Schema
|
||||
|
||||
Unsurprisingly, this takes place in the database part of the project, which is `packages/db`.
|
||||
|
||||
The idea will be to define a new file, `day.ts`, in `packages/db/schema/`, and put the columns and constraints in there.
|
||||
|
||||
If I'm understanding correctly, a difference vs. Phoenix or Rails is that you don't have to write the migrations yourself: just edit the schema file and you can `drizzle-kit generate` the migrations straight out of the single file. Plus, it'll walk you through which columns you want to rename, which you want to make new, etc. Slick.
|
||||
|
||||
## But first, a detour...
|
||||
|
||||
I guess last time I had set up a schema in `packages/db/schema.ts`, and the migrations were in `packages/db/drizzle` -- I felt it was better to rename these:
|
||||
|
||||
- Schemas in their own directory (`schema`)
|
||||
- Migrations called `migrations`, not `drizzle`
|
||||
|
||||
These changes required editing the front matter of a few Drizzle ts files, which I'm sure you can figure out for yourself.
|
||||
|
||||
Then I edited `day.ts` and prepared to generate and run the migrations.
|
||||
|
||||
This was the harder part that I do want to document:
|
||||
|
||||
### 1. Why won't the db generate command work
|
||||
|
||||
After changing the "events" schema I had started with last time to a "days" schema -- mainly to see if the db could migrate from one schema to another conflicting one -- I wasn't able to run the generate command:
|
||||
|
||||
```
|
||||
pnpm drizzle-kit generate
|
||||
```
|
||||
|
||||
`drizzle-kit` wasn't in the PATH, which was actually irrelevant, because I wanted to invoke this using `pnpm db:generate` the way I was able to run `pnpm db:migrate` and `pnpm db:studio`. Why wasn't it working?
|
||||
|
||||
The answer involved two steps. First, I had to add in `packages/db/package.json` a script:
|
||||
|
||||
```js
|
||||
"scripts": {
|
||||
"generate": "drizzle-kit generate"
|
||||
}
|
||||
```
|
||||
|
||||
which was obvious. What was NOT obvious was that the `db:___` syntax wasn't filtering into the db package for me, it was just already defined for `migrate` and `studio` in the root level package.json. Added `db:generate` in there and everything worked great.
|
||||
|
||||
```js
|
||||
"scripts": {
|
||||
"build": "turbo build",
|
||||
"dev": "turbo dev",
|
||||
...
|
||||
"db:generate": "pnpm --filter @lifetracker/db generate",
|
||||
```
|
||||
|
||||
Now, why not have this somehow alias to the one in the `packages/db/package.json` file? IDK, but the migrate and studio ones were written this way, so fuck it.
|
||||
|
||||
### 2. Adding "db:studio" to the dev pipeline in Turbo
|
||||
|
||||
With the migration ostensibly done, I wanted to see the glorious new table definition in the Drizzle Studio, but despite having run `pnpm run dev`, it wasn't running. How could I get it to run alongside?
|
||||
|
||||
This was pretty cool. I changed this in `turbo.json` at the project root:
|
||||
|
||||
```js
|
||||
"dev": {
|
||||
"dependsOn": ["^dev"], # This is the key
|
||||
"cache": false,
|
||||
"persistent": true
|
||||
}
|
||||
```
|
||||
|
||||
... and in the db package's `package.json`, I added:
|
||||
|
||||
```js
|
||||
"scripts": {
|
||||
...
|
||||
"dev": "drizzle-kit studio",
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
This means that now when I run "pnpm dev" from the project root, along with Docusaurus (this shit) and the actual web app, I get the Drizzle Studio page simultaneously. So cool!
|
||||
|
||||
## Can we actually do something now?
|
||||
|
||||
@ -4,7 +4,7 @@ import type * as Preset from '@docusaurus/preset-classic';
|
||||
|
||||
const config: Config = {
|
||||
title: 'Lifetracker Docs',
|
||||
tagline: 'Dinosaurs are cool',
|
||||
tagline: 'Tracking the life of the life tracker',
|
||||
favicon: 'img/favicon.ico',
|
||||
|
||||
// Set the production url of your site here
|
||||
@ -93,8 +93,8 @@ const config: Config = {
|
||||
title: 'Docs',
|
||||
items: [
|
||||
{
|
||||
label: 'Tutorial',
|
||||
to: '/docs/intro',
|
||||
label: 'Dev Environment',
|
||||
to: '/docs/category/how-i-built-this',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@ -19,8 +19,8 @@ function HomepageHeader() {
|
||||
<div className={styles.buttons}>
|
||||
<Link
|
||||
className="button button--secondary button--lg"
|
||||
to="/docs/intro">
|
||||
Docusaurus Tutorial - 5min ⏱️
|
||||
to="/docs/category/how-i-built-this">
|
||||
Jump on in
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -6,13 +6,14 @@
|
||||
"dev": "turbo dev",
|
||||
"lint": "turbo lint",
|
||||
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
|
||||
"db:generate": "pnpm --filter @lifetracker/db generate",
|
||||
"db:studio": "pnpm --filter @lifetracker/db studio",
|
||||
"db:migrate": "pnpm --filter @lifetracker/db run migrate"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^3.2.5",
|
||||
"turbo": "^2.1.3",
|
||||
"typescript": "^5.4.5"
|
||||
"prettier": "^3.3.3",
|
||||
"turbo": "^2.2.3",
|
||||
"typescript": "^5.6.3"
|
||||
},
|
||||
"packageManager": "pnpm@9.10.0",
|
||||
"engines": {
|
||||
|
||||
@ -5,8 +5,8 @@ const databaseURL = "./lifetracker.db";
|
||||
|
||||
export default {
|
||||
dialect: "sqlite",
|
||||
schema: "./schema.ts",
|
||||
out: "./drizzle",
|
||||
schema: "./schema",
|
||||
out: "./migrations",
|
||||
dbCredentials: {
|
||||
url: databaseURL,
|
||||
},
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import "dotenv/config";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import Database from "better-sqlite3";
|
||||
import * as schema from "./schema";
|
||||
import * as schema from "./schema/day";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import path from "path";
|
||||
|
||||
|
||||
@ -2,10 +2,10 @@ import Database from "better-sqlite3";
|
||||
import { ExtractTablesWithRelations } from "drizzle-orm";
|
||||
import { SQLiteTransaction } from "drizzle-orm/sqlite-core";
|
||||
|
||||
import * as schema from "./schema";
|
||||
import * as schema from "./schema/day";
|
||||
|
||||
export { db } from "./drizzle";
|
||||
export * as schema from "./schema";
|
||||
export * as schema from "./schema/day";
|
||||
export { SqliteError } from "better-sqlite3";
|
||||
|
||||
// This is exported here to avoid leaking better-sqlite types outside of this package.
|
||||
|
||||
Binary file not shown.
@ -1,4 +1,4 @@
|
||||
import { db } from "./drizzle";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
|
||||
migrate(db, { migrationsFolder: "./drizzle" });
|
||||
migrate(db, { migrationsFolder: "./migrations" });
|
||||
|
||||
5
packages/db/migrations/0001_hard_lionheart.sql
Normal file
5
packages/db/migrations/0001_hard_lionheart.sql
Normal file
@ -0,0 +1,5 @@
|
||||
ALTER TABLE `events` RENAME TO `days`;--> statement-breakpoint
|
||||
ALTER TABLE `days` RENAME COLUMN `description` TO `comment`;--> statement-breakpoint
|
||||
ALTER TABLE `days` ADD `mood` integer;--> statement-breakpoint
|
||||
ALTER TABLE `days` ADD `date` text NOT NULL;--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `days_date_unique` ON `days` (`date`);
|
||||
66
packages/db/migrations/meta/0001_snapshot.json
Normal file
66
packages/db/migrations/meta/0001_snapshot.json
Normal file
@ -0,0 +1,66 @@
|
||||
{
|
||||
"version": "6",
|
||||
"dialect": "sqlite",
|
||||
"id": "6ad45ca2-1f2d-4e94-a8fd-b749a8577a61",
|
||||
"prevId": "74097c41-1958-4fc8-8371-6e31b3071eb9",
|
||||
"tables": {
|
||||
"days": {
|
||||
"name": "days",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"mood": {
|
||||
"name": "mood",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"date": {
|
||||
"name": "date",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"comment": {
|
||||
"name": "comment",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"days_date_unique": {
|
||||
"name": "days_date_unique",
|
||||
"columns": [
|
||||
"date"
|
||||
],
|
||||
"isUnique": true
|
||||
}
|
||||
},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
}
|
||||
},
|
||||
"enums": {},
|
||||
"_meta": {
|
||||
"schemas": {},
|
||||
"tables": {
|
||||
"\"events\"": "\"days\""
|
||||
},
|
||||
"columns": {
|
||||
"\"days\".\"description\"": "\"days\".\"comment\""
|
||||
}
|
||||
},
|
||||
"internal": {
|
||||
"indexes": {}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,13 @@
|
||||
"when": 1728109876982,
|
||||
"tag": "0000_worried_may_parker",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 1,
|
||||
"version": "6",
|
||||
"when": 1731100507972,
|
||||
"tag": "0001_hard_lionheart",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -5,6 +5,8 @@
|
||||
"private": true,
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
"dev": "drizzle-kit studio",
|
||||
"generate": "drizzle-kit generate",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"migrate": "tsx migrate.ts",
|
||||
"studio": "drizzle-kit studio"
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
import {
|
||||
sqliteTable,
|
||||
integer,
|
||||
text,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const events = sqliteTable("events", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
description: text("description").notNull(),
|
||||
});
|
||||
12
packages/db/schema/day.ts
Normal file
12
packages/db/schema/day.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import {
|
||||
sqliteTable,
|
||||
integer,
|
||||
text,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const days = sqliteTable("days", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
mood: integer("mood"),
|
||||
date: text("date").notNull().unique(),
|
||||
comment: text("comment").notNull(),
|
||||
});
|
||||
2058
pnpm-lock.yaml
generated
2058
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
21
turbo.json
21
turbo.json
@ -3,14 +3,27 @@
|
||||
"ui": "tui",
|
||||
"tasks": {
|
||||
"build": {
|
||||
"dependsOn": ["^build"],
|
||||
"inputs": ["$TURBO_DEFAULT$", ".env*"],
|
||||
"outputs": [".next/**", "!.next/cache/**"]
|
||||
"dependsOn": [
|
||||
"^build"
|
||||
],
|
||||
"inputs": [
|
||||
"$TURBO_DEFAULT$",
|
||||
".env*"
|
||||
],
|
||||
"outputs": [
|
||||
".next/**",
|
||||
"!.next/cache/**"
|
||||
]
|
||||
},
|
||||
"lint": {
|
||||
"dependsOn": ["^lint"]
|
||||
"dependsOn": [
|
||||
"^lint"
|
||||
]
|
||||
},
|
||||
"dev": {
|
||||
"dependsOn": [
|
||||
"^dev"
|
||||
],
|
||||
"cache": false,
|
||||
"persistent": true
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user