From 27dc8bbe0d36bd0382bfc91082505cfe1916396a Mon Sep 17 00:00:00 2001 From: hitesh Date: Mon, 4 Aug 2025 12:03:04 +0000 Subject: [PATCH] Find & Join in Mongo and PostgreSQL --- ... Join Feature in MongoDB and PostgreSQL.md | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 Find & Join Feature in MongoDB and PostgreSQL.md diff --git a/Find & Join Feature in MongoDB and PostgreSQL.md b/Find & Join Feature in MongoDB and PostgreSQL.md new file mode 100644 index 0000000..633c503 --- /dev/null +++ b/Find & Join Feature in MongoDB and PostgreSQL.md @@ -0,0 +1,154 @@ +## Query Across SQL and NoSQL — Without Writing Code + +MongoDB and PostgreSQL are often used together in modern applications — each bringing unique strengths. While PostgreSQL offers structured, relational storage, MongoDB gives flexibility through document-based collections. + +But combining data from both in real time has always been a challenge. Traditionally, you’d have to: +- Write custom backend logic to merge data +- Create multiple API calls or aggregation pipelines +- Maintain sync through fragile ETL workflows + +To solve this challenge, API Maker introduces the **Find & Join** feature. + +## Why Use Find & Join for MongoDB and PostgreSQL + +Modern applications often use a **polyglot database architecture** — leveraging **MongoDB** for unstructured or flexible data and **PostgreSQL** for structured, relational data. + +While this separation is practical from a design and performance standpoint, it introduces a major challenge: + + **How do you fetch connected data across MongoDB and PostgreSQL in a single query** — without building and maintaining complex backend logic? + +This is exactly where **API Maker’s Find & Join feature** shines. + +It allows you to **perform deep, cross-database joins and filters between MongoDB and PostgreSQL** using a single, declarative JSON query — all through REST APIs. + +This page provides information on using the Find & Join feature specifically with MongoDB and PostgreSQL. + +If you're looking for a general overview of the Find & Join feature, please refer to the - [Find & Join Feature](https://apimaker.dev/find-and-join) + +## Schema Setup in Api Maker + +To enable Find & Join operations between MongoDB and PostgreSQL, you need to define relationships in your table/collection schema using **API Maker’s Table schema.** + +For a Detailed Information about the Schema Setup, you can refer to this page - [Schema Setup](https://docs.apimaker.dev/v1/docs/schema/schema.html) + +Once the schema is defined, API Maker can automatically resolve joins at runtime — even across different database types. +### Defining the Relationship in Schema + +Let’s say: +- You have a **`users`** table in **PostgreSQL** +- You have a **`profiles`** collection in **MongoDB** + +You want to use an API that supports the **Find & Join** feature on the `profiles` collection (MongoDB). In this case, you'd define the relationship like this: +```typescript +user_id: { + __type: EType.number, + instance: "Postgres", + database: "accounts_db", + table: "users", + column: "id" +} +``` + +Here what each field means: + +- **`__type: EType.number :`** Indicates that the `user_id` field is a number (to match the PostgreSQL `id` column type). +- **`instance: "Postgres" :`** Specifies the target database engine — in this case, PostgreSQL. +- **`database: "accounts_db" :`** The name of the PostgreSQL database where the `users` table resides. +- **`table: "users" :`** The relational table in PostgreSQL to which the `user_id` field will be linked. +- **`column: "id" :`** The specific column in the `users` table that the `user_id` field refers to. + +With this schema defined, API Maker can: +- Automatically recognize and resolve the relationship between MongoDB and PostgreSQL +- Enable join queries like `user_id.name` directly in your REST API calls +- Eliminate the need for custom backend join logic — it's all handled for you at runtime + +## Sample Query Examples : + +Once you've defined relationships between your MongoDB collections and PostgreSQL tables in the schema, you can use the **Find & Join** feature directly in your API calls — without writing any backend logic. + +API Maker supports the Find & Join Feature in both the : +- **GET requests** (using URL query parameters) +- **POST requests** (using request body for advanced querying) +### **Example Scenario** + +You have: +- A `users` table in **PostgreSQL** +- A `profiles` collection in **MongoDB** with a field `user_id` referencing `users.id` + +**1. GET API Example (Using URL Query Parameter) :** +Use the `find` Query parameter in your auto-generated GET API to filter across relationships. + +**Request:** +```http +GET /api/profiles?find={ "user_id.name": "Alice" } +``` + +This query fetches all `profiles` (from MongoDB) where the related `user` (from PostgreSQL) has the name `"Alice"`. + +**2. POST API Example :** +POST APIs give you more flexibility — allowing you to send a `find` object along with additional options like `sort`, `limit`, and `deep`. + +**Request:** +```http +POST /api/query/profiles +Content-Type: application/json + +{ + "find": { + "user_id.signup_date": { "$gt": "2024-01-01" }, + "user_id.role": "admin" + }, + "limit": 10, + "sort": { + "user_id.signup_date": -1 + } +} +``` + +This will return up to 10 profiles linked to PostgreSQL users who signed up after January 1st, 2024 and have the role `"admin"` — sorted by most recent signup. + +To explore all REST APIs that support **Find & Join**, check out the [auto-generated REST APIs](https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-get-all-api.html) and [schema-based REST APIs](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-get-all-api.html) available on the [API Maker Docs Page](https://docs.apimaker.dev/). +## Comparison to Traditional Methods & Other Platforms + +Joining data between **MongoDB** and **PostgreSQL** has traditionally been a complex, manual process for developers. + +**Traditional Methods:** +Before tools like API Maker, developers had to: +- Write custom backend code to connect and query both databases +- Manually merge results at the application level +- Build and maintain fragile ETL pipelines +- Handle sync issues and performance bottlenecks + +These methods are time-consuming and hard to scale — especially when relationships become more complex. + +How API Maker Simplifies Cross Database Joins and Conditional Filtering : +- **No backend logic** required +- **Schema-based joins** resolved automatically at runtime +- **Real-time querying** across different databases +- Simple REST API interface — easy to use from frontend or backend + +### Compared to Other Platforms + +| Feature | **API Maker** | **Appwrite** | **Supabase** | **Firebase** | +| ----------------------------- | ------------- | ------------ | ------------ | -------------- | +| Cross-DB Joins (Mongo + SQL) | Yes | No | No | No | +| N-Level Nested Joins | Unlimited | No | Basic | No | +| REST Support for Join Queries | Full | Partial | Yes | Firestore only | +| Schema-Based Join Logic | Yes | No | Limited | No | +| Works with Mongo + PostgreSQL | Yes | No | No | No | + +**API Maker** is the only platform among these that offers **real-time, schema-aware, cross database joins** between MongoDB and PostgreSQL — with **no backend logic required**. + +## FAQ's + +1. **Can I join MongoDB collections with PostgreSQL tables in both directions?** + Yes. As long as the relationship is defined in your schema, you can perform joins in either direction — **`MongoDB`** → **`PostgreSQL`** or **`PostgreSQL`** → **`MongoDB`** — using the same query structure. + +2. **How do I filter on PostgreSQL fields from within a MongoDB collection?** + You can use nested dot notation based on your schema definition. + For example: `{ "user_id.role": "admin" }`. + In this case, `user_id` is a reference from the MongoDB `profiles` collection to the PostgreSQL `users` table. + +3. **Is performance impacted by cross-database joins?** + API Maker optimizes cross-database joins at runtime. + For best performance, make sure to **index the join keys** in both MongoDB and PostgreSQL. \ No newline at end of file