用于文件上传的 Firebase 实时数据库结构 [英] Firebase Real Time Database Structure for File Upload
问题描述
我正在使用 AngularFire2 处理我的第一个 Firebase 项目.下面是我的学习项目的总体设计.
I am working on my first Firebase project using AngularFire2. Below is the overall design of my learning project.
- 用户上传照片并将其作为图像存储在 Firebase 存储中.
- 上传的照片列在主页中,按时间戳排序.
以下是我现在开始时的结构.但是我在做连接时感觉很困难.我应该能够获取每次上传的用户详细信息,并能够按时间戳对上传进行排序.
Below is the structure that I have now when I started. But I feel difficulty when doing joins. I should be able to get user details for each uploads and able to sort uploads by timestamp.
User:
- Name
- Email
- Avatar
Uploads:
- ImageURL
- User ID
- Time
我读了几篇非规范化数据结构的博客.对于我给定的场景,我怎样才能最好地重新建模我的数据库结构?
I read few blogs de-normalising the data structure. For my given scenario, how best can i re-model my database structure?
在新提议的解决方案中创建一些示例数据的任何示例都将有助于我的理解.
Any example for creating some sample data in the new proposed solution will be great for my understanding.
图像上传完成后,我将调用以下代码在数据库中创建一个条目.
Once the image upload is done, I am calling the below code to create an entry in the database.
addUpload(image: any): firebase.Promise<any> {
return firebase.database().ref('/userUploads').push({
user: firebase.auth().currentUser.uid,
image: image,
time: new Date().getTime()
});
}
我正在尝试加入 2 个实体,如下所示.我不确定如何有效和正确地做到这一点.
I am trying to join 2 entities as below. i am not sure how can I do it efficiently and correctly.
getUploads(): any {
const rootDef = this.db.database.ref();
const uploads = rootDef.child('userUploads').orderByChild('time');
uploads.on('child_added',snap => {
let userRef =rootDef.child('userProfile/' + snap.child('user').val());
userRef.once('value').then(userSnap => {
???? HOW TO HANDLE HERE
});
});
return ?????;
}
我想得到一个包含所有上传详细信息及其每次上传的相应用户数据的最终列表.
I would like to get a final list having all upload details and its corresponding user data for each upload.
推荐答案
我根据 Franks 的回答做了以下操作,并且有效.我不确定这是否是处理大量数据的最佳方式.
I did the following based on Franks answer and it works. I am not sure if this is the best way for dealing with large number of data.
getUploads() {
return new Promise((resolve, reject) => {
const rootDef = this.db.database.ref();
const uploadsRef = rootDef.child('userUploads').orderByChild('time');
const userRef = rootDef.child("userProfile");
var uploads = [];
uploadsRef.once("value").then((uploadSnaps) => {
uploadSnaps.forEach((uploadSnap) => {
var upload = uploadSnap.val();
userRef.child(uploadSnap.val().user).once("value").then((userSnap) => {
upload.displayName = userSnap.val().displayName;
upload.avatar = userSnap.val().avatar;
uploads.push(upload);
});
});
});
resolve(uploads);
});
}
这篇关于用于文件上传的 Firebase 实时数据库结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!