function writeMetadataToDb({
  firebase,
  uploadTaskSnapshot,
  dbPath,
  options
}) {
  // Support metadata factories from both global config and options
  const { fileMetadataFactory, useFirestoreForStorageMeta } = firebase._.config
  const { metadataFactory, documentId, useSetForMetadata } = options
  const metaFactoryFunction = metadataFactory || fileMetadataFactory
  // Get download URL for use in metadata write
  return getDownloadURLFromUploadTaskSnapshot(uploadTaskSnapshot).then(
    (downloadURL) => {
      // Apply fileMetadataFactory if it exists in config
      const fileData =
        typeof metaFactoryFunction === 'function'
          ? metaFactoryFunction(
              uploadTaskSnapshot,
              firebase,
              uploadTaskSnapshot.metadata,
              downloadURL
            )
          : omitBy(uploadTaskSnapshot.metadata, isUndefined)

      // Create the snapshot handler function
      const resultFromSnap = createUploadMetaResponseHandler({
        fileData,
        firebase,
        uploadTaskSnapshot,
        downloadURL
      })

      // Function for creating promise for writing file metadata (handles writing to RTDB or Firestore)
      const documentIdFromOptions =
        typeof documentId === 'function'
          ? documentId(
              uploadTaskSnapshot,
              firebase,
              uploadTaskSnapshot.metadata,
              downloadURL
            )
          : documentId
      const metaSetPromise = (fileData) => {
        if (useFirestoreForStorageMeta) {
          if (documentIdFromOptions) {
            const docRef = firebase // Write metadata to Firestore
              .firestore()
              .collection(dbPath)
              .doc(documentIdFromOptions)
            return useSetForMetadata === false
              ? docRef.update(fileData).then(() => docRef)
              : docRef.set(fileData, { merge: true }).then(() => docRef)
          }
          return firebase.firestore().collection(dbPath).add(fileData)
        }
        // Create new reference for metadata
        const newMetaRef = firebase.database().ref(dbPath).push()
        // Write metadata to Real Time Database and return new meta ref
        return newMetaRef.set(fileData).then((res) => newMetaRef)
      }

      return metaSetPromise(fileData).then(resultFromSnap)
    }
  )
}
Write file metadata to Database (either Real Time Datbase or Firestore
depending on config).
@param {object} settings - Settings object
@param {object} settings.firebase - Internal firebase object
@param {object} settings.uploadTaskSnapshot - Snapshot from upload task
@param {string} settings.dbPath - Path of meta data with Database (Real Time Or
Firestore depnding on config)
@param {object} settings.options - Options object
@returns {Promise} Resolves with payload (includes snapshot, File, and
metaDataSnapshot)