-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.ts
53 lines (35 loc) · 1.34 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { firestore } from 'firebase/app';
export default class Query{
column;
operator;
value;
constructor(column, operator, value){
if(column == undefined || operator == undefined || value == undefined)
throw new Error("Invalid parameters for Firestore Query");
// TODO: garantir que operador seja um dos operadores válidos
this.column = column;
this.operator = operator;
this.value = value;
}
static build(ref, query){
if(query == undefined || query == null){
throw new Error("Not possible to build a query with empty queries");
}
if(query != null && query.length != null){
return Query.buildMultipleQuery(ref, query);
}else{
return ref.where(query.column, query.operator, query.value)
}
}
static buildMultipleQuery(ref, queries:Query[]):firestore.Query{
if(queries.length == 0){
throw new Error("Not possible to build a query with empty queries");
}
let query;
query = ref.where(queries[0].column, queries[0].operator, queries[0].value);
for(let i = 1; i < queries.length; i++){
query = query.where(queries[i].column, queries[i].operator, queries[i].value);
}
return query;
}
}