Skip to content

feat(database): support the optional startAt key #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/4-querying-lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ const queryObservable = af.database.list('/items', {
| `equalTo` | Limit list to items that contain certain value. |
| `limitToFirst` | Sets the maximum number of items to return from the beginning of the ordered list of results. |
| `limitToLast` | Sets the maximum number of items to return from the end of the ordered list of results. |
| `startAt` | Return items greater than or equal to the specified key or value, depending on the order-by method chosen. |
| `startAt` <sup>1</sup> | Return items greater than or equal to the specified key or value, depending on the order-by method chosen. |
| `endAt` | Return items less than or equal to the specified key or value, depending on the order-by method chosen. |

<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' }`

## Invalid query combinations

**Queries can only be ordered by one method.** This means you can only specify
Expand Down
58 changes: 49 additions & 9 deletions src/database/firebase_list_factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,15 @@ describe('FirebaseListFactory', () => {


it('should emit a new value when a child moves', (done: any) => {
let question = skipAndTake(questions, 1, 2)
subscription = _do.call(question, (data: any) => {
expect(data.length).toBe(2);
expect(data[0].push2).toBe(true);
expect(data[1].push1).toBe(true);
})
.subscribe(() => {
done();
}, done.fail);
let question = skipAndTake(questions, 1, 2)
subscription = _do.call(question, (data: any) => {
expect(data.length).toBe(2);
expect(data[0].push2).toBe(true);
expect(data[1].push1).toBe(true);
})
.subscribe(() => {
done();
}, done.fail);

var child1 = ref.push({ push1: true }, () => {
ref.push({ push2: true }, () => {
Expand Down Expand Up @@ -718,6 +718,46 @@ describe('FirebaseListFactory', () => {
keyToRemove = lastKey;
});
});

describe('startAt(value, key)', () => {

it('should support the optional key parameter to startAt', (done) => {

questions.$ref.ref.set({
val1: Object.assign({}, val1, { data: 0 }),
val2: Object.assign({}, val2, { data: 0 }),
val3: Object.assign({}, val3, { data: 0 })
})
.then(() => {

let query1 = FirebaseListFactory(`${rootDatabaseUrl}/questions`, {
query: {
orderByChild: 'data',
startAt: { value: 0 }
}
});
query1 = take.call(query1, 1);
query1 = toPromise.call(query1);

let query2 = FirebaseListFactory(`${rootDatabaseUrl}/questions`, {
query: {
orderByChild: 'data',
startAt: { value: 0, key: 'val2' }
}
});
query2 = take.call(query2, 1);
query2 = toPromise.call(query2);

Promise.all([query1, query2]).then(([list1, list2]) => {
expect(list1.map(i => i.$key)).toEqual(['val1', 'val2', 'val3']);
expect(list2.map(i => i.$key)).toEqual(['val2', 'val3']);
done();
});
})
.catch(done.fail);
});

});
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/database/firebase_list_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export function FirebaseListFactory (

// check startAt
if (utils.hasKey(query, "startAt")) {
if (utils.hasKey(query.startAt, "value")) {
queried = queried.startAt(query.startAt.value, query.startAt.key);
} else {
queried = queried.startAt(query.startAt);
}
}

if (utils.hasKey(query, "endAt")) {
Expand Down