Getting records
The general form of a query is:
import { getXataClient } from "./xata";
const client = getXataClient();
const data = await client
.db[table]
.select([...])
.filter({ ... })
.sort({ ... })
.page({ ... })
.getMany();
from xata.client import XataClient
xata = XataClient()
data = xata.data().query("{table_name}", {
"columns": [...],
"filter": {
...
},
"sort": {
...
},
"page": {
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT <columns> from \"table_name\" WHERE <filter> ORDER BY <sort> LIMIT <number>;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"columns": [...],
"filter": {
...
},
"sort": {
...
},
"page": {
}
}
For the REST API example, note that a POST request is used, even though the data is not modified, in order to be able to use a body for the request (GET requests with a body are non-standard).
All the requests are optional, so the simplest query request looks like this:
const teams = await xata.db.Teams.getMany();
teams = xata.data().query("Teams")
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * from \"Teams\" LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
The response looks like this:
[
{
"id": "rec_cd8s4kbo8dsvsjilo1ug",
"name": "Matrix",
"owner": {
"id": "myid"
}
}
]
"records": [
{
"id": "rec_c8hng2h26un90p8sr7k0",
"name": "Matrix",
"owner": {
"id": "myid"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
],
"meta": {
"page": {
"cursor": "jMq7DcIwEIDhnjH-2sWRAsItAT2KkOU8bAgB3Zkqyu6IDei_",
"more": false
}
}
{
"records": [
{
"id": "rec_c8hng2h26un90p8sr7k0",
"name": "Matrix",
"owner": {
"id": "myid"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
]
}
{
"records": [
{
"id": "rec_c8hng2h26un90p8sr7k0",
"name": "Matrix",
"owner": {
"id": "myid"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
],
"meta": {
"page": {
"cursor": "jMq7DcIwEIDhnjH-2sWRAsItAT2KkOU8bAgB3Zkqyu6IDei_",
"more": false
}
}
}
For the REST API example, note that the id
, xata.version
, xata.updatedAt
and xata.createdAt
are included in the returned records. We will discuss the meta.page
object when we talk about paginating through the records.
The getFirst()
method returns the first record matching the query, in ascending order of the id
string field. In case there are no matching records it returns null
.
Alternatively, the getFirstOrThrow()
method can be used to throw a "No results found."
console error in case there are no results to return.
The TypeScript SDK provides three methods for querying multiple records:
getPaginated()
: returns a page of records in the query results. Note that the response type is different fromgetMany()
andgetAll()
.
const page = await xata.db.Posts.getPaginated({
pagination: { size: 100, offset: 0 }
});
const firstPageRecords = page.records; // Items 1...100
const hasNextPage = page.hasNextPage();
const nextPage = await page.nextPage();
const nextPageRecords = nextPage.records; // Items 101...200
const hasAnotherNextPage = page.hasNextPage();
// Request different page size for this next call for items 101...110
const anotherPageWithDifferentSize = await page.nextPage(10);
const differentSizedPage = anotherPageWithDifferentSize.records;
getAll()
: returns all the records in the query results. Warning: this is dangerous on large tables (more than 10,000 records), as it will potentially load a lot of data into memory and create a lot of requests to the server.getMany()
: returns the requested number of records for the query in a single response. The default is 20 records, but you can change it by modifying the pagination size.
const page = await xata.db.Posts.getMany({
pagination: { size: 100 }
});
Both the getAll()
and getMany()
will produce multiple requests to the server if the query should return more than the maximum page size, combining all results in a single response. It's important to be aware of this because it can cause multiple round-trips to the server, which can affect the latency.
The getPaginated()
call always performs a single request, therefore it performs a single round-trip.
Unless you have a specific reason to prefer another method, we generally recommend getMany()
, as the best balance between usability and performant defaults, and it is the method that we use in our examples.
You can retrieve a record with a given ID using a request like this:
const user = await xata.db.Users.read('myid');
user = xata.records().get("Users", "myid")
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * from \"Users\" WHERE id=$1;",
"params": ["myid"]
}
// GET https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/data/{record_id}
By default, the Query API returns all columns of the queried table. For link columns, only the ID column of the linked records is included in the response. You can use column selection to both reduce the number of columns returned, and to include columns from linked tables.
For example, if you are only interested in the name and the city of the user, you can make a request like this:
const users = await xata.db.Users.select(['name', 'city']).getMany();
users = xata.data().query("Users", {
"columns": ["name", "city"]
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT name,city from \"Users\" LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"columns": ["name", "city"]
}
A sample response will look like this:
{
"address": {
"city": "New York"
},
"id": "myid",
"name": "Keanu Reeves",
"xata": {
"version": 1,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
It's worth noting that the special columns id, xata.version, xata.createdAt and xata.updatedAt are always returned, even if they are not explicitly requested.
The same syntax can be used to select columns from a linked table, therefore adding new columns to the response. For example, to query all the columns of the Teams table and also add all the columns of the owner user, you can use:
const teams = await xata.db.Teams.select(['*', 'owner.*']).getMany();
users = xata.data().query("Users", {
"columns": ["*", "owner.*"]
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT *,\"Users\".* FROM \"Teams\" LEFT JOIN \"Users\" ON \"Teams\".owner=\"Users\".id;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"columns": ["*", "owner.*"]
}
You can do this transitively as well, for example:
const posts = await xata.db.Posts.select([
"title",
"author.*",
"author.team.*",
]).getMany();
posts = xata.data().query("Posts", {
"columns": [
"title",
"author.*",
"author.team.*"
]
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT title,\"Users\".*,\"Teams\".* FROM \"Posts\" LEFT JOIN \"Users\" ON \"Posts\".author=\"Users\".id LEFT JOIN \"Teams\" ON \"Users\".team=\"Teams\".id;"
}
// POST https://tutorial-ng7s8c.us-east-1.xata.sh/db/tutorial:main/tables/Posts/query
{
"columns": ["title", "author.*", "author.team.*"]
}
In this example, the author
is a link column from Posts
to Users
and team
is a link column from Users
to Teams
.
This section contains a few examples of how to use filtering. To find all supported operators and examples for them, see the Filtering section of the API Guide.
To filter the results, use the filter
function/parameter. For example:
const users = await xata.db.Users.filter({ email: 'keanu@example.com' }).getMany();
users = xata.data().query("Users", {
"filter": {
"email": "keanu@example.com"
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" WHERE email=$1 LIMIT 20;",
"params": ["keanu@example.com"]
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"filter": {
"email": "keanu@example.com"
}
}
Will return only the records with the given email address.
You can also refer to a linked column in filters, for example:
const users = await xata.db.Users.filter({ 'team.name': 'Matrix' }).getMany();
users = xata.data().query("Users", {
"filter": {
"team.name": "Matrix"
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" LEFT JOIN \"Teams\" ON \"Users\".team=\"Teams\".id WHERE \"Teams\".name=$1;",
"params": ["Matrix"]
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"filter": {
"team.name": "Matrix"
}
}
Note that in the above, name
is a column in the Teams
table, and we can refer to it even when querying the Users
table.
To give a more complex filtering example, consider the following:
const users = await xata.db.Users.filter({
zipcode: { $gt: 100 },
$any: [{ name: { $contains: 'Keanu' } }, { name: { $contains: 'Carrie' } }]
}).getMany();
users = xata.data().query("Users", {
"filter": {
"zipcode": {"$gt": 100},
"$any": [
{"name": { "$contains": "Keanu" }},
{"name": { "$contains": "Carrie" }}
]
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" WHERE zipcode > 100 AND (name LIKE '%Keanu%' OR name LIKE '%Carrie%') LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"filter": {
"zipcode": { "$gt": 100 },
"$any": [{ "name": { "$contains": "Keanu" } }, { "name": { "$contains": "Carrie" } }]
}
}
Translating the above filter in English: filter all users with a zipcode greater than 100, and the full name contains either "Keanu" or "Carrie".
The example above demonstrates several operators:
$gt
: which can be applied to number columns, and means "greater than".$contains
: which can be applied to string columns, and does a substring match.$any
: which can be used to create OR conditions. The record matches if any of the conditions enclosed are true.
To see the rest of the operators available, check out the Filtering guide.
To sort the results, use the sort
function/parameter. For example:
const users = await xata.db.Users.sort('name', 'asc').getMany();
users = xata.data().query("Users", {
"sort": {
"name": "asc"
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" ORDER BY \"name\" asc LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"sort": {
"name": "asc"
}
}
To sort descending, use desc
instead of asc
:
const users = await xata.db.Users.sort('name', 'desc').getMany();
users = xata.data().query("Users", {
"sort": {
"name": "desc"
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" ORDER BY \"city\" desc, \"name\" asc LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"sort": {
"name": "desc"
}
}
It is also possible to have secondary sort criteria. For example:
const users = await xata.db.Users.sort('city', 'desc').sort('name', 'asc').getMany();
users = xata.data().query("Users", {
"sort": [
{"city": "desc"},
{"name": "asc"}
]
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" ORDER BY \"city\" desc, \"name\" asc LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"sort": [{ "city": "desc" }, { "name": "asc" }]
}
To sort results in random order, use random
instead of desc
or asc
:
const users = await xata.db.Users.sort('*', 'random').getMany();
users = xata.data().query("Users", {
"sort": {
"*": "random"
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" ORDER BY RANDOM() LIMIT 20;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"sort": {
"*": "random"
}
}
Note that random sorting does not apply to a specific column, hence the special column name "*"
.
This section doesn't apply to the TypeScript SDK, because it is offering a higher-level abstraction over the pagination mechanism. See the query functions section. Only REST API examples are provided in this section.
Xata offers two types of pagination:
- cursor-based, using
after
andbefore
parameters - offset-based, using
offset
andlimit
parameters
The depth of cursor-based pagination is unrestricted. When running a query, you can specify a particular page size.
For example:
const page = await xata.db.Users.getPaginated({
pagination: { size: 2 }
});
users = xata.data().query("Users", {
"page": {
"size": 2
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"page": {
"size": 2
}
}
Returns only the first two records:
{
"records": [
{
"address": {
"street": "123 Main St",
"zipcode": 12345
},
"email": "keanu@example.com",
"full_name": "Keanu Reeves",
"id": "rec_c8hnbch26un1nl0rthkg",
"team": {
"id": "rec_c8hng2h26un90p8sr7k0"
},
"xata": {
"version": 2,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
},
{
"address": null,
"email": "laurence@example.com",
"full_name": "Laurence Fishburne",
"id": "rec_c8hnnh126unff00ifhhg",
"team": {
"id": "rec_c8hng2h26un90p8sr7k0"
},
"xata": {
"version": 0,
"createdAt": "2023-05-15T08:21:31.96526+01:00",
"updatedAt": "2023-05-15T21:58:54.072595+01:00"
}
}
],
"meta": {
"page": {
"cursor": "VMoxDsIwDAXQnWP8OUPSASFfAnZUoZDWtSEEyTFT1bujion9PdKK_",
"more": true
}
}
}
In this case, notice that the meta.page
objects contains "more": true
. This is an indication that there are more records available. The "cursor"
key is a pointer to the current page. To retrieve the next page of results, you can make a request like this:
const page1 = await xata.db.Users.getPaginated({
pagination: { size: 2 }
});
const page2 = await page1.nextPage();
page_1 = xata.data().query("Users", {
"page": {
"size": 2
}
})
# if page_1.has_more_results()
page_2 = xata.data().query("Users", {
"page": {
"size": 2,
"after": page_1.get_cursor()
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"page": {
"size": 2,
"after": "VMoxDsIwDAXQnWP8OUPSASFfAnZUoZDWtSEEyTFT1bujion9PdKK_"
}
}
You can continue like this until "more"
is returned as false
.
The offset-page pagination is limited to querying up to 1000 records and is recommended only for usecases where you don't expect the number of records to grow beyond that. In most cases, you should use the cursor-based pagination.
An example of offset based pagination:
const page = await xata.db.Users.getPaginated({
pagination: { size: 10, offset: 10 }
});
page = xata.data().query("Users", {
"page": {
"size": 10,
"offset": 10
}
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" LIMIT 10 OFFSET 10;"
}
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"page": {
"offset": 10,
"size": 10
}
}
You can find more information about pagination in the API reference.
Now that we can get retrieve data from a database, we might be interested in creating more data, updating data or deleting data. We've got guides for each of these operations.
By specifying the option consistency: eventual
the query request will be serviced by the Replica Store which has a small, typically insignificant, propagation delay compared to the Primary Store as outlined in the Data Model guide.
The default value for the consistency option is strong
, which retrieves data from the Primary Store and guarantees that the response is up to date with the latest state of the record content.
It is recommended to use the Replica Store for queries wherever possible, in order to get the best possible performance out of your branch's assigned units.
const page = await xata.db.Users.getPaginated({
consistency: "eventual"
})
records = xata.data().query("Users", {
"columns": ["*"],
"consistency": "eventual"
})
// POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/sql
{
"statement": "SELECT * FROM \"Users\" LIMIT 20;",
"consistency": "eventual"
}
//POST https://{workspace}.{region}.xata.sh/db/{db}:{branch}/tables/{table}/query
{
"columns": ["*"],
"consistency": "eventual"
}