Skip to content

Commit 1413f5d

Browse files
just-be-devclaude
andcommitted
refactor(micro): move D1 schema, migrations, and drizzle config into micro package
- Move db/schema.ts, db/migrations/, and drizzle.config.ts into packages/micro/ - Add loader-db.ts with a D1 HTTP adapter so drizzle can be used at build time - Rewrite micro-loader to import getMicroPosts from @just-be/micro/loader-db instead of hitting the Cloudflare API directly with raw SQL - Add exports field to micro package.json for the loader-db entrypoint - Add mise tasks for micro:db:generate and micro:db:migrate - Remove drizzle-kit, drizzle-orm, and db:* scripts from root package.json Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4246494 commit 1413f5d

13 files changed

Lines changed: 52 additions & 117 deletions

File tree

mise.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ description = "Preview release notes for a package version"
9393
run = "bun packages/micro/index.ts"
9494
description = "Micro blog CLI (browse posts or create new ones)"
9595
raw = true
96+
97+
[tasks."micro:db:generate"]
98+
dir = "packages/micro"
99+
run = "bun run db:generate"
100+
description = "Generate drizzle migrations for micro blog"
101+
102+
[tasks."micro:db:migrate"]
103+
dir = "packages/micro"
104+
run = "bun run db:migrate"
105+
description = "Apply drizzle migrations for micro blog"

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
"test": "bun test",
2626
"gen-codes": "bun scripts/gen-codes.ts",
2727
"lint": "oxlint",
28-
"lint:fix": "oxlint --fix",
29-
"db:generate": "drizzle-kit generate",
30-
"db:migrate": "drizzle-kit migrate"
28+
"lint:fix": "oxlint --fix"
3129
},
3230
"dependencies": {
3331
"@ascorbic/feed-loader": "^2.0.1",
@@ -41,8 +39,6 @@
4139
"@webtui/css": "^0.1.5",
4240
"astro": "6.0.4",
4341
"beautiful-mermaid": "^1.0.0",
44-
"drizzle-kit": "^0.31.9",
45-
"drizzle-orm": "^0.45.1",
4642
"js-yaml": "^4.1.1",
4743
"marked": "^17.0.1",
4844
"mdast-util-mdx-jsx": "^3.2.0",
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "drizzle-kit";
22

33
export default defineConfig({
4-
schema: "./db/schema.ts",
4+
schema: "./src/schema.ts",
55
out: "./db/migrations",
66
dialect: "sqlite",
77
driver: "d1-http",

packages/micro/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@
1919
},
2020
"files": [
2121
"index.ts",
22-
"src"
22+
"src",
23+
"db"
2324
],
2425
"type": "module",
26+
"exports": {
27+
".": "./index.ts",
28+
"./loader-db": "./src/loader-db.ts"
29+
},
2530
"publishConfig": {
2631
"access": "public"
2732
},
33+
"scripts": {
34+
"db:generate": "drizzle-kit generate",
35+
"db:migrate": "drizzle-kit migrate"
36+
},
2837
"dependencies": {
2938
"@atproto/api": "^0.15.3",
3039
"@clack/prompts": "^0.11.0",
40+
"drizzle-kit": "^0.31.9",
3141
"drizzle-orm": "^0.45.1",
3242
"twitter-api-v2": "^1.17.3",
3343
"wrangler": "4.48.0"

packages/micro/src/db.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import { drizzle } from "drizzle-orm/d1";
2-
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
3-
import { sql } from "drizzle-orm";
2+
import { getPlatformProxy } from "wrangler";
3+
import { microPosts } from "./schema";
44

5-
// Re-define the schema locally for the CLI
6-
export const microPosts = sqliteTable("micro_posts", {
7-
id: integer("id").primaryKey({ autoIncrement: true }),
8-
content: text("content", { length: 280 }).notNull(),
9-
createdAt: integer("created_at", { mode: "timestamp" })
10-
.notNull()
11-
.default(sql`(unixepoch())`),
12-
updatedAt: integer("updated_at", { mode: "timestamp" })
13-
.notNull()
14-
.default(sql`(unixepoch())`)
15-
.$onUpdate(() => new Date()),
16-
syndicatedTo: text("syndicated_to", { mode: "json" })
17-
.$type<string[]>()
18-
.default(sql`'[]'`),
19-
});
5+
export { microPosts };
206

217
export async function getD1Database() {
228
// Use wrangler to get local D1 access
239
// Find the project root (where wrangler.toml is)
2410
const projectRoot = import.meta.dir + "/../../..";
25-
26-
const { getPlatformProxy } = await import("wrangler");
2711
const { env, dispose } = await getPlatformProxy<{ DB: D1Database }>({
2812
configPath: `${projectRoot}/wrangler.toml`,
2913
persist: { path: `${projectRoot}/.wrangler/state/v3` },

packages/micro/src/loader-db.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { desc } from "drizzle-orm";
2+
3+
import { getD1Database } from "./db";
4+
import { microPosts } from "./schema";
5+
6+
export async function getMicroPosts() {
7+
const { db, dispose } = await getD1Database();
8+
try {
9+
return await db.select().from(microPosts).orderBy(desc(microPosts.createdAt));
10+
} finally {
11+
await dispose();
12+
}
13+
}

0 commit comments

Comments
 (0)