hapiest-mysql
Advanced tools
Comparing version 0.0.41 to 0.0.42
'use strict'; | ||
const Promise = require('bluebird'); | ||
const _ = require('lodash'); | ||
@@ -636,4 +637,87 @@ const MysqlDaoQueryHelper = require('./mysqlDaoQueryHelper'); | ||
/** | ||
* Similar to join(), but will divide `objectsToJoin` into batches of size `maxBatchSize` | ||
* @param {object[]|object} objectsToJoin | ||
* @param {string} lookupKey | ||
* @param {object} options | ||
* @param {int} [options.maxBatchSize = 100] - The max batch size for querying this Dao's table. | ||
* @param {string} [options.joinKey = "id"] | ||
* @param {string} [options.resultsKey] | ||
* @return {Promise.<object[]|object>} | ||
*/ | ||
batchJoin(objectsToJoin, lookupKey, options) { | ||
if (!objectsToJoin) { | ||
return Promise.resolve(); | ||
} | ||
const shouldReturnArray = _.isArray(objectsToJoin); | ||
objectsToJoin = _.castArray(objectsToJoin); | ||
options = _.defaults(options, { | ||
joinKey: 'id', | ||
resultsKey: _.camelCase(this.tableName), | ||
maxBatchSize: 100 | ||
}); | ||
// Split into batches based on batch size. | ||
const objectBatches = _.chunk(objectsToJoin, options.maxBatchSize); | ||
// Process each batch, then recombine. | ||
return Promise.map(objectBatches, objectBatch => | ||
this.join(objectBatch, lookupKey, options) | ||
) | ||
.then(_.flatten) | ||
.then(objectsWithJoins => { | ||
return shouldReturnArray ? objectsWithJoins : _.head(objectsWithJoins); | ||
}); | ||
} | ||
/** | ||
* Takes an array of objects and joins entries from this Dao onto each of them. It will take "joinKey" from each object | ||
* (defaulting to "id") and query this Dao using "lookupKey" for all rows matching that array of values (`SELECT * FROM table_name WHERE id IN (x, y, z, ...)`), | ||
* and then join the matching VOs and add them under a new property ("resultsKey") on the original object. | ||
* @param {object[]|object} objectsToJoin - Object or objects which should be joined-onto. | ||
* @param {string} lookupKey - The lookup key to use for selecting entities from this Dao. | ||
* @param {object} options | ||
* @param {string} [options.joinKey = "id"] - The join key to use for the objects in objectsToJoin. | ||
* @param {string} [options.resultsKey] - The key onto which the joined entities will be added. Defaults to the camelCase value of `this.tableName`. | ||
* @return {Promise.<object[]|object>} - Returns original input object(s) decorated with joined VOs under "resultsKey" | ||
*/ | ||
join(objectsToJoin, lookupKey, options) { | ||
if (!objectsToJoin) { | ||
return Promise.resolve(); | ||
} | ||
const shouldReturnArray = _.isArray(objectsToJoin); | ||
objectsToJoin = _.castArray(objectsToJoin); | ||
options = _.defaults(options, { | ||
joinKey: 'id', | ||
resultsKey: _.camelCase(this.tableName) | ||
}); | ||
const joinValues = objectsToJoin.map(object => object[options.joinKey]); | ||
return this.getAll({[lookupKey]: joinValues}) | ||
.then(vos => { | ||
// For each object in the join, find all matching VOs from this Dao to join. | ||
return Promise.map(objectsToJoin, object => { | ||
// Pull all vos which match this object. NOTE: This mutates `vos` (reducing the set each time). | ||
const vosToJoin = _.remove(vos, vo => object[options.joinKey] === vo[lookupKey]); | ||
// Add the matching vos for the join under `resultsKey`. | ||
object[options.resultsKey] = vosToJoin; | ||
return object; | ||
}); | ||
}) | ||
.then(objectsWithJoins => { | ||
return shouldReturnArray ? objectsWithJoins : _.head(objectsWithJoins); | ||
}) | ||
} | ||
} | ||
module.exports = MysqlDao; |
{ | ||
"name": "hapiest-mysql", | ||
"version": "0.0.41", | ||
"version": "0.0.42", | ||
"description": "A wrapper around mysql that provides a very descriptive way of running queries.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
270447
5717
0