Firebase-Firestore 结构化

Firebase-Firestore structuring

假设我有多个公司,每个公司都有多个员工、多条车辆、多条路线、出发地、捕获的损坏和任务(谁必须开车游览)。

我应该如何构建我的 Firestore 以使其简单易懂?当前结构看起来有点像这样:

我有多个 collections,它们包含所有不同的公司、所有车辆、所有员工等等...

- employees:
  - employyeDocuments
    - field with companyID (reference to the company document)
    - field with employeeID (the uid of that employee, used for identification)
- companies:
  - companyDocuments
    - array of all the vehicles (each element is a reference to a vehicle in the vehicle collection)
- assignments:
  - assignmentDocuments
    - field with employeeID
    - field with companyID
    - field with vehicleID
    - field with routeID (all IDs are document references to the according documents)
- routes:
  - routeDocuments
    - information about the route, waypoints, delivery options... (but NO companyID or employeeID, since that route should be reused and is assigned to an employee in assignments)
- damages:
  - damageDocuments
    - any recorded damages

collections和文档比较多,但都遵循类似的规则,这里就不再赘述了。我现在的问题是:是否有更好的方法来构建它以使其更易于阅读并避免意外混淆公司或员工?还是我使用的方法正确?我为我失去概览或数据库变得一团糟而感到难过。

教育会很棒!

总的来说,您的结构看起来不错,但这可能是“多对多”的情况,可以通过多种方式解决。根据经验,保持简单但模块化一直是关键。

保留文档名称作为该实体的 UID 的文档是一种常见的久经考验的解决方案。 简单地引用一个 id 作为字段 属性,公司员工的 id 数组等

您还应该通过将所有当前员工文档合并到雇主或 HR 的主文档中来管理主员工文档,并通过查询可能不在列表中的任何新员工来更新它。

这种 restful 方法有助于减少维护需求并提高整体性能。