Back

How to create a document if it doesn't exist and return ID if it does?

  • 0
  • Databases
  • Web
illmagination
7 Aug, 2023, 14:06

I have an app where users can bookmark webpages. When a user bookmarks a page I'd like to save that page in the database, then get the ID of that document and add it to their profile. That's not an issue. What I want to do is if a second user bookmarks the same page I want to check the bookmarks table to see it it already exists. If it does I want to use the existing ID for user 2 and if it doesn't already exist I want to create it and then update the user's profile with the newly created ID. I believe supabase has something like createIfNotExists. Is there something similar in appwrite? I'm using nextjs

TL;DR
In this support thread, the user wants to know if there is a way to create a document if it doesn't exist and return the ID if it does in Appwrite. Suggestions from other users include: 1. Trying to create the document and if there's an error, fetching the ID. 2. Trying to fetch the document by URL first, and if no results are found, create it. 3. Moving steps 1 and 2 to a cloud function. The user suggests that this feature would be useful, similar to what Supbase offers. They couldn't find an existing solution and are directed to create a feature request on App
Binyamin
7 Aug, 2023, 14:54

In Appwrite you can:

  1. Try to create it and in case you'll get error then fetch the ID.
  2. Try to first fetch it by URL if no results then create it
  3. Move 1 and 2 to a cloud function

That said, it seems like a great feature to have, like Supbase doest. I didn't find one regarding that issue, you can create one here https://github.com/appwrite/appwrite/issues

joeyouss
8 Aug, 2023, 09:49

@illmagination does this seem okay for you?

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