New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hapiest-mysql

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapiest-mysql - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

90

lib/mysqlService.js

@@ -13,4 +13,5 @@ 'use strict';

* @param {MysqlPoolConnectionConfig} [readConnectionConfig] - note, if you leave this NULL then reads will use the write connection
* @param {Logger} [logger] - instance of hapiest-logger
*/
constructor(writeConnectionConfig, readConnectionConfig) {
constructor(writeConnectionConfig, readConnectionConfig, logger) {
this._writePool = Promise.promisifyAll(Mysql.createPool(writeConnectionConfig));

@@ -20,2 +21,4 @@

this._readPool = (readConnectionConfig) ? Promise.promisifyAll(Mysql.createPool(readConnectionConfig)) : this._writePool;
this._logger = logger; // @TODO: throw an error if logger is not of correct type
}

@@ -29,3 +32,3 @@

const selectOneResult = true;
return Internals.selectBase(this._readPool, selectQuery, selectOneResult);
return this._selectBase(this._readPool, selectQuery, selectOneResult);
}

@@ -39,3 +42,3 @@

const selectOneResult = true;
return Internals.selectBase(this._writePool, selectQuery, selectOneResult);
return this._selectBase(this._writePool, selectQuery, selectOneResult);
}

@@ -49,3 +52,3 @@

const selectOneResult = false;
return Internals.selectBase(this._readPool, selectQuery, selectOneResult);
return this._selectBase(this._readPool, selectQuery, selectOneResult);
}

@@ -59,6 +62,32 @@

const selectOneResult = false;
return Internals.selectBase(this._writePool, selectQuery, selectOneResult);
return this._selectBase(this._writePool, selectQuery, selectOneResult);
}
/**
* @param pool
* @param selectQuery
* @param returnOneRow
* @returns {Promise.<object|null>}
*/
_selectBase(pool, selectQuery, returnOneRow) {
return Promise.resolve()
.then(() => MysqlQuery.validateSelect(selectQuery))
.then(() => pool.queryAsync(selectQuery))
.then((rows) => {
if (rows.length === 0) {
const data = returnOneRow ? null : [];
return Promise.resolve(data);
} else {
const data = returnOneRow ? rows[0] : rows;
const jsObj = JSON.parse(JSON.stringify(data)); // using .parse.stringify to convert RowDataPacket --> standard JS objects
return Promise.resolve(jsObj);
}
})
.catch(err => {
this._logSqlError(err, selectQuery);
throw err;
});
}
/**
* @param {string} insertQuery

@@ -72,2 +101,6 @@ * @returns {Promise.<MysqlModificationResult|null,Error>}

.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result)))
.catch(err => {
this._logSqlError(err, insertQuery);
throw err;
})
;

@@ -85,2 +118,6 @@ }

.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result)))
.catch(err => {
this._logSqlError(err, updateQuery);
throw err;
})
;

@@ -94,2 +131,6 @@ }

.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result)))
.catch(err => {
this._logSqlError(err, deleteQuery);
throw err;
})
;

@@ -101,2 +142,6 @@ }

.then(() => this._writePool.queryAsync(query)) // We have to execute generic queries against "master");
.catch(err => {
this._logSqlError(err, query);
throw err;
})
;

@@ -109,28 +154,15 @@ }

}
module.exports = MysqlService;
class Internals {
/**
* @param pool
* @param selectQuery
* @param returnOneRow
* @returns {Promise.<object|null>}
* @param {Error} err
* @param {string} sql
* @private
*/
static selectBase(pool, selectQuery, returnOneRow) {
return Promise.resolve()
.then(() => MysqlQuery.validateSelect(selectQuery))
.then(() => pool.queryAsync(selectQuery))
.then((rows) => {
if (rows.length === 0) {
const data = returnOneRow ? null : [];
return Promise.resolve(data);
} else {
const data = returnOneRow ? rows[0] : rows;
const jsObj = JSON.parse(JSON.stringify(data)); // using .parse.stringify to convert RowDataPacket --> standard JS objects
return Promise.resolve(jsObj);
}
});
_logSqlError(err, sql) {
if (this._logger) {
this._logger.error('Error executing SQL', {sql: sql, err: err});
}
}
}
}
module.exports = MysqlService;

@@ -10,10 +10,11 @@ 'use strict';

