hapiest-mysql
Advanced tools
Comparing version 0.0.25 to 0.0.26
@@ -73,6 +73,23 @@ 'use strict'; | ||
getOneById(id) { | ||
return this.getOne({id: id}); | ||
return this._getOneById(id); | ||
} | ||
/** | ||
* @param id | ||
* @returns {Promise.<object,Error>} | ||
*/ | ||
getOneByIdFromMaster(id) { | ||
return this._getOneById(id, {forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param id | ||
* @param {MysqlGetFromSqlOptions} options | ||
* @private | ||
*/ | ||
_getOneById(id, options) { | ||
return this._getOne({id:id}, options); | ||
} | ||
/** | ||
* @param {object} whereClause | ||
@@ -82,4 +99,22 @@ * @returns {Promise.<object|null,Error>} | ||
getOne(whereClause) { | ||
return this._getOne(whereClause); | ||
} | ||
/** | ||
* @param {object} whereClause | ||
* @returns {Promise.<object|null,Error>} | ||
*/ | ||
getOneFromMaster(whereClause) { | ||
return this._getOne(whereClause, {forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param {object} whereClause | ||
* @param {MysqlGetFromSqlOptions} [options] | ||
* @returns {Promise.<Object|null, Error>} | ||
* @private | ||
*/ | ||
_getOne(whereClause, options) { | ||
const sql = this._queryHelper.getOne(whereClause); | ||
return this.getOneFromSql(sql); | ||
return this._getOneFromSql(sql, options); | ||
} | ||
@@ -91,4 +126,12 @@ | ||
*/ | ||
getOneFromSql(sql) { | ||
return this._getOneFromSql(sql); | ||
} | ||
/** | ||
* @param {string} sql | ||
* @returns {Promise.<Object|null, Error>} | ||
*/ | ||
getOneFromSqlRaw(sql) { | ||
return this.getOneFromSql(sql, {rawResults: true}); | ||
return this._getOneFromSql(sql, {rawResults: true}); | ||
} | ||
@@ -98,13 +141,43 @@ | ||
* @param {string} sql | ||
* @param {object} [options] | ||
* @param {boolean} [options.rawResults=false] | ||
* @returns {Promise.<Object|null, Error>} | ||
*/ | ||
getOneFromSqlFromMaster(sql) { | ||
return this._getOneFromSql(sql, {forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param {string} sql | ||
* @returns {Promise.<Object|null, Error>} | ||
*/ | ||
getOneFromSqlFromMasterRaw(sql) { | ||
return this._getOneFromSql(sql, {rawResults: true, forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @name MysqlGetFromSqlOptions | ||
* @type {Object} | ||
* @property {boolean} [rawResults=false] | ||
* @property {boolean} [forceReadFromMaster=false] | ||
*/ | ||
/** | ||
* @param {string} sql | ||
* @param {MysqlGetFromSqlOptions} [options] | ||
* @returns {Promise.<object|null,Error>} | ||
*/ | ||
getOneFromSql(sql, options) { | ||
_getOneFromSql(sql, options) { | ||
const defaultOptions = { | ||
rawResults: false | ||
rawResults: false, | ||
forceReadFromMaster: false | ||
}; | ||
const optionsToUse = _.merge(defaultOptions, options); | ||
return this._mysqlService.selectOne(sql) | ||
/** @type {MysqlGetFromSqlOptions} */ | ||
const optionsToUse = _.defaults(options, defaultOptions); | ||
const selectFunction = optionsToUse.forceReadFromMaster ? | ||
this._mysqlService.selectOneFromMaster.bind(this._mysqlService) : | ||
this._mysqlService.selectOne.bind(this._mysqlService); | ||
return selectFunction(sql) | ||
.then(dbRow => { | ||
@@ -121,3 +194,3 @@ if (!dbRow) { | ||
this._logger.error(err.message, {sql: sql, err:err}); | ||
throw new Error('MysqlDao.getOneFromSql() failed'); | ||
throw new Error('MysqlDao._getOneFromSql() failed'); | ||
}); | ||
@@ -133,4 +206,22 @@ } | ||
getAll(whereClause) { | ||
return this._getAll(whereClause); | ||
} | ||
/** | ||
* @param {object} whereClause | ||
* @returns {Promise.<object[],Error>} | ||
*/ | ||
getAllFromMaster(whereClause) { | ||
return this._getAll(whereClause, {forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param whereClause | ||
* @param {MysqlGetFromSqlOptions} [options] | ||
* @returns {Promise.<Object[], Error>} | ||
* @private | ||
*/ | ||
_getAll(whereClause, options) { | ||
const sql = this._queryHelper.getAll(whereClause); | ||
return this.getAllFromSql(sql); | ||
return this._getAllFromSql(sql, options); | ||
} | ||
@@ -142,4 +233,12 @@ | ||
*/ | ||
getAllFromSql(sql) { | ||
return this._getAllFromSql(sql); | ||
} | ||
/** | ||
* @param {string} sql | ||
* @returns {Promise.<Object[], Error>} | ||
*/ | ||
getAllFromSqlRaw(sql) { | ||
return this.getAllFromSql(sql, {rawResults: true}); | ||
return this._getAllFromSql(sql, {rawResults: true}); | ||
} | ||
@@ -149,2 +248,18 @@ | ||
* @param {string} sql | ||
* @returns {Promise.<Object|null, Error>} | ||
*/ | ||
getAllFromSqlFromMaster(sql) { | ||
return this._getAllFromSql(sql, {forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param {string} sql | ||
* @returns {Promise.<Object|null, Error>} | ||
*/ | ||
getAllFromSqlFromMasterRaw(sql) { | ||
return this._getAllFromSql(sql, {rawResults: true, forceReadFromMaster: true}); | ||
} | ||
/** | ||
* @param {string} sql | ||
* @param {object} [options] | ||
@@ -154,9 +269,16 @@ * @param {boolean} [options.rawResults=false] | ||
*/ | ||
getAllFromSql(sql, options) { | ||
_getAllFromSql(sql, options) { | ||
const defaultOptions = { | ||
rawResults: false | ||
rawResults: false, | ||
forceReadFromMaster: false | ||
}; | ||
const optionsToUse = _.merge(defaultOptions, options); | ||
return this._mysqlService.selectAll(sql) | ||
/** @type {MysqlGetFromSqlOptions} */ | ||
const optionsToUse = _.defaults(options, defaultOptions); | ||
const selectAllFunction = optionsToUse.forceReadFromMaster ? | ||
this._mysqlService.selectAllFromMaster.bind(this._mysqlService) : | ||
this._mysqlService.selectAll.bind(this._mysqlService); | ||
return selectAllFunction(sql) | ||
.then(dbRows => { | ||
@@ -173,3 +295,3 @@ if (optionsToUse.rawResults) { | ||
this._logger.error(err.message, {sql: sql, err:err}); | ||
throw new Error('MysqlDao.getAllFromSql() failed'); | ||
throw new Error('MysqlDao._getAllFromSql() failed'); | ||
}); | ||
@@ -176,0 +298,0 @@ } |
{ | ||
"name": "hapiest-mysql", | ||
"version": "0.0.25", | ||
"version": "0.0.26", | ||
"description": "A wrapper around mysql that provides a very descriptive way of running queries.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
'use strict'; | ||
const Should = require('should'); | ||
const Mysql = require('mysql'); | ||
const _ = require('lodash'); | ||
const VO = require('hapiest-vo'); | ||
const Sinon = require('sinon'); | ||
@@ -210,2 +210,38 @@ const MysqlDao = require('../../lib/mysqlDao'); | ||
describe('getOneByIdFromMaster', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectOneFromMaster.restore) { | ||
userDao._mysqlService.selectOneFromMaster.restore(); | ||
} | ||
}); | ||
it('Should fetch a single row by id', function() { | ||
let newId = null; | ||
const selectOneFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectOneFromMaster'); | ||
return userDao.create({firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}) | ||
.then(id => { newId = id}) | ||
.then(() => { | ||
const createPromise = userDao.create({firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'}); // Add this to make sure the ID lookup actually does something | ||
return Promise.all([createPromise]); | ||
}) | ||
.then(() => { | ||
const checkRowPromise = userDao.getOneByIdFromMaster(newId) | ||
.then(user => { | ||
Should.exist(user); | ||
user.should.be.an.instanceOf(User); | ||
user.id.should.be.a.Number(); | ||
user.firstName.should.eql('John'); | ||
user.lastName.should.eql('Doe'); | ||
user.email.should.eql('john.doe@gmail.com'); | ||
Should.exist(user.dateCreated); | ||
selectOneFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise]); | ||
}); | ||
}); | ||
}); | ||
describe('getOne', function() { | ||
@@ -238,4 +274,75 @@ beforeEach(databaseSetup); | ||
describe('getOneFromMaster', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectOneFromMaster.restore) { | ||
userDao._mysqlService.selectOneFromMaster.restore(); | ||
} | ||
}); | ||
it('Should fetch a single row by email and first name', function() { | ||
let newId = null; | ||
const selectOneFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectOneFromMaster'); | ||
return userDao.create({firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}) | ||
.then(id => { newId = id}) | ||
.then(() => { | ||
const createPromise = userDao.create({firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'}); // Add this to make sure the ID lookup actually does something | ||
return Promise.all([createPromise]); | ||
}) | ||
.then(() => { | ||
const checkRowPromise = userDao.getOneFromMaster({firstName: 'Jane', email: 'jane.doe@gmail.com'}) | ||
.then(user => { | ||
Should.exist(user); | ||
user.should.be.an.instanceOf(User); | ||
user.id.should.be.a.Number(); | ||
user.firstName.should.eql('Jane'); | ||
user.lastName.should.eql('Doe'); | ||
user.email.should.eql('jane.doe@gmail.com'); | ||
Should.exist(user.dateCreated); | ||
selectOneFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise]); | ||
}); | ||
}); | ||
}); | ||
describe('getOneFromSql', function() { | ||
beforeEach(databaseSetup); | ||
it('Should fetch a single row by email and first name with only email and first_name return in the results', function() { | ||
let newId = null; | ||
return userDao.create({firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}) | ||
.then(id => { newId = id}) | ||
.then(() => { | ||
const createPromise = userDao.create({firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'}); // Add this to make sure the ID lookup actually does something | ||
return Promise.all([createPromise]); | ||
}) | ||
.then(() => { | ||
const sql = "SELECT email, first_name FROM users WHERE first_name='Jane' AND email='jane.doe@gmail.com'"; | ||
const checkRowPromise = userDao.getOneFromSql(sql) | ||
.then(user => { | ||
Should.exist(user); | ||
Should.not.exist(user.id); | ||
Should.not.exist(user.lastName); | ||
Should.not.exist(user.dateCreated); | ||
Should.exist(user.email); | ||
Should.exist(user.firstName); | ||
user.email.should.eql('jane.doe@gmail.com'); | ||
user.firstName.should.eql('Jane'); | ||
}); | ||
return Promise.all([checkRowPromise]); | ||
}); | ||
}); | ||
}); | ||
describe('getOneFromSqlRaw', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.restore) { | ||
userDao._mysqlService.restore(); | ||
} | ||
}); | ||
@@ -270,2 +377,80 @@ it('Should fetch a single row by email and first name with only email and first_name return in the results', function() { | ||
describe('getOneFromSqlFromMaster', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectOneFromMaster.restore) { | ||
userDao._mysqlService.selectOneFromMaster.restore(); | ||
} | ||
}); | ||
it('Should fetch a single row by email and first name with only email and first_name return in the results', function() { | ||
let newId = null; | ||
const selectOneFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectOneFromMaster'); | ||
return userDao.create({firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}) | ||
.then(id => { newId = id}) | ||
.then(() => { | ||
const createPromise = userDao.create({firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'}); // Add this to make sure the ID lookup actually does something | ||
return Promise.all([createPromise]); | ||
}) | ||
.then(() => { | ||
const sql = "SELECT email, first_name FROM users WHERE first_name='Jane' AND email='jane.doe@gmail.com'"; | ||
const checkRowPromise = userDao.getOneFromSqlFromMaster(sql) | ||
.then(user => { | ||
Should.exist(user); | ||
Should.not.exist(user.id); | ||
Should.not.exist(user.lastName); | ||
Should.not.exist(user.dateCreated); | ||
Should.exist(user.email); | ||
Should.exist(user.firstName); | ||
user.email.should.eql('jane.doe@gmail.com'); | ||
user.firstName.should.eql('Jane'); | ||
selectOneFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise]); | ||
}); | ||
}); | ||
}); | ||
describe('getOneFromSqlFromMasterRaw', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectOneFromMaster.restore) { | ||
userDao._mysqlService.selectOneFromMaster.restore(); | ||
} | ||
}); | ||
it('Should fetch a single row by email and first name with only email and first_name return in the results', function() { | ||
let newId = null; | ||
const selectOneFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectOneFromMaster'); | ||
return userDao.create({firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}) | ||
.then(id => { newId = id}) | ||
.then(() => { | ||
const createPromise = userDao.create({firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'}); // Add this to make sure the ID lookup actually does something | ||
return Promise.all([createPromise]); | ||
}) | ||
.then(() => { | ||
const sql = "SELECT email, first_name FROM users WHERE first_name='Jane' AND email='jane.doe@gmail.com'"; | ||
const checkRowPromise = userDao.getOneFromSqlFromMasterRaw(sql) | ||
.then(user => { | ||
Should.exist(user); | ||
Should.not.exist(user.id); | ||
Should.not.exist(user.lastName); | ||
Should.not.exist(user.dateCreated); | ||
Should.exist(user.email); | ||
Should.exist(user.first_name); | ||
user.email.should.eql('jane.doe@gmail.com'); | ||
user.first_name.should.eql('Jane'); | ||
selectOneFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise]); | ||
}); | ||
}); | ||
}); | ||
describe('getAll', function() { | ||
@@ -280,3 +465,4 @@ beforeEach(databaseSetup); | ||
.then((numRows) => { | ||
Should.exist(numRows); | ||
numRows.should.eql(2); | ||
const checkRowPromise1 = userDao.getAll({lastName: 'Doe'}) | ||
@@ -304,2 +490,115 @@ .then(users => { | ||
describe('getAllFromMaster', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectAllFromMaster.restore) { | ||
userDao._mysqlService.selectAllFromMaster.restore(); | ||
} | ||
}); | ||
it('Should return two users', function() { | ||
const selectAllFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectAllFromMaster'); | ||
return userDao.createBulk([ | ||
{firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}, | ||
{firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'} | ||
]) | ||
.then((numRows) => { | ||
Should.exist(numRows); | ||
numRows.should.eql(2); | ||
const checkRowPromise1 = userDao.getAllFromMaster({lastName: 'Doe'}) | ||
.then(users => { | ||
Should.exist(users); | ||
users.should.be.an.Array(); | ||
users.length.should.eql(2); | ||
users[0].should.be.an.instanceOf(User); | ||
users[0].firstName.should.eql('John'); | ||
users[0].lastName.should.eql('Doe'); | ||
users[0].email.should.eql('john.doe@gmail.com'); | ||
users[1].should.be.an.instanceOf(User); | ||
users[1].firstName.should.eql('Jane'); | ||
users[1].lastName.should.eql('Doe'); | ||
users[1].email.should.eql('jane.doe@gmail.com'); | ||
selectAllFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise1]); | ||
}); | ||
}); | ||
}); | ||
describe('getAllFromSql', function() { | ||
beforeEach(databaseSetup); | ||
it('Should return two rows with only first_name populated', function() { | ||
return userDao.createBulk([ | ||
{firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}, | ||
{firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'} | ||
]) | ||
.then((numRows) => { | ||
Should.exist(numRows); | ||
numRows.should.eql(2); | ||
const sql = "SELECT first_name FROM users WHERE last_name='Doe'"; | ||
const checkRowPromise1 = userDao.getAllFromSql(sql) | ||
.then(dbRows => { | ||
Should.exist(dbRows); | ||
dbRows.should.be.an.Array(); | ||
dbRows.length.should.eql(2); | ||
dbRows[0].firstName.should.eql('John'); | ||
dbRows[1].firstName.should.eql('Jane'); | ||
Should.not.exist(dbRows[0].email); | ||
Should.not.exist(dbRows[0].lastName); | ||
Should.not.exist(dbRows[1].email); | ||
Should.not.exist(dbRows[1].lastName); | ||
}); | ||
return Promise.all([checkRowPromise1]); | ||
}); | ||
}); | ||
}); | ||
describe('getAllFromSqlFromMaster', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectAllFromMaster.restore) { | ||
userDao._mysqlService.selectAllFromMaster.restore(); | ||
} | ||
}); | ||
it('Should return two rows with only first_name populated', function() { | ||
const selectAllFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectAllFromMaster'); | ||
return userDao.createBulk([ | ||
{firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}, | ||
{firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'} | ||
]) | ||
.then((numRows) => { | ||
Should.exist(numRows); | ||
numRows.should.eql(2); | ||
const sql = "SELECT first_name FROM users WHERE last_name='Doe'"; | ||
const checkRowPromise1 = userDao.getAllFromSqlFromMaster(sql) | ||
.then(dbRows => { | ||
Should.exist(dbRows); | ||
dbRows.should.be.an.Array(); | ||
dbRows.length.should.eql(2); | ||
dbRows[0].firstName.should.eql('John'); | ||
dbRows[1].firstName.should.eql('Jane'); | ||
Should.not.exist(dbRows[0].email); | ||
Should.not.exist(dbRows[0].lastName); | ||
Should.not.exist(dbRows[1].email); | ||
Should.not.exist(dbRows[1].lastName); | ||
selectAllFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise1]); | ||
}); | ||
}); | ||
}); | ||
describe('getAllFromSqlRaw', function() { | ||
@@ -338,2 +637,43 @@ beforeEach(databaseSetup); | ||
describe('getAllFromSqlFromMasterRaw', function() { | ||
beforeEach(databaseSetup); | ||
afterEach(() => { | ||
if (userDao._mysqlService.selectAllFromMaster.restore) { | ||
userDao._mysqlService.selectAllFromMaster.restore(); | ||
} | ||
}); | ||
it('Should return two rows with only first_name populated', function() { | ||
const selectAllFromMasterSpy = Sinon.spy(userDao._mysqlService, 'selectAllFromMaster'); | ||
return userDao.createBulk([ | ||
{firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com'}, | ||
{firstName: 'Jane', lastName: 'Doe', email: 'jane.doe@gmail.com'} | ||
]) | ||
.then((numRows) => { | ||
const sql = "SELECT first_name FROM users WHERE last_name='Doe'"; | ||
const checkRowPromise1 = userDao.getAllFromSqlFromMasterRaw(sql) | ||
.then(dbRows => { | ||
Should.exist(dbRows); | ||
dbRows.should.be.an.Array(); | ||
dbRows.length.should.eql(2); | ||
dbRows[0].first_name.should.eql('John'); | ||
dbRows[1].first_name.should.eql('Jane'); | ||
Should.not.exist(dbRows[0].email); | ||
Should.not.exist(dbRows[0].firstName); | ||
Should.not.exist(dbRows[0].lastName); | ||
Should.not.exist(dbRows[1].email); | ||
Should.not.exist(dbRows[1].firstName); | ||
Should.not.exist(dbRows[1].lastName); | ||
selectAllFromMasterSpy.calledOnce.should.be.True(); | ||
}); | ||
return Promise.all([checkRowPromise1]); | ||
}); | ||
}); | ||
}); | ||
describe('updateById', function() { | ||
@@ -340,0 +680,0 @@ beforeEach(databaseSetup); |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
166610
3702
0