Appwrite Database Tutorial #3 - Creating New Documents

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

Key Takeaways

The video demonstrates how to create new documents in an Appwrite database using Next.js and JavaScript, covering topics such as NoSQL database interaction, actions feature, and document creation.

Full Transcript

okay then so now we've set up the connection to the app right project let's go ahead and start adding some data to it now app right uses a nosql database and within that we have collections of documents where each document is a bit like a JavaScript object with key value pairs so we already set up a database collection in the first lesson called notes and we just want to now start adding documents to that now to do that we need two things the database ID and the collection ID so that we can specify EX exactly where we want to save these new documents to find those first head to your console and then you also need to select your project once you've done that go to databases which lists all of the databases that you've created for this project and you can see the database ID right here so copy and paste that in notepad or something because we'll be needing that shortly next click on the database to see a list of collections inside it and we've only got one collection called notes with an idea of notes as as well so copy that value down too because we'll be using that one as well okay so now we have those two values let's head back to the code and add the logic to save new documents all right then so we already have a new note form component set up and ready to go so inside here we're storing whatever user types into the text area using some States called content and that updates every time the text area value changes and currently when this form gets submitted it triggers a handle submit function which then prevents the default action of the page refreshing after that it checks the content isn't empty and then resets the value of the content so it's inside this if check right here before we reset the content value that we need to save that new content as a note document to the database now instead of writing all of that logic inside this function instead I'm going to create an action for this logic and put all of that in inside an actions file Now actions are a relatively new feature of nextjs and they're basically functions which can be run on the server and return a value to the clients and we can invoke those actions directly from client components like this one so then let's make a new actions folder over here to store this action file inside of and then inside that folder we want to create a new file called note actions. TS and now for any database interaction that we need we're going to make an action for that inside this file so then to make an action we just need to export a function and put the action logic inside that function to begin with we'll export an async function which I'm going to call add notes and this function should take in an argument called content and the type of that content should be a string now the return value is going to be a promise because this is an asynchronous function but that promise will eventually resolve to be a note object and that's going to be the note that we save to the database now remember at the start of this course I showed you a file down here called types. D.S where we registered a custom interface or type if you like that we can use in this application called notes and that interface said that any note objects must have these three Fields an ID field a created app field and a Content field all strings also notice these first two Fields have dollar signs at the start and that's because these are Fields generated by app right and ight adds these dollar signs to the fields to mark them as such anyway back inside the actions file we're saying the return value will eventually resolve to be of type notes with those three Fields now one thing before we go any further I've just noticed we have this utils folder right here where we have this app right file that we set up in the last lesson and I've actually put this utils folder inside the app folder but normally what I like to do is put that inside the source folder outside of the app folder as well the app folder I like to keep for things like components and actions then any utility files or service files I generally put just inside the source folder outside of the app folder so inside this function then we need to somehow create a new document inside the database and to do that we need to import the databases instance we created in the previous lesson from inside the ight file remember we exported the databases value right here and that's what we now need to import in the actions file so we can use it to interact with the database so now at the top of the actions file we can say import that we want curly braces and we want to import databases and that comes from slaight okay then we can come back down here and we can just say const uh response is equal to databases which we just imported and then we can use a method on that called create documents now this method takes in a few different arguments the first one is the database ID as a string value which for me is Notes app the second one is the collection ID as a string value which in my case is just notes remember I showed you how to get both of these ID values at the start of this lesson we also need to pass in two more arguments as well the ID of the new documents and also an object to represent the new object or document we want to save so we can tell app right to generate a unique ID for us by saying ID capitals do unique and then invoking that method but we also need to import this ID thing right here from ight in order to use it so we'll put the top of the file we can just say import curly braces and then we want ID and capitals and that just comes from the ight package we installed all right cool so finally we just need to pass in the object which we want to save as the documents inside the collection so let's actually make that new object up here at the top of the function by saying const new note is equal to just an object and I want to add a Content field to this object to specify the note contents and the value of that is just the content argument we take into this action so that's the only field we need to add and then appar right's going to add on an ID field for us as well as a couple of other automatic Fields as well which we'll see shortly anyway now we can pass that new no object in as a final argument right here and now when we invoke this action it's going to reach into the notes collection and save this new note document and it's going to apply an autogenerated unique ID to it as well now as a response to this create document function we actually get that new note document back from app right and it contains a bunch of different properties on it created by app right along with the content property we added and that document now gets stored in this response constant so we can access all those properties directly on it now remember this function returns a promise which eventually resolves to a note object so what we'll do is create a new object by saying const note is equal to an empty object and then inside this we can just add the properties of a note object which remember if we take a look at the types file are the dollar sign ID dollar sign created app and also the content and the first two properties have that dollar sign in front of them remember because ight generates those fields s so we can go back to the action and now the dollar sign ID is the first property and we can set that to be equal to the response we get back and then access the dollar sign ID property on that next we need the dollar sign created app property and the value of that is just going to be response do dollar sign created at and then finally we need the content property and the value of that is just response. content without a dollar sign because we created that property right all right so once that's all done we can just go ahead and return that not object from this action okay then so now we have this we can go back to the new Note Form component where we Tred to add a new note and inside here we want to invoke this action this add note action so I'm just going to say right here await add notes I'm going to click on this and it should Auto Import this thing for me up here from the actions file which it does awesome so remember we have to pass as an argument right here the content now we have that stored in this state so we can just pass it through like so and that's all there is to it so let's try this out now all right then so before we try this out I'm actually going to go to the database over here we can see this notes collection I'm going to click on it and we can see right now we have no documents right so if we go back over here I'm going to write something in like make a tutorial and I'm going to click add notes now apart from this clearing nothing happens on the front end we don't see any changes and that's right because we've not implemented any front end changes yet all we've done is sent a request to aight to add a new document to the notes collection but if we go over here and refresh now we should see that we have this document right here and this document ID the ight generated for us okay and you can click on that to see the data if you want so the content property make a tutorial that's the one we had the overview down here you can delete the document if you want we don't want to do that we're going to be deleting documents from the front end later on but anyway now we know that works now in the next lesson what I'd like to do is when the page first loads we want to fetch all the documents so we're going to do that and create an action for that in the next lesson

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

