我计划使用firebase开发类似于instagram的生产就绪混合离子3移动应用程序.我选择firestore nosql来存储数据和查询.
我有在RDMS中设计模式的经验(忽略第二个绿色用户表).但我正在努力为我的应用程序设计Nosql架构.
下面我添加了我想将其转换为nosql的RDMS模式.
我想要有效地进行多次查询.
>在主页的会话会议中显示最新照片列表.
>在主页中显示用户关注的关注者照片列表.
>列出标签为“tag”的照片
以上列表的简单SQL查询
>从照片LIMIT = 50中选择*
这是一个提议:
One Collection名为照片
照片文件有:
>自动生成的ID
>包含日期(即时间戳)的创建字段
>额外的字段,如描述,网址/路径等.
>标签字段作为具有键值对的对象,如{black:true,cats:true},请参阅此帮助文档以获取基本原理:https://firebase.google.com/docs/firestore/solutions/arrays.下面的HTML代码显示了如何使用/查询它
>评论的子集合
One Collection命名为users
用户文档包含:
>用户的ID(即来自身份验证的uid)
>一个followUsers对象,其中包含用户的id,后跟当前用户{userId1:true,userId2:true}
>一个followPhotosId对象,保存照片的id,然后是当前用户{9HzWzyXmcnBDuhTZKoQw:true,fO0OvzJs9M8p9N0jufte:true}
>一个followPhotos子集合,其中包含所关注照片的详细信息
以下HTML页面显示了如何执行您在帖子中列出的查询,以及一些查询和写入数据的查询:这些最新查询应该用于多个云函数,这些函数专用于保持followPhotosId对象并在用户时跟随照片集合同步跟随新的其他用户和/或当用户(后面跟着一个或多个用户)添加或删除照片时.
<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,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,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,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,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>