Back

Trying to get documents of specific user with admin credential

  • 0
  • Databases
  • Self Hosted
adminini
31 Aug, 2024, 17:01

help me fix it as i'm trying to get specifc last document of specific user... maybe it's wrong direction

TypeScript
     response = await database.list_documents(database_id, collection_id, [  Query.equal("$permissions.read", [f"user:{user_id}"]),Query.limit(1), Query.order_desc('$createdAt')])

Got - Error getting the latest document: Invalid query: Attribute not found in schema: $permissions ....

TL;DR
Developers are trying to retrieve documents for a specific user with admin credentials using Appwrite. They discuss storing documents with user IDs, using user IDs as document IDs, and querying permissions. A suggested solution is to use the user ID as part of the document ID and then fetch it accordingly. However, queries on permissions are not supported, leading to an issue with the provided code snippet.
Steven
31 Aug, 2024, 21:02

Uh ya, you can't query on permissions like that

adminini
31 Aug, 2024, 22:01

how can i? if i have collection that serve several users- isn't it the best way to filter on the server side per user?

adminini
31 Aug, 2024, 22:02

The walkaround i did is to add attribute of the user_id for the collection, which looks to me pretty ugly practice

Steven
31 Aug, 2024, 22:02

Sure but you can't do a query that isn't supported...

Steven
31 Aug, 2024, 22:03

Yes, that is one approach. Otherwise, I often use the user id for the document id

adminini
31 Aug, 2024, 22:05

So to put a prefix in the document id that is the userid? and then how to query that?

Steven
31 Aug, 2024, 22:32

No use it as the document id.

For example, I have one app where I have a collection of user documents. Each user should have 1 document. So, I use the user's id for that document id when I create the document in the users collection. Then, when I need to fetch the user's document, I use the get document call

adminini
31 Aug, 2024, 22:47

but if i got more?

adminini
31 Aug, 2024, 22:47

you suggest to store all the "documents" in one document? the problem is that there is limit on that array document

adminini
31 Aug, 2024, 22:48

I think it is a feature suggestion to add the query for permission, that will make the life much easier. Don't you think?

Steven
31 Aug, 2024, 22:48

So a user will have multiple? Will they have access to others?

Steven
31 Aug, 2024, 23:01
adminini
31 Aug, 2024, 23:32

No - they don't have permission - it's document level permission

adminini
31 Aug, 2024, 23:33

The real usecase happenning is when the server is adding data to users..and not the users themselfs..

Steven
1 Sep, 2024, 00:10

Ya having an attribute for the user id is probably the best approach

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