【问题标题】:How to design an appropriate Firebase/NoSQL data structure如何设计合适的 Firebase/NoSQL 数据结构
【发布时间】:2017-03-14 11:02:11
【问题描述】:

我是 Firebase 的新手,正在使用 NoSQL 数据库结构。我正在努力实现 Weight Lifting Tracker iOS 应用程序的最终目标。

该应用程序将容纳不同的用户,每个用户都会记录自己的锻炼情况,然后应该能够查看他们进行的锻炼的历史记录以及他们举起的重量,以便监控进度。

数据库将保存一个预先定义的练习列表,并能够添加独特的练习,以便它仅适用于该用户以及预定义的列表。

用户将能够定义一个例程,该例程是通过存储名称、创建日期、该锻炼中的锻炼以及锻炼之间休息的默认计时器来创建的。

然后用户将执行一个锻炼程序,他们将在其中实际选择上面创建的程序并跟踪他们举起的重量和组数,例如

Bench Press:
Set 1: 80kg x 8 reps
Set 2: 80kg x 8 reps
Set 3: 80kg x 6 reps
...

但是,用户可能并不总是在开始锻炼之前创建例程,因此他们将从预定义的锻炼列表中选择锻炼。

我的 NoSQL 数据库有以下结构,想知道我所做的是否正确,这是我第一次尝试为该问题设计模型,因此欢迎任何建议来帮助改进和可能解释,这样我就可以离开并改进整体设计:

{
  "users": {
    "user unique identifier": {
      "name": "User Full Name",
      "email": "User Email",
      "profileImageUrl": "Profile Image Url",
      "routines": {
        "routine unique identifier": true
      },
      "workout routine":{
        "workout routine unique identifier": true
      }
    }
  },
  "exercises": {
    "exercise unique identifier": {
      "exercise_name": "exercise name",
      "body_part": "body part",
      "routines": {
        "routine unique identifier": true
      }
    }
  },
  "routines": {
    "routine unique identifier": {
      "routine_name": "routine name",
      "routine_create_date": "routine created date",
      "exercises":{
        "exercise unique identifier": true
      },
      "default timer": "timer duration"
    }
  },
  "workout routine": {
    "workout routine unique identifier": {
      "date": "workout routine start date",
      "start_time": "workout routine start time",
      "end_time": "workout routine end time",
      "duration": "start_time - end_time",
      "end_date": "workout routine end date",
      "routine unique identifier": {
        "exercise unique identifier": {
          "exercise name": "exercise name",
          "set n weight": "weight lifted in set n"
          "set n reps": "Reps performed for set n",
        }
      }
    }
  }
}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    应该是数组,而不是对象

    • users.uid.workoutroutine
    • exercises.uid.routines
    • routines.uid.exercieses

    我建议您的 workoutroutine.uid 引用其他对象,而不是使它们成为内部的另一个对象。

    这样做的原因是您在转换对象时没有任何问题,在当前结构下,uid.routineId 将有不同的键(这可能会出现转换错误。

    您可以在 Firebase 的 Structure Data 文档中阅读有关最佳做法的更多信息

    编辑

    抱歉,我实际上是指使用类似于以下伪代码的定义的密钥对结构

    {users:
        [userid]:{
            ...
            workoutroutine:[{
                workoutroutineId:XXXX},
                ....
            ]
         }
    }
    

    这样,上述推理仍然有效,同时为您提供避免任何转换错误的灵活性。而且,正如@Frank-van-Puffelen 所说,没有一个正确的设计,将取决于您的应用程序。将其视为改进它的建议。

    【讨论】:

    • Firebase 文档非常明确地指出 not 使用数组:firebase.googleblog.com/2014/04/…
    • 我被数组而不是对象的建议弄糊涂了,@FrankvanPuffelen 你能就我的上述设计提出建议吗?
    • NoSQL 数据库没有单一的正确设计。这完全取决于您的应用程序想要如何使用数据。见NoSQL data modeling
    • @FrankvanPuffelen 上述结构是否可行,如果不可行,您会推荐什么结构?很抱歉问了很多问题,我真的很想了解我如何才能开始工作!
    • 它是一种数据结构——它本身并没有“有效”或无效。如果您在使特定用例在此数据结构上工作时遇到问题,请发布minimal code that reproduces that problem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2021-08-18
    • 2017-10-23
    • 1970-01-01
    • 2014-12-18
    • 2018-07-31
    • 1970-01-01
    相关资源
    最近更新 更多