feat: add turso database client

This commit is contained in:
Lewis Wynne 2026-01-23 03:47:18 +00:00
parent c23358ed8e
commit ac667a243d
2 changed files with 51 additions and 0 deletions

1
apps/blog/src/env.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference types="astro/client" />

50
apps/blog/src/lib/db.ts Normal file
View file

@ -0,0 +1,50 @@
import { createClient } from '@libsql/client';
export const db = createClient({
url: import.meta.env.TURSO_DATABASE_URL,
authToken: import.meta.env.TURSO_AUTH_TOKEN,
});
export interface GuestbookEntry {
id: number;
name: string;
message: string;
url: string | null;
created_at: string;
approved: number;
}
export async function getApprovedEntries(): Promise<GuestbookEntry[]> {
const result = await db.execute(
'SELECT * FROM guestbook WHERE approved = 1 ORDER BY created_at DESC'
);
return result.rows as unknown as GuestbookEntry[];
}
export async function getPendingEntries(): Promise<GuestbookEntry[]> {
const result = await db.execute(
'SELECT * FROM guestbook WHERE approved = 0 ORDER BY created_at DESC'
);
return result.rows as unknown as GuestbookEntry[];
}
export async function createEntry(name: string, message: string, url: string | null): Promise<void> {
await db.execute({
sql: 'INSERT INTO guestbook (name, message, url) VALUES (?, ?, ?)',
args: [name, message, url],
});
}
export async function approveEntry(id: number): Promise<void> {
await db.execute({
sql: 'UPDATE guestbook SET approved = 1 WHERE id = ?',
args: [id],
});
}
export async function deleteEntry(id: number): Promise<void> {
await db.execute({
sql: 'DELETE FROM guestbook WHERE id = ?',
args: [id],
});
}