如何使用Polymerfire将数据插入Firebase(多节点+多路径) [英] How to insert data into Firebase using Polymerfire (mutiple nodes + multiple paths)

查看:152
本文介绍了如何使用Polymerfire将数据插入Firebase(多节点+多路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是我有一个< iron-form> ,其中有一个< paper-textarea> 字段接受一个电子邮件地址的字符串列表,我分析到一个数组,然后我想:


  1. 存储单个电子邮件地址在我的Firebase中(用于索引和查找),
  2. 在多个位置(每个数据扇出技巧),
  3. 因为我不希望在列表很长的情况下进行100次API调用)和
  4. 而不覆盖任何现有数据。


    具体而言,我想从状态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
    {old:data}
    不会被覆盖。 $ b> p>

    背景



    我试图扩展这个问题和答案

    那里,我们插入一个单一的节点在一个新的位置有三个选项:

    $ ol

  5. 使用 firebase-query

  6. JS SDK

  7. 使用 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实时数据库支持任意复杂的原子深度更新(博客文章)。它的工作方式如下:


  1. 您可以使用一个 .update() call

  2. 更新映射关键字的完整路径将被替换,所以如果不想删除父对象, li>
  3. 路径与您目前的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:

  1. Store the individual email addresses in my Firebase (for indexing and lookup purposes),
  2. at multiple locations (per data fan out technique),
  3. with a single write operation (because I don't want to make 100 API calls if the list is that long) and
  4. without overwriting any existing data.

Specifically, I want to start with State A, as follows:

State A

my-app
 |
 - emails
 |  |
 |  - email1@example,com
 |     |- old: "data"
 |  - email2@example,com
 |     |- old: "data"
 - users
    |
    - c0djhbQi6vc4YMB-fDgJ

And achieve State B as follows:

State B

my-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:

  1. using firebase-query
  2. JS SDK
  3. 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.

Similar to what's shown here.

https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields

function 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:

  1. You can update any arbitrarily deep path with a single .update() call
  2. 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
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