DEV Community

BhargavMantha
BhargavMantha

Posted on • Edited 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
constructor(
@InjectRepository(Users)
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)

Collapse
 
notaduck profile image
notaduck

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.

Collapse
 
bhargavmantha profile image
BhargavMantha

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