How to call a SQL stored procedure in Adonis JS

53 views Asked by At

I'm new to AdonisJS, so please bear with me if this question looks like a dumb question to you. I wanted to know how to call a stored procedure in AdonisJS. Any link or example would literally help me. Thank you.

1

There are 1 answers

0
Ugochukwu Obinna On BEST ANSWER

First, Define Your Stored Procedure: To begin with, your database has to have a stored procedure built. You can construct this method with a database migration in AdonisJS, or with your favorite database administration tool.

Using AdonisJS Query Builder: After your stored procedure has been defined, you can use the Database Query Builder in AdonisJS to invoke it.

here is an example below,

// Import the Database module from AdonisJS
const Database = use('Database');

// Define a method to call the stored procedure
async function callStoredProcedure() {
  try {
    // Use the `raw` method to call the stored procedure
    const result = await Database.raw('CALL your_stored_procedure(?, ?)', [param1, param2]);
    // Process the result
    console.log(result);
  } catch (error) {
    // Handle any errors
    console.error(error);
  }
}

// Call the method to execute the stored procedure
callStoredProcedure();