-
-
Notifications
You must be signed in to change notification settings - Fork 4
Added database connection from Turso to Astro DB. #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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[]; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure you should cast here. In theory, you should be able to get the type directly from the query, and if this cast is right then it should match BlorboEntry. Is your VSCode or whatever editor set up so you can check the TypeScript types? If you want to change the type, you should |
||
|
|
||
|
|
||
|
|
||
| // <img src={blorbo.image_url} alt={'A picture of '+ blorbo.name + ' from ' + blorbo.fandom } width="100" /> | ||
|
|
||
| --- | ||
|
|
||
| <Layout> | ||
| <article class="rounded-xl bg-white drop-shadow-purple"> | ||
| <h1>Supporters' Blorbos</h1> | ||
| <p><strong>This is where the supporters' blorbos will be displayed.</strong></p> | ||
| <ul class="list-disc pl-6"> | ||
| {blorboEntries.map((blorbo) => ( | ||
| <> | ||
| <li> | ||
| <strong>{blorbo.name}</strong> from <em>{blorbo.fandom}</em> <br /> | ||
| <span>Blorbo ID: {blorbo.id}</span><br /> | ||
| <span>Image url: {blorbo.image_url}</span><br /> | ||
| <span>Email: {blorbo.email}</span> <br /> | ||
| <span>Status: {blorbo.subscription_status}</span> <br /> | ||
| <span>Tier: {blorbo.subscription_tier}</span><br /> | ||
| <span>Supporter ID: {blorbo.supporter_id}</span><br /> | ||
| <span>Owner: {blorbo.external_id}</span> | ||
| </li> | ||
| </> | ||
| ))} | ||
| </ul> | ||
| </article> | ||
| </Layout> | ||
Uh oh!
There was an error while loading. Please reload this page.