Skip to content

Commit c7d4b97

Browse files
Compat class for Query
1 parent 5ba4c26 commit c7d4b97

File tree

6 files changed

+226
-582
lines changed

6 files changed

+226
-582
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 2 additions & 202 deletions
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';
@@ -185,182 +161,6 @@ export class WriteBatch
185161
}
186162
}
187163

188-
export class Query<T = legacy.DocumentData>
189-
extends Compat<exp.Query<T>>
190-
implements legacy.Query<T> {
191-
constructor(readonly firestore: Firestore, delegate: exp.Query<T>) {
192-
super(delegate);
193-
}
194-
195-
where(
196-
fieldPath: string | FieldPath,
197-
opStr: legacy.WhereFilterOp,
198-
value: any
199-
): Query<T> {
200-
return new Query<T>(
201-
this.firestore,
202-
query(this._delegate, where(unwrap(fieldPath), opStr, unwrap(value)))
203-
);
204-
}
205-
206-
orderBy(
207-
fieldPath: string | FieldPath,
208-
directionStr?: legacy.OrderByDirection
209-
): Query<T> {
210-
return new Query<T>(
211-
this.firestore,
212-
query(this._delegate, orderBy(unwrap(fieldPath), directionStr))
213-
);
214-
}
215-
216-
limit(n: number): Query<T> {
217-
return new Query<T>(this.firestore, query(this._delegate, limit(n)));
218-
}
219-
220-
limitToLast(n: number): Query<T> {
221-
return new Query<T>(this.firestore, query(this._delegate, limitToLast(n)));
222-
}
223-
224-
startAt(...args: any[]): Query<T> {
225-
return new Query(
226-
this.firestore,
227-
query(this._delegate, startAt(...unwrap(args)))
228-
);
229-
}
230-
231-
startAfter(...args: any[]): Query<T> {
232-
return new Query(
233-
this.firestore,
234-
query(this._delegate, startAfter(...unwrap(args)))
235-
);
236-
}
237-
238-
endBefore(...args: any[]): Query<T> {
239-
return new Query(
240-
this.firestore,
241-
query(this._delegate, endBefore(...unwrap(args)))
242-
);
243-
}
244-
245-
endAt(...args: any[]): Query<T> {
246-
return new Query(
247-
this.firestore,
248-
query(this._delegate, endAt(...unwrap(args)))
249-
);
250-
}
251-
252-
isEqual(other: legacy.Query<T>): boolean {
253-
return queryEqual(this._delegate, (other as Query<T>)._delegate);
254-
}
255-
256-
get(options?: legacy.GetOptions): Promise<QuerySnapshot<T>> {
257-
let query: Promise<exp.QuerySnapshot<T>>;
258-
if (options?.source === 'cache') {
259-
query = getDocsFromCache(this._delegate);
260-
} else if (options?.source === 'server') {
261-
query = getDocsFromServer(this._delegate);
262-
} else {
263-
query = getDocs(this._delegate);
264-
}
265-
return query.then(result => new QuerySnapshot(this.firestore, result));
266-
}
267-
268-
onSnapshot(observer: {
269-
next?: (snapshot: QuerySnapshot<T>) => void;
270-
error?: (error: legacy.FirestoreError) => void;
271-
complete?: () => void;
272-
}): () => void;
273-
onSnapshot(
274-
options: legacy.SnapshotListenOptions,
275-
observer: {
276-
next?: (snapshot: QuerySnapshot<T>) => void;
277-
error?: (error: legacy.FirestoreError) => void;
278-
complete?: () => void;
279-
}
280-
): () => void;
281-
onSnapshot(
282-
onNext: (snapshot: QuerySnapshot<T>) => void,
283-
onError?: (error: legacy.FirestoreError) => void,
284-
onCompletion?: () => void
285-
): () => void;
286-
onSnapshot(
287-
options: legacy.SnapshotListenOptions,
288-
onNext: (snapshot: QuerySnapshot<T>) => void,
289-
onError?: (error: legacy.FirestoreError) => void,
290-
onCompletion?: () => void
291-
): () => void;
292-
onSnapshot(...args: any): () => void {
293-
const options = extractSnapshotOptions(args);
294-
const observer = wrapObserver<QuerySnapshot<T>, exp.QuerySnapshot<T>>(
295-
args,
296-
snap => new QuerySnapshot(this.firestore, snap)
297-
);
298-
return onSnapshot(this._delegate, options, observer);
299-
}
300-
301-
withConverter<U>(converter: legacy.FirestoreDataConverter<U>): Query<U> {
302-
return new Query<U>(
303-
this.firestore,
304-
this._delegate.withConverter(
305-
converter as UntypedFirestoreDataConverter<U>
306-
)
307-
);
308-
}
309-
}
310-
311-
export class CollectionReference<T = legacy.DocumentData>
312-
extends Query<T>
313-
implements legacy.CollectionReference<T> {
314-
constructor(
315-
readonly firestore: Firestore,
316-
readonly _delegate: exp.CollectionReference<T>
317-
) {
318-
super(firestore, _delegate);
319-
}
320-
321-
readonly id = this._delegate.id;
322-
readonly path = this._delegate.path;
323-
324-
get parent(): DocumentReference<legacy.DocumentData> | null {
325-
const docRef = this._delegate.parent;
326-
return docRef
327-
? new DocumentReference<legacy.DocumentData>(this.firestore, docRef)
328-
: null;
329-
}
330-
331-
doc(documentPath?: string): DocumentReference<T> {
332-
if (documentPath !== undefined) {
333-
return new DocumentReference(
334-
this.firestore,
335-
doc(this._delegate, documentPath)
336-
);
337-
} else {
338-
return new DocumentReference(this.firestore, doc(this._delegate));
339-
}
340-
}
341-
342-
add(data: T): Promise<DocumentReference<T>> {
343-
return addDoc(this._delegate, unwrap(data)).then(
344-
docRef => new DocumentReference(this.firestore, docRef)
345-
);
346-
}
347-
348-
isEqual(other: CollectionReference<T>): boolean {
349-
return refEqual(this._delegate, other._delegate);
350-
}
351-
352-
withConverter<U>(
353-
converter: legacy.FirestoreDataConverter<U>
354-
): CollectionReference<U> {
355-
return new CollectionReference<U>(
356-
this.firestore,
357-
this._delegate.withConverter(
358-
converter as UntypedFirestoreDataConverter<U>
359-
)
360-
);
361-
}
362-
}
363-
364164
export class FieldPath
365165
extends Compat<FieldPathExp>
366166
implements legacy.FieldPath {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import { getDatastore } from './components';
8181
import { ByteString } from '../../../src/util/byte_string';
8282
import { Bytes } from './bytes';
8383
import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
84+
import { Compat } from '../../../src/compat/compat';
8485

8586
/**
8687
* Document data (for use with {@link setDoc()}) consists of fields mapped to
@@ -595,6 +596,10 @@ function newQueryBoundFromDocOrFields<T>(
595596
docOrFields: Array<unknown | DocumentSnapshot<T>>,
596597
before: boolean
597598
): Bound {
599+
if (docOrFields[0] instanceof Compat) {
600+
docOrFields[0] = docOrFields[0]._delegate;
601+
}
602+
598603
if (docOrFields[0] instanceof DocumentSnapshot) {
599604
return newQueryBoundFromDocument(
600605
query._query,

0 commit comments

Comments
 (0)