用于文件上传的Firebase实时数据库结构 [英] Firebase Real Time Database Structure for File Upload
问题描述
我正在使用AngularFire2开发我的第一个Firebase项目。以下是我的学习项目的总体设计。
$ ul
下面是我现在的结构当我开始。但是在加入时感到困难。我应该能够获得每个上传的用户详细信息,并能够按时间戳对上传进行分类。
用户:
- 名称
- 电子邮件
- 头像
上传:
- ImageURL
- 用户名
- 时间
我读了很多博客去规范数据结构。对于我的情况,我如何最好地重新建模我的数据库结构?
在新建议的解决方案中创建一些示例数据的任何示例都是非常好的理解。
完成图片上传后,我会调用以下代码在数据库中创建一个条目。
addUpload(image:any):firebase.Promise< any> {
return firebase.database()。ref('/ userUploads')。push({
user:firebase.auth()。currentUser.uid,
image:image,
time:new Date()。getTime()
});
}
我试图加入2个实体,如下所示。我不知道如何有效和正确地做到这一点。
getUploads():any {
const rootDef = this.db.database.ref();
const uploads = rootDef.child('userUploads')。orderByChild('time');
$ b uploads.on('child_added',snap => {
let userRef = rootDef.child('userProfile /'+ snap.child('user')。val()) ;
userRef.once('value')。then(userSnap => {
????如何处理
});
});
return ?????;
$ / code>
我想得到一个包含所有上传细节和相应用户的最终列表数据为每个上传。
我做了以下基于弗兰克答案,它的工作原理。我不确定这是处理大量数据的最佳方式。
getUploads(){
返回新的Promise((resolve,reject)=> {
const rootDef = this.db.database.ref();
const uploadsRef = rootDef.child('userUploads')。 orderByChild('time');
const userRef = rootDef.child(userProfile);
var uploads = [];
$ b $ uploadsRef.once(value)。然后((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);
});
});
});
解析(上传);
});
}
I am working on my first Firebase project using AngularFire2. Below is the overall design of my learning project.
- Users uploads photos and it's stored in the Firebase storage as images.
- The uploaded photos are listed in the homepage sorted based on timestamp.
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()
});
}
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.
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屋!