Skip to content
Related Articles

Related Articles

How to Run Synchronous Queries using sync-sql Module in Node.js ?
  • Difficulty Level : Expert
  • Last Updated : 28 Apr, 2020

The sync-sql module is designed to make synchronous queries to the database. Since normal SQL queries are asynchronous in node.js but if you want to run it synchronously, then it is possible using this module. In synchronized SQL, the result set is returned when the query is executed.

Note: Do not use this module in production mode as node.js is designed to be asynchronous.

Installation of sync-sql module:

  1. You can visit the link Install sync-sql module. You can install this package by using this command.
    npm install sync-sql
  2. After installing sync-sql you can check your sync-sql version in command prompt using the command.
    npm version sync-sql
  3. After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
    node index.js
  4. Now open MySQL and create a database for example ‘demo’.

Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

const Mysql = require('sync-mysql')
  
const connection = new Mysql({
    host:'localhost',
    user:'root',
    password:'password',
    database:'demo'
})
  
var result = connection.query('SELECT NOW()')
console.log(result)

chevron_right


Steps to run the program:

  1. The project structure will look like this:
    project structure
  2. Make sure you install sync-sql using following commands:
    npm install sync-sql
  3. Run index.js file using below command:
    node index.js

    Output of above command

So this is how you can run synchronized SQL queries in node js using sync-sql package.

My Personal Notes arrow_drop_up
Recommended Articles
Page :