Skip to content

Commit 6cd65fc

Browse files
Compat class for Query and CollectionReference (#4065)
1 parent 6c6c49a commit 6cd65fc

File tree

6 files changed

+660
-1021
lines changed

6 files changed

+660
-1021
lines changed

packages/firestore/exp/src/api/reference.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import { DocumentSnapshot, QuerySnapshot } from './snapshot';
2828
import {
2929
applyFirestoreDataConverter,
3030
ensureFirestoreConfigured,
31-
SnapshotMetadata,
32-
validateHasExplicitOrderByForLimitToLast
31+
SnapshotMetadata
3332
} from '../../../src/api/database';
3433
import { ViewSnapshot } from '../../../src/core/view_snapshot';
3534
import {
@@ -39,7 +38,8 @@ import {
3938
newUserDataReader,
4039
Query,
4140
SetOptions,
42-
UpdateData
41+
UpdateData,
42+
validateHasExplicitOrderByForLimitToLast
4343
} from '../../../lite/src/api/reference';
4444
import { Document } from '../../../src/model/document';
4545
import {
@@ -87,7 +87,20 @@ import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
8787
export {
8888
DocumentReference,
8989
CollectionReference,
90-
Query
90+
Query,
91+
collection,
92+
collectionGroup,
93+
doc,
94+
query,
95+
where,
96+
limit,
97+
limitToLast,
98+
orderBy,
99+
startAt,
100+
startAfter,
101+
endAt,
102+
endBefore,
103+
queryEqual
91104
} from '../../../lite/src/api/reference';
92105

93106
/**

packages/firestore/exp/test/shim.ts

+2-202
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,7 @@
1818
import * as legacy from '@firebase/firestore-types';
1919
import * as exp from '../index';
2020

21-
import {
22-
addDoc,
23-
doc,
24-
FieldPath as FieldPathExp,
25-
getDocs,
26-
getDocsFromCache,
27-
getDocsFromServer,
28-
onSnapshot,
29-
query,
30-
queryEqual,
31-
refEqual,
32-
endAt,
33-
endBefore,
34-
startAfter,
35-
startAt,
36-
limitToLast,
37-
limit,
38-
orderBy,
39-
where,
40-
Bytes as BytesExp
41-
} from '../../exp/index';
42-
import { UntypedFirestoreDataConverter } from '../../src/api/user_data_reader';
21+
import { FieldPath as FieldPathExp, Bytes as BytesExp } from '../../exp/index';
4322
import {
4423
isPlainObject,
4524
validateSetOptions
@@ -48,10 +27,7 @@ import { Compat } from '../../src/compat/compat';
4827
import {
4928
Firestore,
5029
DocumentReference,
51-
DocumentSnapshot,
52-
QuerySnapshot,
53-
wrapObserver,
54-
extractSnapshotOptions
30+
DocumentSnapshot
5531
} from '../../src/api/database';
5632

5733
export { GeoPoint, Timestamp } from '../index';
@@ -128,182 +104,6 @@ export class Transaction
128104
}
129105
}
130106

131-
export class Query<T = legacy.DocumentData>
132-
extends Compat<exp.Query<T>>
133-
implements legacy.Query<T> {
134-
constructor(readonly firestore: Firestore, delegate: exp.Query<T>) {
135-
super(delegate);
136-
}
137-
138-
where(
139-
fieldPath: string | FieldPath,
140-
opStr: legacy.WhereFilterOp,
141-
value: any
142-
): Query<T> {
143-
return new Query<T>(
144-
this.firestore,
145-
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
146-
);
147-
}
148-
149-
orderBy(
150-
fieldPath: string | FieldPath,
151-
directionStr?: legacy.OrderByDirection
152-
): Query<T> {
153-
return new Query<T>(
154-
this.firestore,
155-
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
156-
);
157-
}
158-
159-
limit(n: number): Query<T> {
160-
return new Query<T>(this.firestore, query(this._delegate, limit(n)));
161-
}
162-
163-
limitToLast(n: number): Query<T> {
164-
return new Query<T>(this.firestore, query(this._delegate, limitToLast(n)));
165-
}
166-
167-
startAt(...args: any[]): Query<T> {
168-
return new Query(
169-
this.firestore,
170-
query(this._delegate, startAt(...unwrap(args)))
171-
);
172-
}
173-
174-
startAfter(...args: any[]): Query<T> {
175-
return new Query(
176-
this.firestore,
177-
query(this._delegate, startAfter(...unwrap(args)))
178-
);
179-
}
180-
181-
endBefore(...args: any[]): Query<T> {
182-
return new Query(
183-
this.firestore,
184-
query(this._delegate, endBefore(...unwrap(args)))
185-
);
186-
}
187-
188-
endAt(...args: any[]): Query<T> {
189-
return new Query(
190-
this.firestore,
191-
query(this._delegate, endAt(...unwrap(args)))
192-
);
193-
}
194-
195-
isEqual(other: legacy.Query<T>): boolean {
196-
return queryEqual(this._delegate, (other as Query<T>)._delegate);
197-
}
198-
199-
get(options?: legacy.GetOptions): Promise<QuerySnapshot<T>> {
200-
let query: Promise<exp.QuerySnapshot<T>>;
201-
if (options?.source === 'cache') {
202-
query = getDocsFromCache(this._delegate);
203-
} else if (options?.source === 'server') {
204-
query = getDocsFromServer(this._delegate);
205-
} else {
206-
query = getDocs(this._delegate);
207-
}
208-
return query.then(result => new QuerySnapshot(this.firestore, result));
209-
}
210-
211-
onSnapshot(observer: {
212-
next?: (snapshot: QuerySnapshot<T>) => void;
213-
error?: (error: legacy.FirestoreError) => void;
214-
complete?: () => void;
215-
}): () => void;
216-
onSnapshot(
217-
options: legacy.SnapshotListenOptions,
218-
observer: {
219-
next?: (snapshot: QuerySnapshot<T>) => void;
220-
error?: (error: legacy.FirestoreError) => void;
221-
complete?: () => void;
222-
}
223-
): () => void;
224-
onSnapshot(
225-
onNext: (snapshot: QuerySnapshot<T>) => void,
226-
onError?: (error: legacy.FirestoreError) => void,
227-
onCompletion?: () => void
228-
): () => void;
229-
onSnapshot(
230-
options: legacy.SnapshotListenOptions,
231-
onNext: (snapshot: QuerySnapshot<T>) => void,
232-
onError?: (error: legacy.FirestoreError) => void,
233-
onCompletion?: () => void
234-
): () => void;
235-
onSnapshot(...args: any): () => void {
236-
const options = extractSnapshotOptions(args);
237-
const observer = wrapObserver<QuerySnapshot<T>, exp.QuerySnapshot<T>>(
238-
args,
239-
snap => new QuerySnapshot(this.firestore, snap)
240-
);
241-
return onSnapshot(this._delegate, options, observer);
242-
}
243-
244-
withConverter<U>(converter: legacy.FirestoreDataConverter<U>): Query<U> {
245-
return new Query<U>(
246-
this.firestore,
247-
this._delegate.withConverter(
248-
converter as UntypedFirestoreDataConverter<U>
249-
)
250-
);
251-
}
252-
}
253-
254-
export class CollectionReference<T = legacy.DocumentData>
255-
extends Query<T>
256-
implements legacy.CollectionReference<T> {
257-
constructor(
258-
readonly firestore: Firestore,
259-
readonly _delegate: exp.CollectionReference<T>
260-
) {
261-
super(firestore, _delegate);
262-
}
263-
264-
readonly id = this._delegate.id;
265-
readonly path = this._delegate.path;
266-
267-
get parent(): DocumentReference<legacy.DocumentData> | null {
268-
const docRef = this._delegate.parent;
269-
return docRef
270-
? new DocumentReference<legacy.DocumentData>(this.firestore, docRef)
271-
: null;
272-
}
273-
274-
doc(documentPath?: string): DocumentReference<T> {
275-
if (documentPath !== undefined) {
276-
return new DocumentReference(
277-
this.firestore,
278-
doc(this._delegate, documentPath)
279-
);
280-
} else {
281-
return new DocumentReference(this.firestore, doc(this._delegate));
282-
}
283-
}
284-
285-
add(data: T): Promise<DocumentReference<T>> {
286-
return addDoc(this._delegate, unwrap(data)).then(
287-
docRef => new DocumentReference(this.firestore, docRef)
288-
);
289-
}
290-
291-
isEqual(other: CollectionReference<T>): boolean {
292-
return refEqual(this._delegate, other._delegate);
293-
}
294-
295-
withConverter<U>(
296-
converter: legacy.FirestoreDataConverter<U>
297-
): CollectionReference<U> {
298-
return new CollectionReference<U>(
299-
this.firestore,
300-
this._delegate.withConverter(
301-
converter as UntypedFirestoreDataConverter<U>
302-
)
303-
);
304-
}
305-
}
306-
307107
export class FieldPath
308108
extends Compat<FieldPathExp>
309109
implements legacy.FieldPath {

0 commit comments

Comments
 (0)