Back

[SOLVED] Send Array of object in document attribute?

  • 0
  • Databases
  • Web
bulk4966
8 Mar, 2023, 17:51

how do you send array of objects to the document or how do you set it up? Like this, [ { name:'abc', age:12 } ]

TL;DR
Solution: Each blog post should be stored as a separate document with its own unique ID. When saving a post, set the permissions for that user using `Role.user([USER_ID])`. Then, use the `Query` class to query the collection, which will automatically return an array of documents that belong to that user. Alternatively, you can store each blog post with the user's ID as an attribute and query for that attribute to filter the results. It is important to set document-level permissions on the collection to ensure security. To send an array of objects to the document, use the following format: ``` [ { name:'
Drake
8 Mar, 2023, 18:29

Array of objects? For what? The create and update document APIs run on 1 document.

bulk4966
8 Mar, 2023, 18:34

yeah. But if we need to have 1 documents per user and have all the data associated to that user in one document. Then how do we achieve that?

Lets assume I am writing a blog, I need to have a document listing all of my blogs as array of objects. if 2nd user logs in to the system, he should have the document of his blog posts. So that we can fetch it all at once.

makepies
8 Mar, 2023, 18:44

This is unconventional, esp in light of what I posted in <#564160731327758347> about having to specify max string length in whatever field you want to store your array. You might want to consider storing one document per blog, and setting document-level permissions on that blog so only the user can get their own blogs. That way you can simply run a Query on the collection to get all blogs from one user

bulk4966
8 Mar, 2023, 18:48

So it means, Each document will be a separate blog object, with a unique identifier being lets say user-id. And I will have to query it based on that userID

Drake
8 Mar, 2023, 18:49

ya, Appwrite's database works best with a flat design.

makepies
8 Mar, 2023, 18:53

Not quite. Store each blog post as a separate document with its own unique id - not the users. When saving a post, set the permissions for that user using Role.user([USER_ID]). Then use the Query class to query the collection. By default, since that user only has permissions to get their own documents, you'll get an array of documents from the Query response that contains all and only that one users records. You can of course store EACH blog post with the users id as an attribute and then query for that attribute to use that users id as a filter .... but unless you're reading these on the server, you'll still want to set document-level permissions on that collection. https://appwrite.io/docs/permissions

bulk4966
8 Mar, 2023, 21:14

Interesting. Now it makes sense. Thanks for the response.

Drake
8 Mar, 2023, 21:22

[SOLVED] Send Array of object in document attribute?

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