remove astro:db and add drizzle

This commit is contained in:
haetae 2025-09-22 16:35:22 -04:00
parent 3c3d596c9c
commit e384937596
12 changed files with 4902 additions and 1056 deletions

View File

@ -1,8 +1,7 @@
// @ts-check
import { defineConfig } from 'astro/config';
import { defineConfig, envField } from 'astro/config';
import { modifiedTime } from './src/utils/lastModified.mjs';
import mdx from "@astrojs/mdx";
import db from "@astrojs/db";
import node from "@astrojs/node";
import devOnlyRoutes from '@fujocoded/astro-dev-only';
@ -15,7 +14,6 @@ export default defineConfig({
},
integrations: [
mdx(),
db(),
devOnlyRoutes({
// dryRun: true,
routePatterns: ["/guestbook/admin"]
@ -24,6 +22,11 @@ export default defineConfig({
adapter: node({
mode: "standalone",
}),
env: {
schema: {
TURSO_DATABASE_URL: envField.string({ context: "server", access: "secret" }),
}
},
experimental: {
fonts: [
{

1006
bun.lock

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +0,0 @@
import { column, defineDb, defineTable, NOW } from 'astro:db';
const Guestbook = defineTable({
columns: {
id: column.number({ primaryKey: true }),
username: column.text(),
website: column.text({ optional: true }),
message: column.text({ multiline: true }),
published: column.date({ default: NOW }),
updated: column.date({ optional: true }),
reply: column.text({ optional: true, multiline: true }),
},
});
// https://astro.build/db/config
export default defineDb({
tables: { Guestbook },
});

8
db/index.ts Normal file
View File

@ -0,0 +1,8 @@
import { TURSO_DATABASE_URL } from "astro:env/server";
import { drizzle } from "drizzle-orm/libsql/web";
import { createClient } from "@libsql/client";
const client = createClient({
url: TURSO_DATABASE_URL,
});
export const db = drizzle({ client });

12
db/schema.ts Normal file
View File

@ -0,0 +1,12 @@
import { sql } from "drizzle-orm";
import { int, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const guestbookTable = sqliteTable("guestbook_table", {
id: int().primaryKey({ autoIncrement: true }),
username: text().notNull(),
website: text(),
message: text().notNull(),
published: text().notNull().default(sql`(CURRENT_TIMESTAMP)`),
updated: text().$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
reply: text(),
});

13
drizzle.config.ts Normal file
View File

@ -0,0 +1,13 @@
import { loadEnv } from "vite";
import { defineConfig } from 'drizzle-kit';
const { TURSO_DATABASE_URL } = loadEnv(process.env.NODE_ENV!, process.cwd(), "");
export default defineConfig({
out: "./db/migrations",
schema: "./db/schema.ts",
dialect: "turso",
dbCredentials: {
url: TURSO_DATABASE_URL,
},
});

Binary file not shown.

View File

@ -9,18 +9,20 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/db": "0.17.1",
"@astrojs/mdx": "^4.3.3",
"@astrojs/node": "9.4.1",
"@astrojs/mdx": "^4.3.6",
"@astrojs/node": "9.4.4",
"@astrojs/rss": "4.0.12",
"@fujocoded/astro-dev-only": "0.0.4",
"astro": "5.13.0",
"@libsql/client": "^0.15.15",
"astro": "5.13.10",
"astro-breadcrumbs": "^3.3.1",
"dayjs": "^1.11.13",
"isomorphic-dompurify": "^2.26.0",
"marked": "^16.1.2"
"dayjs": "^1.11.18",
"drizzle-orm": "^0.44.5",
"isomorphic-dompurify": "^2.28.0",
"marked": "^16.3.0"
},
"devDependencies": {
"@types/node": "^22.17.1"
"@types/node": "^22.18.6",
"drizzle-kit": "^0.31.4"
}
}

4834
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

5
pnpm-workspace.yaml Normal file
View File

@ -0,0 +1,5 @@
onlyBuiltDependencies:
- esbuild
- sharp
nodeLinker: hoisted

View File

@ -1,6 +1,8 @@
import { ActionError, defineAction } from "astro:actions";
import { z } from "astro:content";
import { db, eq, Guestbook, isDbError } from "astro:db";
import { db } from "db";
import { guestbookTable } from "db/schema";
import { eq } from "drizzle-orm";
import DOMPurify from "isomorphic-dompurify";
export const guestbook = {
@ -24,7 +26,7 @@ export const guestbook = {
const sanitized = DOMPurify.sanitize(addLine);
try {
const entry = await db.insert(Guestbook).values({
const entry = await db.insert(guestbookTable).values({
username,
website,
message: sanitized,
@ -32,10 +34,7 @@ export const guestbook = {
return entry[0];
} catch (e) {
if (isDbError(e)) {
return new Response(`Cannot insert entry\n\n${e.message}`, { status: 400 });
}
return new Response('An unexpected error occurred', { status: 500 });
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
}
},
}),
@ -51,7 +50,7 @@ export const guestbook = {
throw new ActionError({ code: "UNAUTHORIZED" });
}
const entry = await db.select().from(Guestbook).where(eq(Guestbook.id, id));
const entry = await db.select().from(guestbookTable).where(eq(guestbookTable.id, id));
if (!entry) {
throw new ActionError({
code: "NOT_FOUND",
@ -63,17 +62,14 @@ export const guestbook = {
const sanitized = DOMPurify.sanitize(addLine);
try {
const update = await db.update(Guestbook).set({
const update = await db.update(guestbookTable).set({
reply: sanitized,
updated: new Date(),
}).where(eq(Guestbook.id, id)).returning();
updated: new Date().toDateString(),
}).where(eq(guestbookTable.id, id)).returning();
return update[0];
} catch (e) {
if (isDbError(e)) {
return new Response(`Cannot update entry\n\n${e.message}`, { status: 400 });
}
return new Response('An unexpected error occurred', { status: 500 });
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
}
},
}),
@ -87,7 +83,7 @@ export const guestbook = {
throw new ActionError({ code: "UNAUTHORIZED" });
}
const entry = await db.select().from(Guestbook).where(eq(Guestbook.id, id));
const entry = await db.select().from(guestbookTable).where(eq(guestbookTable.id, id));
if (!entry) {
throw new ActionError({
code: "NOT_FOUND",
@ -96,14 +92,11 @@ export const guestbook = {
}
try {
const entry = await db.delete(Guestbook).where(eq(Guestbook.id, id)).returning();
const entry = await db.delete(guestbookTable).where(eq(guestbookTable.id, id)).returning();
return entry[0];
} catch (e) {
if (isDbError(e)) {
return new Response(`Cannot update entry\n\n${e.message}`, { status: 400 });
}
return new Response('An unexpected error occurred', { status: 500 });
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
}
},
}),

View File

@ -3,7 +3,7 @@ import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
export default function (date: Date | string, iso = false, format?: string) {
export default function (date: string, iso = false, format?: string) {
if (iso) {
return dayjs(date).utc(true).toISOString();
} else {