This video teaches how to create new documents in an Appwrite database using Next.js and JavaScript, covering topics such as NoSQL database interaction and document creation. By following this tutorial, viewers can learn how to use Appwrite to store and manage data.

Key Takeaways
  1. Head to the console and select the project
  2. Go to databases and copy the database ID
  3. Click on the database to see a list of collections and copy the collection ID
  4. Create a new actions folder and file for database logic
  5. Export an async function called addNotes that takes content as a string and returns a promise resolving to a note object
  6. Set the dollar sign ID to the response from Appwrite
  7. Set the created app property to the response from Appwrite
  8. Set the content property to the response from Appwrite
  9. Invoke the add note action with the content as an argument
  10. Refresh the page to see the newly added document
💡 Appwrite provides a simple and efficient way to create and manage documents in a NoSQL database, and can be easily integrated with Next.js to build robust and scalable applications.

Related Reads

📰
The Object in Your Field Is Not the Object You Wrote
Understand how @Transactional works and why it may silently fail due to proxy classes, and learn how to troubleshoot and resolve these issues
Medium · Programming
📰
I built an offline CLI that audits a legacy PHP app in one command — and shows you the fix
Learn how to build an offline CLI tool to audit legacy PHP apps and get fixes with one command
Dev.to · getobserver
📰
Web Developer Travis McCracken on The Simplicity of Net/HTTP in Go
Learn how to leverage Net/HTTP in Go for simple backend development from web developer Travis McCracken
Dev.to · Travis McCracken Web Developer
📰
Laravel Policies Explained: Protect Your Application the Right Way
Learn how to protect your Laravel application with policies, controlling user actions and permissions
Medium · Programming
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →