Skip to content

Commit 7d7eff6

Browse files
authored
Merge 2c1dc38 into 4986a3e
2 parents 4986a3e + 2c1dc38 commit 7d7eff6

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,28 @@ 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 childOne = node.push();
1383+
const childTwo = node.push();
1384+
await childOne.set(1);
1385+
await childTwo.set(2);
1386+
const snap = await node.orderByKey().startAfter(childOne.key).get();
1387+
expect(Object.keys(snap.val())).to.deep.equal([childTwo.key]);
1388+
expect(Object.values(snap.val())).to.deep.equal([snap.val()[childTwo.key]]);
1389+
});
1390+
1391+
it('Ensure endBefore on key index works', async () => {
1392+
const node = getRandomNode() as Reference;
1393+
const childOne = node.push();
1394+
const childTwo = node.push();
1395+
await childOne.set(1);
1396+
await childTwo.set(2);
1397+
const snap = await node.orderByKey().endBefore(childTwo.key).get();
1398+
expect(Object.keys(snap.val())).to.deep.equal([childOne.key]);
1399+
expect(Object.values(snap.val())).to.deep.equal([snap.val()[childOne.key]]);
1400+
});
1401+
13801402
it('Ensure startAt / endAt with priority works.', async () => {
13811403
const node = getRandomNode() as Reference;
13821404

0 commit comments

Comments
 (0)