Skip to content

Commit c469b11

Browse files
cartantdavideast
authored andcommitted
feat(database): support the optional startAt key (#821)
1 parent 5c5ff7b commit c469b11

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

Diff for: docs/4-querying-lists.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ const queryObservable = af.database.list('/items', {
2828
| `equalTo` | Limit list to items that contain certain value. |
2929
| `limitToFirst` | Sets the maximum number of items to return from the beginning of the ordered list of results. |
3030
| `limitToLast` | Sets the maximum number of items to return from the end of the ordered list of results. |
31-
| `startAt` | Return items greater than or equal to the specified key or value, depending on the order-by method chosen. |
31+
| `startAt` <sup>1</sup> | Return items greater than or equal to the specified key or value, depending on the order-by method chosen. |
3232
| `endAt` | Return items less than or equal to the specified key or value, depending on the order-by method chosen. |
3333

34+
<sup>1</sup> The Firebase SDK supports [an optional `key` parameter](https://firebase.google.com/docs/reference/js/firebase.database.Reference#startAt) when ordering by child, value, or priority. You can specify the `key` parameter using `startAt: { value: 'some-value', key: 'some-key' }`
35+
3436
## Invalid query combinations
3537

3638
**Queries can only be ordered by one method.** This means you can only specify

Diff for: src/database/firebase_list_factory.spec.ts

+49-9
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,15 @@ describe('FirebaseListFactory', () => {
418418

419419

420420
it('should emit a new value when a child moves', (done: any) => {
421-
let question = skipAndTake(questions, 1, 2)
422-
subscription = _do.call(question, (data: any) => {
423-
expect(data.length).toBe(2);
424-
expect(data[0].push2).toBe(true);
425-
expect(data[1].push1).toBe(true);
426-
})
427-
.subscribe(() => {
428-
done();
429-
}, done.fail);
421+
let question = skipAndTake(questions, 1, 2)
422+
subscription = _do.call(question, (data: any) => {
423+
expect(data.length).toBe(2);
424+
expect(data[0].push2).toBe(true);
425+
expect(data[1].push1).toBe(true);
426+
})
427+
.subscribe(() => {
428+
done();
429+
}, done.fail);
430430

431431
var child1 = ref.push({ push1: true }, () => {
432432
ref.push({ push2: true }, () => {
@@ -718,6 +718,46 @@ describe('FirebaseListFactory', () => {
718718
keyToRemove = lastKey;
719719
});
720720
});
721+
722+
describe('startAt(value, key)', () => {
723+
724+
it('should support the optional key parameter to startAt', (done) => {
725+
726+
questions.$ref.ref.set({
727+
val1: Object.assign({}, val1, { data: 0 }),
728+
val2: Object.assign({}, val2, { data: 0 }),
729+
val3: Object.assign({}, val3, { data: 0 })
730+
})
731+
.then(() => {
732+
733+
let query1 = FirebaseListFactory(`${rootDatabaseUrl}/questions`, {
734+
query: {
735+
orderByChild: 'data',
736+
startAt: { value: 0 }
737+
}
738+
});
739+
query1 = take.call(query1, 1);
740+
query1 = toPromise.call(query1);
741+
742+
let query2 = FirebaseListFactory(`${rootDatabaseUrl}/questions`, {
743+
query: {
744+
orderByChild: 'data',
745+
startAt: { value: 0, key: 'val2' }
746+
}
747+
});
748+
query2 = take.call(query2, 1);
749+
query2 = toPromise.call(query2);
750+
751+
Promise.all([query1, query2]).then(([list1, list2]) => {
752+
expect(list1.map(i => i.$key)).toEqual(['val1', 'val2', 'val3']);
753+
expect(list2.map(i => i.$key)).toEqual(['val2', 'val3']);
754+
done();
755+
});
756+
})
757+
.catch(done.fail);
758+
});
759+
760+
});
721761
});
722762
});
723763

Diff for: src/database/firebase_list_factory.ts

+4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ export function FirebaseListFactory (
7070

7171
// check startAt
7272
if (utils.hasKey(query, "startAt")) {
73+
if (utils.hasKey(query.startAt, "value")) {
74+
queried = queried.startAt(query.startAt.value, query.startAt.key);
75+
} else {
7376
queried = queried.startAt(query.startAt);
77+
}
7478
}
7579

7680
if (utils.hasKey(query, "endAt")) {

0 commit comments

Comments
 (0)