Home > Mobile >  Structuring data in Firebase Firestore for "File explorer/Google Drive" style application
Structuring data in Firebase Firestore for "File explorer/Google Drive" style application

Time:05-31

So recently I started using Firebase with NOSQL database for my flutter project. I'd like to create something of a shape of Windows file explorer, where basicly user is able to create "folder", or file of one type, and inside each folder he has the same option. In the end user will be able to organize his workspace however he wants. Poorly created visualisation

At first my idea was to create collection of "Folder/Files", where each documment there would have Parent and Children parameters, with documment ID's referencing other documments. But I feel like this would create a lot of unnecessary querries to database.

Now I'm thinking of creating Collection "Structures" that would basicly have all data in array/list style, but then I feel like I would have trouble getting all the data and handling it.

Also, this project is about wine storage, where each user would basicly recreate their real life wine cellar with all wine cabinets and shelfs, and then be able to filter out/search for the bottles that are neede at given time.

Currently my Firestore looks like this: Firestroe Database

WineVaults are basicly Wineries, so when the user logs in, he sees those, which he has access to, then when he clicks it, he should go inside of that structure, and be able to create Folders/Wine bottles inside.

CodePudding user response:

The question's title is misleading, relative to what you're actually after. a "File Explorer" means there's an infinitely-sized tree with an unknown size vertically and horizontally. What you're actually after is a tree with a fixed vertical size (Winery > Folder > Bottle) and an infinite horizontal size.

As such, I would structure the data like this: //wineries/shelves/bottle.

So for example, you might have a structure similar to the bellow, with wineries, shelves, and bottles being collections (and sub-collections):

{
  "wineries": [
    "wineryId": {...},
    "wineryId": {
      "address": "",
      "shelves": [
        "shelfId": {...}
        "shelfId": {
          "bottles": [
            "bottleId": {
              "label": "",
              "name": "",
              "year" : ""
            },
          ],
          "description": "",
          "location": ""
        }
      ]
    }
  ]
}

Note that one of the nice things of Firestore is that you can use Collection Groups to fetch all bottles in the system, for example. Even if they are "located" in a nested sub-collection somewhere.

CodePudding user response:

I think that your original idea was good. Keeping all the shelves, bottles in an array/list/map have the following limitations:

  1. firesotre documents have a size limit of 1MB - what would you do when your user will reach that limit?
  2. every time a user is searching / modifying his data you need to read and write his whole inventory

Your original idea will create more reads, but you only need to read the next level in the tree not the whole thing.

I also like the flexibility of the original idea

If I were to implement this, I would create a user collection, a "folder"/"node" collection (for wineries, shelves etc...) and a "file" collection for the bottles.

If you need security (allowing a user to see only his files/folders) it can be a sub-collection under the user.

A DB intent is to store data and allow you to search the data, I don't think that downloading all of the user's data and searching it manually is a good idea

  • Related