Xata & Astro
Xata is a Serverless Data Platform that combines the features of a relational database, a search engine, and an analytics engine by exposing a single consistent REST API.
Adding a database with Xata
Section titled Adding a database with XataPrerequisites
Section titled Prerequisites- A Xata account with a created database. (You can use the sample database from the Web UI.)
- An Access Token (
XATA_TOKEN_API
). - Your Database URL.
After you update and initialize the Xata CLI, you will have your API token in your .env
file and database URL defined.
By the end of the setup, you should have:
And the databaseURL
defined:
Environment configuration
Section titled Environment configurationTo have IntelliSense and type safety for your environment variables, edit or create the file env.d.ts
in your src/
directory:
Using the codegeneration from the Xata CLI and choosing the TypeScript option, generated an instance of the SDK for you, with types tailored to your database schema. Additionally, @xata.io/client
was added to your package.json
.
Your Xata environment variables and database url were automatically pulled by the SDK instance, so there’s no more setup work needed.
Now, your project should have the following structure:
Directorysrc/
- xata.codegen.ts
- env.d.ts
- .env
- astro.config.mjs
- package.json
- .xatarc
Create your queries
Section titled Create your queriesTo query your posts, import and use getXataClient()
in a .astro
file. The example below queries the first 50 posts from Xata’s Sample Blog Database.
It’s important to note the SDK needs to be regenerated everytime your schema changes. So, avoid making changes to the generated files the Xata CLI creates because once schema updates, your changes will be overwritten.