Skip to main content

Examples

This section provides various examples demonstrating how to use the fetchAllRecords() method to perform common queries.

💡PostgreSQL Support

For all examples below, when fetching data from an external PostgreSQL database, specify the db connection name. This allows you to use Manta's friendly syntax to query your existing external databases.

await manta.fetchAllRecords({
db: "your_postgres_db",
table: "orders",
where: { status: "shipped" },
});

Basic pagination​

This example fetches the first page of 10 users, sorted by their first name in ascending order.

TypeScript
const users = await manta.fetchAllRecords({
table: "users",
fields: ["user_id", "first_name", "last_name", "email"],
page: 1,
list: 10,
orderBy: "first_name",
order: "asc",
});

Filtering with operators​

This example fetches all orders with an amount greater than $100.

TypeScript
const expensiveOrders = await manta.fetchAllRecords({
table: "orders",
fields: ["order_id", "product_name", "amount"],
where: {
amount: { greaterThan: 100 },
},
page: 1,
list: 10,
});

Search functionality​

This example demonstrates how to perform a keyword search for 'Alice' across both the first_name and email columns.

TypeScript
const searchResults = await manta.fetchAllRecords({
table: "users",
fields: ["user_id", "first_name", "last_name", "email"],
search: {
columns: ["first_name", "email"],
query: "Alice",
},
page: 1,
list: 10,
});

Relationships​

This example shows how to fetch support tickets and include the related user data in a single query.

TypeScript
const ticketsWithUsers = await manta.fetchAllRecords({
table: "support-tickets",
fields: ["ticket_id", "subject", "status"],
with: {
users: {
fields: ["first_name", "last_name"],
on: { from: "user_id", to: "user_id" },
},
},
page: 1,
list: 10,
});

Complex query​

This example combines multiple query parameters; including filtering, searching, and a filtered relationship, in a single API call.

TypeScript
const complexQuery = await manta.fetchAllRecords({
table: "users",
fields: ["user_id", "first_name", "last_name", "email"],
where: {
first_name: { notEquals: "NonExistent" },
},
search: {
columns: ["first_name", "email"],
query: "Alice",
},
with: {
orders: {
fields: ["order_id", "amount"],
on: { from: "user_id", to: "customer_id" },
filter: { amount: { greaterThan: 100 } },
},
},
page: 1,
list: 5,
orderBy: "first_name",
order: "asc",
});

Fetch all records​

This example shows how to fetch all records from a table by simply omitting the page and list parameters.

TypeScript
const allUsers = await manta.fetchAllRecords({
table: "users",
fields: ["user_id", "first_name", "last_name"],
orderBy: "first_name",
order: "asc",
});