Skip to content

Commit 7006e28

Browse files
committed
refactor QueryParams
1 parent 6b41c7a commit 7006e28

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

packages/database/src/api/Query.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ import {
4949
} from '../core/view/EventRegistration';
5050

5151
import { Repo } from '../core/Repo';
52-
import { QueryParams } from '../core/view/QueryParams';
52+
import {
53+
QueryParams,
54+
limitToFirst,
55+
limitToLast,
56+
startAfter,
57+
startAt,
58+
endAt,
59+
endBefore,
60+
getQueryObject,
61+
orderBy
62+
} from '../core/view/QueryParams';
5363
import { Reference } from './Reference';
5464
import { DataSnapshot } from './DataSnapshot';
5565

@@ -355,7 +365,7 @@ export class Query {
355365
return new Query(
356366
this.repo,
357367
this.path,
358-
this.queryParams_.limitToFirst(limit),
368+
limitToFirst(this.queryParams_, limit),
359369
this.orderByCalled_
360370
);
361371
}
@@ -384,7 +394,7 @@ export class Query {
384394
return new Query(
385395
this.repo,
386396
this.path,
387-
this.queryParams_.limitToLast(limit),
397+
limitToLast(this.queryParams_, limit),
388398
this.orderByCalled_
389399
);
390400
}
@@ -416,7 +426,7 @@ export class Query {
416426
);
417427
}
418428
const index = new PathIndex(parsedPath);
419-
const newParams = this.queryParams_.orderBy(index);
429+
const newParams = orderBy(this.queryParams_, index);
420430
Query.validateQueryEndpoints_(newParams);
421431

