Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): Added option to include document IDs on valueChanges() #1976

Merged
merged 6 commits into from May 11, 2019

Conversation

codediodeio
Copy link
Contributor

Checklist

Description

An 'idField' string arg can be used with collection.valueChanges() to include the document ID on the emitted data payload.

Code sample

collectionRef.valueChanges('myIdKey').subscribe()

// emits [ { myIdKey: 'MrfFpRBfWLTd7LqiTt9u', ...data }, ... ]

An 'idField' string arg can be used with collection.valueChanges() to include the document ID on the emitted data payload.
Copy link
Contributor

@markgoho markgoho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jamesdaniels jamesdaniels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Maybe we can union the return type T with [idField]: string if possible? But that can be follow on work.

Copy link
Member

@davideast davideast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Just a little comment on the API.

@@ -104,12 +104,18 @@ export class AngularFirestoreCollection<T=DocumentData> {
/**
* Listen to all documents in the collection and its possible query as an Observable.
*/
valueChanges(): Observable<T[]> {
valueChanges(idField?: string): Observable<T[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this for future proofing? Not a huge deal but it could help.

Suggested change
valueChanges(idField?: string): Observable<T[]> {
valueChanges({ idField: string }: ValueChangeOptions?): Observable<T[]> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second using an options object for future proofing. Great PR otherwise.

@jorroll
Copy link

jorroll commented Jan 25, 2019

@codediodeio I just created a new PR to update this PR and make valueChanges() accept an options object for idField instead of a single string argument -- to future proof things.

If you accept this change, it will (hopefully) make this PR here able to be merged.

feat(firestore): changed valueChanges() arg to options object
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

@jorroll
Copy link

jorroll commented May 10, 2019

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

I am ok with my commits being contributed to this project 👍 !

@davideast per your comment, valueChanges() has been updated to accept an options object rather than a simple string valueChanges(options?: { idField: string }): Observable<T[]>.

cc @jamesdaniels

@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

ℹ️ Googlers: Go here for more info.

1 similar comment
@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

ℹ️ Googlers: Go here for more info.

@jamesdaniels jamesdaniels merged commit 7108875 into angular:master May 11, 2019
@jamesdaniels
Copy link
Member

Thanks @codediodeio and @thefliik, will ship in the next beta and this will land in the 5.2 release.

const ITEMS = 1;
const { ref, stocks, names } = await collectionHarness(afs, ITEMS);
const idField = 'myCustomID';
const sub = stocks.valueChanges({idField}).subscribe(data => {

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe they are destructuring the valueChanges object and grabbing just the idField property.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{idField} is shorthand for {idField: idField}
https://stackoverflow.com/questions/34414766/ Shorthand property names (ES2015)

@rtpHarry
Copy link
Contributor

rtpHarry commented Jan 5, 2020

thanks... you just ruined all the tutorials out there :)

@Crypt0Graphic
Copy link

Crypt0Graphic commented Jan 17, 2020

When i use valueChanges({ idField: 'id' }) and map i get an error like below. If i directly subscribe to it there is no problem.

No problem with snapshotChanges() in both situation.

  getList() {
    return this.af.collection('Clubs', ref => ref.orderBy('name')).valueChanges({ idField: 'id' });
  }
    const newarray = [];
    this.subscription = this.clubsService
      .getList()
      .pipe(
        switchMap(val => from(val)),
        map(x => {
          const modified = { ...x, fCount: x.followers.length };
          newarray.push(modified);
          return newarray;
        })
      )

**error TS2339: Property 'followers' does not exist on type '{ id: string; }'.**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants