From 4dd517b80b2d799bafa6ade96dd99d42af059101 Mon Sep 17 00:00:00 2001 From: TheFoxInBoots Date: Mon, 11 Nov 2024 09:50:11 +0100 Subject: [PATCH] Added database connection from Turso to Astro DB. --- astro.config.mts | 6 ++-- db/config.ts | 30 +++++++++++++++++++ src/components/tursoClient.js | 7 +++++ src/pages/supporters.astro | 54 +++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 db/config.ts create mode 100644 src/components/tursoClient.js create mode 100644 src/pages/supporters.astro diff --git a/astro.config.mts b/astro.config.mts index ee8f6a7..e98f6fe 100644 --- a/astro.config.mts +++ b/astro.config.mts @@ -1,4 +1,7 @@ import { defineConfig } from "astro/config"; +import 'dotenv/config'; +import db from "@astrojs/db"; + import icon from "astro-icon"; import mdx from "@astrojs/mdx"; import tailwind from "@astrojs/tailwind"; @@ -7,7 +10,6 @@ import remarkCapitalizeTitles, { } from "@fujocoded/remark-capitalize-titles"; import rehypeSlug from "rehype-slug"; import rehypeAutolinkHeadings from "rehype-autolink-headings"; - import metaTags from "astro-meta-tags"; // https://astro.build/config @@ -27,4 +29,4 @@ export default defineConfig({ ], ], }, -}); +}); \ No newline at end of file diff --git a/db/config.ts b/db/config.ts new file mode 100644 index 0000000..9810d67 --- /dev/null +++ b/db/config.ts @@ -0,0 +1,30 @@ +import { defineDb, defineTable, column } from 'astro:db'; + +export const Subscribers = defineTable({ + columns: { + id: column.number({ primaryKey: true }), + external_id: column.text(), + email: column.text(), + subscription_status: column.text(), + subscription_tier: column.text(), + }, +}); + +export const BlorboEntries = defineTable({ + columns: { + id: column.number({ primaryKey: true }), + supporter_id: column.number(), + name: column.text(), + fandom: column.text(), + image_url: column.text(), + }, +}); + +const db = defineDb({ + tables: { + Subscribers, + BlorboEntries, + }, +}); + +export default db; diff --git a/src/components/tursoClient.js b/src/components/tursoClient.js new file mode 100644 index 0000000..24633a7 --- /dev/null +++ b/src/components/tursoClient.js @@ -0,0 +1,7 @@ +// tursoClient.js +import { createClient } from "@libsql/client"; + +export const turso = createClient({ + url: process.env.TURSO_DATABASE_URL, + authToken: process.env.TURSO_AUTH_TOKEN, +}); \ No newline at end of file diff --git a/src/pages/supporters.astro b/src/pages/supporters.astro new file mode 100644 index 0000000..1b880cd --- /dev/null +++ b/src/pages/supporters.astro @@ -0,0 +1,54 @@ +--- +import Layout from "../layouts/Layout.astro"; +import { turso } from "../components/tursoClient"; + +type BlorboEntry = { + id: number; + name: string; + fandom: string; + image_url: string; + supporter_id: number; + external_id: string; // external_id of the user + email: string; + subscription_status: string; + subscription_tier: string; +}; + +// Execute the query and get the result +const result = await turso.execute(` + SELECT b.id, b.name, b.fandom, b.image_url, b.supporter_id, s.external_id, s.email, s.subscription_status, s.subscription_tier + FROM Blorbos b + JOIN Supporters s ON b.supporter_id = s.id +`); + +// Extract rows from the result +const blorboEntries: BlorboEntry[] = result.rows as unknown as BlorboEntry[]; + + + +// {'A + +--- + + +
+

Supporters' Blorbos

+

This is where the supporters' blorbos will be displayed.

+
    + {blorboEntries.map((blorbo) => ( + <> +
  • + {blorbo.name} from {blorbo.fandom}
    + Blorbo ID: {blorbo.id}
    + Image url: {blorbo.image_url}
    + Email: {blorbo.email}
    + Status: {blorbo.subscription_status}
    + Tier: {blorbo.subscription_tier}
    + Supporter ID: {blorbo.supporter_id}
    + Owner: {blorbo.external_id} +
  • + + ))} +
+
+