2017-09-26 13:57:25 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* CollectionReference representation wrapper
|
|
|
|
*/
|
|
|
|
import DocumentReference from './DocumentReference';
|
2017-11-17 14:22:46 +00:00
|
|
|
import Query from './Query';
|
2017-09-27 16:20:32 +00:00
|
|
|
import { firestoreAutoId } from '../../utils';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Firestore from './';
|
2018-01-25 18:25:39 +00:00
|
|
|
import type {
|
2018-02-14 13:00:19 +00:00
|
|
|
QueryDirection,
|
|
|
|
QueryListenOptions,
|
|
|
|
QueryOperator,
|
|
|
|
} from './types';
|
2018-01-11 18:28:14 +00:00
|
|
|
import type FieldPath from './FieldPath';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type Path from './Path';
|
2018-02-14 13:00:19 +00:00
|
|
|
import type { Observer, ObserverOnError, ObserverOnNext } from './Query';
|
2017-11-17 11:07:52 +00:00
|
|
|
import type QuerySnapshot from './QuerySnapshot';
|
2017-09-26 13:57:25 +00:00
|
|
|
|
2017-11-17 11:07:52 +00:00
|
|
|
/**
|
2017-09-26 13:57:25 +00:00
|
|
|
* @class CollectionReference
|
|
|
|
*/
|
|
|
|
export default class CollectionReference {
|
|
|
|
_collectionPath: Path;
|
2017-11-17 11:07:52 +00:00
|
|
|
_firestore: Firestore;
|
2017-09-26 13:57:25 +00:00
|
|
|
_query: Query;
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
constructor(firestore: Firestore, collectionPath: Path) {
|
2017-09-26 13:57:25 +00:00
|
|
|
this._collectionPath = collectionPath;
|
|
|
|
this._firestore = firestore;
|
|
|
|
this._query = new Query(firestore, collectionPath);
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
get firestore(): Firestore {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._firestore;
|
|
|
|
}
|
|
|
|
|
|
|
|
get id(): string | null {
|
|
|
|
return this._collectionPath.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get parent(): DocumentReference | null {
|
|
|
|
const parentPath = this._collectionPath.parent();
|
2018-01-25 18:25:39 +00:00
|
|
|
return parentPath
|
|
|
|
? new DocumentReference(this._firestore, parentPath)
|
|
|
|
: null;
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 09:18:24 +00:00
|
|
|
add(data: Object): Promise<DocumentReference> {
|
2017-09-26 13:57:25 +00:00
|
|
|
const documentRef = this.doc();
|
2018-01-25 18:25:39 +00:00
|
|
|
return documentRef.set(data).then(() => Promise.resolve(documentRef));
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doc(documentPath?: string): DocumentReference {
|
|
|
|
const newPath = documentPath || firestoreAutoId();
|
|
|
|
|
|
|
|
const path = this._collectionPath.child(newPath);
|
|
|
|
if (!path.isDocument) {
|
|
|
|
throw new Error('Argument "documentPath" must point to a document.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DocumentReference(this._firestore, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// From Query
|
2017-12-15 11:46:37 +00:00
|
|
|
endAt(...snapshotOrVarArgs: any[]): Query {
|
|
|
|
return this._query.endAt(snapshotOrVarArgs);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
endBefore(...snapshotOrVarArgs: any[]): Query {
|
|
|
|
return this._query.endBefore(snapshotOrVarArgs);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get(): Promise<QuerySnapshot> {
|
|
|
|
return this._query.get();
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
limit(limit: number): Query {
|
|
|
|
return this._query.limit(limit);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
onSnapshot(
|
2018-01-05 17:20:02 +00:00
|
|
|
optionsOrObserverOrOnNext: QueryListenOptions | Observer | ObserverOnNext,
|
|
|
|
observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
|
2018-01-25 18:25:39 +00:00
|
|
|
onError?: ObserverOnError
|
2017-12-15 11:46:37 +00:00
|
|
|
): () => void {
|
2018-01-25 18:25:39 +00:00
|
|
|
return this._query.onSnapshot(
|
|
|
|
optionsOrObserverOrOnNext,
|
|
|
|
observerOrOnNextOrOnError,
|
|
|
|
onError
|
|
|
|
);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
orderBy(fieldPath: string | FieldPath, directionStr?: QueryDirection): Query {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._query.orderBy(fieldPath, directionStr);
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
startAfter(...snapshotOrVarArgs: any[]): Query {
|
|
|
|
return this._query.startAfter(snapshotOrVarArgs);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 11:46:37 +00:00
|
|
|
startAt(...snapshotOrVarArgs: any[]): Query {
|
|
|
|
return this._query.startAt(snapshotOrVarArgs);
|
2017-09-26 13:57:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
where(fieldPath: string, opStr: QueryOperator, value: any): Query {
|
2017-09-26 13:57:25 +00:00
|
|
|
return this._query.where(fieldPath, opStr, value);
|
|
|
|
}
|
|
|
|
}
|