Node.js MySQL vs Node.js MySQL + Sculter.js (which one is better?)
Tags: javascript, node, mysql, sculterjs
Published: 2022-05-23 11:59:49

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](http://sculterjs.freetzi.com/)?

![Sculter.JS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgd3826fe1evf62p6ka0.png)
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
```
### 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:
```js
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");
  });
});
```
Now in the Node.js MySQL Sculter.js team we will create a table named `mydb`:
```js
const sculter = require("sculter.js");

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

console.log("Database created");
```
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:
```js
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");
  });
});
```
We will create table with Node.JS MySQL Sculter.js trio:
```js
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");
``` 
In the table `customers` we now enter the data in pairs MySQL Node.js:
```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 data with Node.js MySQL Sculter.js:
```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");
```
To verify that the data is stored, we output them to the JS console. We will do this in the first Node.js MySQL:
```js
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);
  });
});
```
We extract data using Sculter.js:
```js
const sculter = require("sculter.js");

sculter.send({
   connect: ["localhost", "yourusername", "yourpassword", "mydb"],
   sql: "SELECT * FROM customers",
   outTo: "console"
});
```
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'}
]
```
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 😉)