hapiest-mysql
Advanced tools
Comparing version
@@ -28,2 +28,3 @@ 'use strict'; | ||
get connectionLimit() { return this.get('connectionLimit'); } | ||
get multipleStatements() { return this.get('multipleStatements'); } | ||
@@ -30,0 +31,0 @@ } |
@@ -40,2 +40,3 @@ 'use strict'; | ||
* @param {int} config.connectionLimit | ||
* @param {bool} [config.multipleStatements] | ||
* | ||
@@ -68,2 +69,3 @@ * @returns {MysqlPoolConnectionConfig} | ||
Internals.mergeIfDefined(nodeConfig, config, writeOrRead, 'port'); | ||
Internals.mergeIfDefined(nodeConfig, config, writeOrRead, 'multipleStatements'); | ||
@@ -70,0 +72,0 @@ return config; |
@@ -11,3 +11,4 @@ 'use strict'; | ||
password: Joi.string().required(), | ||
connectionLimit: Joi.number().integer().required() | ||
connectionLimit: Joi.number().integer().required(), | ||
multipleStatements: Joi.bool().optional() | ||
}; |
@@ -94,3 +94,3 @@ 'use strict'; | ||
.then(() => this._writePool.queryAsync(insertQuery)) | ||
.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result))) | ||
.then((result) => MysqlInsertResultFactory.createFromResult(result)) | ||
.catch(err => { | ||
@@ -111,3 +111,3 @@ this._logSqlError(err, insertQuery); | ||
.then(() => this._writePool.queryAsync(updateQuery)) | ||
.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result))) | ||
.then((result) => MysqlInsertResultFactory.createFromResult(result)) | ||
.catch(err => { | ||
@@ -120,2 +120,6 @@ this._logSqlError(err, updateQuery); | ||
/** | ||
* @param {string} deleteQuery | ||
* @returns {Promise.<MysqlModificationResult,Error>} | ||
*/ | ||
delete(deleteQuery) { | ||
@@ -125,3 +129,3 @@ return Promise.resolve() | ||
.then(() => this._writePool.queryAsync(deleteQuery)) | ||
.then((result) => Promise.resolve(MysqlInsertResultFactory.createFromResult(result))) | ||
.then((result) => MysqlInsertResultFactory.createFromResult(result)) | ||
.catch(err => { | ||
@@ -134,5 +138,9 @@ this._logSqlError(err, deleteQuery); | ||
/** | ||
* @param {string} query | ||
* @returns {Promise.<*>} | ||
*/ | ||
executeGenericQuery(query) { | ||
return Promise.resolve() | ||
.then(() => this._writePool.queryAsync(query)) // We have to execute generic queries against "master"); | ||
.then(() => {return this._writePool.queryAsync(query)}) // We have to execute generic queries against "master"); | ||
.catch(err => { | ||
@@ -145,2 +153,11 @@ this._logSqlError(err, query); | ||
/** | ||
* Executes all queries using executeGenericQuery | ||
* @param {String[]} queries | ||
* @returns {Promise} | ||
*/ | ||
executeQueries(queries) { | ||
return Promise.mapSeries(queries, (query) => this.executeGenericQuery(query)); | ||
} | ||
clean(input) { | ||
@@ -147,0 +164,0 @@ return Mysql.escape(input); |
@@ -6,2 +6,4 @@ 'use strict'; | ||
// @TODO: add a nodeConfig "root" concept so createFromNodeConfig can apply for multiple database connections within a config file | ||
class MysqlServiceFactory { | ||
@@ -8,0 +10,0 @@ |
{ | ||
"name": "hapiest-mysql", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A wrapper around mysql that provides a very descriptive way of running queries.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,5 +9,6 @@ { | ||
"password": "badpassword", | ||
"connectionLimit": 100 | ||
"connectionLimit": 100, | ||
"multipleStatements": true | ||
} | ||
} | ||
} |
@@ -40,2 +40,4 @@ 'use strict'; | ||
writeConfig.connectionLimit.should.eql(100); | ||
Should.exist(writeConfig.multipleStatements); | ||
writeConfig.multipleStatements.should.be.true; | ||
}); | ||
@@ -42,0 +44,0 @@ |
@@ -37,3 +37,3 @@ 'use strict'; | ||
` | ||
CREATE TABLE __testing ( | ||
CREATE TABLE IF NOT EXISTS __testing ( | ||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, | ||
@@ -54,9 +54,12 @@ colInt INT NOT NULL, | ||
let currentContext = Promise.resolve(); | ||
queries.forEach(query => { | ||
currentContext = currentContext.then(() => mysqlService.executeGenericQuery(query)); | ||
}); | ||
currentContext.catch(err => done(err)).then(() => done()); | ||
mysqlService.executeQueries(queries) | ||
.then(() => done(), (err) => done(err)); | ||
} | ||
function databaseTeardown(done) { | ||
const queries = ['DROP TABLE IF EXISTS __testing']; | ||
mysqlService.executeQueries(queries) | ||
.then(() => done(), (err) => done(err)); | ||
} | ||
/********************************************************** | ||
@@ -70,2 +73,3 @@ * TESTS | ||
before(databaseSetup); | ||
after(databaseTeardown); | ||
@@ -197,2 +201,3 @@ describe('selectOne', function() { | ||
beforeEach(databaseSetup); | ||
after(databaseTeardown); | ||
@@ -241,2 +246,3 @@ it('Should insert a value into the database and return data about the insertion', function() { | ||
beforeEach(databaseSetup); | ||
after(databaseTeardown); | ||
@@ -271,2 +277,3 @@ it('Should update a value in the database and return data about the update', function() { | ||
beforeEach(databaseSetup); | ||
after(databaseTeardown); | ||
@@ -298,2 +305,4 @@ it('Should delete a row in the database and return data about the delete', function() { | ||
// @TODO: add tests for the generic query functions | ||
describe('clean', function() { | ||
@@ -350,5 +359,3 @@ it('Should escape little Bobby DROP TABLEs', function() { | ||
// @TODO: add testing to ensure logging works on SQL error | ||
}); |
@@ -8,2 +8,3 @@ 'use strict'; | ||
const Async = require('async'); | ||
const NodeConfig = require('config-uncached'); | ||
@@ -39,7 +40,67 @@ describe('MysqlServiceFactory', function() { | ||
}) | ||
.then(() => next()) | ||
.catch(err => next(err)); | ||
}] | ||
}, (err, results) => { | ||
Internals.databaseTeardown(mysqlService, (errTeardown) => { | ||
done(err || errTeardown); | ||
}); | ||
}); | ||
}); | ||
it('Should load from config-2/test.json', function(done) { | ||
const nodeConfig = Internals.resetNodeConfig('config-2'); | ||
const mysqlService = MysqlServiceFactory.createFromNodeConfig(nodeConfig); | ||
Should.exist(mysqlService); | ||
mysqlService.should.be.an.instanceOf(MysqlService); | ||
mysqlService.should.have.property('_writePool'); | ||
mysqlService.should.have.property('_readPool'); | ||
mysqlService._writePool.should.eql(mysqlService._readPool); | ||
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) => { | ||
done(err); | ||
Internals.databaseTeardown(mysqlService, (errTeardown) => { | ||
done(err || errTeardown); | ||
}); | ||
}); | ||
@@ -59,5 +120,10 @@ }); | ||
process.env.NODE_CONFIG_DIR = Path.resolve(__dirname, '../unit-helper/mysqlServiceFactory', configDirName); | ||
return require('config-uncached')(true); | ||
const nodeConfig = NodeConfig(true); | ||
return nodeConfig; | ||
} | ||
/** | ||
* @param {MysqlService} mysqlService | ||
* @param done | ||
*/ | ||
static databaseSetup(mysqlService, done) { | ||
@@ -83,9 +149,18 @@ const queries = [ | ||
let currentContext = Promise.resolve(); | ||
queries.forEach(query => { | ||
currentContext = currentContext.then(() => mysqlService.executeGenericQuery(query)); | ||
}); | ||
currentContext.catch(err => done(err)).then(() => done()); | ||
mysqlService.executeQueries(queries) | ||
.then(() => done(), (err) => done(err)); | ||
} | ||
/** | ||
* @param {MysqlService} mysqlService | ||
* @param done | ||
*/ | ||
static databaseTeardown(mysqlService, done) { | ||
const queries = ['DROP TABLE IF EXISTS __testing']; | ||
mysqlService.executeQueries(queries) | ||
.then(() => done(), (err) => done(err)); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
74555
39.17%27
28.57%1691
39.98%