grapher/lib/namedQuery/namedQuery.server.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

import prepareForProcess from '../query/lib/prepareForProcess.js';
import Base from './namedQuery.base';
import deepClone from 'lodash.clonedeep';
2017-11-26 19:15:00 +02:00
import MemoryResultCacher from './cache/MemoryResultCacher';
import generateQueryId from './cache/generateQueryId';
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});
}
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() {
let body = prepareForProcess(this.body, this.params);
2017-11-25 14:45:31 +02:00
return this.collection.find(body.$filters || {}, {fields: {_id: 1}});
}
2017-11-26 19:15:00 +02:00
/**
* @param cacher
*/
cacheResults(cacher) {
if (!cacher) {
cacher = new MemoryResultCacher();
}
this.cacher = cacher;
}
}