* @param {Config} nodeConfig
* @param {Logger} [logger] - instance of hapiest-logger
*
* @returns {MysqlService}
*/
static createFromNodeConfig(nodeConfig) {
static createFromNodeConfig(nodeConfig, logger) {
const writeConnectionConfig = MysqlPoolConnectionConfigFactory.createWriteConnectionConfigFromNodeConfig(nodeConfig);
const readConnectionConfig = MysqlPoolConnectionConfigFactory.createReadConnectionConfigFromNodeConfig(nodeConfig);
return MysqlServiceFactory.create(writeConnectionConfig, readConnectionConfig);
return MysqlServiceFactory.create(writeConnectionConfig, readConnectionConfig, logger);
}

@@ -23,8 +24,10 @@

* @param {object} connectionConfig
* @param {Logger} [logger] - instance of hapiest-logger
*
* @returns {MysqlService}
*/
static createFromObjWithOnePool(connectionConfig) {
static createFromObjWithOnePool(connectionConfig, logger) {
const writeConnectionConfig = MysqlPoolConnectionConfigFactory.createConnectionConfig(connectionConfig);
return new MysqlService(writeConnectionConfig);
const readConnectionConfig = null;
return MysqlServiceFactory.create(writeConnectionConfig, readConnectionConfig, logger);
}

@@ -35,9 +38,10 @@

* @param {object} readConnectionConfigObj
* @param {Logger} [logger] - instance of hapiest-logger
*
* @returns {MysqlService}
*/
static createFromObj(writeConnectionConfigObj, readConnectionConfigObj) {
static createFromObj(writeConnectionConfigObj, readConnectionConfigObj, logger) {
const writeConnectionConfig = MysqlPoolConnectionConfigFactory.createConnectionConfig(writeConnectionConfigObj);
const readConnectionConfig = MysqlPoolConnectionConfigFactory.createConnectionConfig(readConnectionConfigObj);
return MysqlServiceFactory.create(writeConnectionConfig, readConnectionConfig);
return MysqlServiceFactory.create(writeConnectionConfig, readConnectionConfig, logger);
}

@@ -48,7 +52,8 @@

* @param {MysqlPoolConnectionConfig} readConnectionConfig
* @param {Logger} [logger] - instance of hapiest-logger
*
* @returns {MysqlService}
*/
static create(writeConnectionConfig, readConnectionConfig) {
return new MysqlService(writeConnectionConfig, readConnectionConfig);
static create(writeConnectionConfig, readConnectionConfig, logger) {
return new MysqlService(writeConnectionConfig, readConnectionConfig, logger);
}

@@ -55,0 +60,0 @@

{
"name": "hapiest-mysql",
"version": "0.0.2",
"version": "0.0.3",
"description": "A wrapper around mysql that provides a very descriptive way of running queries.",

@@ -34,2 +34,4 @@ "main": "index.js",

"config-uncached": "^1.0.2",
"hapiest-logger": "0.0.3",
"intercept-stdout": "^0.1.2",
"path": "^0.12.7",

@@ -36,0 +38,0 @@ "should": "^8.3.1"

@@ -7,2 +7,3 @@ 'use strict';

const Promise = require('bluebird');
const interceptStdout = require('intercept-stdout');

@@ -13,2 +14,6 @@ const MysqlServiceFactory = require('../../lib/mysqlServiceFactory');

const MysqlModificationResult = require('../../lib/mysqlModificationResult');
const LoggerFactory = require('hapiest-logger/lib/loggerFactory');
const LoggerConfigFactory = require('hapiest-logger/lib/loggerConfigFactory');
const loggerConfig = LoggerConfigFactory.createFromJsObj({enabled: true, consoleTransport: {enabled:true, level: 'info'}});
const logger = LoggerFactory.createLogger(loggerConfig);

@@ -27,3 +32,3 @@ /**********************************************************

};
const mysqlService = MysqlServiceFactory.createFromObjWithOnePool(writeConnectionConfig);
const mysqlService = MysqlServiceFactory.createFromObjWithOnePool(writeConnectionConfig, logger);

@@ -305,3 +310,41 @@ function databaseSetup(done) {

describe('_logError', function() {
it('Should call .error function on the logger', function() {
let loggedTxt = '';
let unhookIntercept = null;
let error = null;
return Promise.resolve()
.then(() => {
unhookIntercept = interceptStdout((txt) => {loggedTxt += txt;}); // Need this to intercept the logger's output to stdout
})
.then(() => mysqlService.selectOne('SELECT ERROR SYNTAX...'))
.catch((err) => {
error = err;
})
.then(() => {
Should.exist(error);
const loggedObj = JSON.parse(loggedTxt);
loggedObj.should.have.property('message');
loggedObj.should.have.property('level');
loggedObj.should.have.property('data');
loggedObj.message.should.eql('Error executing SQL');
loggedObj.level.should.eql('error');
loggedObj.data.should.have.property('err');
loggedObj.data.should.have.property('errorStack');
loggedObj.data.should.have.property('errorMessage');
loggedObj.data.should.have.property('sql');
loggedObj.data.errorStack.should.be.a.String();
loggedObj.data.errorMessage.should.be.a.String();
loggedObj.data.sql.should.eql('SELECT ERROR SYNTAX...');
})
});
});
// @TODO: add testing to ensure logging works on SQL error
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc