Skip to content

Commit

Permalink
Migrate to Drizzle ORM (#31)
Browse files Browse the repository at this point in the history
* Add drizzle dependency

* Add title to each page

* Migrate to drizzle orm

* Do not generate css twice in dev mode startup

* Update to @types/bun to 1.2

* Update wc-minimap to 0.1.2

* Automatically run schema migrations

* Fix drizzle migrations for compiled build
  • Loading branch information
carterworks authored Jan 23, 2025
1 parent a990e91 commit 96f9d93
Show file tree
Hide file tree
Showing 23 changed files with 624 additions and 244 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"recommendations": [
"astro-build.astro-vscode",
"bradlc.vscode-tailwindcss",
"biomejs.biome",
"editorconfig.editorconfig",
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ARG PORT=4321
# Copy built application
COPY --from=build /app/dist/yazzy /app/yazzy
COPY --from=build /app/node_modules /app/node_modules
COPY --from=build /app/src/db/migrations /app/src/db/migrations

# Start the server by default, this can be overwritten at runtime
ENV HOST=${HOST}
Expand Down
176 changes: 170 additions & 6 deletions bun.lock

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit";
import schema from "./src/db/schema.ts" with { type: "file" };

export default defineConfig({
out: "./src/db/migrations",
schema,
dialect: "sqlite",
dbCredentials: {
url: process.env.DB_PATH ?? ":memory:",
},
});
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_PATH=./articles.sqlite3
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@tailwindcss/typography": "^0.5.16",
"ai": "^4.1.2",
"dompurify": "^3.1.6",
"drizzle-orm": "^0.38.4",
"hono": "^4.6.18",
"jsdom": "^26.0.0",
"tailwindcss": "^4.0.0",
Expand All @@ -18,17 +19,19 @@
},
"scripts": {
"check": "biome check --write",
"dev": "bun run css && NODE_ENV=development bun --watch run src/server.tsx",
"dev": "NODE_ENV=development bun --watch run src/server.tsx",
"css": "tailwindcss --input static/styles.css --output static/styles.min.css --cwd src",
"build": "bun run css --minify && NODE_ENV=production bun build src/server.tsx --compile --outfile dist/yazzy"
"build": "bun run css --minify && NODE_ENV=production bun build src/server.tsx --compile --outfile dist/yazzy && mkdir -p dist/src/db/migrations/meta && cp -r src/db/migrations/ dist/src/db/migrations/"
},
"type": "module",
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@types/bun": "^1.1.14",
"@libsql/client": "^0.14.0",
"@types/bun": "^1.2.0",
"@types/dompurify": "^3.0.5",
"@types/jsdom": "^21.1.7",
"@types/turndown": "^5.0.5",
"drizzle-kit": "^0.30.2",
"lefthook": "^1.10.10"
},
"trustedDependencies": [
Expand Down
5 changes: 4 additions & 1 deletion src/components/AISummary.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { FC } from "hono/jsx";

const AISummary: FC<{ url: string; summary?: string }> = ({ url, summary }) => {
const AISummary: FC<{ url: string; summary?: string | null }> = ({
url,
summary,
}) => {
const proseClasses =
"prose dark:prose-invert font-humanist mt-2 prose-p:mt-0 prose-headings:font-transitional prose-headings:my-0 prose-h2:text-lg";
if (summary) {
Expand Down
22 changes: 22 additions & 0 deletions src/db/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Database } from "bun:sqlite";
import { sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import log from "../services/log";
import * as schema from "./schema";

function initalizeDB(location: string = process.env.DB_PATH || ":memory:") {
const client = new Database(location);
const db = drizzle({ client, schema });
db.run(sql`PRAGMA journal_mode=WAL;`);
runMigrations(db);
return db;
}

function runMigrations(db: ReturnType<typeof initalizeDB>) {
migrate(db, { migrationsFolder: "./src/db/migrations" });
log("Migrations complete");
}

const db = initalizeDB();
export default db;
19 changes: 19 additions & 0 deletions src/db/migrations/0000_talented_albert_cleary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
CREATE TABLE `articles` (
`url` text PRIMARY KEY,
`title` text,
`author` text,
`published` integer,
`topics` text,
`tags` text,
`markdownContent` text,
`textContent` text,
`htmlContent` text,
`createdAt` integer,
`summary` text
);
--> statement-breakpoint
CREATE TABLE `schema_version` (
`version` integer PRIMARY KEY
);
20 changes: 20 additions & 0 deletions src/db/migrations/0001_awesome_nicolaos.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
DROP TABLE `schema_version`;--> statement-breakpoint
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_articles` (
`url` text PRIMARY KEY NOT NULL,
`title` text,
`author` text,
`published` integer,
`topics` text,
`tags` text,
`markdownContent` text,
`textContent` text,
`htmlContent` text,
`createdAt` integer DEFAULT (CURRENT_TIMESTAMP),
`summary` text
);
--> statement-breakpoint
INSERT INTO `__new_articles`("url", "title", "author", "published", "topics", "tags", "markdownContent", "textContent", "htmlContent", "createdAt", "summary") SELECT "url", "title", "author", "published", "topics", "tags", "markdownContent", "textContent", "htmlContent", "createdAt", "summary" FROM `articles`;--> statement-breakpoint
DROP TABLE `articles`;--> statement-breakpoint
ALTER TABLE `__new_articles` RENAME TO `articles`;--> statement-breakpoint
PRAGMA foreign_keys=ON;
119 changes: 119 additions & 0 deletions src/db/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"id": "00000000-0000-0000-0000-000000000000",
"prevId": "",
"version": "6",
"dialect": "sqlite",
"tables": {
"articles": {
"name": "articles",
"columns": {
"url": {
"autoincrement": false,
"name": "url",
"type": "text",
"primaryKey": true,
"notNull": false
},
"title": {
"autoincrement": false,
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false
},
"author": {
"autoincrement": false,
"name": "author",
"type": "text",
"primaryKey": false,
"notNull": false
},
"published": {
"autoincrement": false,
"name": "published",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"topics": {
"autoincrement": false,
"name": "topics",
"type": "text",
"primaryKey": false,
"notNull": false
},
"tags": {
"autoincrement": false,
"name": "tags",
"type": "text",
"primaryKey": false,
"notNull": false
},
"markdownContent": {
"autoincrement": false,
"name": "markdownContent",
"type": "text",
"primaryKey": false,
"notNull": false
},
"textContent": {
"autoincrement": false,
"name": "textContent",
"type": "text",
"primaryKey": false,
"notNull": false
},
"htmlContent": {
"autoincrement": false,
"name": "htmlContent",
"type": "text",
"primaryKey": false,
"notNull": false
},
"createdAt": {
"autoincrement": false,
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"summary": {
"autoincrement": false,
"name": "summary",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"compositePrimaryKeys": {},
"indexes": {},
"foreignKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"schema_version": {
"name": "schema_version",
"columns": {
"version": {
"autoincrement": false,
"name": "version",
"type": "integer",
"primaryKey": true,
"notNull": false
}
},
"compositePrimaryKeys": {},
"indexes": {},
"foreignKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
106 changes: 106 additions & 0 deletions src/db/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"version": "6",
"dialect": "sqlite",
"id": "bd3b440b-12da-4c60-bf23-22d97d060696",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"articles": {
"name": "articles",
"columns": {
"url": {
"name": "url",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"title": {
"name": "title",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"author": {
"name": "author",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"published": {
"name": "published",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"topics": {
"name": "topics",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"tags": {
"name": "tags",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"markdownContent": {
"name": "markdownContent",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"textContent": {
"name": "textContent",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"htmlContent": {
"name": "htmlContent",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "(CURRENT_TIMESTAMP)"
},
"summary": {
"name": "summary",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
20 changes: 20 additions & 0 deletions src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "7",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "6",
"when": 1737487318393,
"tag": "0000_talented_albert_cleary",
"breakpoints": true
},
{
"idx": 1,
"version": "6",
"when": 1737504467575,
"tag": "0001_awesome_nicolaos",
"breakpoints": true
}
]
}
2 changes: 2 additions & 0 deletions src/db/relations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import { relations } from "drizzle-orm/relations";
// import { } from "./schema";
Loading

0 comments on commit 96f9d93

Please sign in to comment.