try {
// Try to get the document
doc = await database.getDocument(DATABASE_ID, COLLECTION_ID, getUniqueId(movie.id));
// Update count if found,
await database.updateDocument(DATABASE_ID, COLLECTION_ID, doc.$id, {
count: doc.count + 1,
});
} catch (error) {
if (error.code === 404) {
// Document not found → create a new one
console.log("Document not found, creating new...");
await database.createDocument(DATABASE_ID, COLLECTION_ID, getUniqueId(movie.id), {
searchTerm,
count: 1,
movie_id: movie.id,
poster_url: `https://image.tmdb.org/t/p/w500${movie.poster_path}`,
});
} else {
// Some other error
throw error;
}
}