react-native-firebase/lib/modules/database/query.js

96 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-03-02 11:40:08 +00:00
/**
* @flow
*/
import Reference from './reference.js';
import { objectToUniqueId } from './../../utils';
// todo doc methods
2017-03-02 11:40:08 +00:00
/**
* @class Query
*/
export default class Query {
modifiers: Array<DatabaseModifier>;
2017-03-02 11:40:08 +00:00
constructor(ref: Reference, path: string, existingModifiers?: Array<DatabaseModifier>) {
2017-03-02 11:40:08 +00:00
this.modifiers = existingModifiers ? [...existingModifiers] : [];
this._reference = ref;
2017-03-02 11:40:08 +00:00
}
/**
*
* @param name
* @param key
* @return {Reference|*}
*/
orderBy(name: string, key?: string) {
this.modifiers.push({
type: 'orderBy',
name,
key,
});
return this._reference;
2017-03-02 11:40:08 +00:00
}
/**
*
* @param name
* @param limit
* @return {Reference|*}
*/
limit(name: string, limit: number) {
this.modifiers.push({
type: 'limit',
name,
limit,
});
return this._reference;
2017-03-02 11:40:08 +00:00
}
/**
*
* @param name
* @param value
* @param key
* @return {Reference|*}
*/
filter(name: string, value: any, key?: string) {
this.modifiers.push({
type: 'filter',
name,
value,
valueType: typeof value,
key,
});
return this._reference;
2017-03-02 11:40:08 +00:00
}
/**
*
* @return {[*]}
*/
getModifiers(): Array<DatabaseModifier> {
2017-03-02 11:40:08 +00:00
return [...this.modifiers];
}
/**
*
* @return {*}
*/
queryIdentifier() {
// convert query modifiers array into an object for generating a unique key
const object = {};
for (let i = 0, len = this.modifiers.length; i < len; i++) {
const { name, type, value } = this.modifiers[i];
object[`${type}-${name}`] = value;
}
return objectToUniqueId(object);
}
2017-03-02 11:40:08 +00:00
}