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.22 to 0.0.23

test/unit-helper/mysqlServiceFactory/config-3/test.json

9

index.js
'use strict';
module.exports = require('./lib/mysqlService');
/**
*
* @type {{serviceFactory: MysqlServiceFactory, daoArgsFactory: MysqlDaoArgsFactory}}
*/
module.exports = {
serviceFactory: require('./lib/mysqlServiceFactory'),
daoArgsFactory: require('./lib/mysqlDaoArgsFactory')
};

@@ -22,2 +22,5 @@ 'use strict';

/**
* @returns {string|string[]}
*/
get host() { return this.get('host'); }

@@ -24,0 +27,0 @@ get port() { return this.get('port'); }

2

lib/mysqlPoolConnectionConfigSchema.js

@@ -6,3 +6,3 @@ 'use strict';

module.exports = {
host: Joi.string().hostname().required(),
host: Joi.alternatives().try(Joi.string().hostname().required(), Joi.array().items(Joi.string().hostname()).required()),
port: Joi.number().integer().optional(),

@@ -9,0 +9,0 @@ database: Joi.string().required(),

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

const Promise = require('bluebird');
const Chance = require('chance');
const _ = require('lodash');
const MysqlQuery = require('./mysqlQuery');

@@ -19,9 +21,42 @@ const MysqlInsertResultFactory = require('./mysqlModificationResultFactory');

// Read from the write DB if a separate connection isn't provided for reading
this._readPool = (readConnectionConfig) ? Promise.promisifyAll(Mysql.createPool(readConnectionConfig)) : this._writePool;
if (readConnectionConfig) {
const readConnectionConfigs = this._expandMysqlPoolConnectionConfig(readConnectionConfig);
this._readPools = readConnectionConfigs.map(readConfig => Promise.promisifyAll(Mysql.createPool(readConfig)));
} else {
// Read from the write DB if a separate connection isn't provided for reading
this._readPools = [this._writePool];
}
this._logger = logger; // @TODO: throw an error if logger is not of correct type
this._chance = new Chance();
}
/**
* Converts input to an array, creating one output for every host input (facilitates multiple read slaves)
* @param {MysqlPoolConnectionConfig} mysqlPoolConnectionConfig
* @returns {MysqlPoolConnectionConfig[]}
* @private
*/
_expandMysqlPoolConnectionConfig(mysqlPoolConnectionConfig) {
const expandedConnectionConfigs = [];
if (Array.isArray(mysqlPoolConnectionConfig.host)) {
const baseConfig = mysqlPoolConnectionConfig.toJSON();
mysqlPoolConnectionConfig.host.forEach(host => {
const config = _.clone(baseConfig);
config.host = host;
expandedConnectionConfigs.push(config);
})
} else {
expandedConnectionConfigs.push(mysqlPoolConnectionConfig);
}
return expandedConnectionConfigs;
}
_getReadPool() {
const slaveIndex = this._chance.integer({min:0, max:this._readPools.length-1});
return this._readPools[slaveIndex];
}
/**
* @param {string} selectQuery

@@ -32,3 +67,3 @@ * @returns {Promise.<object|null,Error>}

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

@@ -51,3 +86,3 @@

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

@@ -54,0 +89,0 @@

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

@@ -26,5 +26,7 @@ "main": "index.js",

"bluebird": "^3.3.5",
"chance": "1.0.3",
"config": "^1.20.1",
"hapiest-vo": "^0.0.4",
"joi": "^8.1.0",
"lodash": "4.13.1",
"mysql": "^2.10.2",

@@ -39,4 +41,5 @@ "squel": "^5.2.1"

"path": "^0.12.7",
"should": "^8.3.1"
"should": "^8.3.1",
"sinon": "1.17.4"
}
}
'use strict';
const Should = require('should');
const MysqlService = require('../../index');
const Index = require('../../index');
const MysqlServicePoolConnectionConfigFactory = require('../../lib/mysqlPoolConnectionConfigFactory');
describe('index.js', function() {
it('Should be a function b/c its a class constructor', function() {
MysqlService.should.be.a.Function();
it('Should expose MysqlServiceFactory and MysqlDaoArgsFactory', function() {
Index.should.have.properties('serviceFactory','daoArgsFactory');
const writeConfig = MysqlServicePoolConnectionConfigFactory.createConnectionConfig({
host: 'localhost',
database: 'hapiestmysql',
user: 'hapiestmysql',
password: 'hapiestmysql',
connectionLimit: 1
});
const mysqlService = new MysqlService(writeConfig);
Index.serviceFactory.should.have.properties('createFromNodeConfig', 'createFromObjWithOnePool', 'createFromObj', 'create');
Index.serviceFactory.createFromNodeConfig.should.be.a.Function();
Index.serviceFactory.createFromObjWithOnePool.should.be.a.Function();
Index.serviceFactory.createFromObj.should.be.a.Function();
Index.serviceFactory.create.should.be.a.Function();
mysqlService.should.be.an.instanceOf(MysqlService);
mysqlService.selectOne.should.be.a.Function();
mysqlService.selectOneFromMaster.should.be.a.Function();
mysqlService.selectAll.should.be.a.Function();
mysqlService.selectAllFromMaster.should.be.a.Function();
mysqlService.insert.should.be.a.Function();
mysqlService.update.should.be.a.Function();
mysqlService.delete.should.be.a.Function();
mysqlService.clean.should.be.a.Function();
Index.daoArgsFactory.should.have.properties('createFromJsObj');
Index.daoArgsFactory.createFromJsObj.should.be.a.Function();
});
});

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

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

@@ -31,3 +32,10 @@

};
const mysqlService = MysqlServiceFactory.createFromObjWithOnePool(writeConnectionConfig, logger);
const readConnectionConfig = {
host: ['localhost','localhost'],
database: 'hapiestmysql',
user: 'hapiestmysql',
password: 'hapiestmysql',
connectionLimit: 1
};
const mysqlService = MysqlServiceFactory.createFromObj(writeConnectionConfig, readConnectionConfig, logger);

@@ -107,2 +115,24 @@ function databaseSetup(done) {

});
it('Should use all available read pools randomly', function() {
// Creating this locally so stubbing functions don't leak to other tests
const localMysqlService = MysqlServiceFactory.createFromObj(writeConnectionConfig, readConnectionConfig, logger);
const querySpy0 = Sinon.spy(localMysqlService._readPools[0], 'query');
const querySpy1 = Sinon.spy(localMysqlService._readPools[1], 'query');
const queriesToRun = [];
for (let i=0; i<10; i++) {
queriesToRun.push('SELECT id, colInt, colVarchar, date_created_timestamp, date_created_datetime, date_created_date FROM __testing ORDER BY id ASC');
}
return Promise.map(queriesToRun, query => localMysqlService.selectOne(query))
.then(results => {
Should.exist(results);
results.should.be.an.Array();
results.length.should.eql(10);
querySpy0.callCount.should.be.greaterThan(0);
querySpy1.callCount.should.be.greaterThan(0);
});
})
});

@@ -109,0 +139,0 @@

@@ -22,3 +22,3 @@ 'use strict';

mysqlService.should.have.property('_writePool');
mysqlService.should.have.property('_readPool');
mysqlService.should.have.property('_readPools');

@@ -58,5 +58,5 @@ Async.auto({

mysqlService.should.have.property('_writePool');
mysqlService.should.have.property('_readPool');
mysqlService.should.have.property('_readPools');
mysqlService._writePool.should.eql(mysqlService._readPool);
mysqlService._writePool.should.eql(mysqlService._readPools[0]);

@@ -109,2 +109,59 @@ Async.auto({

it('Should load from config-3/test.json', function(done) {
const nodeConfig = Internals.resetNodeConfig('config-3');
const mysqlService = MysqlServiceFactory.createFromNodeConfig(nodeConfig);
Should.exist(mysqlService);
mysqlService.should.be.an.instanceOf(MysqlService);
mysqlService.should.have.property('_writePool');
mysqlService.should.have.property('_readPools');
mysqlService._readPools.length.should.eql(2);
Async.auto({
databaseSetup: Async.apply(Internals.databaseSetup, mysqlService),
checkMysqlService: ['databaseSetup', (results, next) => {
mysqlService.selectAll('SELECT * FROM __testing')
.then(results => {
Should.exist(results);
results.should.be.an.Array();
results.length.should.eql(3);
results.should.deepEqual([
{id: 1, colInt: 1, colVarchar: 'one'},
{id: 2, colInt: 2, colVarchar: 'two'},
{id: 3, colInt: 3, colVarchar: 'three'}
]);
})
.then(next)
.catch(err => next(err));
}],
confirmMultipleStatements: ['databaseSetup', (results, next) => {
mysqlService.executeGenericQuery('SELECT 1 as answer; SELECT 1 as answer;')
.then((results) => {
Should.exist(results);
results.should.be.an.Array();
results.length.should.eql(2);
results[0].should.be.an.Array();
Should.exist(results[0][0]);
Should.exist(results[0][0].answer);
results[0][0].answer.should.eql(1);
results[1].should.be.an.Array();
Should.exist(results[1][0]);
Should.exist(results[1][0].answer);
results[0][0].answer.should.eql(1);
})
.then(next)
.catch(err => next(err));
}]
}, (err, results) => {
Internals.databaseTeardown(mysqlService, (errTeardown) => {
done(err || errTeardown);
});
});
});
});

@@ -111,0 +168,0 @@

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