Queries and results
How .from().select() works, column selection, .single() / .maybeSingle(), and typing rows with @xylex-group/athena.
Every table read starts with .from(tableName) and ends with .select() (or a terminal like .single()).
Select rows
const { data, error, errorDetails, status } = await athena.from("users").select();
if (error) {
console.error("query failed:", error);
} else {
console.table(data);
}Columns
Omit the argument for all columns (*). Otherwise pass a comma-separated string or an array:
const { data } = await athena.from("users").select("id, name, email");
const { data: rows } = await athena.from("users").select(["id", "name"]);Row typing
Annotate the row type for inference on data:
interface User {
id: number;
name: string;
email: string;
active: boolean;
}
const { data } = await athena.from<User>("users").select("id, name");
// data is User[] | nullSingle row
.single() (and .maybeSingle(), same behavior) runs a select and returns the first row as an object, or null if there are no rows.
const { data: user } = await athena
.from("users")
.select("id, name")
.eq("id", 42)
.single();
if (user) console.log(user.name);Combine with sorting to fetch the latest row: .order("created_at", { ascending: false }).single().
Related
- Filtering — chain
.eq/.ilikebefore.select() - Pagination and limits —
.limit,.offset, page-based fields - Overview