#AstroDB in JS Vanilla

6 messages · Page 1 of 1 (latest)

vapid stream
#

Can i use AstroDB in JS Vanilla? can you give me examples of code about Import the DB, select and insert please? thanks

stiff pastureBOT
#

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

#

Yes, you can use Astro DB in a project using Vanilla JS. Astro DB is a fully-managed SQL database designed for the Astro ecosystem. It provides a powerful, local, type-safe tool to query and model content as a relational database.

Here are some examples of how you can use Astro DB:

Importing the DB:

import { db } from 'astro:db';

This line of code imports the db object from Astro DB, which you can use to interact with your database.

Selecting data:

import { db, Comment } from 'astro:db';
const comments = await db.select().from(Comment);

This example selects all rows of a Comment table. The select() method is used to specify the data you want to fetch from the database, and from(Comment) specifies the table you want to fetch the data from.

Inserting data:

import { db, Comment } from 'astro:db';
if (Astro.request.method === 'POST') {
  const formData = await Astro.request.formData();
  const author = formData.get('author');
  const content = formData.get('content');
  if (typeof author === 'string' && typeof content === 'string') {
    await db.insert(Comment).values({ author, content });
  }
}

This example inserts a row into a Comment table based on a parsed form POST request. The insert() method is used to insert data into the database, and values({ author, content }) specifies the data you want to insert.

Please note that these examples are provided in the context of an Astro project, and the exact usage may vary depending on your specific project setup.

For more detailed information and examples, you can refer to the Astro DB guide (https://docs.astro.build/en/guides/astro-db/).

#

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

vapid stream
#

<@&1095491216776380548> Failed to resolve import "/home/user/shorter-url/db/seed.js" from "astro:db". Does the file exist? with the import