Introduction
The MantaHQ SDK provides a unified interface to interact with your data. Instead of writing custom logic for every data source, you can use a single, consistent syntax to manage your data across different environments (databases). This section covers how to use the SDK to perform database operations.
What is the unified data layer?
MantaHQ acts as a unified data layer between your application and your infrastructure. By abstracting the complexities of specific database drivers, the SDK allows you to focus on building features rather than managing connections or writing complex SQL queries.
Whether you are querying native data tables or an external database (e.g. PostgreSQL etc.), the methods like fetchOneRecord() or createRecords() remain identical.
Supported databases
MantaHQ currently supports the following data sources:
- Manta Tables: Our native, high-performance storage built directly into the platform.
- PostgreSQL: Connect your existing Postgres database.
You can connect these sources in the Connections section of your MantaHQ dashboard before interacting with them via the SDK.
What you can do
Use the SDK to perform standard CRUD (create, read, update, and delete) operations:
- Fetch data: Retrieve single or multiple records with complex filtering using the
whereparameter. - Manage relationships: Fetch related data across tables (e.g., users with their orders) in a single request.
- Create records: Insert new data into your tables.
- Update and Delete: Modify or remove existing records safely.
Examples
The MantaHQ SDK uses a consistent syntax for all queries. The primary difference when querying an external database versus a native Manta table is the use of the db parameter.
- Manta data table
- PostgreSQL
When querying a native Manta table, you do not need to provide a db parameter. The SDK automatically routes the request to your native Manta storage.
import { MantaClient } from "mantahq-sdk";
const manta = new MantaClient({
sdkKey: "manta_sk_live_xxxx",
});
// Query a native Manta data table
const mantaUsers = await manta.fetchAllRecords({
table: "users", // No db parameter required
where: {
status: { equals: "active" },
signup_date: { greaterThan: "2024-01-01" },
},
fields: ["id", "first_name", "email", "signup_date"],
search: {
columns: ["first_name", "email"],
query: "john",
},
page: 1,
list: 20,
orderBy: "signup_date",
order: "desc",
});
When querying an external database like PostgreSQL, you must include the db parameter. This parameter identifies the specific database connection you configured in the MantaHQ dashboard.
import { MantaClient } from "mantahq-sdk";
const manta = new MantaClient({
sdkKey: "manta_sk_live_xxxx",
});
// Query an external PostgreSQL database
const externalUsers = await manta.fetchAllRecords({
db: "analytics_pg", // Unique identifier for your connected database
table: "users",
where: {
status: { equals: "active" },
signup_date: { greaterThan: "2024-01-01" },
},
fields: ["id", "first_name", "email", "signup_date"],
search: {
columns: ["first_name", "email"],
query: "john",
},
page: 1,
list: 20,
orderBy: "signup_date",
order: "desc",
});
What this example does:
- Identifies the source: Uses
db: 'analytics_pg'to point to a specific external PostgreSQL connection. If thedbparameter is absent, it points to the native Manta table. - Filters records: Uses
whereto find only active users who signed up after January 1, 2024. - Selects fields: Returns only the
id,first_name,email, andsignup_date. - Performs a search: Scans the
first_nameandemailcolumns for the string "john". - Handles pagination: Returns the first page with a limit of 20 records.
- Sorts results: Orders the list by
signup_datefrom newest to oldest.