0

Programatically I've been able to implement rules and a client side query for my realtime database that can find if a Username under a users table exists or not. Before creating the ".indexOn": "username" rule, I was getting a warning that the client was downloading all the data and filtering it client side.

My question is what exactly does the ".indexOn" do? I'm not getting that warning anymore, so I'm curious what is being sent to the client now? Is it only the usernames and nothing else that's being sent and filtered?

Why is the filtering/querying not being done on the firebase server and sending back the resulting user instead, wouldn't that be more efficient/safe?

My query is

db.database.ref(`/users`).orderByChild('username').equalTo(control.value)
                .once("value").then(res => {
                    return res.exists() ? {usernameTaken : true} : null;
                })

and my data is structured as:

"users": {
    "UID": {
        "username" : "exampleName"
    }
}

Just trying to get a better understanding of how/what is happening, thank you!

Please let me know if more information is needed.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

According to the documentation on indexing:

Firebase allows you to do ad-hoc queries on your data using an arbitrary child key. If you know in advance what your indexes will be, you can define them via the .indexOn rule in your Firebase Realtime Database Rules to improve query performance.

Realtime Database doesn't know in advance all the kinds of queries you might want to perform. It creates some indexes ahead of time, but you need to tell it when you want to query with nested children with orderByChild orderByValue.

The client isn't sending anything different. Instead, the server is now equipped with an index that can perform the query efficiently without forcing the client to receive all the data and do the filtering and ordering itself.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441