如何使用Polymerfire将数据插入Firebase(多节点+多路径) [英] How to insert data into Firebase using Polymerfire (mutiple nodes + multiple paths)
问题描述
我的用例是我有一个< iron-form>
,其中有一个< paper-textarea>
字段接受一个电子邮件地址的字符串列表,我分析到一个数组,然后我想:
- 存储单个电子邮件地址在我的Firebase中(用于索引和查找),
- 在多个位置(每个数据扇出技巧),
- 因为我不希望在列表很长的情况下进行100次API调用)和
- 而不覆盖任何现有数据。
具体而言,我想从状态A开始,如下所示:
状态A
my-app
|
- 电子邮件
| |
| - email1 @ example,com
| | - 旧:数据
| - email2 @ example,com
| | - 旧:数据
- 用户
|
- c0djhbQi6vc4YMB-fDgJ
实现B状态如下:
州B
my-app
|
- 电子邮件
| |
| - email1 @ example,com
| | - 旧:数据
| - email2 @ example,com
| | - 旧:数据
| - email3 @ example,com
| | - new:data
| - email4 @ example,com
| | - 新:数据
- 用户
|
- c0djhbQi6vc4YMB-fDgJ
|
- 电子邮件
|
- email3 @ example,com
| - new:data
- email4 @ example,com
| - new:data
$ c注意:{old:data}
不会被覆盖。 $ b> p>
背景
我试图扩展这个问题和答案。
那里,我们插入一个单一的节点在一个新的位置有三个选项:
$ ol
- 使用
firebase-query
- JS SDK
- 使用
firebase-document
- 使用
现在,我需要对于多个节点(具有用户定义的而不是自动生成的密钥;即,密钥是特定的电子邮件地址),执行相同类型的插入(不删除或替换旧数据)。我还需要使用数据扇出技术来使用单个写入操作来更新多个路径。
与此处显示的内容类似
https://firebase.google .com / docs / database / web / read-and-write#update_specific_fields
函数writeNewPost(uid,username,picture,title,body){
//一个帖子条目。
var postData = {
author:username,
uid:uid,
body:body,
title:title,
starCount:0,
authorPic:picture
};
//获取新帖子的关键字。
var newPostKey = firebase.database()。ref()。child('posts')。push()。key;
// * * * * * * * * * * * * * * * *
//上面的行需要更改以支持用户生成的密钥,例如电子邮件地址
// * * * * * * * * * * * * * * * *
//在帖子列表和用户的帖子列表中同时写入新帖子的数据。
var updates = {};
updates ['/ posts /'+ newPostKey] = postData;
updates ['/ user-posts /'+ uid +'/'+ newPostKey] = postData;
返回firebase.database()。ref()。update(updates);
$ / code>
另外请注意,其中一个评论提到:
没有任何理由
newPostKey
不能成为电子邮件地址...
现在的挑战是我需要在一次调用中同时将多个键写入多个位置。
Firebase实时数据库支持任意复杂的原子深度更新(博客文章)。它的工作方式如下:
- 您可以使用一个
.update()$更新任意深度的路径c $ c> call
- 更新映射关键字的完整路径将被替换,所以如果不想删除父对象, li>
- 路径与您目前的ref相关
b
$ b
var update = {};
update ['emails / email3 @ example,com'] = {new:'data'};
update ['emails / email4 @ example,com'] = {new:'data'};
update ['users / c0djhbQi6vc4YMB-fDgJ / emails / email3 @ example,com'] = {new:'data'};
update ['users / c0djhbQi6vc4YMB-fDgJ / emails / email4 @ example,com'] = {new:'data'};
firebase.database()。ref()。update(update);
这将同时更新所有的位置。为了使其动态化,只需在构造键时使用字符串插值。
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:
- You can update any arbitrarily deep path with a single
.update()
call - The full path on the key side of your update map will be replaced, so you must address subkeys directly if you don't want to blow away the parent
- Paths are relative to your current ref
So let's take your example:
var update = {};
update['emails/email3@example,com'] = {new: 'data'};
update['emails/email4@example,com'] = {new: 'data'};
update['users/c0djhbQi6vc4YMB-fDgJ/emails/email3@example,com'] = {new: 'data'};
update['users/c0djhbQi6vc4YMB-fDgJ/emails/email4@example,com'] = {new: 'data'};
firebase.database().ref().update(update);
This will update all of the locations simultaneously. To make it dynamic, simply use string interpolation when constructing the keys.
这篇关于如何使用Polymerfire将数据插入Firebase(多节点+多路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!