Zum Inhalt springen

Daten abfragen

Greifen Sie auf jede Collection über client.data.<collectionName> (camelCase, automatisch in snake_case umgewandelt) oder client.data.collection<Record<string, unknown>>("slug") (expliziter Slug) zu:

// Property-style access (camelCase → snake_case slug)
client.data.blogPosts // → slug "blog_posts"
client.data.users // → slug "users"
// Dynamic access by slug
client.data.collection<Record<string, unknown>>("blog_posts")

Strict Mode (generiertes SDK): Wenn Sie das generierte collectionsDictionary an createRebaseClient übergeben, validiert der Daten-Proxy Property-Zugriffe zum Zeitpunkt des Zugriffs. Ein Tippfehler wie client.data.prodcuts wirft sofort einen Fehler mit einer hilfreichen Meldung und einem Vorschlag für die nächstgelegene Übereinstimmung, anstatt später einen verwirrenden 404 zu erzeugen. Verwenden Sie client.data.collection<Record<string, unknown>>("slug"), um die Validierung für dynamische oder zur Laufzeit bestimmte Slugs zu umgehen.

// All products (default limit: 20)
const { data, meta } = await client.data.products.find();
// With pagination, filtering, and sorting
const { data, meta } = await client.data.products.find({
where: { active: ["==", true], price: [">=", 100] },
orderBy: ["created_at", "desc"],
limit: 25,
offset: 0
});
// data is Entity<M>[] — each item has { id, values, path }
// meta has { total, limit, offset, hasMore }
const product = await client.data.products.findById(42);
// Returns Entity<M> | undefined
const newProduct = await client.data.products.create({
name: "New Product",
price: 29.99,
active: true
});
// With a specific ID
const newProduct = await client.data.products.create(
{ name: "Custom ID Product" },
"my-custom-id"
);
const updated = await client.data.products.update(42, {
name: "Updated Name",
price: 39.99
});
await client.data.products.delete(42);
const total = await client.data.products.count();
// With filters
const activeCount = await client.data.products.count({
where: { active: ["==", true] }
});

Verketten Sie Methoden für ausdrucksstärkere Abfragen:

const { data } = await client.data.products
.where("price", ">=", 100)
.where("active", "==", true)
.orderBy("created_at", "desc")
.limit(10)
.find();
Methode Beschreibung Beispiel
.where(field, op, value) Fügt eine Filterbedingung hinzu .where("age", ">=", 18)
.orderBy(field, dir) Sortiert die Ergebnisse .orderBy("name", "asc")
.limit(n) Begrenzt die Anzahl der Ergebnisse .limit(25)
.offset(n) Überspringt die ersten N Ergebnisse .offset(50)
.search(text) Volltextsuche .search("laptop")
.include(...relations) Bindet verwandte Entitäten ein .include("author", "tags")
.find() Führt die Abfrage aus Gibt FindResponse<M> zurück
.listen(onUpdate, onError?) Abonniert Echtzeit-Updates Gibt unsubscribe() zurück
Operator Alias Beschreibung
"==" "eq" Gleich
"!=" "neq" Ungleich
">" "gt" Größer als
">=" "gte" Größer oder gleich
"<" "lt" Kleiner als
"<=" "lte" Kleiner oder gleich
"in" Wert in einem Array
"not-in" "nin" Wert nicht in einem Array
"array-contains" "cs" Array-Feld enthält den Wert
"array-contains-any" "csa" Array-Feld enthält einen der Werte

Der Parameter where in find() unterstützt zwei Formate:

// 1. Tuple syntax — [operator, value] (recommended)
await client.data.products.find({
where: {
status: ["==", "active"],
featured: ["==", true],
price: [">=", 100],
category: ["in", ["electronics", "gadgets"]],
deleted_at: ["!=", null]
}
});
// 2. Pre-serialized PostgREST string syntax (advanced)
await client.data.products.find({
where: { status: "eq.published", price: "gte.100" }
});

Hinweis: Vorserialisierte PostgREST-Strings (Format 2) sind ein Notausgang, um Filterwerte zu übergeben, die bereits im Wire-Format vorliegen. Bevorzugen Sie die Tuple-Syntax für Typsicherheit und Lesbarkeit.

// Offset-based pagination
const page1 = await client.data.products.find({ limit: 20, offset: 0 });
const page2 = await client.data.products.find({ limit: 20, offset: 20 });
// Check if more pages exist
if (page1.meta.hasMore) {
// fetch next page
}
// Page-number pagination (1-indexed)
const page = await client.data.products.find({ page: 2, limit: 20 });
// Sort by field (format: ["field", "direction"])
const { data } = await client.data.products.find({
orderBy: ["created_at", "desc"]
});
// Fluent style
const { data } = await client.data.products
.orderBy("price", "asc")
.find();
// Via find params
const { data } = await client.data.products.find({
searchString: "wireless headphones"
});
// Fluent style
const { data } = await client.data.products
.search("wireless headphones")
.limit(10)
.find();

Relationen können eingebunden werden, sodass verwandte Entitäten zusammen mit den Primärdaten zurückgegeben werden, statt nur ihrer Fremdschlüssel-IDs.

// Include specific relations
const { data } = await client.data.posts
.include("author", "categories")
.find();
// Include all defined relations
const { data } = await client.data.posts
.include("*")
.find();
const { data } = await client.data.posts.find({
include: ["author", "categories"]
});
const { data } = await client.data.posts
.where("status", "==", "published")
.include("author")
.orderBy("published_at", "desc")
.limit(10)
.find();

Wenn Relationen eingebunden sind, enthält die Antwort sowohl den skalaren Fremdschlüssel als auch das hydrierte Relationsobjekt:

const { data } = await client.data
.collection<{ author_id: string; author?: { name: string } }>("posts")
.include("author")
.find();
for (const post of data) {
// Scalar foreign key — always present
console.log(post.author_id); // "uuid-1234"
// Hydrated relation — present when included
console.log(post.author?.name); // "Jane Doe"
}

Hinweis: Ohne .include("author") wird nur das skalare Feld author_id zurückgegeben. Das hydrierte author-Objekt ist undefined.

Die Relationsnamen, die Sie an include() übergeben, müssen mit dem relationName übereinstimmen, der im relations-Array der Collection definiert ist:

// Collection definition
relations: [
{ relationName: "author", target: () => usersCollection, ... },
{ relationName: "categories", target: () => categoriesCollection, ... }
]
// SDK usage — names must match
client.data.articles.include("author", "categories").find()

Rufen Sie benutzerdefinierte Server-Endpunkte auf, die über das Functions-System registriert wurden:

// Using client.functions.invoke()
const result = await client.functions.invoke<{ summary: string }>(
"generate-summary",
{ articleId: 42 }
);
// With options
const result = await client.functions.invoke<{ status: string }>(
"process-order",
{ orderId: 123 },
{ method: "POST", path: "status/check" }
);
// Shorthand via client.call()
const result = await client.call<{ summary: string }>(
"functions/generate-summary",
{ articleId: 42 }
);