react-native-firebase/lib/modules/firestore/CollectionReference.js

111 lines
2.9 KiB
JavaScript
Raw Normal View History

/**
* @flow
* CollectionReference representation wrapper
*/
import DocumentReference from './DocumentReference';
2017-11-17 14:22:46 +00:00
import Query from './Query';
import { firestoreAutoId } from '../../utils';
import type Firestore from './';
import type {
GetOptions,
MetadataChanges,
QueryDirection,
QueryOperator,
} from './types';
import type FieldPath from './FieldPath';
import type Path from './Path';
import type { Observer, ObserverOnError, ObserverOnNext } from './Query';
import type QuerySnapshot from './QuerySnapshot';
/**
* @class CollectionReference
*/
export default class CollectionReference {
_collectionPath: Path;
_firestore: Firestore;
_query: Query;
constructor(firestore: Firestore, collectionPath: Path) {
this._collectionPath = collectionPath;
this._firestore = firestore;
this._query = new Query(firestore, collectionPath);
}
get firestore(): Firestore {
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;
}
add(data: Object): Promise<DocumentReference> {
const documentRef = this.doc();
2018-01-25 18:25:39 +00:00
return documentRef.set(data).then(() => Promise.resolve(documentRef));
}
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
endAt(...snapshotOrVarArgs: any[]): Query {
return this._query.endAt(snapshotOrVarArgs);
}
endBefore(...snapshotOrVarArgs: any[]): Query {
return this._query.endBefore(snapshotOrVarArgs);
}
get(options?: GetOptions): Promise<QuerySnapshot> {
return this._query.get(options);
}
limit(limit: number): Query {
return this._query.limit(limit);
}
onSnapshot(
optionsOrObserverOrOnNext: MetadataChanges | Observer | ObserverOnNext,
observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
2018-01-25 18:25:39 +00:00
onError?: ObserverOnError
): () => void {
2018-01-25 18:25:39 +00:00
return this._query.onSnapshot(
optionsOrObserverOrOnNext,
observerOrOnNextOrOnError,
onError
);
}
orderBy(fieldPath: string | FieldPath, directionStr?: QueryDirection): Query {
return this._query.orderBy(fieldPath, directionStr);
}
startAfter(...snapshotOrVarArgs: any[]): Query {
return this._query.startAfter(snapshotOrVarArgs);
}
startAt(...snapshotOrVarArgs: any[]): Query {
return this._query.startAt(snapshotOrVarArgs);
}
where(fieldPath: string, opStr: QueryOperator, value: any): Query {
return this._query.where(fieldPath, opStr, value);
}
}