all files / firebase/modules/firestore/ CollectionReference.js

100% Statements 24/24
83.33% Branches 5/6
100% Functions 16/16
100% Lines 23/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                        686× 686× 686×                                   642×   642× 642×     640×                     616×                     40×               66×                   20×      
/**
 * @flow
 * CollectionReference representation wrapper
 */
import DocumentReference from './DocumentReference';
import Query from './Query';
import { firestoreAutoId } from '../../utils';
 
import type Firestore from './';
import type {
  QueryDirection,
  QueryListenOptions,
  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();
    return parentPath
      ? new DocumentReference(this._firestore, parentPath)
      : null;
  }
 
  add(data: Object): Promise<DocumentReference> {
    const documentRef = this.doc();
    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(): Promise<QuerySnapshot> {
    return this._query.get();
  }
 
  limit(limit: number): Query {
    return this._query.limit(limit);
  }
 
  onSnapshot(
    optionsOrObserverOrOnNext: QueryListenOptions | Observer | ObserverOnNext,
    observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
    onError?: ObserverOnError
  ): () => void {
    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);
  }
}