Skip to content

Update REST and wire protocol query params for startAfter, endBefore #6706

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 9 commits into from
Nov 18, 2022
62 changes: 52 additions & 10 deletions packages/database-compat/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,34 +375,76 @@ describe('Query Tests', () => {
expect(queryId(path)).to.equal('default');

expect(queryId(path.startAt('pri', 'name'))).to.equal(
'{"sn":"name","sp":"pri"}'
'{"sin":true,"sn":"name","sp":"pri"}'
);
expect(queryId(path.startAfter('pri', 'name'))).to.equal(
'{"sn":"name-","sp":"pri"}'
'{"sin":false,"sn":"name","sp":"pri"}'
);
expect(queryId(path.endAt('pri', 'name'))).to.equal(
'{"ein":true,"en":"name","ep":"pri"}'
);
expect(queryId(path.endBefore('pri', 'name'))).to.equal(
'{"ein":false,"en":"name","ep":"pri"}'
);

expect(queryId(path.startAt('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAt('spri').endBefore('epri'))).to.equal(
'{"ein":false,"ep":"epri","sin":true,"sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endAt('epri'))).to.equal(
'{"ep":"epri","sn":"[MAX_NAME]","sp":"spri"}'
'{"ein":true,"ep":"epri","sin":false,"sp":"spri"}'
);
expect(queryId(path.startAfter('spri').endBefore('epri'))).to.equal(
'{"ein":false,"ep":"epri","sin":false,"sp":"spri"}'
);

expect(
queryId(path.startAt('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAt('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":true,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endAt('epri', 'ename'))
).to.equal('{"en":"ename","ep":"epri","sn":"sname-","sp":"spri"}');
).to.equal(
'{"ein":true,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);
expect(
queryId(path.startAfter('spri', 'sname').endBefore('epri', 'ename'))
).to.equal(
'{"ein":false,"en":"ename","ep":"epri","sin":false,"sn":"sname","sp":"spri"}'
);

expect(queryId(path.startAt('pri').limitToFirst(100))).to.equal(
'{"l":100,"sp":"pri","vf":"l"}'
'{"l":100,"sin":true,"sp":"pri","vf":"l"}'
);
expect(queryId(path.startAfter('pri').limitToFirst(100))).to.equal(
'{"l":100,"sn":"[MAX_NAME]","sp":"pri","vf":"l"}'
'{"l":100,"sin":false,"sp":"pri","vf":"l"}'
);
expect(queryId(path.endAt('pri').limitToLast(100))).to.equal(
'{"ein":true,"ep":"pri","l":100,"vf":"r"}'
);
expect(queryId(path.endBefore('pri').limitToLast(100))).to.equal(
'{"ein":false,"ep":"pri","l":100,"vf":"r"}'
);

expect(queryId(path.startAt('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sp":"bar"}'
'{"i":"foo","sin":true,"sp":"bar"}'
);
expect(queryId(path.startAfter('bar').orderByChild('foo'))).to.equal(
'{"i":"foo","sn":"[MAX_NAME]","sp":"bar"}'
'{"i":"foo","sin":false,"sp":"bar"}'
);
expect(queryId(path.endAt('bar').orderByChild('foo'))).to.equal(
'{"ein":true,"ep":"bar","i":"foo"}'
);
expect(queryId(path.endBefore('bar').orderByChild('foo'))).to.equal(
'{"ein":false,"ep":"bar","i":"foo"}'
);
});

Expand Down
59 changes: 22 additions & 37 deletions packages/database/src/core/view/QueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { KEY_INDEX } from '../snap/indexes/KeyIndex';
import { PathIndex } from '../snap/indexes/PathIndex';
import { PRIORITY_INDEX, PriorityIndex } from '../snap/indexes/PriorityIndex';
import { VALUE_INDEX } from '../snap/indexes/ValueIndex';
import { predecessor, successor } from '../util/NextPushId';
import { MAX_NAME, MIN_NAME } from '../util/util';

import { IndexedFilter } from './filter/IndexedFilter';
Expand All @@ -36,8 +35,10 @@ import { RangedFilter } from './filter/RangedFilter';
const enum WIRE_PROTOCOL_CONSTANTS {
INDEX_START_VALUE = 'sp',
INDEX_START_NAME = 'sn',
INDEX_START_IS_INCLUSIVE = 'sin',
INDEX_END_VALUE = 'ep',
INDEX_END_NAME = 'en',
INDEX_END_IS_INCLUSIVE = 'ein',
LIMIT = 'l',
VIEW_FROM = 'vf',
VIEW_FROM_LEFT = 'l',
Expand All @@ -53,8 +54,10 @@ const enum REST_QUERY_CONSTANTS {
PRIORITY_INDEX = '$priority',
VALUE_INDEX = '$value',
KEY_INDEX = '$key',
START_AFTER = 'startAfter',
START_AT = 'startAt',
END_AT = 'endAt',
END_BEFORE = 'endBefore',
LIMIT_TO_FIRST = 'limitToFirst',
LIMIT_TO_LAST = 'limitToLast'
}
Expand Down Expand Up @@ -191,10 +194,12 @@ export class QueryParams {
copy.limitSet_ = this.limitSet_;
copy.limit_ = this.limit_;
copy.startSet_ = this.startSet_;
copy.startAfterSet_ = this.startAfterSet_;
copy.indexStartValue_ = this.indexStartValue_;
copy.startNameSet_ = this.startNameSet_;
copy.indexStartName_ = this.indexStartName_;
copy.endSet_ = this.endSet_;
copy.endBeforeSet_ = this.endBeforeSet_;
copy.indexEndValue_ = this.indexEndValue_;
copy.endNameSet_ = this.endNameSet_;
copy.indexEndName_ = this.indexEndName_;
Expand Down Expand Up @@ -273,21 +278,7 @@ export function queryParamsStartAfter(
indexValue: unknown,
key?: string | null
): QueryParams {
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = successor(indexValue as string);
}
params = queryParamsStartAt(queryParams, indexValue, key);
} else {
let childKey: string;
if (key == null) {
childKey = MAX_NAME;
} else {
childKey = successor(key);
}
params = queryParamsStartAt(queryParams, indexValue, childKey);
}
const params: QueryParams = queryParamsStartAt(queryParams, indexValue, key);
params.startAfterSet_ = true;
return params;
}
Expand Down Expand Up @@ -318,21 +309,7 @@ export function queryParamsEndBefore(
indexValue: unknown,
key?: string | null
): QueryParams {
let childKey: string;
let params: QueryParams;
if (queryParams.index_ === KEY_INDEX) {
if (typeof indexValue === 'string') {
indexValue = predecessor(indexValue as string);
}
params = queryParamsEndAt(queryParams, indexValue, key);
} else {
if (key == null) {
childKey = MIN_NAME;
} else {
childKey = predecessor(key);
}
params = queryParamsEndAt(queryParams, indexValue, childKey);
}
const params: QueryParams = queryParamsEndAt(queryParams, indexValue, key);
params.endBeforeSet_ = true;
return params;
}
Expand Down Expand Up @@ -374,18 +351,22 @@ export function queryParamsToRestQueryStringParameters(
qs[REST_QUERY_CONSTANTS.ORDER_BY] = stringify(orderBy);

if (queryParams.startSet_) {
qs[REST_QUERY_CONSTANTS.START_AT] = stringify(queryParams.indexStartValue_);
const startParam = queryParams.startAfterSet_
? REST_QUERY_CONSTANTS.START_AFTER
: REST_QUERY_CONSTANTS.START_AT;
qs[startParam] = stringify(queryParams.indexStartValue_);
if (queryParams.startNameSet_) {
qs[REST_QUERY_CONSTANTS.START_AT] +=
',' + stringify(queryParams.indexStartName_);
qs[startParam] += ',' + stringify(queryParams.indexStartName_);
}
}

if (queryParams.endSet_) {
qs[REST_QUERY_CONSTANTS.END_AT] = stringify(queryParams.indexEndValue_);
const endParam = queryParams.endBeforeSet_
? REST_QUERY_CONSTANTS.END_BEFORE
: REST_QUERY_CONSTANTS.END_AT;
qs[endParam] = stringify(queryParams.indexEndValue_);
if (queryParams.endNameSet_) {
qs[REST_QUERY_CONSTANTS.END_AT] +=
',' + stringify(queryParams.indexEndName_);
qs[endParam] += ',' + stringify(queryParams.indexEndName_);
}
}

Expand All @@ -411,12 +392,16 @@ export function queryParamsGetQueryObject(
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_START_NAME] =
queryParams.indexStartName_;
}
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_START_IS_INCLUSIVE] =
!queryParams.startAfterSet_;
}
if (queryParams.endSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_VALUE] = queryParams.indexEndValue_;
if (queryParams.endNameSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_NAME] = queryParams.indexEndName_;
}
obj[WIRE_PROTOCOL_CONSTANTS.INDEX_END_IS_INCLUSIVE] =
!queryParams.endBeforeSet_;
}
if (queryParams.limitSet_) {
obj[WIRE_PROTOCOL_CONSTANTS.LIMIT] = queryParams.limit_;
Expand Down