SteveMellross
SteveMellross

Reputation: 3473

Is there any way to reliably specify an index within a nested Array (or Map) in Firebase Bolt?

Creating an index on an array is pretty straight forwards:

type Root { level_1: Level_1[]; }
type Level_1 { fieldToBeIndexed: String; }

path / is Root {}
path /level_1 { index() = ["fieldToBeIndexed"]; }

Giving the following (with validation removed for readability):

{
  "rules": {
    "level_1": {
      "$key1": {
        "fieldToBeIndexed": {}
      },
      ".indexOn": ["fieldToBeIndexed"]
    }
  }
}

But because an automatically generated key ($key1) is used for the array it means we can't reliably reference any path below it in case changes in the structure (or implementation) cause the key's name to change.

Since it seems indexes can only be defined in path statements does that mean it is not possible to index fieldToBeIndexed in the following scenario?

type Root { level_1: Level_1[]; }
type Level_1 { level_2: Level_2[]; }
type Level_2 { fieldToBeIndexed: String; }

path / is Root {}

Which translates to:

{
  "rules": {
    "level_1": {
      "$key1": {
        "level_2": {
          "$key2": {
            "fieldToBeIndexed": {}
          }
          // How to get the index here?
        }
      }
    }
  }
}

Upvotes: 3

Views: 126

Answers (1)

mckoss
mckoss

Reputation: 7034

You're right - in order to place an index BENEATH a wildcard path, you have to specifically name the wildcard so you can use it in a path statement. If you combine types and paths for the same location - you should allow for a type of Any | Null for the property that has a separate path statement.

type Level1 {
  fieldToBeIndexed: String;
  level_2: Any | Null;
}

type Level2 {
  anotherIndexedField: String;
}

path /level_1 {
  index() = "fieldToBeIndexed";

  /$primary is Level1 {
    /level_2 {
      index() = "anotherIndexedField";

      /$secondary is Level2;
    }
  }
}

Upvotes: 3

Related Questions