bolt icon indicating copy to clipboard operation
bolt copied to clipboard

Ability to add indexes beneath an array/map

Open SteveMellross opened this issue 10 years ago • 1 comments

In relation to this question http://stackoverflow.com/q/34648125/3036007

It would be great to be able to build most of the structure using nested types but unfortunately indexes can not be applied beneath an array/map due to the automatically generated key names.

It would be great to have the ability to specify how an array/map field should be indexed in either the field definition or the definition of the type used by the array/map.

Maybe something like one of the following:

type Root {
    users: UserList;
}

type UserList extends Map<String, User> {
    index() = ["email"];
}

type User {
    email: String;
    name: String;
}

or

type Root {
    users: User[];
}

type User {
    index() = ["email"];
    email: String;
    name: String;
}

It would also be good to be able to control the name of the index key some how so the path can be explicitly referenced if needed. E.g. For users: User[]; the key could be $usersKey or $usersId. The plural makes it a bit non standard but at least it's deterministic and probably safer than using the type name.

SteveMellross avatar Jan 09 '16 04:01 SteveMellross

This should work:

path /users is User[] {
  index() = "email";
}

type User {
    email: String;
    name: String;
}

it produces:

{
  "rules": {
    "users": {
      "$key1": {
        ".validate": "newData.hasChildren(['email', 'name'])",
        "email": {
          ".validate": "newData.isString()"
        },
        "name": {
          ".validate": "newData.isString()"
        },
        "$other": {
          ".validate": "false"
        }
      },
      ".indexOn": [
        "email"
      ]
    }
  }
}

mckoss avatar Jan 10 '16 19:01 mckoss