My use case is that I have an <iron-form>
with a single <paper-textarea>
field that accepts a string list of email addresses which I parse into an array, then I want to:
- Store the individual email addresses in my Firebase (for indexing and lookup purposes),
- at multiple locations (per data fan out technique),
- with a single write operation (because I don't want to make 100 API calls if the list is that long) and
- without overwriting any existing data.
Specifically, I want to start with State A, as follows:
State Amy-app
|
- emails
| |
| - email1@example,com
| |- old: "data"
| - email2@example,com
| |- old: "data"
- users
|
- c0djhbQi6vc4YMB-fDgJ
And achieve State B as follows:
State Bmy-app
|
- emails
| |
| - email1@example,com
| |- old: "data"
| - email2@example,com
| |- old: "data"
| - email3@example,com
| |- new: "data"
| - email4@example,com
| |- new: "data"
- users
|
- c0djhbQi6vc4YMB-fDgJ
|
- emails
|
- email3@example,com
|- new: "data"
- email4@example,com
|- new: "data"
Notice: The {old: "data"}
is not overwritten.
Background
I seek to extend this SO question and answer.
There, we inserted a single node in a new location with three options:
- using
firebase-query
- JS SDK
- using
firebase-document
Now, I need to do the same type of insertion (without deletion or replacing old data) for multiple nodes (with a user defined, not autogenerated, key; i.e., keys are specific email addresses). I also need to use the data fan out technique to update multiple paths with a single write operation.
https://firebase.google.com/docs/database/web/read-and-write#update_specific_fieldsfunction writeNewPost(uid, username, picture, title, body) {
// A post entry.
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// * * * * * * * * * * * * * * * *
// THE ABOVE LINE NEEDS TO CHANGE TO SUPPORT USER-GENERATED KEYS SUCH AS EMAIL ADDRESSES
// * * * * * * * * * * * * * * * *
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
Also note, one of the comments mentions:
There's no reason
newPostKey
above couldn't be an email address...
The challenge is that I need to write multiple keys to multiple locations simultaneously in a single call.
The Firebase Realtime Database supports arbitrarily complex atomic deep updates (blog post). It works like so:
.update()
callSo let's take your example:
This will update all of the locations simultaneously. To make it dynamic, simply use string interpolation when constructing the keys.