DEV Community


Posted on • Updated on

Running the stored procedure from NestJs

using the repository that was created,
for instance you want to interact with the table users
and say you have the repository UserRepository. 
Enter fullscreen mode Exit fullscreen mode
  1. dependency inject the user repository into the current service
private  userRepository:  Repository<UserRepository>
)  {}
Enter fullscreen mode Exit fullscreen mode
  1. now use the created userReposotory to run the command to invoke the stored procedure
this.userRepository.query("call dev_db.listUsers(?)",[the_input_paramenters_required]);
Enter fullscreen mode Exit fullscreen mode

--the above query returns a promise with result of stored Procedure or error
So make sure you add await to function having the above line

Top comments (2)

notaduck profile image

BhargavMantha I am almos sure that this.userRepository.query(call dev_db.listUsers(${the_input_paramenters_required}); is prone to sql injection, you should use a param list instead.

bhargavmantha profile image

Thank you so much for pointing that out notaduck. My apologies for missing your comment. :)