prescottprue / react-redux-firebase

Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
https://react-redux-firebase.com
MIT License
2.55k stars 556 forks source link

feat(storage): upload file metadata to existing firestore document #936

Closed JonnyBoy333 closed 4 years ago

JonnyBoy333 commented 4 years ago

Description

This pull requests enhances the uploadFile and uploadFiles methods when using Firestore as your database to store the file metadata. Current behavior is to specify a path to a collection, when a file is uploaded the metadata will create a new document with that data in the specified collection.

In some cases you may already have a document in that collection that you would like to update with the metadata instead of creating a new document. In my use case I have a contacts collection with a list of contact documents and I would like to update an existing contact document with a file download Url from the metadata instead of creating a new document with that information. This PR allows for that functionality by specifying a documentId property in the uploadFile options object. If this property exist the corresponding document will get updated, otherwise a new document will be created.

I've also updated the documentation with this new feature as well as the typescript typings. While updating the docs and typings I noticed some related properties that were missing, like the metadata and metadataFactory properties on the upload options object so I took the liberty of adding typings for those and adding them to the documentation as well. I found that to be important since they impact how my feature works.

Small side note that some of the minor changes in the documentation where made from a pre-commit linting script and not actually something that I changed, hopefully that doesn't cause confusion.

Here's the example I put in the upload examples to see it in action:

Update Firestore Document

If using Firestore for you database and you would like to update a specific document after a file has uploaded you can specify the options.documentId property. In this example the document with id 12345 in the contacts collection will have the fileUrl property updated with the file's download url. More details can be found in the upload file section of the Firebase docs.

const storagePath = 'contacts/sleepygary'
const dbPath = 'contacts'

firebase
  .uploadFile(storagePath, file, dbPath, {
    metadataFactory: (uploadRes, firebase, metadata, downloadURL) => {
      return { fileUrl: downloadURL }
    },
    documentId: '12345'
  })
  .then(() => {
    console.log('File uploaded successfully')
  })

Check List

Relevant Issues

https://github.com/prescottprue/react-redux-firebase/issues/665

codecov[bot] commented 4 years ago

Codecov Report

Merging #936 into master will decrease coverage by 0.54%. The diff coverage is 25.00%.

@@            Coverage Diff             @@
##           master     #936      +/-   ##
==========================================
- Coverage   88.87%   88.33%   -0.55%     
==========================================
  Files          29       29              
  Lines         791      797       +6     
==========================================
+ Hits          703      704       +1     
- Misses         88       93       +5     
JonnyBoy333 commented 4 years ago

@prescottprue Made some updates today per your suggestions. Take a look when you have a chance. Open to feedback.

JonnyBoy333 commented 4 years ago

Well, I did update this PR with a useSetForMetadata property on the uploadFiles function. This functions the same way as useSet on the updateProfile function. It defaults to true so you don't have to set it, but if for some reason you want the operation to fail if the documentId is not found you can set this to false and have that functionality. I ran some tests and made sure this is functioning as intended.

I do apologize that I'm not fluent at writing tests in mocha yet. I was hoping I could expand on existing tests around this but all the current ones seem to be around the realtime db and not firestore. I hope this doesn't block this PR.

Otherwise this PR should be ready to go with all your suggestions in place.

prescottprue commented 4 years ago

Thanks for all of the work! Going to merge and release as part of v3.5.0