Astro DB
本頁內容尚未翻譯。
Astro DB is a fully-managed SQL database designed for the Astro ecosystem. Develop locally in Astro and deploy to any libSQL-compatible database.
Astro DB is a complete solution to configuring, developing, and querying your data. A local database is created in .astro/content.db
whenever you run astro dev
to manage your data without the need for Docker or a network connection.
Installation
Section titled InstallationInstall the @astrojs/db
integration using the built-in astro add
command:
Define your database
Section titled Define your databaseInstalling @astrojs/db
with the astro add
command will automatically create a db/config.ts
file in your project where you will define your database tables:
Tables
Section titled TablesData in Astro DB is stored using SQL tables. Tables structure your data into rows and columns, where columns enforce the type of each row value.
Define your tables in your db/config.ts
file by providing the structure of the data in your existing libSQL database, or the data you will collect in a new database. This will allow Astro to generate a TypeScript interface to query that table from your project. The result is full TypeScript support when you access your data with property autocompletion and type-checking.
To configure a database table, import and use the defineTable()
and column
utilities from astro:db
. Then, define a name (case-sensitive) for your table and the type of data in each column.
This example configures a Comment
table with required text columns for author
and body
. Then, makes it available to your project through the defineDb()
export.
Columns
Section titled ColumnsAstro DB supports the following column types:
Table References
Section titled Table ReferencesRelationships between tables are a common pattern in database design. For example, a Blog
table may be closely related to other tables of Comment
, Author
, and Category
.
You can define these relations between tables and save them into your database schema using reference columns. To establish a relationship, you will need:
- An identifier column on the referenced table. This is usually an
id
column with theprimaryKey
property. - A column on the base table to store the referenced
id
. This uses thereferences
property to establish a relationship.
This example shows a Comment
table’s authorId
column referencing an Author
table’s id
column.
Seed your database for development
Section titled Seed your database for developmentIn development, Astro will use your DB config to generate local types according to your schemas. These will be generated fresh from your seed file each time the dev server is started, and will allow you to query and work with the shape of your data with type safety and autocompletion.
You will not have access to production data during development unless you connect to a remote database during development. This protects your data while allowing you to test and develop with a working database with type-safety.
To seed development data for testing and debugging into your Astro project, create a db/seed.ts
file. Import both the db
object and your tables defined in astro:db
. insert
some initial data into each table. This development data should match the form of both your database schema and production data.
The following example defines two rows of development data for a Comment
table, and an Author
table:
Your development server will automatically restart your database whenever this file changes, regenerating your types and seeding this development data from seed.ts
fresh each time.
Connect a libSQL database for production
Section titled Connect a libSQL database for productionAstro DB can connect to any local libSQL database or to any server that exposes the libSQL remote protocol, whether managed or self-hosted.
To connect Astro DB to a libSQL database, set the following environment variables obtained from your database provider:
ASTRO_DB_REMOTE_URL
: the connection URL to the location of your local or remote libSQL DB. This may include URL configuration options such as sync and encryption as paramaters.ASTRO_DB_APP_TOKEN
: the auth token to your libSQL server. This is required for remote databases, and not needed for local DBs like files or in-memory databases
Depending on your service, you may have access to a CLI or web UI to retrieve these values. The following section will demonstrate connecting to Turso and setting these values as an example, but you are free to use any provider.
Getting started with Turso
Section titled Getting started with TursoTurso is the company behind libSQL, the open-source fork of SQLite that powers Astro DB. They provide a fully managed libSQL database platform and are fully compatible with Astro.
The steps below will guide you through the process of installing the Turso CLI, logging in (or signing up), creating a new database, getting the required environmental variables, and pushing the schema to the remote database.
-
Install the Turso CLI.
-
Login or signup to Turso.
-
Create a new database. In this example the database name is
andromeda
. -
Run the
show
command to see information about the newly created database:Copy the
URL
value and set it as the value forASTRO_DB_REMOTE_URL
. -
Create a new token to authenticate requests to the database:
Copy the output of the command and set it as the value for
ASTRO_DB_APP_TOKEN
. -
Push your DB schema and metadata to the new Turso database.
-
Congratulations, now you have a database connected! Give yourself a break. 👾
To explore more features of Turso, check out the Turso docs.
Connecting to remote databases
Section titled Connecting to remote databasesAstro DB allows you to connect to both local and remote databases. By default, Astro uses a local database file for dev
and build
commands, recreating tables and inserting development seed data each time.
To connect to a hosted remote database, use the --remote
flag. This flag enables both readable and writable access to your remote database, allowing you to accept and persist user data in production environments.
While remote connections are generally possible with any deployment platform using static or server rendering mode, there are currently some limitations. Non-Node runtimes like Cloudflare and Deno don’t currently support DB on server-rendered routes when using libSQL. Support for these platforms is planned for future implementation.
Configure your build command to use the --remote
flag:
You can also use the flag directly in the command line:
Be careful when using --remote
in development. This connects to your live production database, and all changes (inserts, updates, deletions) will be persisted.
The --remote
flag uses the connection to the remote DB both locally during the build and on the server. Ensure you set the necessary environment variables in both your local development environment and your deployment platform.
When deploying your Astro DB project, make sure your deployment platform’s build command is set to npm run build
(or the equivalent for your package manager) to utilize the --remote
flag configured in your package.json
.
Remote URL configuration options
Section titled Remote URL configuration optionsThe ASTRO_DB_REMOTE_URL
environment variable configures the location of your database as well as other options like sync and encryption.
URL scheme and host
Section titled URL scheme and hostlibSQL supports both HTTP and WebSockets as the transport protocol for a remote server. It also supports using a local file or an in-memory DB. Those can be configured using the following URL schemes in the connection URL:
memory:
will use an in-memory DB. The host must be empty in this case.file:
will use a local file. The host is the path to the file (file:path/to/file.db
).libsql:
will use a remote server through the protocol preferred by the library (this might be different across versions). The host is the address of the server (libsql://your.server.io
).http:
will use a remote server through HTTP.https:
can be used to enable a secure connection. The host is the same as forlibsql:
.ws:
will use a remote server through WebSockets.wss:
can be used to enable a secure connection. The host is the same as forlibsql:
.
Details of the libSQL connection (e.g. encryption key, replication, sync interval) can be configured as query parameters in the remote connection URL.
For example, to have an encrypted local file work as an embedded replica to a libSQL server, you can set the following environment variables:
Using a database file is an advanced feature, and care should be taken when deploying to prevent overriding your database and losing your production data.
Additionally, this method will not work in serverless deployments, as the file system is not persisted in those environments.
encryptionKey
Section titled encryptionKeylibSQL has native support for encrypted databases. Passing this search parameter will enable encryption using the given key:
syncUrl
Section titled syncUrlEmbedded replicas are a feature of libSQL clients that creates a full synchronized copy of your database on a local file or in memory for ultra-fast reads. Writes are sent to a remote database defined on the syncUrl
and synchronized with the local copy.
Use this property to pass a separate connection URL to turn the database into an embedded replica of another database. This should only be used with the schemes file:
and memory:
. The parameter must be URL encoded.
For example, to have an in-memory embedded replica of a database on libsql://your.server.io
, you can set the connection URL as such:
syncInterval
Section titled syncIntervalInterval between embedded replica synchronizations in seconds. By default it only synchronizes on startup and after writes.
This property is only used when syncUrl
is also set. For example, to set an in-memory embedded replica to synchronize every minute set the following environment variable:
Query your database
Section titled Query your databaseYou can query your database from any Astro page, endpoint, or action in your project using the provided db
ORM and query builder.
Drizzle ORM
Section titled Drizzle ORMAstro DB includes a built-in Drizzle ORM client. There is no setup or manual configuration required to use the client. The Astro DB db
client is automatically configured to communicate with your database (local or remote) when you run Astro. It uses your exact database schema definition for type-safe SQL queries with TypeScript errors when you reference a column or table that doesn’t exist.
Select
Section titled SelectThe following example selects all rows of a Comment
table. This returns the complete array of seeded development data from db/seed.ts
which is then available for use in your page template:
select()
API reference for a complete overview.
Insert
Section titled InsertTo accept user input, such as handling form requests and inserting data into your remote hosted database, configure your Astro project for on-demand rendering and add an adapter for your deployment environment.
This example inserts a row into a Comment
table based on a parsed form POST request:
You can also use Astro actions to insert data into an Astro DB table. The following example inserts a row into a Comment
table using an action:
See the Drizzle insert()
API reference for a complete overview.
Delete
Section titled DeleteYou can also query your database from an API endpoint. This example deletes a row from a Comment
table by the id
parameter:
See the Drizzle delete()
API reference for a complete overview.
Filtering
Section titled FilteringTo query for table results by a specific property, use Drizzle options for partial selects. For example, add a .where()
call to your select()
query and pass the comparison you want to make.
The following example queries for all rows in a Comment
table that contain the phrase “Astro DB.” Use the like()
operator to check if a phrase is present within the body
:
Drizzle utilities
Section titled Drizzle utilitiesAll Drizzle utilities for building queries are exposed from the astro:db
module. This includes:
- Filter operators like
eq()
andgt()
- Aggregation helpers like
count()
- The
sql
helper for writing raw SQL queries
Relationships
Section titled RelationshipsYou can query related data from multiple tables using a SQL join. To create a join query, extend your db.select()
statement with a join operator. Each function accepts a table to join with and a condition to match rows between the two tables.
This example uses an innerJoin()
function to join Comment
authors with their related Author
information based on the authorId
column. This returns an array of objects with each Author
and Comment
row as top-level properties:
See the Drizzle join reference for all available join operators and config options.
Batch Transactions
Section titled Batch TransactionsAll remote database queries are made as a network request. You may need to “batch” queries together into a single transaction when making a large number of queries, or to have automatic rollbacks if any query fails.
This example seeds multiple rows in a single request using the db.batch()
method:
See the Drizzle db.batch()
docs for more details.
Pushing changes to your database
Section titled Pushing changes to your databaseYou can push changes made during development to your database.
Pushing table schemas
Section titled Pushing table schemasYour table schema may change over time as your project grows. You can safely test configuration changes locally and push to your remote database when you deploy.
You can push your local schema changes to your remote database via the CLI using the astro db push --remote
command:
This command will verify that your local changes can be made without data loss and, if necessary, suggest how to safely make changes to your schema in order to resolve conflicts.
Pushing breaking schema changes
Section titled Pushing breaking schema changesThis will destroy your database. Only perform this command if you do not need your production data.
If you must change your table schema in a way that is incompatible with your existing data hosted on your remote database, you will need to reset your production database.
To push a table schema update that includes a breaking change, add the --force-reset
flag to reset all production data:
Renaming tables
Section titled Renaming tablesIt is possible to rename a table after pushing your schema to your remote database.
If you do not have any important production data, then you can reset your database using the --force-reset
flag. This flag will drop all of the tables in the database and create new ones so that it matches your current schema exactly.
To rename a table while preserving your production data, you must perform a series of non-breaking changes to push your local schema to your remote database safely.
The following example renames a table from Comment
to Feedback
:
-
In your database config file, add the
deprecated: true
property to the table you want to rename: -
Add a new table schema (matching the existing table’s properties exactly) with the new name:
-
Push to your remote database with
astro db push --remote
. This will add the new table and mark the old as deprecated. -
Update any of your local project code to use the new table instead of the old table. You might need to migrate data to the new table as well.
-
Once you are confident that the old table is no longer used in your project, you can remove the schema from your
config.ts
: -
Push to your remote database again with
astro db push --remote
. The old table will be dropped, leaving only the new, renamed table.
Pushing table data
Section titled Pushing table dataYou may need to push data to your remote database for seeding or data migrations. You can author a .ts
file with the astro:db
module to write type-safe queries. Then, execute the file against your remote database using the command astro db execute <file-path> --remote
:
The following Comments can be seeded using the command astro db execute db/seed.ts --remote
:
See the CLI reference for a complete list of commands.
Building Astro DB integrations
Section titled Building Astro DB integrationsAstro integrations can extend user projects with additional Astro DB tables and seed data.
Use the extendDb()
method in the astro:db:setup
hook to register additional Astro DB config and seed files.
The defineDbIntegration()
helper provides TypeScript support and auto-complete for the astro:db:setup
hook.
Integration config and seed files follow the same format as their user-defined equivalents.
Type safe operations in integrations
Section titled Type safe operations in integrationsWhile working on integrations, you may not be able to benefit from Astro’s generated table types exported from astro:db
.
For full type safety, use the asDrizzleTable()
utility to create a table reference object you can use for database operations.
For example, given an integration setting up the following Pets
database table:
The seed file can import Pets
and use asDrizzleTable()
to insert rows into your table with type checking:
The value returned by asDrizzleTable('Pets', Pets)
is equivalent to import { Pets } from 'astro:db'
, but is available even when Astro’s type generation can’t run.
You can use it in any integration code that needs to query or insert into the database.
Migrate from Astro Studio to Turso
Section titled Migrate from Astro Studio to Turso- In the Studio dashboard, navigate to the project you wish to migrate. In the settings tab, use the “Export Database” button to download a dump of your database.
- Follow the official instructions to install the Turso CLI and sign up or log in to your Turso account.
- Create a new database on Turso using the
turso db create
command. - Fetch the database URL using the Turso CLI, and use it as the environment variable
ASTRO_DB_REMOTE_URL
. - Create a token to access your database, and use it as the environment variable
ASTRO_DB_APP_TOKEN
. - Push your DB schema and metadata to the new Turso database.
- Import the database dump from step 1 into your new Turso DB.
- Once you have confirmed your project connects to the new database, you can safely delete the project from Astro Studio.