Security Rules: unique() operator
I am submitting a feature request to introduce a 'unique()' operator in Firestore's security rules, offering a streamlined method to enforce data uniqueness within collections. This operator would not only simplify validation and potentially reduce reads by leveraging Firestore's hidden index table, but also eliminate the need for a separate cloud function, similar to the 'get()' and 'exists()' operations.
Background:
At present, ensuring data uniqueness in Firestore often requires workarounds that are less than optimal and can impact scalability.
Feature Proposal:
The proposed 'unique()' operator would operate much like the 'exists()' operation, but with a focus on reading Firestore's concealed index table. This efficient approach would verify the uniqueness of specified fields within collections.
Comparison to Existing Operations:
Similar to the 'get()' and 'exists()' operations, the 'unique()' operator would provide developers a straightforward means to validate crucial data points such as usernames, email addresses, and more, all without the need for complex cloud functions.
Example Usage:
For instance, to ascertain unique usernames:
Firestore Security Rules:
function isUniqueUsername(username) {
return unique(/databases/$(database)/documents/users, ['username', '==', username]);
}
Benefits:
Introducing the 'unique()' operator not only simplifies the validation process and leverages the hidden index table but also eliminates the requirement for an additional cloud function. This comprehensive approach enhances Firestore's performance while maintaining developer-friendly functionalities.
Conclusion:
By implementing the 'unique()' operator, Firestore could effectively address the challenge of maintaining data uniqueness while offering a holistic solution that eliminates the need for cloud functions.
Thank you for considering this feature request. I'm optimistic about the significant value it could add to Firestore and our development workflows.
-
Patrícia Charles avelar Sol commented
Estalonele
-
Dennis Kugelmann commented
I didn't know that I needed it until I saw this 😍
This would be so useful!