kossnocorp / typesaurus

🦕 Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
415 stars 34 forks source link

want to have docChanges on onQuery etc. #65

Closed reedom closed 3 years ago

reedom commented 4 years ago

For the client side performance improvement, I want to have "docChanges" on "onQuery" and other "on" series. https://firebase.google.com/docs/firestore/query-data/listen#view_changes_between_snapshots

To achieve it, I think there are several API candidates while the docChanges should be supplied optionally for performance reasons.

  1. Introduce variant functions for each "on" series. E.g. onQueryWithDocChanges for onQuery

    export default function onQueryWithDocChanges<Model>(
    collection: Collection<Model> | CollectionGroup<Model>,
    queries: Query<Model, keyof Model>[],
    onResult: (docs: Doc<Model>[], docChanges: DocChange<Model>[]),
    onError?: (err: Error) => any
    ): () => void {
  2. Add an options specification argument to let clients specify their requirement

    export default function onQuery<Model>(
    collection: Collection<Model> | CollectionGroup<Model>,
    queries: Query<Model, keyof Model>[],
    onResult: (docs: Doc<Model>[], extra?: { docChanges?: DocChange<Model>[] }),
    onError?: (err: Error) => any,
    options?: { docChanges?: boolean }
    ): () => void {
  3. Add an "getter" to onResult's argument

    export default function onQuery<Model>(
    collection: Collection<Model> | CollectionGroup<Model>,
    queries: Query<Model, keyof Model>[],
    onResult: (docs: Doc<Model>[], getDocChanges: () => DocChange<Model>[]) => any,
    onError?: (err: Error) => any
    ): () => void {

A sample implementation to the 3rd. https://github.com/kossnocorp/typesaurus/compare/master...reedom:feature/docchanges?expand=1 But as reviewing Firebase.QuerySnapshot now again, chances are that someone might want to get readTime. So, the 2nd would be better while it's expandable.

kossnocorp commented 4 years ago

I love it, and I agree that the 2nd option works the best. I will happily accept a pull-request and help if it would be needed.