- Mastering Firebase for Android Development
- Ashok Kumar S
- 183字
- 2021-06-18 18:49:13
Customizing database rules
Database rules are versatile and powerful and if we want to customize certain operations we can achieve it through rules. For instance, if we require giving access to read all the data and no write access to users, we can achieve this by using the following rules:
{
"rules": {
".read": true,
".write": false
}
}
The preceding rules will allow the user to have a read access to the database. Since there isn't any path specified, the complete database is readable, but not writable. If we want to customize on node basis we can take the node name into the rules and we can give the rules as follows. Test the rules in the simulator before publishing them:
{
"rules": {
".write" : false,
"Donor" : {
".read" : true
}
}
}
The following screenshot illustrates custom rules for read-write access to a particular node:
We can customize the rules to a level where we can specify which node needs to be read, which node can be written, which cannot be written, and a lot more.