Skip to content

Compat class for Query and CollectionReference #4065

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 4 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import { DocumentSnapshot, QuerySnapshot } from './snapshot';
import {
applyFirestoreDataConverter,
ensureFirestoreConfigured,
SnapshotMetadata,
validateHasExplicitOrderByForLimitToLast
SnapshotMetadata
} from '../../../src/api/database';
import { ViewSnapshot } from '../../../src/core/view_snapshot';
import {
Expand All @@ -39,7 +38,8 @@ import {
newUserDataReader,
Query,
SetOptions,
UpdateData
UpdateData,
validateHasExplicitOrderByForLimitToLast
} from '../../../lite/src/api/reference';
import { Document } from '../../../src/model/document';
import {
Expand Down Expand Up @@ -87,7 +87,20 @@ import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
export {
DocumentReference,
CollectionReference,
Query
Query,
collection,
collectionGroup,
doc,
query,
where,
limit,
limitToLast,
orderBy,
startAt,
startAfter,
endAt,
endBefore,
queryEqual
} from '../../../lite/src/api/reference';

/**
Expand Down
204 changes: 2 additions & 202 deletions packages/firestore/exp/test/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,7 @@
import * as legacy from '@firebase/firestore-types';
import * as exp from '../index';

import {
addDoc,
doc,
FieldPath as FieldPathExp,
getDocs,
getDocsFromCache,
getDocsFromServer,
onSnapshot,
query,
queryEqual,
refEqual,
endAt,
endBefore,
startAfter,
startAt,
limitToLast,
limit,
orderBy,
where,
Bytes as BytesExp
} from '../../exp/index';
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
import { FieldPath as FieldPathExp, Bytes as BytesExp } from '../../exp/index';
import {
isPlainObject,
validateSetOptions
Expand All @@ -48,10 +27,7 @@ import { Compat } from '../../src/compat/compat';
import {
Firestore,
DocumentReference,
DocumentSnapshot,
QuerySnapshot,
wrapObserver,
extractSnapshotOptions
DocumentSnapshot
} from '../../src/api/database';

export { GeoPoint, Timestamp } from '../index';
Expand Down Expand Up @@ -185,182 +161,6 @@ export class WriteBatch
}
}

export class Query<T = legacy.DocumentData>
extends Compat<exp.Query<T>>
implements legacy.Query<T> {
constructor(readonly firestore: Firestore, delegate: exp.Query<T>) {
super(delegate);
}

where(
fieldPath: string | FieldPath,
opStr: legacy.WhereFilterOp,
value: any
): Query<T> {
return new Query<T>(
this.firestore,
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
);
}

orderBy(
fieldPath: string | FieldPath,
directionStr?: legacy.OrderByDirection
): Query<T> {
return new Query<T>(
this.firestore,
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
);
}

limit(n: number): Query<T> {
return new Query<T>(this.firestore, query(this._delegate, limit(n)));
}

limitToLast(n: number): Query<T> {
return new Query<T>(this.firestore, query(this._delegate, limitToLast(n)));
}

startAt(...args: any[]): Query<T> {
return new Query(
this.firestore,
query(this._delegate, startAt(...unwrap(args)))
);
}

startAfter(...args: any[]): Query<T> {
return new Query(
this.firestore,
query(this._delegate, startAfter(...unwrap(args)))
);
}

endBefore(...args: any[]): Query<T> {
return new Query(
this.firestore,
query(this._delegate, endBefore(...unwrap(args)))
);
}

endAt(...args: any[]): Query<T> {
return new Query(
this.firestore,
query(this._delegate, endAt(...unwrap(args)))
);
}

isEqual(other: legacy.Query<T>): boolean {
return queryEqual(this._delegate, (other as Query<T>)._delegate);
}

get(options?: legacy.GetOptions): Promise<QuerySnapshot<T>> {
let query: Promise<exp.QuerySnapshot<T>>;
if (options?.source === 'cache') {
query = getDocsFromCache(this._delegate);
} else if (options?.source === 'server') {
query = getDocsFromServer(this._delegate);
} else {
query = getDocs(this._delegate);
}
return query.then(result => new QuerySnapshot(this.firestore, result));
}

onSnapshot(observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: legacy.FirestoreError) => void;
complete?: () => void;
}): () => void;
onSnapshot(
options: legacy.SnapshotListenOptions,
observer: {
next?: (snapshot: QuerySnapshot<T>) => void;
error?: (error: legacy.FirestoreError) => void;
complete?: () => void;
}
): () => void;
onSnapshot(
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: legacy.FirestoreError) => void,
onCompletion?: () => void
): () => void;
onSnapshot(
options: legacy.SnapshotListenOptions,
onNext: (snapshot: QuerySnapshot<T>) => void,
onError?: (error: legacy.FirestoreError) => void,
onCompletion?: () => void
): () => void;
onSnapshot(...args: any): () => void {
const options = extractSnapshotOptions(args);
const observer = wrapObserver<QuerySnapshot<T>, exp.QuerySnapshot<T>>(
args,
snap => new QuerySnapshot(this.firestore, snap)
);
return onSnapshot(this._delegate, options, observer);
}

withConverter<U>(converter: legacy.FirestoreDataConverter<U>): Query<U> {
return new Query<U>(
this.firestore,
this._delegate.withConverter(
converter as UntypedFirestoreDataConverter<U>
)
);
}
}

export class CollectionReference<T = legacy.DocumentData>
extends Query<T>
implements legacy.CollectionReference<T> {
constructor(
readonly firestore: Firestore,
readonly _delegate: exp.CollectionReference<T>
) {
super(firestore, _delegate);
}

readonly id = this._delegate.id;
readonly path = this._delegate.path;

get parent(): DocumentReference<legacy.DocumentData> | null {
const docRef = this._delegate.parent;
return docRef
? new DocumentReference<legacy.DocumentData>(this.firestore, docRef)
: null;
}

doc(documentPath?: string): DocumentReference<T> {
if (documentPath !== undefined) {
return new DocumentReference(
this.firestore,
doc(this._delegate, documentPath)
);
} else {
return new DocumentReference(this.firestore, doc(this._delegate));
}
}

add(data: T): Promise<DocumentReference<T>> {
return addDoc(this._delegate, unwrap(data)).then(
docRef => new DocumentReference(this.firestore, docRef)
);
}

isEqual(other: CollectionReference<T>): boolean {
return refEqual(this._delegate, other._delegate);
}

withConverter<U>(
converter: legacy.FirestoreDataConverter<U>
): CollectionReference<U> {
return new CollectionReference<U>(
this.firestore,
this._delegate.withConverter(
converter as UntypedFirestoreDataConverter<U>
)
);
}
}

export class FieldPath
extends Compat<FieldPathExp>
implements legacy.FieldPath {
Expand Down
Loading