Getting a connection

First, let's have some constants that we will be using later; apart from the Flow and strict usage lines, we just require the MariaDB library, the promisify() function, and we define four constants to access the database:

// Source file: src/dbaccess.js

/* @flow */
"use strict";

const mariaSQL = require("mariasql");
const { promisify } = require("util");

const DB_HOST = "127.0.0.1";
const DB_USER = "fkereki";
const DB_PASS = "modernJS!!";
const DB_SCHEMA = "world";

// continues...

Now, let's get a database connection. We just create a new object, and promisify its .query() method. The dbConn variable will be passed as a parameter to every function that will need to access the database:

// ...continued

function getDbConnection(host, user, password, db) {
const dbConn = new mariaSQL({ host, user, password, db });
dbConn.query = promisify(dbConn.query);
return dbConn;
}

const dbConn = getDbConnection(DB_HOST, DB_USER, DB_PASS, DB_SCHEMA);

// continues...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.93