Translate

New Live Courses

UpComing SoftWare Training Demos

🚀 *Playwright With AI Testing Demo In Telugu*
🗓️ *Date:* 28 Oct2026, 08:00 PM IST
📝 *Register Now!:*
https://www.vlrt.in/3r
👥 *Join WA Community:*
https://www.vlrt.in/3w
💡 *Course Content:*
https://www.vlrt.in/3c
▶️ *Demo Videos:*
https://www.vlrt.in/3v

🚀 *Gen AI Engineer (Production Focused)*
🗓️ *Date:* 29th Oct 2026, 06:00PM IST
📝 *Register Now!:*
https://www.vlrt.in/gr
👥 *Join WA Community:*
https://www.vlrt.in/gw
💡*Course Content:*
https://www.vlrt.in/ai
▶️ *Demo Videos:*
https://www.vlrt.in/gv

🚀 *Vulnerability Management Training Demo*
🗓️ *Date:* 28th Oct 2026, 10:00 AM IST
📝*Register Now!:*
https://www.vlrt.in/vr
👥 *Join Community:*
https://www.vlrt.in/cw
💡 *Course Content:*
https://www.vlrt.in/vc
▶️ *Demo Videos:*
https://www.vlrt.in/Vm

Friday, 15 March 2024

If database table does not have autoincrement primary key how can you pick the newly inserted records always from Muleapi?143

 If database table does not have autoincrement primary key how can you pick the newly inserted records always from Muleapi?


Here are two approaches to identify newly inserted records in MuleSoft 4 when the database table doesn't have an auto-incrementing primary key:

Method 1: Utilizing Database Cursors (Recommended):

  1. Database Cursor: Leverage a database cursor to efficiently retrieve recently inserted data.

  2. Open Cursor: Establish a cursor pointing to the table you're interested in.

  3. Fetch Data: Execute the cursor to fetch data progressively, specifying a condition to filter recently inserted records.

Here's a Mule flow example:


XML


<flow name="get-new-records">
  <db:select config-ref="dbConfig" doc:name="Open Cursor">
    <db:sql>SELECT * FROM your_table WHERE inserted_at >= current_timestamp - interval '1 minute';</db:sql>  <db:cursor/>  </db:select>

  <db:poll doc:name="Fetch New Records">
    <db:cursor-fetch config-ref="dbConfig" />  </db:poll>

  <logger message="New record: #[payload]" level="INFO" />
</flow>

Explanation:

  • The db:select element opens a cursor with a SQL statement targeting rows inserted within a specific timeframe (adjustable using current_timestamp).

  • The db:poll element retrieves data from the opened cursor in batches.

  • The retrieved data (payload) contains the newly inserted records.

Advantages:

  • Efficient for handling large datasets.

  • Reduces database load compared to fetching the entire table.

Method 2: Periodic Checks with Filtering (Alternative):

  1. Scheduled Flow: Set up a flow to run periodically (e.g., every minute).

  2. Database Query: Execute a query that retrieves data based on a specific criteria, like a timestamp column indicating insertion time.

Here's a Mule flow example:


XML


<flow name="get-new-records">
  <scheduler frequency="1min" doc:name="Schedule Check">
    </scheduler>

  <db:select config-ref="dbConfig" doc:name="Select New Records">
    <db:sql>SELECT * FROM your_table WHERE inserted_at >= current_timestamp - interval '1 minute';</db:sql>
  </db:select>

  <logger message="New records: #[payload]" level="INFO" />
</flow>

Explanation:

  • The flow runs periodically based on the defined schedule.

  • The db:select element retrieves data inserted within a specific timeframe.

  • The retrieved data (payload) contains the newly inserted records.

Disadvantages:

  • Less efficient for high data insertion rates as the entire table might be scanned periodically.

  • Can put additional load on the database compared to using a cursor.

Choosing the Right Approach:

  • Database Cursors: Generally preferred for their efficiency in handling large datasets and minimizing database load.

  • Periodic Checks: Consider this approach for scenarios with a lower data insertion rate or when cursor functionality might not be readily available in your database system.

Additional Considerations:

  • Error Handling: Implement proper error handling mechanisms to gracefully handle situations like database connection issues or unexpected data retrieval results.

  • Security: Ensure the database user has the necessary privileges to execute the required SQL statements.

Remember that the optimal approach depends on your specific requirements, data volume, and database capabilities. Evaluate both methods and choose the one that best suits your scenario.


identify query params & uri params in MuleSoft 142

 identify query params & uri params in MuleSoft


In MuleSoft 4, you can identify query parameters and URI parameters using the following methods:

1. Using DataWeave:

DataWeave provides a convenient and expressive approach to access and manipulate request parameters:

  • URI Parameters:
    These are captured using the attributes.uriParams object within your DataWeave expression.



%dw 2.0
---
// Assuming your URI is like: /api/products/{productId}?name=Alice
output = {
  productId: attributes.uriParams.productId,  // Access URI parameter
  // ... other logic using payload or headers
};

  • Query Parameters:
    These are accessed using the attributes.queryParams object.



%dw 2.0
---
// Assuming your URI is like: /api/products?name=Alice&category=electronics
output = {
  name: attributes.queryParams.name,  // Access query parameter
  category: attributes.queryParams.category,
  // ... other logic using payload or headers
};

Explanation:

  • attributes: This object provides access to various message attributes, including request parameters.

  • uriParams: This object specifically holds the URI parameters extracted from the request path.

  • queryParams: This object holds the query parameters extracted from the URL's query string.

2. Using MEL (Message Expression Language):

While less preferred due to verbosity, MEL can also be used to access parameters:

  • URI Parameters:


XML


%dw 2.0
---
output = {
  productId: mel:message.inboundProperties['http.request.uri.params']['productId']
};

  • Query Parameters:


XML


%dw 2.0
---
output = {
  name: mel:message.inboundProperties['http.request.query.params']['name']
};

Explanation:

  • mel:message: This expression refers to the current message object.

  • inboundProperties: This object contains various message properties, including request parameters.

  • http.request.uri.params: This property holds the URI parameters.

  • http.request.query.params: This property holds the query parameters.

Choosing the Right Approach:

  • DataWeave: This is the recommended method due to its:

  • Readability

  • Conciseness

  • Ability to combine accessing parameters with other data manipulation steps within a single expression.

  • MEL: While functional, MEL expressions can become complex for intricate logic or combining data access with transformations.

Additional Considerations:

  • Ensure you understand the structure of your request URL to correctly identify the presence and location of URI and query parameters.

  • Error handling can be incorporated within DataWeave or MEL to gracefully handle cases where parameters might be missing or invalid.

Here are some helpful resources for further reference: