2016-11-07 18:44:09 +02:00
|
|
|
import prepareForProcess from '../query/lib/prepareForProcess.js';
|
2016-10-14 10:57:26 +03:00
|
|
|
import Base from './namedQuery.base';
|
2017-11-26 23:02:26 +02:00
|
|
|
import deepClone from 'lodash.clonedeep';
|
2017-11-26 19:15:00 +02:00
|
|
|
import MemoryResultCacher from './cache/MemoryResultCacher';
|
|
|
|
import generateQueryId from './cache/generateQueryId';
|
2016-10-14 10:57:26 +03:00
|
|
|
|
|
|
|
export default class extends Base {
|
|
|
|
/**
|
|
|
|
* Retrieves the data.
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
fetch() {
|
|
|
|
const query = this.collection.createQuery(
|
|
|
|
deepClone(this.body),
|
|
|
|
deepClone(this.params)
|
|
|
|
);
|
|
|
|
|
2017-11-26 19:15:00 +02:00
|
|
|
if (this.cacher) {
|
|
|
|
const cacheId = generateQueryId(this.queryName, this.params);
|
|
|
|
return this.cacher.get(cacheId, {query});
|
|
|
|
}
|
|
|
|
|
2016-10-14 10:57:26 +03:00
|
|
|
return query.fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param args
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
fetchOne(...args) {
|
|
|
|
return _.first(this.fetch(...args));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the count of matching elements.
|
|
|
|
*
|
|
|
|
* @returns {any}
|
|
|
|
*/
|
|
|
|
getCount() {
|
2017-11-26 19:15:00 +02:00
|
|
|
const countCursor = this.getCursorForCounting();
|
|
|
|
|
|
|
|
if (this.cacher) {
|
|
|
|
const cacheId = 'count::' + generateQueryId(this.queryName, this.params);
|
|
|
|
|
|
|
|
return this.cacher.get(cacheId, {countCursor});
|
|
|
|
}
|
|
|
|
|
|
|
|
return countCursor.count();
|
2017-11-25 14:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the cursor for counting
|
|
|
|
* This is most likely used for counts cursor
|
|
|
|
*/
|
|
|
|
getCursorForCounting() {
|
2016-11-07 18:44:09 +02:00
|
|
|
let body = prepareForProcess(this.body, this.params);
|
2017-11-25 14:45:31 +02:00
|
|
|
|
|
|
|
return this.collection.find(body.$filters || {}, {fields: {_id: 1}});
|
2016-10-14 10:57:26 +03:00
|
|
|
}
|
2017-11-26 19:15:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param cacher
|
|
|
|
*/
|
|
|
|
cacheResults(cacher) {
|
|
|
|
if (!cacher) {
|
|
|
|
cacher = new MemoryResultCacher();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cacher = cacher;
|
|
|
|
}
|
2016-10-14 10:57:26 +03:00
|
|
|
}
|