adonis-lucid-mongodb
Advanced tools
Comparing version 1.0.33 to 1.0.34
@@ -8,3 +8,3 @@ { | ||
}, | ||
"version": "1.0.33", | ||
"version": "1.0.34", | ||
"scripts": { | ||
@@ -11,0 +11,0 @@ "lint": "standard", |
@@ -79,30 +79,2 @@ 'use strict' | ||
/** | ||
* makes the join query to be used by other | ||
* methods. | ||
* | ||
* @param {Boolean} ignoreSelect | ||
* | ||
* @public | ||
*/ | ||
_makeJoinQuery (ignoreSelect) { | ||
const self = this | ||
const selectionKeys = [ | ||
`${this.related.collection}.*`, | ||
`${this.pivotCollection}.${this.pivotLocalKey} as ${this.pivotPrefix}${this.pivotLocalKey}`, | ||
`${this.pivotCollection}.${this.pivotOtherKey} as ${this.pivotPrefix}${this.pivotOtherKey}` | ||
] | ||
_.each(this.pivotItems, (item) => { | ||
selectionKeys.push(`${this.pivotCollection}.${item} as ${this.pivotPrefix}${item}`) | ||
}) | ||
if (!ignoreSelect) { | ||
this.relatedQuery.select.apply(this.relatedQuery, selectionKeys) | ||
} | ||
this.relatedQuery.innerJoin(this.pivotCollection, function () { | ||
this.on(`${self.related.collection}.${self.toKey}`, `${self.pivotCollection}.${self.pivotOtherKey}`) | ||
}) | ||
} | ||
/** | ||
* decorates the current query chain before execution | ||
@@ -133,130 +105,137 @@ */ | ||
* _getPivotQuery () { | ||
const query = this.relatedQuery.clone() | ||
const connection = yield query.connect() | ||
query.queryBuilder.collection(connection.collection(this.pivotCollection)) | ||
return query | ||
} | ||
/** | ||
* Returns a cloned query with the join statement to be | ||
* used for fetching aggregates or paginate results. | ||
* getAlternate | ||
* | ||
* @param {String} expression | ||
* | ||
* @return {Object} | ||
* | ||
* @private | ||
*/ | ||
_getAlternateQuery (expression) { | ||
const self = this | ||
const countByQuery = this.relatedQuery.clone() | ||
* _pivotOtherKeys () { | ||
const query = this.relatedQuery.clone() | ||
const connection = yield query.connect() | ||
const pivotQuery = query.queryBuilder.collection(connection.collection(this.pivotCollection)) | ||
const pivots = yield pivotQuery.where(this.pivotLocalKey, this.parent[this.fromKey]).find() | ||
return _.map(pivots, this.pivotOtherKey) | ||
} | ||
countByQuery.innerJoin(this.pivotCollection, function () { | ||
this.on(`${self.related.collection}.${self.toKey}`, `${self.pivotCollection}.${self.pivotOtherKey}`) | ||
}).where(`${this.pivotCollection}.${this.pivotLocalKey}`, this.parent[this.fromKey]) | ||
return countByQuery | ||
/** | ||
* decorates the current query chain before execution | ||
*/ | ||
_getThroughQuery (pivotOtherKeys) { | ||
return this.relatedQuery.whereIn(this.toKey, pivotOtherKeys) | ||
} | ||
/** | ||
* paginates over a set of results based upon current page | ||
* and values to be fetched per page. | ||
* Fetch over the related rows | ||
* | ||
* @method paginate | ||
* | ||
* @param {Number} page | ||
* @param {Number} perPage | ||
* | ||
* @return {Array} | ||
* @return {Object} | ||
*/ | ||
paginate (page, perPage) { | ||
* fetch () { | ||
this._validateRead() | ||
/** | ||
* It is important to decorate the actual query | ||
* builder after fetching the alternate query | ||
* since fresh query builder is required | ||
* to return alternate query | ||
*/ | ||
this._decorateRead() | ||
/** | ||
* calling the paginate method on proxies query builder | ||
* which optionally accepts a countByQuery | ||
*/ | ||
return this.relatedQuery.paginate(page, perPage) | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).fetch() | ||
} | ||
/** | ||
* Returns the existence query to be used when main | ||
* query is dependent upon childs. | ||
* Fetch first over the related rows | ||
* | ||
* @param {Function} [callback] | ||
* @return {Object} | ||
*/ | ||
counts (callback) { | ||
this._makeJoinQuery(true) | ||
this.relatedQuery.count('*').whereRaw(`${this.pivotCollection}.${this.pivotLocalKey} = ${this.parent.constructor.collection}.${this.fromKey}`) | ||
if (typeof (callback) === 'function') { | ||
callback(this.relatedQuery) | ||
} | ||
return this.relatedQuery.modelQueryBuilder | ||
* first () { | ||
this._validateRead() | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).first() | ||
} | ||
/** | ||
* Returns count of rows for the related row | ||
* Paginates over the related rows | ||
* | ||
* @param {String} expression | ||
* @param {Number} page | ||
* @param {Number} [perPage=20] | ||
* | ||
* @return {Array} | ||
* @return {Object} | ||
*/ | ||
count (expression) { | ||
* paginate (page, perPage) { | ||
this._validateRead() | ||
return this._getAlternateQuery().count(expression) | ||
/** | ||
* creating the query clone to be used as countByQuery, | ||
* since selecting fields in countBy requires unwanted | ||
* groupBy clauses. | ||
*/ | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).paginate(page, perPage) | ||
} | ||
/** | ||
* Returns avg for a given column | ||
* Returns count of rows for the related row | ||
* | ||
* @param {String} column | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
avg (column) { | ||
* count (groupBy) { | ||
this._validateRead() | ||
return this._getAlternateQuery().avg(column) | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).count(groupBy) | ||
} | ||
/** | ||
* Return min value for a column | ||
* Returns sum for a given key | ||
* | ||
* @param {String} column | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
min (column) { | ||
* sum (key, groupBy) { | ||
this._validateRead() | ||
return this._getAlternateQuery().min(column) | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).sum(key, groupBy) | ||
} | ||
/** | ||
* Return max value for a column | ||
* Returns avg for a given key | ||
* | ||
* @param {String} column | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
max (column) { | ||
* avg (key, groupBy) { | ||
this._validateRead() | ||
return this._getAlternateQuery().max(column) | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).avg(key, groupBy) | ||
} | ||
/** | ||
* Throws exception since update should be | ||
* done after getting the instance. | ||
* Returns min for a given key | ||
* | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
increment () { | ||
throw CE.ModelRelationException.unSupportedMethod('increment', 'BelongsToMany') | ||
* min (key, groupBy) { | ||
this._validateRead() | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).min(key, groupBy) | ||
} | ||
/** | ||
* Throws exception since update should be | ||
* done after getting the instance. | ||
* Returns max for a given key | ||
* | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
decrement () { | ||
throw CE.ModelRelationException.unSupportedMethod('decrement', 'BelongsToMany') | ||
* max (key, groupBy) { | ||
this._validateRead() | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).max(key, groupBy) | ||
} | ||
@@ -475,2 +454,15 @@ | ||
/** | ||
* update | ||
* | ||
* @public | ||
* | ||
* @return {Object} | ||
*/ | ||
* update (values) { | ||
this._validateRead() | ||
const pivotOtherKeys = yield this._pivotOtherKeys() | ||
return yield this._getThroughQuery(pivotOtherKeys).update(values) | ||
} | ||
/** | ||
* Pick selected fields from the pivot collection. | ||
@@ -477,0 +469,0 @@ * |
@@ -26,2 +26,4 @@ 'use strict' | ||
this.viaForeignKey = throughForeignKey || this.through.foreignKey // author_id | ||
this.relatedQuery = this.through.query() | ||
this.alternateQuery = this.related.query() | ||
} | ||
@@ -35,3 +37,3 @@ | ||
* _getAlternateIds () { | ||
const alternates = yield this.relatedQuery.select([this.fromKey, this.toKey]) | ||
const alternates = yield this.alternateQuery.select([this.fromKey, this.toKey]) | ||
.where(this.toKey, this.parent[this.fromKey]).fetch() | ||
@@ -45,6 +47,5 @@ return alternates.map(this.viaKey).value() | ||
_getThroughQuery (alternateIds) { | ||
return this.through.whereIn(this.viaForeignKey, alternateIds) | ||
return this.relatedQuery.whereIn(this.viaForeignKey, alternateIds) | ||
} | ||
/** | ||
@@ -94,52 +95,69 @@ * Fetch over the related rows | ||
* | ||
* @param {String} expression | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
* count (expression) { | ||
* count (groupBy) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).count(expression) | ||
return yield this._getThroughQuery(alternateIds).count(groupBy) | ||
} | ||
/** | ||
* Returns avg for a given column | ||
* Returns sum for a given key | ||
* | ||
* @param {String} column | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
* avg (column) { | ||
* sum (key, groupBy) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).avg(column) | ||
return yield this._getThroughQuery(alternateIds).sum(key, groupBy) | ||
} | ||
/** | ||
* Return min value for a column | ||
* Returns avg for a given key | ||
* | ||
* @param {String} column | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
* min (column) { | ||
* avg (key, groupBy) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).min(column) | ||
return yield this._getThroughQuery(alternateIds).avg(key, groupBy) | ||
} | ||
/** | ||
* Return max value for a column | ||
* Returns min for a given key | ||
* | ||
* @param {String} column | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
* max (column) { | ||
* min (key, groupBy) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).max(column) | ||
return yield this._getThroughQuery(alternateIds).min(key, groupBy) | ||
} | ||
/** | ||
* Returns max for a given key | ||
* | ||
* @param {String} key | ||
* @param {String} groupBy | ||
* | ||
* @return {Array} | ||
*/ | ||
* max (key, groupBy) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).max(key, groupBy) | ||
} | ||
/** | ||
* will eager load the relation for multiple values on related | ||
@@ -242,4 +260,17 @@ * model and returns an object with values grouped by foreign | ||
/** | ||
* update | ||
* | ||
* @public | ||
* | ||
* @return {Object} | ||
*/ | ||
* update (values) { | ||
this._validateRead() | ||
const alternateIds = yield this._getAlternateIds() | ||
return yield this._getThroughQuery(alternateIds).update(values) | ||
} | ||
} | ||
module.exports = HasManyThrough |
@@ -26,9 +26,6 @@ 'use strict' | ||
this.related = this._resolveModel(related) | ||
this.relatedQuery = this.related ? this.related.query() : null | ||
return new Proxy(this, proxyHandler) | ||
} | ||
get relatedQuery () { | ||
return this.related.query() | ||
} | ||
/** | ||
@@ -133,31 +130,2 @@ * empty placeholder to be used when unable to eagerload | ||
/** | ||
* Returns the existence query to be used when main | ||
* query is dependent upon childs. | ||
* | ||
* @param {Function} [callback] | ||
* @return {Object} | ||
*/ | ||
exists (callback) { | ||
const relatedQuery = this.relatedQuery.whereRaw(`${this.related.collection}.${this.toKey} = ${this.parent.constructor.collection}.${this.fromKey}`) | ||
if (typeof (callback) === 'function') { | ||
callback(relatedQuery) | ||
} | ||
return relatedQuery.modelQueryBuilder | ||
} | ||
/** | ||
* Returns the counts query for a given relation | ||
* | ||
* @param {Function} [callback] | ||
* @return {Object} | ||
*/ | ||
counts (callback) { | ||
const relatedQuery = this.relatedQuery.count('*').whereRaw(`${this.related.collection}.${this.toKey} = ${this.parent.constructor.collection}.${this.fromKey}`) | ||
if (typeof (callback) === 'function') { | ||
callback(relatedQuery) | ||
} | ||
return relatedQuery.modelQueryBuilder | ||
} | ||
/** | ||
* Returns the query builder instance for related model. | ||
@@ -164,0 +132,0 @@ * |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
237713
8435