DEV Community

Jahongir Sobirov
Jahongir Sobirov

Posted on

1

An easy way to use a MySQL database in Node.js (Node.js MySQL vs Node.js MySQL + NanoQL)

There are many libraries and frameworks for the Node.js runtime, and among them MySQL has developed its own library for node.js. You can query and manage MySQL database through this library. However, you have to write a lot of code for each request😨:

// Example of creating database in Node.js MySQL
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword"
});

con.connect(function(err) {
  if(err){
     console.log(err.message);
  };
  console.log("Connected!");
  con.query("CREATE DATABASE mydb", function (err, result) {
    if(err){
     console.log(err.message);
    };
    console.log("Database created");
  });
});

Enter fullscreen mode Exit fullscreen mode

And it can be confusing to programmers who are just starting to program. Or, if you are working on a big project there is also a possibility that you will end up with a lot of code like this.
But recently a NanoQL library has been developed. The main and purpose of this library is to make it easy to send and manage MySQL queries and execute programs through shortcodes. Now let's create a database in MySQL with NanoQL:
(Quick start and installation NanoQL:)

npm install nanoql
Enter fullscreen mode Exit fullscreen mode
// Example of creating database
var Nanoql = require("nanoql");

var con = new Nanoql(["localhost", "yourusername", "yourpassword"]);
console.log("Connected!");

con.sql(`CREATE DATABASE mydb`, (err, res)=>{
   con.isErr(err);
   console.log("Database created");
});
Enter fullscreen mode Exit fullscreen mode

Now let's look at other examples:

// Creating table in Node.js MySQL
var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "yourusername",
  password: "yourpassword",
  database: "mydb"
});

con.connect(function(err) {
  if(err){
     console.log(err.message);
  };
  console.log("Connected!");
  var sql = "CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if(err){
     console.log(err.message);
    };
    console.log("Table created");
  });
});
Enter fullscreen mode Exit fullscreen mode
// Creating table in Node.js MySQL + NanoQL
var Nanoql = require("nanoql");

var con = new Nanoql(["localhost", "yourusername", "yourpassword", "mydb"]);
console.log("Connected!");

con.sql(`CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))`, (err, res)=>{
   con.isErr(err);
   console.log("Table created");
});
Enter fullscreen mode Exit fullscreen mode

If you want to save time using MySQL and want to program in easy ways, I recommend NanoQL.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more