Back

Storing Dictionary (or Map) as Attribute in Appwrite Collection

  • 1
  • Databases
  • Flutter
  • Cloud
tom
8 Jan, 2024, 03:09

Hi Appwrite Team! I have a collection to store information about users of my app, including name, username, and followers. The 'name' and 'username' attributes are simple strings, while the 'followers' attribute is of type String, but with Array checked off. That way, the 'followers' attribute stores a list of strings (other user ids who follow that user). That all makes perfect sense.

Suppose there is additional information about users that is stored in a dictionary (or map), like preferences.

userPrefs = {'likesDogs' : 'yes', 'likesCats' : 'no', 'likeChineseFood' : 'yes', ...}

Is there a way to store this userPrefs map as a single attribute in Appwrite rather than having to create a unique attribute for every key in the userPrefs map? Thanks for the help!

TL;DR
Is it possible to store a dictionary (map) as an attribute in an Appwrite collection instead of creating a separate attribute for each key in the dictionary? Is it best practice to create a separate collection for userPrefs and relate it to the main collection using a userID?
tom
8 Jan, 2024, 03:18

In reality, the userPrefs map I am working with has dozens of keys, with different data types as values.

Is best-practice here to create a separate collection called userPrefs and unpack the key, value pairs there? And then include a userID for who it corresponds to, so I can relate the two collections?

Drake
10 Jan, 2024, 02:25

It depends on how you'll be using the data. If you need to query it, you'll need a collection. If you don't, you could store it as a JSON string

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more