DEV Community

Cover image for #30DaysofAppwrite : Database Design 🧐
Damodar Lohani for Appwrite

Posted on • Updated on

#30DaysofAppwrite : Database Design 🧐

Intro

#30DaysOfAppwrite is a month-long event focused on giving developers a walk-through of all of Appwrite's features, starting from the basics to more advanced features like Cloud Functions! Alongside we will also be building a fully-featured Medium clone to demonstrate how these
concepts can be applied when building a real-world app. We also have some exciting prizes for developers who follow along with us!

Database Design 🧐

Welcome back to another session on the Appwrite Database 👋 . We hope you have gone through the Day 15 article. It is important as we build upon the knowledge gained on Day 15 and plan and prepare the database for our demo application.

Planning Data Structure

We will use the Collection and Attributes features provided by Appwrite Database to plan the data structures needed for our application. First, let's put down the requirements of our application.

Post

A post refers to content that can be posted by any authenticated user. Anyone can signup and create a post. Our post will have title, cover image, text, published ( to signify whether the post is a draft or published) , tags, created date and id of the creator. Now we will plan this using the Attributes provided by Appwrite database.

First, we will create a collection as described on Day 15 and name it Posts. Then we will add the following attributes from the console and update the collection.

Title Cover image Text Published Tags Created Date User Id
Type String String String Boolean String Integer String
Attribute ID title cover text published tags created_at user_id
Size 255 1024 2000000 N/A 1024 N/A 255
Min N/A N/A N/A N/A N/A leave blank N/A
Max N/A N/A N/A N/A N/A leave blank N/A
Required True True True True False True True
Array False False True True True False False

Which will look like this in the console:

Posts Collection UI

For the permissions, the read permission should be [role:all] as anyone should be able to read the posts, while the write permission for this collection should be [role:member] so that only logged in users can create a post.

We also need to create an index for the user_id and published attribute which we will use later in the functions to query the posts. For each index set the index type to Key and set the index key to the attribute name.

Post Indexes

Profile

We want to let our users have their profile with a public name so that we can display the author information in each post. We will also want to add a user's post as an embedded document in the collection so that we can fetch it easily with the user's profile.

Let's create another collection named Users with the following attributes

User Id Name Posts
Type String String String
Attribute ID user name posts
Size 255 512 512
Required True True False
Array False False True

Profile Collection UI

As for the permissions, read permission should be [role:all] as anyone should be able to read, and write permission can be [role:member] so that anyone signed in can create a profile.

As we did for the posts, we will also create an index for the user attribute in the Profiles collection. Make sure the index type is set to key and the index key is set to the attribute name.

Profile Indexes

So, this is how easy it is to plan data structures for any application using Collections and Attributes - it is very similar to traditional Relational Databases where we plan using tables and columns.

We have now planned the database required for our application. On Day 17, we will continue to use these collections in our application.

Credits

We hope you liked this write-up. You can follow #30DaysOfAppwrite on Social Media to keep up with all of our posts. The complete event timeline can be found here

Feel free to reach out to us on Discord if you would like to learn more about Appwrite, Aliens or Unicorns 🦄. Stay tuned for tomorrow's article! Until then 👋

Top comments (0)