Skip to content
Back

How to Fetch Item and Include the User/Author with JS

  • 0
  • Databases
  • React Native
psygo
6 May, 2025, 07:16

A typical workflow with ORMs is to, for example, fetch an item from the DB and include the user who created it. How would I do that with AppWrite? Is that easily available in the docs somewhere?

Here's an example:

TypeScript
const response = await databases.getDocument(
  DATABASE_ID,
  BOOKS_COLLECTION_ID,
  id,
  // Maybe I should add some sort of query here?
)
TL;DR
Developers are looking for a way to include the user or author when fetching an item using JavaScript in Appwrite. A feature request for joins has been suggested but an alternative solution is to establish a link via an attribute. Using Query.equal in the JS package, the user can fetch data based on the author's ID saved in the book document. While a JOIN equivalent isn't currently available, developers can achieve this functionality by saving the author's ID in the book document and using a Query.equal statement to fetch the corresponding user data. For detailed information, developers can refer to the provided links and documentation on databases queries on Appwrite's website.
psygo
6 May, 2025, 07:20

Also, do I need to create a table on the DB to fetch info on other users?

Axistro
6 May, 2025, 08:58

You need to have an attribute representing the creator of the doc.

Axistro
6 May, 2025, 08:59

Could you share more details about your usecases?

psygo
6 May, 2025, 09:04

For now, it's as simple as it gets. I have users, each user can create a book (which consists of a title, description, and the author's ID). Then, when a user click's on a book's page, I would like to show the book itself, as well as its author's username. It should be equivalent to a simple SQL JOIN.

psygo
6 May, 2025, 09:05

Drizzle and Prisma both have that sort of thing in their docs, but I haven't been able to find it on Appwrite yet.

psygo
6 May, 2025, 09:06

I did create a second table for user data, replicating and extending the auth data.

Axistro
6 May, 2025, 09:07

You could use relationship But doing it with saving the id of the author/user in the book document will be far more stable.

Axistro
6 May, 2025, 09:07

Fetch the doc using query.equal of userid with the creator id in your books collection

psygo
6 May, 2025, 09:08

I do save the author's id in the book document.

psygo
6 May, 2025, 09:08

How do I do that in the JS package?

Axistro
6 May, 2025, 09:10
TypeScript
const response = await databases.listDocument(
  DATABASE_ID,
  BOOKS_COLLECTION_ID,
  [
        Query.equal('Author':['name']),
        
    ]
)
Axistro
6 May, 2025, 09:11

By default there will be upto 25 docuement present if condition meets You could decrease or increase using Query.limit

Axistro
6 May, 2025, 09:12

Check out here for more details

psygo
6 May, 2025, 09:16

I don't think that's what I want actually. That's a filter, what I'm looking for is a JOIN statement equivalent between 2 tables. That sort of thing only tackles 1 table.

Axistro
6 May, 2025, 09:18
psygo
6 May, 2025, 09:28

That's so essential to these sorts of products though. Very strange. It should be kinda like the first thing on a feature list priority queue, I believe.

Axistro
6 May, 2025, 09:47

You could make a feature request in github 🤷‍♂️.

psygo
6 May, 2025, 09:57

How do they plan on dealing with that sort of issue. Do they plan on implementing joins at some point? Do they have an alternative solution? Is this how it's going to be forever?

Axistro
6 May, 2025, 10:05

The best i can say is if The Join feature request is more wanted by users then it might be added in the future version But currently the way is to establish a link via atrribute

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