Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 35b5c91

Browse files
#120 Chaining range types
1 parent 63da64c commit 35b5c91

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

docs/DATABASE.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,25 @@ Let's say we have the structure as defined at `setValue`, then use this query to
9090
// order by company.country
9191
orderBy: {
9292
type: firebase.QueryOrderByType.CHILD,
93-
value: 'country' // mandatory when type is 'child'
94-
},
95-
// but only companies named 'Telerik'
96-
// (this range relates to the orderBy clause)
97-
range: {
98-
type: firebase.QueryRangeType.EQUAL_TO,
99-
value: 'Bulgaria'
93+
value: 'since' // mandatory when type is 'child'
10094
},
95+
// but only companies 'since' a certain year (Telerik's value is 2000, which is imaginary btw)
96+
// use either a 'range'
97+
//range: {
98+
// type: firebase.QueryRangeType.EQUAL_TO,
99+
// value: 2000
100+
///},
101+
// .. or 'chain' ranges like this:
102+
ranges: [
103+
{
104+
type: firebase.QueryRangeType.START_AT,
105+
value: 1999
106+
},
107+
{
108+
type: firebase.QueryRangeType.END_AT,
109+
value: 2000
110+
}
111+
],
101112
// only the first 2 matches
102113
// (note that there's only 1 in this case anyway)
103114
limit: {
@@ -108,7 +119,7 @@ Let's say we have the structure as defined at `setValue`, then use this query to
108119
);
109120
```
110121

111-
For supported values of the orderBy/range/limit's `type` properties, take a look at the [`firebase-common.d.ts`](firebase-common.d.ts) TypeScript definitions in this repo.
122+
For supported values of the orderBy/range/ranges/limit's `type` properties, take a look at the [`firebase-common.d.ts`](firebase-common.d.ts) TypeScript definitions in this repo.
112123

113124
### update
114125
Changes the values of the keys specified in the dictionary without overwriting other keys at this location.

firebase.android.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,27 @@ firebase.query = function (updateCallback, path, options) {
941941
}
942942
}
943943

944+
// ranges
945+
if (options.ranges) {
946+
for (var i=0; i < options.ranges.length; i++) {
947+
var range = options.ranges[i];
948+
if (!range.value) {
949+
reject("Please set ranges["+i+"].value");
950+
return;
951+
}
952+
if (range.type === firebase.QueryRangeType.START_AT) {
953+
query = query.startAt(range.value);
954+
} else if (range.type === firebase.QueryRangeType.END_AT) {
955+
query = query.endAt(range.value);
956+
} else if (range.type === firebase.QueryRangeType.EQUAL_TO) {
957+
query = query.equalTo(range.value);
958+
} else {
959+
reject("Invalid ranges["+i+"].type, use constants like firebase.QueryRangeType.START_AT");
960+
return;
961+
}
962+
}
963+
}
964+
944965
// limit
945966
if (options.limit && options.limit.type) {
946967
if (!options.limit.value) {

firebase.d.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ declare module "nativescript-plugin-firebase" {
2828
storageBucket?: string;
2929
}
3030

31+
export interface QueryRangeOption {
32+
type: QueryRangeType;
33+
value: string;
34+
}
35+
3136
/**
3237
* The options object passed into the query function.
3338
*/
@@ -45,11 +50,15 @@ declare module "nativescript-plugin-firebase" {
4550

4651
/**
4752
* You can further restrict the returned results by specifying restrictions.
53+
* Need more than one range restriction? Use 'ranges' instead.
4854
*/
49-
range?: {
50-
type: QueryRangeType;
51-
value: string;
52-
}
55+
range?: QueryRangeOption;
56+
57+
/**
58+
* Same as 'range', but for a 'chain of ranges'.
59+
* You can further restrict the returned results by specifying restrictions.
60+
*/
61+
ranges?: QueryRangeOption[];
5362

5463
/**
5564
* You can limit the number of returned rows if you want to.

firebase.ios.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,27 @@ firebase.query = function (updateCallback, path, options) {
984984
}
985985
}
986986

987+
// ranges
988+
if (options.ranges) {
989+
for (var i=0; i < options.ranges.length; i++) {
990+
var range = options.ranges[i];
991+
if (!range.value) {
992+
reject("Please set ranges["+i+"].value");
993+
return;
994+
}
995+
if (range.type === firebase.QueryRangeType.START_AT) {
996+
query = query.queryStartingAtValue(range.value);
997+
} else if (range.type === firebase.QueryRangeType.END_AT) {
998+
query = query.queryEndingAtValue(range.value);
999+
} else if (range.type === firebase.QueryRangeType.EQUAL_TO) {
1000+
query = query.queryEqualToValue(range.value);
1001+
} else {
1002+
reject("Invalid ranges["+i+"].type, use constants like firebase.QueryRangeType.START_AT");
1003+
return;
1004+
}
1005+
}
1006+
}
1007+
9871008
// limit
9881009
if (options.limit && options.limit.type) {
9891010
if (!options.limit.value) {

0 commit comments

Comments
 (0)