Start using the SDK
Now that you got the SDK added as project dependency and generated your types from the schema using the CLI’s codegen (if you didn’t, please check Installation). It’s time to create our first queries!
Importing the Client
The .ts
file generated from the codegen exports a getXataClient
method. This is a sync method that will return the instance of your SDK, already configured with: your schema types, XATA_API_KEY
, XATA_BRANCH
, and XATA_DATABASE_URL
- everything coming from your project configuration.
1 2 3 4 5 6 7 8 9
// file generated by xata init import { getXataClient } from './src/xata' const xata = getXataClient() // if `posts` is a table in your DB const { records } = await xata.db.posts.getPaginated() console.log(records)
The above snippet queries posts
table for the first 20 records, and returns an object with them in records
array as well as a cursor in a meta
object.
Regenerating Schema Types
If your schema changes, you can regenerate your schema types for your Xata database via the CLI:
- Globally installed:
xata codegen
- Straight from the package registry:
npx @xata.io/cli@latest codegen
Read more
See our TypeScript Client session for more complete references on how to interact with your Xata database.