#Remotely hosted existing database

1 messages · Page 1 of 1 (latest)

modest trailBOT
#

Hi @weary lily. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To access a remotely hosted existing database in Astro, you can use the --remote flag when running the dev or build commands. This will connect to your live production database, allowing both readable and writable access. However, be careful using --remote in development as all inserts, updates, or deletions will be persisted.

Here's how you can build or develop with a remote connection:

# Build with a remote connection
astro build --remote

# Develop with a remote connection
astro dev --remote

To use a remote connection, you will need an app token to authenticate with Astro Studio. You can visit the Astro Studio dashboard for token creation and setup instructions.

Please note that Astro is winding down Astro Studio, and users will no longer be able to create databases after October 1, 2024. It is recommended to migrate your existing Studio databases to Turso, or connect Astro DB to any libSQL server instead.

For more details, you can refer to the following documentation:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@weary lily kapa.ai is still learning and improving, please let us know how it did by reacting below

weary lily
#

@modest trail what about mariadb database?

modest trailBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm sorry, but the provided knowledge sources do not contain any specific information about using MariaDB with Astro. As an AI, I can only provide information based on the given knowledge sources.

For more detailed information, I would recommend checking the official Astro documentation or MariaDB's documentation on how to connect it with JavaScript frameworks.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@weary lily kapa.ai is still learning and improving, please let us know how it did by reacting below

weary lily
#

@modest trail How to get data from database on ss, not in the build time?

modest trailBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To fetch data from a database on the server-side (not at build time) in Astro, you need to enable on-demand rendering and add an SSR adapter for your deployment environment.

Here's an example of how you can fetch data from a database on the server-side:

---import { db, Comment } from 'astro:db';

if (Astro.request.method === 'POST') {  
  // parse form data  
  const formData = await Astro.request.formData();  
  const author = formData.get('author');  
  const content = formData.get('content');  
  if (typeof author === 'string' && typeof content === 'string') {    
    // insert form data into the Comment table    
    await db.insert(Comment).values({ author, content });  
  }
}

// render the new list of comments on each request
const comments = await db.select().from(Comment);
---

In this example, the data is fetched from the database when a POST request is made. The fetched data is then used to render the new list of comments on each request.

You can also fetch data from your database inside an API endpoint. Here's an example of how you can delete a row from a Comment table by the id param:

import type { APIRoute } from "astro";
import { db, Comment, eq } from 'astro:db';

export const DELETE: APIRoute = async (ctx) => {  
  await db.delete(Comment).where(eq(Comment.id, ctx.params.id ));  
  return new Response(null, { status: 204 });
}

In this example, the DELETE API endpoint is used to delete a row from the Comment table based on the id param.

#

@weary lily kapa.ai is still learning and improving, please let us know how it did by reacting below