Turso & Astro
Turso is a distributed database built on libSQL, a fork of SQLite. It is optimized for low query latency, making it suitable for global applications.
Initializing Turso in Astro
Section titled Initializing Turso in AstroPrerequisites
Section titled Prerequisites- The Turso CLI installed and signed in
- A Turso Database with schema
- Your Database URL
- An Access Token
Configure environment variables
Section titled Configure environment variablesObtain your database URL using the following command:
Create an auth token for the database:
Add the output from both commands above into your .env
file at the root of your project. If this file does not exist, create one.
Do not use the PUBLIC_
prefix when creating these private environment variables. This will expose these values on the client.
Install LibSQL Client
Section titled Install LibSQL ClientInstall the @libsql/client
to connect Turso to Astro:
Initialize a new client
Section titled Initialize a new clientCreate a file turso.ts
in the src
folder and invoke createClient
, passing it TURSO_DATABASE_URL
and TURSO_AUTH_TOKEN
:
Querying your database
Section titled Querying your databaseTo access information from your database, import turso
and execute a SQL query inside any .astro
component.
The following example fetches all posts
from your table, then displays a list of titles in a <BlogIndex />
component:
SQL Placeholders
Section titled SQL PlaceholdersThe execute()
method can take an object to pass variables to the SQL statement, such as slug
, or pagination.
The following example fetches a single entry from the posts
table WHERE
the slug
is the retrieved value from Astro.params
, then displays the title of the post.