使用snapshotChanges()的可计费读取

时间:2018-09-25 19:31:38

标签: angular firebase rxjs google-cloud-firestore

我正在使用Cloud Firestore,并且当我将文档添加到我的一个根集合中并且在该集合上记录snapshotChanges()的结果时,日志中会显示每个文档集合。

我了解Firebase会根据返回的文档(例如查询)收费。

snapshotChanges()是否还会导致对每个文档的计费阅读?

firebase.service.ts

@Injectable()
export class FirebaseService {

  public recordsCollection: AngularFirestoreCollection<any[]>;
  public records$: Observable<any[]>;

  public financialsCollection: AngularFirestoreCollection<any[]>;
  public financials$: Observable<any[]>;

  constructor(private db: AngularFirestore) {

    this.recordsCollection = this.db.collection<any[]>('records');
    this.records$ = this.mapAndReplayCollection(this.recordsCollection);

    this.financialsCollection = this.db.collection<any[]>('financials');
    this.financials$ = this.mapAndReplayCollection(this.financialsCollection);

  }

  private mapAndReplayCollection(collection: AngularFirestoreCollection<any[]>): any { 
   return collection.snapshotChanges()
      .pipe(
        map(changes => {
          return changes.map(a => {
           console.log(`${collection.ref.id} snapshotChanges: ${a.payload.doc.id}`);
           return { realId: a.payload.doc.id, ...a.payload.doc.data() }
          })
        }),
       shareReplay(1),
    );
  }

}

1 个答案:

答案 0 :(得分:0)

认为您的方法是正确的,所有排放都是来自您的持久性采集实例(可观察到的热)。但是我建议您只是扩展财务收集链以创建financial$financialsWithReplay$流,使其看起来更加明确,而不是将它们包装在函数中(这可能就是为什么您不确定其是否缓存了最后一个结果)

this.financials$ = this.financialsCollection.snapshotChanges()
  .pipe(
    map(changes => {
      return changes.map(a => {
       console.log(`${collection.ref.id} snapshotChanges: ${a.payload.doc.id}`);
       return { realId: a.payload.doc.id, ...a.payload.doc.data() }
      })
    })
);

this.financialsWithReplay$=this.financials$.pipe(shareReplay(1))