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

support for FirstoreDataConverter #40

Open
goleary opened this issue Aug 28, 2020 · 6 comments
Open

support for FirstoreDataConverter #40

goleary opened this issue Aug 28, 2020 · 6 comments

Comments

@goleary
Copy link

goleary commented Aug 28, 2020

I'm curious if you have thought about adding support for withConverter by allowing users of this libary to provide their own type converters?

It might enable usage such as this:

const postConverter = {
  toFirestore(post: Post): firebase.firestore.DocumentData {
    return {title: post.title, author: post.author};
  },
  fromFirestore(
    snapshot: firebase.firestore.QueryDocumentSnapshot,
    options: firebase.firestore.SnapshotOptions
  ): Post {
    const data = snapshot.data(options)!;
    if(data.title == null || data.author == null) {
      throw new Error('Data from firestore malformed');
    }
    return new Post(data.title, data.author);
  }
};
...
const {error, data}= useCollection('posts', {converter: postConverter});

where data ends up being Post[] and add only accepts objects of type Post (that are then run through the converter)

https://firebase.google.com/docs/reference/js/firebase.firestore.FirestoreDataConverter

@nandorojo
Copy link
Owner

Interesting. Sure, I'm open to adding this. The closest thing so far is theparseDates field.

@abdel-h
Copy link

abdel-h commented Sep 2, 2020

Yes it will be very interesting, for example encoding / decoding a geo point

@goleary
Copy link
Author

goleary commented Sep 10, 2020

FYI I've started playing around with this here.

It seems to fit into the library quite well at least for useCollection.

There's more work to be done but I'm happy to submit a PR once I get to spend a bit more time with it and get it to a good place.

@nandorojo
Copy link
Owner

@goleary Thanks, looks good. I added some notes, once those are fixed it should be good to PR.

@cheunjm
Copy link

cheunjm commented Jan 12, 2021

@goleary @nandorojo any updates on this PR? if not, I am happy to work on this feature

@nandorojo
Copy link
Owner

You're welcome to!

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

No branches or pull requests

4 participants