422432
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/ true);
@@ -428,7 +438,7 @@ export class Query {
428438
orderByKey(): Query {
429439
validateArgCount('Query.orderByKey', 0, 0, arguments.length);
430440
this.validateNoPreviousOrderByCall_('Query.orderByKey');
431-
const newParams = this.queryParams_.orderBy(KEY_INDEX);
441+
const newParams = orderBy(this.queryParams_, KEY_INDEX);
432442
Query.validateQueryEndpoints_(newParams);
433443
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/ true);
434444
}
@@ -439,7 +449,7 @@ export class Query {
439449
orderByPriority(): Query {
440450
validateArgCount('Query.orderByPriority', 0, 0, arguments.length);
441451
this.validateNoPreviousOrderByCall_('Query.orderByPriority');
442-
const newParams = this.queryParams_.orderBy(PRIORITY_INDEX);
452+
const newParams = orderBy(this.queryParams_, PRIORITY_INDEX);
443453
Query.validateQueryEndpoints_(newParams);
444454
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/ true);
445455
}
@@ -450,7 +460,7 @@ export class Query {
450460
orderByValue(): Query {
451461
validateArgCount('Query.orderByValue', 0, 0, arguments.length);
452462
this.validateNoPreviousOrderByCall_('Query.orderByValue');
453-
const newParams = this.queryParams_.orderBy(VALUE_INDEX);
463+
const newParams = orderBy(this.queryParams_, VALUE_INDEX);
454464
Query.validateQueryEndpoints_(newParams);
455465
return new Query(this.repo, this.path, newParams, /*orderByCalled=*/ true);
456466
}
@@ -463,7 +473,7 @@ export class Query {
463473
validateFirebaseDataArg('Query.startAt', 1, value, this.path, true);
464474
validateKey('Query.startAt', 2, name, true);
465475

466-
const newParams = this.queryParams_.startAt(value, name);
476+
const newParams = startAt(this.queryParams_, value, name);
467477
Query.validateLimit_(newParams);
468478
Query.validateQueryEndpoints_(newParams);
469479
if (this.queryParams_.hasStart()) {
@@ -490,7 +500,7 @@ export class Query {
490500
validateFirebaseDataArg('Query.startAfter', 1, value, this.path, false);
491501
validateKey('Query.startAfter', 2, name, true);
492502

493-
const newParams = this.queryParams_.startAfter(value, name);
503+
const newParams = startAfter(this.queryParams_, value, name);
494504
Query.validateLimit_(newParams);
495505
Query.validateQueryEndpoints_(newParams);
496506
if (this.queryParams_.hasStart()) {
@@ -511,7 +521,7 @@ export class Query {
511521
validateFirebaseDataArg('Query.endAt', 1, value, this.path, true);
512522
validateKey('Query.endAt', 2, name, true);
513523

514-
const newParams = this.queryParams_.endAt(value, name);
524+
const newParams = endAt(this.queryParams_, value, name);
515525
Query.validateLimit_(newParams);
516526
Query.validateQueryEndpoints_(newParams);
517527
if (this.queryParams_.hasEnd()) {
@@ -532,7 +542,7 @@ export class Query {
532542
validateFirebaseDataArg('Query.endBefore', 1, value, this.path, false);
533543
validateKey('Query.endBefore', 2, name, true);
534544

535-
const newParams = this.queryParams_.endBefore(value, name);
545+
const newParams = endBefore(this.queryParams_, value, name);
536546
Query.validateLimit_(newParams);
537547
Query.validateQueryEndpoints_(newParams);
538548
if (this.queryParams_.hasEnd()) {
@@ -589,7 +599,7 @@ export class Query {
589599
* An object representation of the query parameters used by this Query.
590600
*/
591601
queryObject(): object {
592-
return this.queryParams_.getQueryObject();
602+
return getQueryObject(this.queryParams_);
593603
}
594604

595605
queryIdentifier(): string {

packages/database/src/core/ReadonlyRestClient.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { ServerActions } from './ServerActions';
2828
import { RepoInfo } from './RepoInfo';
2929
import { AuthTokenProvider } from './AuthTokenProvider';
3030
import { Query } from '../api/Query';
31+
import { toRestQueryStringParameters } from './view/QueryParams';
3132

3233
/**
3334
* An implementation of ServerActions that communicates with the server via REST requests.
@@ -94,9 +95,9 @@ export class ReadonlyRestClient extends ServerActions {
9495
const thisListen = {};
9596
this.listens_[listenId] = thisListen;
9697

97-
const queryStringParameters = query
98-
.getQueryParams()
99-
.toRestQueryStringParameters();
98+
const queryStringParameters = toRestQueryStringParameters(
99+
query.getQueryParams()
100+
);
100101

101102
this.restRequest_(
102103
pathString + '.json',
@@ -136,9 +137,9 @@ export class ReadonlyRestClient extends ServerActions {
136137
}
137138

138139
get(query: Query): Promise<string> {
139-
const queryStringParameters = query
140-
.getQueryParams()
141-
.toRestQueryStringParameters();
140+
const queryStringParameters = toRestQueryStringParameters(
141+
query.getQueryParams()
142+
);
142143

143144
const pathString = query.path.toString();
144145

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class QueryParams {
203203
}
204204
}
205205

206-
function getNodeFilter(queryParams: QueryParams): NodeFilter {
206+
export function getNodeFilter(queryParams: QueryParams): NodeFilter {
207207
if (queryParams.loadsAllData()) {
208208
return new IndexedFilter(queryParams.getIndex());
209209
} else if (queryParams.hasLimit()) {
@@ -333,7 +333,7 @@ export function endBefore(
333333
return params;
334334
}
335335

336-
function orderBy(queryParams: QueryParams, index: Index): QueryParams {
336+
export function orderBy(queryParams: QueryParams, index: Index): QueryParams {
337337
const newParams = queryParams.copy();
338338
newParams.index_ = index;
339339
return newParams;
@@ -349,7 +349,7 @@ export function toRestQueryStringParameters(
349349
): Record<string, string | number> {
350350
const qs: Record<string, string | number> = {};
351351

352-
if (this.isDefault()) {
352+
if (queryParams.isDefault()) {
353353
return qs;
354354
}
355355

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Node } from '../snap/Node';
3434
import { Path, pathGetFront, pathIsEmpty } from '../util/Path';
3535
import { WriteTreeRef } from '../WriteTree';
3636
import { CancelEvent, Event } from './Event';
37+
import { getNodeFilter } from './QueryParams';
3738

3839
/**
3940
* A view represents a specific location and query that has 1 or more event registrations.
@@ -54,7 +55,7 @@ export class View {
5455
const params = this.query_.getQueryParams();
5556

5657
const indexFilter = new IndexedFilter(params.getIndex());
57-
const filter = params.getNodeFilter();
58+
const filter = getNodeFilter(params);
5859

5960
this.processor_ = new ViewProcessor(filter);
6061

0 commit comments

Comments
 (0)