Skip to content

Commit c9a2df8

Browse files
committed
feat(ts): genericize AV.Query
close #548
1 parent d08107d commit c9a2df8

File tree

1 file changed

+73
-53
lines changed

1 file changed

+73
-53
lines changed

storage.d.ts

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ export class Relation extends BaseObject {
222222
parentClass: string,
223223
relationKey: string,
224224
child: Object
225-
): Query;
225+
): Query<any>;
226226

227227
//Adds a AV.Object or an array of AV.Objects to the relation.
228228
add(object: Object): void;
229229

230230
// Returns a AV.Query that is limited to objects in this relation.
231-
query(): Query;
231+
query(): Query<any>;
232232

233233
// Removes a AV.Object or an array of AV.Objects from this relation.
234234
remove(object: Object): void;
@@ -268,7 +268,7 @@ export class Object extends BaseObject {
268268
attributes: any;
269269
changed: boolean;
270270
className: string;
271-
query: Query;
271+
query: Query<this>;
272272

273273
constructor(className?: string, options?: any);
274274
constructor(attributes?: string[], options?: any);
@@ -311,8 +311,15 @@ export class Object extends BaseObject {
311311
previousAttributes(): any;
312312
relation(attr: string): Relation;
313313
remove(attr: string, item: any): this;
314-
save(attrs?: object | null, options?: Object.SaveOptions): Promise<this>;
315-
save(key: string, value: any, options?: Object.SaveOptions): Promise<this>;
314+
save(
315+
attrs?: object | null,
316+
options?: Object.SaveOptions<this>
317+
): Promise<this>;
318+
save(
319+
key: string,
320+
value: any,
321+
options?: Object.SaveOptions<this>
322+
): Promise<this>;
316323
set(key: string, value: any, options?: Object.SetOptions): this;
317324
setACL(acl: ACL, options?: Object.SetOptions): this;
318325
unset(attr: string, options?: Object.SetOptions): this;
@@ -325,9 +332,12 @@ export namespace Object {
325332

326333
interface DestroyAllOptions extends AuthOptions {}
327334

328-
interface SaveOptions extends AuthOptions, SilentOption, WaitOption {
335+
interface SaveOptions<T extends Queriable>
336+
extends AuthOptions,
337+
SilentOption,
338+
WaitOption {
329339
fetchWhenSave?: boolean;
330-
where?: Query;
340+
where?: Query<T>;
331341
}
332342

333343
interface SaveAllOptions extends AuthOptions {}
@@ -390,11 +400,13 @@ export class Events {
390400
unbind(eventName?: string, callback?: Function, context?: any): Events;
391401
}
392402

393-
declare class BaseQuery extends BaseObject {
403+
declare type Queriable = Object | File;
404+
405+
declare class BaseQuery<T extends Queriable> extends BaseObject {
394406
className: string;
395407

408+
constructor(objectClass: new (...any) => T);
396409
constructor(objectClass: string);
397-
constructor(objectClass: Object);
398410

399411
addAscending(key: string): this;
400412
addAscending(key: string[]): this;
@@ -407,7 +419,7 @@ declare class BaseQuery extends BaseObject {
407419
limit(n: number): this;
408420
skip(n: number): this;
409421

410-
find(options?: AuthOptions): Promise<Object[]>;
422+
find(options?: AuthOptions): Promise<T[]>;
411423
}
412424

413425
/**
@@ -466,49 +478,57 @@ declare class BaseQuery extends BaseObject {
466478
* }
467479
* });</pre></p>
468480
*/
469-
export class Query extends BaseQuery {
470-
static or(...querys: Query[]): Query;
471-
static and(...querys: Query[]): Query;
472-
static doCloudQuery<T>(
481+
export class Query<T extends Queriable> extends BaseQuery<T> {
482+
static or<U extends Queriable>(...querys: Query<U>[]): U;
483+
static and<U extends Queriable>(...querys: Query<U>[]): U;
484+
static doCloudQuery<U extends Queriable>(
473485
cql: string,
474486
pvalues?: any,
475487
options?: AuthOptions
476-
): Promise<T>;
477-
478-
containedIn(key: string, values: any[]): Query;
479-
contains(key: string, substring: string): Query;
480-
containsAll(key: string, values: any[]): Query;
481-
count<T>(options?: AuthOptions): Promise<T>;
482-
descending(key: string): Query;
483-
descending(key: string[]): Query;
484-
doesNotExist(key: string): Query;
485-
doesNotMatchKeyInQuery(key: string, queryKey: string, query: Query): Query;
486-
doesNotMatchQuery(key: string, query: Query): Query;
487-
each<T>(callback: Function, options?: AuthOptions): Promise<T>;
488-
endsWith(key: string, suffix: string): Query;
489-
equalTo(key: string, value: any): Query;
490-
exists(key: string): Query;
491-
first<T>(options?: AuthOptions): Promise<T>;
492-
get<T>(objectId: string, options?: AuthOptions): Promise<T>;
493-
greaterThan(key: string, value: any): Query;
494-
greaterThanOrEqualTo(key: string, value: any): Query;
495-
includeACL(value?: boolean): Query;
496-
lessThan(key: string, value: any): Query;
497-
lessThanOrEqualTo(key: string, value: any): Query;
498-
matches(key: string, regex: RegExp, modifiers?: any): Query;
499-
matchesKeyInQuery(key: string, queryKey: string, query: Query): Query;
500-
matchesQuery(key: string, query: Query): Query;
501-
near(key: string, point: GeoPoint): Query;
502-
notContainedIn(key: string, values: any[]): Query;
503-
notEqualTo(key: string, value: any): Query;
504-
select(...keys: string[]): Query;
505-
select(keys: string[]): Query;
506-
startsWith(key: string, prefix: string): Query;
507-
withinGeoBox(key: string, southwest: GeoPoint, northeast: GeoPoint): Query;
508-
withinKilometers(key: string, point: GeoPoint, maxDistance: number): Query;
509-
withinMiles(key: string, point: GeoPoint, maxDistance: number): Query;
510-
withinRadians(key: string, point: GeoPoint, maxDistance: number): Query;
511-
scan<T>(
488+
): Promise<U>;
489+
490+
containedIn(key: string, values: any[]): this;
491+
contains(key: string, substring: string): this;
492+
containsAll(key: string, values: any[]): this;
493+
count(options?: AuthOptions): Promise<T>;
494+
descending(key: string): this;
495+
descending(key: string[]): this;
496+
doesNotExist(key: string): this;
497+
doesNotMatchKeyInQuery<U extends Queriable>(
498+
key: string,
499+
queryKey: string,
500+
query: Query<U>
501+
): this;
502+
doesNotMatchQuery<U extends Queriable>(key: string, query: Query<U>): this;
503+
each(callback: Function, options?: AuthOptions): Promise<T>;
504+
endsWith(key: string, suffix: string): this;
505+
equalTo(key: string, value: any): this;
506+
exists(key: string): this;
507+
first(options?: AuthOptions): Promise<T>;
508+
get(objectId: string, options?: AuthOptions): Promise<T>;
509+
greaterThan(key: string, value: any): this;
510+
greaterThanOrEqualTo(key: string, value: any): this;
511+
includeACL(value?: boolean): this;
512+
lessThan(key: string, value: any): this;
513+
lessThanOrEqualTo(key: string, value: any): this;
514+
matches(key: string, regex: RegExp, modifiers?: any): this;
515+
matchesKeyInQuery<U extends Queriable>(
516+
key: string,
517+
queryKey: string,
518+
query: Query<U>
519+
): this;
520+
matchesQuery<U extends Queriable>(key: string, query: Query<U>): this;
521+
near(key: string, point: GeoPoint): this;
522+
notContainedIn(key: string, values: any[]): this;
523+
notEqualTo(key: string, value: any): this;
524+
select(...keys: string[]): this;
525+
select(keys: string[]): this;
526+
startsWith(key: string, prefix: string): this;
527+
withinGeoBox(key: string, southwest: GeoPoint, northeast: GeoPoint): this;
528+
withinKilometers(key: string, point: GeoPoint, maxDistance: number): this;
529+
withinMiles(key: string, point: GeoPoint, maxDistance: number): this;
530+
withinRadians(key: string, point: GeoPoint, maxDistance: number): this;
531+
scan(
512532
options?: { orderedBy?: string; batchSize?: number },
513533
authOptions?: AuthOptions
514534
): AsyncIterator<T>;
@@ -519,9 +539,9 @@ declare class LiveQuery extends EventEmitter {
519539
unsubscribe(): Promise<void>;
520540
}
521541

522-
declare class FriendShipQuery extends Query {}
542+
declare class FriendShipQuery extends Query<User> {}
523543

524-
export class SearchQuery extends BaseQuery {
544+
export class SearchQuery<T extends Queriable> extends BaseQuery<T> {
525545
sid(sid: string): this;
526546
queryString(q: string): this;
527547
highlights(highlights: string[]): this;
@@ -997,7 +1017,7 @@ export namespace Push {
9971017
push_time?: Date;
9981018
expiration_time?: Date;
9991019
expiration_interval?: number;
1000-
where?: Query;
1020+
where?: Query<Installation>;
10011021
cql?: string;
10021022
data?: any;
10031023
alert?: string;

0 commit comments

Comments
 (0)