DEV Community

Cover image for Node.js MySQL vs Node.js MySQL + Sculter.js (which one is better?)
Jahongir Sobirov
Jahongir Sobirov

Posted on

 

Node.js MySQL vs Node.js MySQL + Sculter.js (which one is better?)

The Node.js MySQL pair is familiar to everyone. But in MySQL you have to write a lot of code to achieve the goal and it is often incomprehensible to beginners. Today I am offering a Sculter.JS framework designed to make it easier to write code in MySQL. Today we are going to compare the Node.jS MySQL pair with the Node.js MySQL + Sculter.JS trio.

What is Sculter.JS?

Sculter.JS
Sculter.JS is a framework for Node.js developed for MySQL, whose main function is to make it easier to write code in MySQL and make the code understandable to applications.

npm i sculter.js
Enter fullscreen mode Exit fullscreen mode

Comparing teams

Let’s now create a database in both to compare these two teams.
Let's create the first database named mydb in MySQL Node.js team:

var mysql = require('mysql');

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

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

Now in the Node.js MySQL Sculter.js team we will create a table named mydb:

const sculter = require("sculter.js");

sculter.send({
   connect: ["localhost", "yourusername", "yourpassword"],
   sql: "CREATE DATABASE mydb"
});

console.log("Database created");
Enter fullscreen mode Exit fullscreen mode

We created a database in both teams. Now we create a table in them. We call our table customers. Let's create a table with the first team:

var mysql = require('mysql');

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

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table created");
  });
});
Enter fullscreen mode Exit fullscreen mode

We will create table with Node.JS MySQL Sculter.js trio:

const sculter = require("sculter.js");

sculter.send({
   connect: ["localhost", "yourusername", "yourpassword", "mydb"],
   sql: "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))"
});

console.log("Table created");
Enter fullscreen mode Exit fullscreen mode

In the table customers we now enter the data in pairs MySQL Node.js:



con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "INSERT INTO customers (name, address) VALUES ?";
  var values = [
    ['John', 'Highway 71'],
    ['Peter', 'Lowstreet 4'],
    ['Amy', 'Apple st 652'],
    ['Hannah', 'Mountain 21'],
    ['Michael', 'Valley 345'],
    ['Sandy', 'Ocean blvd 2'],
    ['Betty', 'Green Grass 1'],
    ['Richard', 'Sky st 331'],
    ['Susan', 'One way 98'],
    ['Vicky', 'Yellow Garden 2'],
    ['Ben', 'Park Lane 38'],
    ['William', 'Central st 954'],
    ['Chuck', 'Main Road 989'],
    ['Viola', 'Sideway 1633']
  ];
  con.query(sql, [values], function (err, result) {
    if (err) throw err;
    console.log("Records inserted");
  });
});
Enter fullscreen mode Exit fullscreen mode

Enter data with Node.js MySQL Sculter.js:

const sculter = require("sculter.js");

sculter.send({
   connect: ["localhost", "yourusername", "yourpassword", "mydb"],
   sql: "INSERT INTO customers (name, address) VALUES ?",
   values: [
    ['John', 'Highway 71'],
    ['Peter', 'Lowstreet 4'],
    ['Amy', 'Apple st 652'],
    ['Hannah', 'Mountain 21'],
    ['Michael', 'Valley 345'],
    ['Sandy', 'Ocean blvd 2'],
    ['Betty', 'Green Grass 1'],
    ['Richard', 'Sky st 331'],
    ['Susan', 'One way 98'],
    ['Vicky', 'Yellow Garden 2'],
    ['Ben', 'Park Lane 38'],
    ['William', 'Central st 954'],
    ['Chuck', 'Main Road 989'],
    ['Viola', 'Sideway 1633']
  ] 
});

console.log("Records inserted");
Enter fullscreen mode Exit fullscreen mode

To verify that the data is stored, we output them to the JS console. We will do this in the first 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) throw err;
  con.query("SELECT * FROM customers", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});
Enter fullscreen mode Exit fullscreen mode

We extract data using Sculter.js:

const sculter = require("sculter.js");

sculter.send({
   connect: ["localhost", "yourusername", "yourpassword", "mydb"],
   sql: "SELECT * FROM customers",
   outTo: "console"
});
Enter fullscreen mode Exit fullscreen mode

Results:

[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id: 2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy', address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address: 'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty', address: 'Green Grass 1'},
  { id: 8, name: 'Richard', address: 'Sky st 331'},
  { id: 9, name: 'Susan', address: 'One way 98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 12, name: 'William', address: 'Central st 954'},
  { id: 13, name: 'Chuck', address: 'Main Road 989'},
  { id: 14, name: 'Viola', address: 'Sideway 1633'}
]
Enter fullscreen mode Exit fullscreen mode

I liked the Node.js MySQL Sculter.js trio. Leave it in the comments which team you liked. (Maybe Sculter.js will help you too πŸ˜‰)

Top comments (2)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Personally I would pick PostgreSQL but the others work fine too.

Collapse
 
jahongir2007 profile image
Jahongir Sobirov

thanks

12 Rarely Used Javascript APIs You Need

Practical examples of some unique Javascript APIs that beautifully demonstrate a practical use-case.