gpt4 book ai didi

firebase - 将instagram之类的rdms转换为firebase nosql

转载 作者:行者123 更新时间:2023-12-04 20:28:56 24 4
gpt4 key购买 nike

我正计划使用Firebase开发类似于 instagram 的可用于生产的混合 ionic3 移动应用程序。我选择 firestore nosql来存储数据和查询。

我有在RDMS中设计架构的经验(忽略第二个绿色用户表)。但是我正在努力为我的应用程序设计 NoSql 模式。
下面我添加了RDMS模式,我想将其转换为nosql。
rdms table schema

我想有效地进行几个查询。

  • 在主页上显示用于访客 session 的最新照片列表。
  • 在首页中显示关注者照片的列表,随后是用户。
  • 列出被“tag”标记的照片

  • 上面列表的简单SQL查询
  • Select * from photo LIMIT=50

  • 为了有效查询,应如何将 照片用户表转换为Firestore NoSql或NoSql?

    最佳答案

    这是一个建议:

    一个名为photos的集合

    具有以下内容的photo文档:

  • 自动生成的ID
  • 包含日期(即时间戳记)的creation字段
  • 额外的字段,例如描述,URL/路径等。
  • 作为带有键-值对的对象的tags字段(例如{black: true, cats: true}),请参阅此帮助文档以了解基本原理:https://firebase.google.com/docs/firestore/solutions/arrays。以下HTML代码显示了如何使用/查询它
  • comments的子集合

  • 一个名为 users的集合

    具有以下内容的 user文档:
  • 用户ID(即身份验证中的uid)
  • 包含用户ID的followedUsers对象,后跟当前用户的{userId1: true, userId2: true}
  • 一个followedPhotosId对象,用于保存照片的ID,后跟当前用户的{9HzWzyXmcnBDuhTZKoQw: true, fO0OvzJs9M8p9N0jufte: true}
  • 一个followedPhotos子集合,其中包含后面的照片的详细信息

  • 以下HTML页面显示了如何执行您在帖子中列出的查询以及一些读取和写入数据的查询:这些最新查询应在多个Cloud Function中使用,这些Cloud Function专门用于在用户使用时使 followedPhotosId对象和 followedPhotos集合保持同步跟随一个新的其他用户和/或一个用户(随后有一个或多个用户)添加或删除照片。
    <html>

    <head>

    <!-- Firebase App is always required and must be first -->
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-app.js"></script>

    <!-- Add additional services that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-database.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-functions.js"></script>


    </head>

    <body>


    <script>
    // Initialize Firebase
    var config = {
    apiKey: "",
    authDomain: "xxxxx.firebaseapp.com",
    databaseURL: "https://xxxx.firebaseio.com",
    projectId: "xxxxxx"
    };

    firebase.initializeApp(config);

    var firestoredb = firebase.firestore();


    firestoredb.collection("photos").orderBy("creation", "desc")
    .get()
    .then(function(querySnapshot) {
    console.log("YOUR QUERY #1");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });


    firestoredb.collection("users").doc("user1").collection("followedPhotos")
    .get()
    .then(function(querySnapshot) {
    console.log("YOUR QUERY #2");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });


    firestoredb.collection("photos")
    .where('tags.cats', '==', true) //You should create a photo with a "cats" tag
    .get()
    .then(function(querySnapshot) {
    console.log("YOUR QUERY #3");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });




    firestoredb.collection("photos").orderBy("creation", "desc").limit(50)
    .get()
    .then(function(querySnapshot) {
    console.log("YOUR QUERY #4, i.e; Select * from photo LIMIT=50");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });

    firestoredb.collection("users")
    .where('followedUsers.user2', '==', true)
    .get()
    .then(function(querySnapshot) {
    console.log("Get all the users who follows user2. To use in the Cloud Function");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });


    firestoredb.collection("users")
    .where('followedPhotosId.9HzWzyXmcnBDuhTZKoQw', '==', true)
    .get()
    .then(function(querySnapshot) {
    console.log("Get all the users who follow photo with Id 9HzWzyXmcnBDuhTZKoQw. To use in the Cloud Function");
    querySnapshot.forEach(function(doc) {
    // doc.data() is never undefined for query doc snapshots
    console.log(doc.id, " => ", doc.data());
    });
    })
    .catch(function(error) {
    console.log("Error getting documents: ", error);
    });


    firestoredb.collection("users").doc("user1").get().then(function(doc) {
    if (doc.exists) {
    console.log("Update the followedPhotosId object for user1 after a user she/he follows has added a photo with id abcdefghijklmn. To use in the Cloud Function");
    var followedPhotosId = doc.data().followedPhotosId;;
    Object.assign(followedPhotosId, {abcdefghijklmn: true});
    firestoredb.collection("users").doc("user1").set({followedPhotosId: followedPhotosId});
    } else {
    // doc.data() will be undefined in this case
    console.log("No such document!");
    }
    }).catch(function(error) {
    console.log("Error getting document:", error);
    });




    </script>


    <body>
    </html>

    关于firebase - 将instagram之类的rdms转换为firebase nosql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438091/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com