Populate
Populate allows you to replace IDs within your data with other data from Firebase. This is very useful when trying to keep your data flat. Some would call it a join, but it was modeled after the mongo populate method.
List of todo items where todo item can contain an owner parameter which is a user's UID like so:
{ text: 'Some Todo Item', owner: "Iq5b0qK2NtgggT6U3bU6iZRGyma2" }
Populate allows you to replace the owner parameter with another value on Firebase under that key. That value you can be a string (number and boolean treated as string), or an object
Example Data
todos: {
123: {
text: 'Some Todo Item',
owner: "Iq5b0qK2NtgggT6U3bU6iZRGyma2"
}
}
displayNames: {
Iq5b0qK2NtgggT6U3bU6iZRGyma2: 'Scott Prue',
6Ra53mf3U9Qmdwah6rXBMgY8smu1: 'Rick Sanchez'
}
users: {
Iq5b0qK2NtgggT6U3bU6iZRGyma2: {
displayName: 'Scott Prue',
email: '[email protected]'
},
6Ra53mf3U9Qmdwah6rXBMgY8smu1: {
displayName: 'Rick Sanchez',
email: '[email protected]'
}
}
String, Number, or Boolean
When trying to replace the owner parameter with a string such as a displayName from a /displayNames
root follow the pattern of #populate=paramToPopulate:populateRoot
.
Example Query
@firebaseConnect([
{ path: '/todos', populates: [{ child: 'owner', root: 'displayNames' }] }
// '/todos#populate=owner:displayNames', // equivalent string notation
])
Result
123: {
text: 'Some Todo Item',
owner: 'Scott Prue'
}
Object
Population can also be used to populate a parameter with an object. An example of this would be populating the owner parameter, which is an ID, with the matching key from the users list.
Example Query
@firebaseConnect([
{ path: '/todos', populates: [{ child: 'owner', root: 'users' }] }
// '/todos#populate=owner:users'// equivalent string notation
])
Example Result
123: {
text: 'Some Todo Item',
owner: {
displayName: 'Scott Prue',
email: '[email protected]'
}
}
Object's Parameter
There is also the option to load a parameter from within a population object. An example of this could be populating the owner parameter with the displayName property of the user with a matching ID:
Example Query
@firebase([
{ path: '/todos', populates: [{ child: 'owner', root: 'users', childParam: 'email' }] }
'/todos#populate=owner:users:email'
])
Example Result
123: {
text: 'Some Todo Item',
owner: '[email protected]'
}