Custom keys

Using the realtime database

Firebase push keys

Firebase's push method automatically generates a key to save the new child. For example calling database.ref('users').push({ name: 'Elon' }) will save create the following structure in the realtime database:

{
  "users": {
    "-L13_r6yHjZJV2V8yTQm": {
      "name": "Elon"
    }
  }
}

And -L13_r6yHjZJV2V8yTQm (the automatically generated key) will be returned.

You can learn more about those keys (and their cool properties) by reading this firebase blog article: The 2^120 Ways to Ensure Unique Identifiers and the associated gist. There's also this cool npm package if you want to generate those keys yourself: firebase-key.

Choosing your own keys

Since calling push (or RSF's database.create) automatically generates the key, you won't have an option to choose it yourself.

The alternative is to use the set method (or RSF's database.update) with the key already built in the reference:

function createUserSaga * (userData) {
  yield call(
    rsf.database.update,
    `users/${userData.username}`,
    userData
  )
}

See issue#82 for more info.

Using firestore

There is a very similar behaviour when using firestore:

function createUserSaga * (userData) {
  yield call(
    rsf.firestore.setDocument,
    `users/${userData.username}`,
    userData
  )
}