Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

firebase rule for unique property in firestore

I've found that question relatively often asked here, but i still cant figure out how to manage a rule for unique properties. I have following document datamodel:

users/{usereId}/Object
users/usernames/Object

The first Object contains basic information about the user, like:

{
email: "example@hotmail.edu"
photoURL: null
providerId: null
role: "admin"
username:"hello_world"
}

meanwhile the usernames objects only contains the username as the property key and the uid as the value, for instance:

{
hello_world:"F3YAm8ynF1fXaurezxaQTg8RzMB3"
}

I set it up this way, because I want that every user has a unique username. And its less time consuming iterating through the second object than through the first ones. But back to my issue. I need that hello_world is unique within the write operation. But my rules so far does not work. I have:

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /{document=**} {
      allow read, write: if request.auth.uid != null
    }
    
    match /users/{userID} {
        allow create: if !exists(/databases/$(database)/documents/users/$(request.resource.data.username)) <== does not apply
    }
    
  }
}

The second match is, what should apply the unique property rule. Has anyone an idea how to set the rule correctly?

In the console the object model looks as follows

enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...