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

Added document ID to every synced document. #3

Merged
merged 3 commits into from
Apr 16, 2022

Conversation

megamidge
Copy link
Contributor

It's required to have the document ID of the document for making updates in the Firestore database to a specific document.
This pull request adds the documents ID as a non-writable and non-enumerable property of every synced document.

Non Writable:
No chance of accidentally overwriting the ID - it shouldn't ever change.

Non Enumerable:
Dumping the document to DOM won't show the ID (preserving current behaviour):

<p>{{myDocument}}</P

But still enables access to the ID when it's needed with

myDocument.id

This is a required feature in my opinion.

@kazuooooo
Copy link
Owner

@megamidge
Hi, sorry for the late reply.
And thank you for your great PR.

Your PR looks good, but I'm worried about users doesn't realize it's synced with Id.

 type ExampleDoc = {
   name: string,
   age: number
 }
 
 this.sync('docData', docRef)  // id property unexpected

So, I want to add type checking, do you have any idea to implement this?

@megamidge
Copy link
Contributor Author

No worries on the time, we all got stuff to do.

I would argue that users not realising the ID of the document is synced is a bit of moot point; I can't see a situation where you wouldn't want it.
However it could be implemented as an optional parameter that defaults to true.
In my experience there's not many occasions where you don't want the ID of the document beyond just showing the data. Anything more complex, like updating the document, requires the ID. Moreover the ID will be an 'invisible' property - it won't appear in enumerations - you'd only see it's value if you specifically used the key.

With regards to type checking, i'm not too well versed in typescript. If you're able to get the keys of the type definition, you could iterate over the keys in the type definition and the keys in the document.

@kazuooooo
Copy link
Owner

@megamidge
Thank you!
I agree with this.

Anything more complex, like updating the document, requires the ID

OK, I'm merging!
Thank you for your contribution!!!

@kazuooooo kazuooooo merged commit 7944034 into kazuooooo:master Apr 16, 2022
@megamidge
Copy link
Contributor Author

@kazuooooo I recall you mentioning something along the lines of type checking.
If the plugin can receive the type expected when we tell it to sync, type checking could be done to at least some extent in the snapshot handlers.
You could pass the type using something like typeof. Keys can be checked using keyof.
You could use this to only take the keys from the firestore document defined in the type. Any extra fields could be ignored.

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

Successfully merging this pull request may close these issues.

2 participants