2017-03-02 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
2017-12-04 12:07:41 +00:00
|
|
|
* Query representation wrapper
|
2017-03-02 11:40:08 +00:00
|
|
|
*/
|
2017-11-23 17:29:40 +00:00
|
|
|
import { objectToUniqueId } from '../../utils';
|
|
|
|
|
|
|
|
import type { DatabaseModifier } from '../../types';
|
2018-02-14 10:05:28 +00:00
|
|
|
import type Reference from './Reference';
|
2017-08-14 10:05:49 +00:00
|
|
|
|
|
|
|
// todo doc methods
|
2017-03-02 11:40:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Query
|
|
|
|
*/
|
2017-07-12 14:49:33 +00:00
|
|
|
export default class Query {
|
2017-12-04 12:07:41 +00:00
|
|
|
_reference: Reference;
|
2017-04-26 11:21:53 +00:00
|
|
|
modifiers: Array<DatabaseModifier>;
|
2017-03-02 11:40:08 +00:00
|
|
|
|
2018-01-26 11:55:37 +00:00
|
|
|
constructor(ref: Reference, existingModifiers?: Array<DatabaseModifier>) {
|
2017-03-02 11:40:08 +00:00
|
|
|
this.modifiers = existingModifiers ? [...existingModifiers] : [];
|
2017-08-05 20:45:15 +00:00
|
|
|
this._reference = ref;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param key
|
|
|
|
* @return {Reference|*}
|
|
|
|
*/
|
2017-04-26 11:21:53 +00:00
|
|
|
orderBy(name: string, key?: string) {
|
|
|
|
this.modifiers.push({
|
2017-12-04 12:07:41 +00:00
|
|
|
id: `orderBy-${name}:${key || ''}`,
|
2017-04-26 11:21:53 +00:00
|
|
|
type: 'orderBy',
|
|
|
|
name,
|
|
|
|
key,
|
|
|
|
});
|
2017-08-05 20:45:15 +00:00
|
|
|
|
|
|
|
return this._reference;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param limit
|
|
|
|
* @return {Reference|*}
|
|
|
|
*/
|
2017-04-26 11:21:53 +00:00
|
|
|
limit(name: string, limit: number) {
|
|
|
|
this.modifiers.push({
|
2017-10-23 16:03:49 +00:00
|
|
|
id: `limit-${name}:${limit}`,
|
2017-04-26 11:21:53 +00:00
|
|
|
type: 'limit',
|
|
|
|
name,
|
|
|
|
limit,
|
|
|
|
});
|
2017-08-05 20:45:15 +00:00
|
|
|
|
|
|
|
return this._reference;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
* @param key
|
|
|
|
* @return {Reference|*}
|
|
|
|
*/
|
2017-07-12 14:49:33 +00:00
|
|
|
filter(name: string, value: any, key?: string) {
|
2017-04-26 11:21:53 +00:00
|
|
|
this.modifiers.push({
|
2017-12-04 12:07:41 +00:00
|
|
|
id: `filter-${name}:${objectToUniqueId(value)}:${key || ''}`,
|
2017-04-26 11:21:53 +00:00
|
|
|
type: 'filter',
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
valueType: typeof value,
|
|
|
|
key,
|
|
|
|
});
|
2017-08-05 20:45:15 +00:00
|
|
|
|
|
|
|
return this._reference;
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 10:05:49 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {[*]}
|
|
|
|
*/
|
2017-04-26 11:21:53 +00:00
|
|
|
getModifiers(): Array<DatabaseModifier> {
|
2017-03-02 11:40:08 +00:00
|
|
|
return [...this.modifiers];
|
|
|
|
}
|
2017-08-14 10:05:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
queryIdentifier() {
|
2017-10-23 16:03:49 +00:00
|
|
|
// sort modifiers to enforce ordering
|
|
|
|
const sortedModifiers = this.getModifiers().sort((a, b) => {
|
|
|
|
if (a.id < b.id) return -1;
|
|
|
|
if (a.id > b.id) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2017-08-14 10:05:49 +00:00
|
|
|
|
2017-10-23 16:03:49 +00:00
|
|
|
// Convert modifiers to unique key
|
|
|
|
let key = '{';
|
|
|
|
for (let i = 0; i < sortedModifiers.length; i++) {
|
|
|
|
if (i !== 0) key += ',';
|
|
|
|
key += sortedModifiers[i].id;
|
2017-08-14 10:05:49 +00:00
|
|
|
}
|
2017-10-23 16:03:49 +00:00
|
|
|
key += '}';
|
2017-08-14 10:05:49 +00:00
|
|
|
|
2017-10-23 16:03:49 +00:00
|
|
|
return key;
|
2017-08-14 10:05:49 +00:00
|
|
|
}
|
2017-03-02 11:40:08 +00:00
|
|
|
}
|