Prithvi Uppalapati
Prithvi Uppalapati

Reputation: 800

Firebase rules to avoid Duplicate Entries

What are the firebase rules required to avoid duplicate entries in below users array at sList collection level

"sList" : {
    "-KZawgegLrIyq9h6GSf8" : {
      "name" : "Test",
      "users" : [ "-KZawhnFZLcqFKNwZnSi", "-KZawhnFZLcqFKNwZnSi", "-KZawhnFZLcqFKNwZnSi", "-KZawhnFZLcqFKNwZnSi", "-KZawxBSAwL-lbi7dF-h", "-KZawxBSAwL-lbi7dF-h", "-KZawxBSAwL-lbi7dF-h", "-KZawxBSAwL-lbi7dF-h", "-KZawxBgz8k7v8-fKpDV", "-KZawxBgz8k7v8-fKpDV", "-KZawxBgz8k7v8-fKpDV", "-KZawxBgz8k7v8-fKpDV" ]
    }
  }

Upvotes: 0

Views: 781

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

What you're trying to model is a set: a collection of unique entries.

What you've modeled is an array: a sequence of non-unique entries.

The simplest and best solution is to change your data model to actually reflect a set. The closest you can get to that in Firebase is:

"sList" : {
    "-KZawgegLrIyq9h6GSf8" : {
      "name" : "Test",
      "users" : { 
        "-KZawhnFZLcqFKNwZnSi": true, 
        "-KZawxBSAwL-lbi7dF-h": true, 
        "-KZawxBgz8k7v8-fKpDV": true
      }
    }
  }

With such a set-like structure, duplicates are automatically prevented by the data structure itself. You won't need to write security rules for that.

Upvotes: 1

Related Questions