(Day 2) Project: BLOG
This code manages posts and files (like blog posts and images) in an app using Appwrite. It includes features to create, update, delete, and fetch posts and files. Let’s break it down in simple steps.
1. What Does the Code Do?
This code is part of a system to:
Handle blog posts:
Create, update, delete, and fetch posts.
Store and retrieve posts from a database.
Handle files:
Upload and delete files (like images).
Get a preview of uploaded files.
2. What Tools Are Being Used?
import { Client, Account, ID, Databases, Query } from "appwrite";
import conf from "../conf/conf";
Client: Connects the app to Appwrite services.
Databases: Manages data (like posts) in the database.
Storage: Handles file uploads and storage.
Query: Helps filter and search for specific posts.
conf: A file storing important settings (e.g., database IDs, bucket IDs).
3. Creating a Class
export class Service {
client = new Client();
databases;
bucket;
constructor() {
this.client.setEndpoint(conf.appwriteUrl);
this.client.setProject(conf.appwriteProjectId);
this.databases = new Databases(this.client);
this.bucket = new Storage(this.client);
}
}
What is a class? A blueprint to group related functions (methods).
Purpose: The
Service
class handles posts and files.client: Connects to Appwrite.
databases: Manages post data.
bucket: Manages file uploads.
4. Handling Posts
a. Creating a Post
async createPost({ title, slug, content, featuredImage, status, userId }) {
try {
return await this.databases.createDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
slug,
{ title, content, featuredImage, status, userId }
);
} catch (error) {
console.log(error);
throw error;
}
}
Purpose: Adds a new post to the database.
Parameters:
title, content, featuredImage, status
: Post details.slug
: A unique ID for the post.
Appwrite Task:
createDocument
saves the post in the database.
b. Updating a Post
async updatePosts(slug, { title, content, featuredImage, status }) {
try {
return await this.databases.updateDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
slug,
{ title, content, featuredImage, status }
);
} catch (error) {
console.log(error);
throw error;
}
}
Purpose: Updates an existing post in the database.
How It Works: Finds the post using
slug
and updates its details.
c. Deleting a Post
async deletePost(slug) {
try {
await this.databases.deleteDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
slug
);
return true;
} catch (error) {
console.log(error);
throw error;
return false;
}
}
- Purpose: Removes a post from the database using its
slug
.
d. Fetching a Single Post
async getPost(slug) {
try {
return await this.databases.getDocument(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
slug
);
} catch (error) {
console.log(error);
throw error;
}
}
- Purpose: Fetches a specific post using
slug
.
e. Fetching Multiple Posts
async getPosts(queries = [Query.equal("status", "active")]) {
try {
return await this.databases.listDocuments(
conf.appwriteDatabaseId,
conf.appwriteCollectionId,
queries
);
} catch (error) {
console.log(error);
throw error;
return false;
}
}
Purpose: Fetches a list of posts.
Queries: Filters posts (e.g., only fetch posts with
status: active
).
5. Handling Files
a. Uploading a File
async uploadFile(file) {
try {
return await this.bucket.createFile(
conf.appwriteBucketId,
ID.unique(),
file
);
} catch (error) {
console.log(error);
throw error;
return false;
}
}
Purpose: Uploads a file (like an image) to Appwrite.
How It Works:
Generates a unique ID using
ID.unique()
.Saves the file in the storage bucket.
b. Deleting a File
async deleteFile(fileId) {
try {
await this.bucket.deleteFile(
conf.appwriteBucketId,
fileId
);
return true;
} catch (error) {
console.log(error);
throw error;
return false;
}
}
- Purpose: Deletes a file using its
fileId
.
c. Getting a File Preview
getFilePreview(fileId) {
return this.bucket.getFilePreview(
conf.appwriteBucketId,
fileId
);
}
- Purpose: Retrieves a preview (URL or thumbnail) of the file.
6. Creating and Exporting the Object
const service = new Service();
export default service;
service: An object of the
Service
class.Why? This object makes it easy to use methods (like
createPost
oruploadFile
) in other parts of the app.
Why Use This Code?
Organized: Groups all post and file actions in one place.
Reusable: Methods like
createPost
oruploadFile
can be reused in different parts of the app.Error Handling: Includes
try...catch
blocks to manage errors cleanly.Flexibility: Handles both individual posts/files and lists of them.