remove astro:db and add drizzle
This commit is contained in:
parent
3c3d596c9c
commit
e384937596
@ -1,8 +1,7 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import { defineConfig } from 'astro/config';
|
import { defineConfig, envField } from 'astro/config';
|
||||||
import { modifiedTime } from './src/utils/lastModified.mjs';
|
import { modifiedTime } from './src/utils/lastModified.mjs';
|
||||||
import mdx from "@astrojs/mdx";
|
import mdx from "@astrojs/mdx";
|
||||||
import db from "@astrojs/db";
|
|
||||||
import node from "@astrojs/node";
|
import node from "@astrojs/node";
|
||||||
import devOnlyRoutes from '@fujocoded/astro-dev-only';
|
import devOnlyRoutes from '@fujocoded/astro-dev-only';
|
||||||
|
|
||||||
@ -15,7 +14,6 @@ export default defineConfig({
|
|||||||
},
|
},
|
||||||
integrations: [
|
integrations: [
|
||||||
mdx(),
|
mdx(),
|
||||||
db(),
|
|
||||||
devOnlyRoutes({
|
devOnlyRoutes({
|
||||||
// dryRun: true,
|
// dryRun: true,
|
||||||
routePatterns: ["/guestbook/admin"]
|
routePatterns: ["/guestbook/admin"]
|
||||||
@ -24,6 +22,11 @@ export default defineConfig({
|
|||||||
adapter: node({
|
adapter: node({
|
||||||
mode: "standalone",
|
mode: "standalone",
|
||||||
}),
|
}),
|
||||||
|
env: {
|
||||||
|
schema: {
|
||||||
|
TURSO_DATABASE_URL: envField.string({ context: "server", access: "secret" }),
|
||||||
|
}
|
||||||
|
},
|
||||||
experimental: {
|
experimental: {
|
||||||
fonts: [
|
fonts: [
|
||||||
{
|
{
|
||||||
|
18
db/config.ts
18
db/config.ts
@ -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
8
db/index.ts
Normal 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
12
db/schema.ts
Normal 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
13
drizzle.config.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
BIN
guestbook.db
BIN
guestbook.db
Binary file not shown.
18
package.json
18
package.json
@ -9,18 +9,20 @@
|
|||||||
"astro": "astro"
|
"astro": "astro"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/db": "0.17.1",
|
"@astrojs/mdx": "^4.3.6",
|
||||||
"@astrojs/mdx": "^4.3.3",
|
"@astrojs/node": "9.4.4",
|
||||||
"@astrojs/node": "9.4.1",
|
|
||||||
"@astrojs/rss": "4.0.12",
|
"@astrojs/rss": "4.0.12",
|
||||||
"@fujocoded/astro-dev-only": "0.0.4",
|
"@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",
|
"astro-breadcrumbs": "^3.3.1",
|
||||||
"dayjs": "^1.11.13",
|
"dayjs": "^1.11.18",
|
||||||
"isomorphic-dompurify": "^2.26.0",
|
"drizzle-orm": "^0.44.5",
|
||||||
"marked": "^16.1.2"
|
"isomorphic-dompurify": "^2.28.0",
|
||||||
|
"marked": "^16.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^22.17.1"
|
"@types/node": "^22.18.6",
|
||||||
|
"drizzle-kit": "^0.31.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4834
pnpm-lock.yaml
generated
Normal file
4834
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
5
pnpm-workspace.yaml
Normal file
5
pnpm-workspace.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
onlyBuiltDependencies:
|
||||||
|
- esbuild
|
||||||
|
- sharp
|
||||||
|
|
||||||
|
nodeLinker: hoisted
|
@ -1,6 +1,8 @@
|
|||||||
import { ActionError, defineAction } from "astro:actions";
|
import { ActionError, defineAction } from "astro:actions";
|
||||||
import { z } from "astro:content";
|
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";
|
import DOMPurify from "isomorphic-dompurify";
|
||||||
|
|
||||||
export const guestbook = {
|
export const guestbook = {
|
||||||
@ -24,7 +26,7 @@ export const guestbook = {
|
|||||||
const sanitized = DOMPurify.sanitize(addLine);
|
const sanitized = DOMPurify.sanitize(addLine);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const entry = await db.insert(Guestbook).values({
|
const entry = await db.insert(guestbookTable).values({
|
||||||
username,
|
username,
|
||||||
website,
|
website,
|
||||||
message: sanitized,
|
message: sanitized,
|
||||||
@ -32,10 +34,7 @@ export const guestbook = {
|
|||||||
|
|
||||||
return entry[0];
|
return entry[0];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isDbError(e)) {
|
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
|
||||||
return new Response(`Cannot insert entry\n\n${e.message}`, { status: 400 });
|
|
||||||
}
|
|
||||||
return new Response('An unexpected error occurred', { status: 500 });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -51,7 +50,7 @@ export const guestbook = {
|
|||||||
throw new ActionError({ code: "UNAUTHORIZED" });
|
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) {
|
if (!entry) {
|
||||||
throw new ActionError({
|
throw new ActionError({
|
||||||
code: "NOT_FOUND",
|
code: "NOT_FOUND",
|
||||||
@ -63,17 +62,14 @@ export const guestbook = {
|
|||||||
const sanitized = DOMPurify.sanitize(addLine);
|
const sanitized = DOMPurify.sanitize(addLine);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const update = await db.update(Guestbook).set({
|
const update = await db.update(guestbookTable).set({
|
||||||
reply: sanitized,
|
reply: sanitized,
|
||||||
updated: new Date(),
|
updated: new Date().toDateString(),
|
||||||
}).where(eq(Guestbook.id, id)).returning();
|
}).where(eq(guestbookTable.id, id)).returning();
|
||||||
|
|
||||||
return update[0];
|
return update[0];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isDbError(e)) {
|
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
|
||||||
return new Response(`Cannot update entry\n\n${e.message}`, { status: 400 });
|
|
||||||
}
|
|
||||||
return new Response('An unexpected error occurred', { status: 500 });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
@ -87,7 +83,7 @@ export const guestbook = {
|
|||||||
throw new ActionError({ code: "UNAUTHORIZED" });
|
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) {
|
if (!entry) {
|
||||||
throw new ActionError({
|
throw new ActionError({
|
||||||
code: "NOT_FOUND",
|
code: "NOT_FOUND",
|
||||||
@ -96,14 +92,11 @@ export const guestbook = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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];
|
return entry[0];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isDbError(e)) {
|
return new Response(`An unexpected error occurred\n\n${e}`, { status: 500 });
|
||||||
return new Response(`Cannot update entry\n\n${e.message}`, { status: 400 });
|
|
||||||
}
|
|
||||||
return new Response('An unexpected error occurred', { status: 500 });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
@ -3,7 +3,7 @@ import utc from "dayjs/plugin/utc";
|
|||||||
|
|
||||||
dayjs.extend(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) {
|
if (iso) {
|
||||||
return dayjs(date).utc(true).toISOString();
|
return dayjs(date).utc(true).toISOString();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user