npm.io
1.0.4 • Published 6 years ago

eden-util

Licence
UNLICENSED
Version
1.0.4
Deps
0
Size
4 kB
Vulns
0
Weekly
0

Eden-Util

Description

This module has some of my own utility funcs. It was created because of my own needs, such as, formatting DATE obj for yyyy/mm/dd HHSS string, reading a JSON FILE and retorning the conversation from it to JS OBJ and testing MSSQLConn that retorns true Success or String, the error message.

List of functions:

  • readJSON(fs, staticPath, _path)
  • getFormattedDate(dateObj)
  • testMSSQLConn(sql, stringCon)

The method readJSON and testMSSQLConn run synchronously (blocking)!


module.exports = {
    readJSON: (fs, staticPath, _path) => {
        let path;
        if(typeof _path == 'undefined')path = staticPath
        else path = _path;
        return JSON.parse(fs.readFileSync(path, 'utf-8'));
    }
    ,getFormattedDate: (dateObj) => {
        let dd   = dateObj.getUTCDate(),
        mm   = dateObj.getUTCMonth()+1,
        yyyy = dateObj.getUTCFullYear(),
        hour = dateObj.getUTCHours(),
        minu = dateObj.getUTCMinutes(),
        seg = dateObj.getUTCSeconds();

        if(dd<10)  { dd='0'+dd } 
        if(hour<10)  { hour='0'+hour } 
        if(mm<10)  { mm='0'+mm } 
        if(minu<10){ minu='0'+minu } 
        if(seg<10){ seg='0'+seg } 
        return yyyy+'-'+mm+'-'+dd+' '+hour+':'+minu+':'+seg;
    }
    ,testMSSQLConn: async (sql, stringConn) => {
        let msgReturn,
        testConn = (sql, stringConnection) => {
            return new Promise((resolve) =>{    
                sql.query(stringConnection, 'SELECT 1', (err, rows) => {
                    if(err) msgReturn = err;
                    else msgReturn = true;
                    resolve();
                });
            });
        }

        await testConn(sql, stringConn);    
        return msgReturn;
    }
}	

Using utility functions example:


const fs = require('fs')
,sql = require("msnodesqlv8")
,utilFunc =  require('eden-utility');
async function main(){ let cfgObj = utilFunc.readJSON(fs, './configFile.json'); let testConn = await utilFunc.testMSSQLConn(sql, cfgObj.dbConn); if(testConn !== true) throw Error(testConn); let currentDate = utilFunc.getFormattedDate(new Date()); console.log(currentDate); }

main();

In this example above, you'll need a configFile.json file in your project folder. It must have dbConn property with available connection string