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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay