Appwrite Database Tutorial #5 - Deleting Documents

Net Ninja · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

Deletes documents from an Appwrite database using a delete action

Full Transcript

okay then my friends so now we can add new note documents and we can fetch them all as well when the page first loads the next thing I'd like to show you is how we can delete documents from the database when we click on these notes in the browser so just like with the other two database interactions we'll be making an action to delete documents inside this no actions file and then we'll be invoking that action from inside the notel list component whenever a user clicks on a note so then in the note actions file let's export another async function fun and this time we're going to call that function delete note now as an argument to this function I want to pass in the ID of the note that we want to delete so that app right can find that document in the collection to delete it so we'll call this argument note ID and that's going to be a string and that document ID by the way is going to be the one that aight generated for us when we created new documents anyway we're not going to explicitly return a value from this action so we won't add the return typon and then it's going to default to a promise that resolves to void and we can see that if we hover over the function name so then now we need to perform this delete request in here and it's really simple to do we can just say await and then we're going to use the databases instance which we already imported into this file and we're going to use a method on that called delete documents and invoke it and this method takes in three arguments the first is the database ID as a string which which in my case is just Notes app the second is the collection ID as a string which is just notes and the third is the ID of the document that we want to delete within that collection and we accept that as an argument to this function note ID so we can just pass that in as the third argument right here and that's pretty much it my friends now when we invoke this action it's going to go ahead and delete a document from the notes collection so then if we go back to the not list components you're going to see that when we output the list of notes we do two things first we assign an ID attribute to each list item for each notes and the value of that is the ID of that note using the dollar sign ID property second we attach a click event to each list item which fires a handle delete function and passes in the ID of the not we clicked on as an argument so then inside that handle delete function we accept that no ID but we don't really do anything with it yet now the first thing I'm going to do in here is paste in a tiny bit of code for stylistic purposes when we delete a notes and what this bit of code does is grab the element we clicked on by finding the element in the Dom using the get element by ID method and we just pass in the ID of the note because remember we appli the ID attribute down here on the LI tag once we have the element we check it actually exists and then we add a class to the element called cross outs and in the CSS file that class adds a strike through to the note so it looks like we're crossing it out now after we've done that we also want to invoke the delete note action to go ahead and delete the note from the database so let's say await and then we're going to invoke the delete note function and when you click on this it should Auto Import the action for you if not make sure you import it at the top of the file otherwise this ain't going to work all right cool now we just need to pass the notes ID as an argument to the action which we can do and now I think that's pretty much everything so we can save this and try it out in a browser all right so now if I click on one of these like sleep more then it's going to cross it out and that's the CSS um because we applied that conditional class that crossed out class when we tried to delete something it's not gone from the page but that's because we're not updating the state within the component the page itself but we are still deleting the document from aight I can demo that by coming over here and you can see it was which one was it buy some milk or sleep more that I crossed out sleep more so this document right here whenever refresh should be gone which it is and also when I refresh over here then it should be gone as well because remember when we first load the page we fetch the documents from scratch and since it doesn't exist over here anymore then we're not fetching that document we don't see it on the page anymore so we are successfully deleting those documents now and this is the same kind of thing as when we added some documents right if we Tred to add something then although it gets added over here we don't see it straight away over here we have to refresh to refetch those documents and then we see so we are going to combat this using real time uh subscriptions in the next lesson whereby when we make changes to the database the database notifies us of those changes and we can update the state locally within the component to reflect that change in the browser as well

Original Description

Appwrite is a backend-as-a-service platform, that provides database, authentication, storage, cloud functions and other services. In this Appwrite tutorial, we'll be exploring how to set up and use an Appwrite database. 🔥🥷🏼Sign up to Appwrite for free here - https://apwr.dev/netninja50 🧠🥷🏼Appwrite docs - https://appwrite.io/docs 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/appwrite-tutorial 📂🥷🏼 Starter project on GitHub: https://github.com/iamshaunjp/appwrite-tutorial/tree/starter-project
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →