Skip to content

Commit 393d5a5

Browse files
committed
Fix startAfter/endBefore for orderByKeys queries
1 parent 4986a3e commit 393d5a5

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

packages/database/src/api/Query.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,15 @@ export class Query {
101101
'or equalTo() must be a string.';
102102
if (params.hasStart()) {
103103
const startName = params.getIndexStartName();
104-
if (
105-
startName !== MIN_NAME &&
106-
!(params.hasStartAfter() && startName === MAX_NAME)
107-
) {
104+
if (startName !== MIN_NAME) {
108105
throw new Error(tooManyArgsError);
109106
} else if (typeof startNode !== 'string') {
110107
throw new Error(wrongArgTypeError);
111108
}
112109
}
113110
if (params.hasEnd()) {
114111
const endName = params.getIndexEndName();
115-
if (
116-
endName !== MAX_NAME &&
117-
!(params.hasEndBefore() && endName === MIN_NAME)
118-
) {
112+
if (endName !== MAX_NAME) {
119113
throw new Error(tooManyArgsError);
120114
} else if (typeof endNode !== 'string') {
121115
throw new Error(wrongArgTypeError);

packages/database/src/core/view/QueryParams.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,21 @@ export class QueryParams {
289289
}
290290

291291
startAfter(indexValue: unknown, key?: string | null): QueryParams {
292-
let childKey: string;
293-
if (key == null) {
294-
childKey = MAX_NAME;
292+
let params: QueryParams;
293+
if (this.index_ === KEY_INDEX) {
294+
if (typeof indexValue === 'string') {
295+
indexValue = successor(indexValue as string);
296+
}
297+
params = this.startAt(indexValue, key);
295298
} else {
296-
childKey = successor(key);
299+
let childKey: string;
300+
if (key == null) {
301+
childKey = MAX_NAME;
302+
} else {
303+
childKey = successor(key);
304+
}
305+
params = this.startAt(indexValue, childKey);
297306
}
298-
const params: QueryParams = this.startAt(indexValue, childKey);
299307
params.startAfterSet_ = true;
300308
return params;
301309
}
@@ -324,12 +332,20 @@ export class QueryParams {
324332

325333
endBefore(indexValue: unknown, key?: string | null): QueryParams {
326334
let childKey: string;
327-
if (key == null) {
328-
childKey = MIN_NAME;
335+
let params: QueryParams;
336+
if (this.index_ === KEY_INDEX) {
337+
if (typeof indexValue === 'string') {
338+
indexValue = predecessor(indexValue as string);
339+
}
340+
params = this.endAt(indexValue, key);
329341
} else {
330-
childKey = predecessor(key);
342+
if (key == null) {
343+
childKey = MIN_NAME;
344+
} else {
345+
childKey = predecessor(key);
346+
}
347+
params = this.endAt(indexValue, childKey);
331348
}
332-
const params: QueryParams = this.endAt(indexValue, childKey);
333349
params.endBeforeSet_ = true;
334350
return params;
335351
}

packages/database/test/query.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,38 @@ describe('Query Tests', () => {
13771377
expect(removed).to.equal('b ');
13781378
});
13791379

1380+
it('Ensure startAfter on key index works', async () => {
1381+
const node = getRandomNode() as Reference;
1382+
const keys = [];
1383+
const values = [];
1384+
for (var i = 0; i < 10; i++) {
1385+
const ref = node.push();
1386+
keys.push(ref.key);
1387+
const value = { index: i };
1388+
values.push(value);
1389+
await ref.set(value);
1390+
}
1391+
const snap = await node.orderByKey().startAfter(keys[0]).get();
1392+
expect(Object.keys(snap.val())).to.deep.equal(keys.slice(1));
1393+
expect(Object.values(snap.val())).to.deep.equal(values.slice(1));
1394+
});
1395+
1396+
it('Ensure endBefore on key index works', async () => {
1397+
const node = getRandomNode() as Reference;
1398+
const keys = [];
1399+
const values = [];
1400+
for (var i = 0; i < 10; i++) {
1401+
const ref = node.push();
1402+
keys.push(ref.key);
1403+
const value = { index: i };
1404+
values.push(value);
1405+
await ref.set(value);
1406+
}
1407+
const snap = await node.orderByKey().endBefore(keys[9]).get();
1408+
expect(Object.keys(snap.val())).to.deep.equal(keys.slice(0, 9));
1409+
expect(Object.values(snap.val())).to.deep.equal(values.slice(0, 9));
1410+
});
1411+
13801412
it('Ensure startAt / endAt with priority works.', async () => {
13811413
const node = getRandomNode() as Reference;
13821414

0 commit comments

Comments
 (0)