#combine data from 2 different DB's

1 messages · Page 1 of 1 (latest)

tame hingeBOT
#

Hello! I'm AppsmithBot, here to assist you with your Appsmith queries.

To combine data from two different databases like MySQL and MongoDB in Appsmith, you can follow these steps:

  1. Create Queries for Each Database:
  • First, create a query to fetch data from your MySQL database.
  • Then, create another query to fetch data from your MongoDB database.
  1. Use JavaScript to Combine Data:
  • You can use a JavaScript function in a JSObject to combine the data from these two queries based on the common column.

Here's a basic example of how you might set this up:

export default {
combineData: () => {
const mysqlData = Query1.data; // Replace Query1 with your MySQL query name
const mongoData = Query2.data; // Replace Query2 with your MongoDB query name

// Assuming both datasets have a common column named 'id'
const combinedData = mysqlData.map(mysqlRow => {
const mongoRow = mongoData.find(mongoRow => mongoRow.id === mysqlRow.id);
return { ...mysqlRow, ...mongoRow };
});

return combinedData;
}
}

  1. Use Combined Data:
  • You can then use the combined data in your widgets by referencing ** {?{ JSObject1.combineData() }?} **.

This approach allows you to fetch and combine data from different databases within Appsmith. Let me know if you need further assistance!

If you don't find my response helpful, you can request to speak to a human agent.

ionic surge
#

how can i run this function using buttton widget ?

tame hingeBOT
#

To run a JavaScript function using a Button widget in Appsmith, you can follow these steps:

  1. Create the JSObject:
  • Ensure you have your JSObject with the function defined, as shown in the previous example.
  1. Add a Button Widget:
  • Drag and drop a Button widget onto your canvas.
  1. Configure the Button's onClick Property:
  • Select the Button widget.
  • In the property pane on the right, find the onClick property.
  • Set the onClick property to run your JavaScript function using the mustache syntax. For example, if your JSObject is named JSObject1 and the function is combineData, set it as:

{?{ JSObject1.combineData() }?}

This setup will execute the combineData function whenever the button is clicked. Let me know if you need more help!

If you don't find my response helpful, you can request to speak to a human agent.

ionic surge
#

ok ..we dont have to specifically use {?{ JSObject1.combineData.run() }?} ?

tame hingeBOT
#

In Appsmith, when you want to execute a function from a JSObject using a Button widget, you should use the following syntax:

{?{ JSObject1.combineData() }?}

You do not need to use .run() for functions defined in a JSObject. The .run() method is typically used for executing queries, not JavaScript functions within JSObjects. So, the correct way to trigger your combineData function on a button click is by using the syntax above.

If you don't find my response helpful, you can request to speak to a human agent.

ionic surge
#

its not refreshing my table data

tame hingeBOT
#

To ensure that your table data is refreshed with the combined data when the button is clicked, follow these steps:

  1. Ensure the Table Widget is Bound to the Combined Data:
  • Make sure your Table widget's Table Data property is set to the result of your JavaScript function. For example:

{?{ JSObject1.combineData() }?}

  1. Check the Function Logic:
  • Verify that your combineData function is correctly combining the data and returning the expected result.
  1. Trigger a Refresh:
  • If the table is not updating, ensure that the function is being called correctly. You can add a console.log statement inside your function to check if it's being executed:

export default {
combineData: () => {
console.log("Function executed");
const mysqlData = Query1.data;
const mongoData = Query2.data;

const combinedData = mysqlData.map(mysqlRow => {
const mongoRow = mongoData.find(mongoRow => mongoRow.id === mysqlRow.id);
return { ...mysqlRow, ...mongoRow };
});

return combinedData;
}
}

  1. Re-run Queries if Needed:
  • If your data might change and you need to fetch fresh data each time, ensure that your queries (Query1 and Query2) are re-run before combining the data. You can do this by calling Query1.run() and Query2.run() before executing the combine function.

Here's how you can modify the button's onClick to ensure queries are run before combining:

{?{ Query1.run(() => Query2.run(() => JSObject1.combineData())) }?}

This setup will run Query1, then Query2, and finally execute the combineData function, ensuring that the table is updated with the latest data.

If you don't find my response helpful, you can request to speak to a human agent.