Appwrite
BaaS Platform
Appwrite
Overview
Appwrite is a privacy and security-focused open-source BaaS platform. While being self-hostable, it also offers a cloud version, enabling developers to have complete control over their environment. It provides comprehensive backend services including database, authentication, storage, and cloud functions through unified APIs, supporting modern app development.
Details
Appwrite provides a BaaS environment that developers can fully control on their own infrastructure, built with a privacy-first design philosophy. It supports everything from simple Docker Compose setups to full-scale Kubernetes deployments, earning high praise from enterprises and developers who prioritize data sovereignty.
It comprehensively provides features necessary for modern applications including real-time databases, diverse authentication providers, file storage, cloud functions, and push notifications. It offers SDKs for a wide range of platforms from web, mobile, desktop to server-side applications.
Pros and Cons
Pros
- Open source: Source code is public, offering high transparency and customizability
- Self-host ready: Complete data control through on-premises operation
- Privacy focused: GDPR compliant and supports data localization
- Comprehensive features: All-in-one solution for authentication, database, storage, and functions
- Rich SDKs: Support for diverse programming languages and platforms
Cons
- Self-hosting overhead: Infrastructure management and maintenance required
- Learning curve: Need to adapt to unique APIs and architecture
- Community-based: Limited commercial support
- Scaling complexity: Optimization for large-scale operations requires specialized knowledge
Reference Links
Code Examples
1. Basic Client Initialization and Authentication
import { Client, Account } from "appwrite";
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const account = new Account(client);
// User registration
const user = await account.create(
'unique-user-id',
'[email protected]',
'secure-password',
'John Doe'
);
console.log(user);
// Login
const session = await account.createEmailPasswordSession(
'[email protected]',
'secure-password'
);
console.log(session);
2. Database Operations (CRUD)
import { Client, Databases } from "appwrite";
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const databases = new Databases(client);
// Create document
const document = await databases.createDocument(
'database-id',
'posts-collection-id',
'unique-document-id',
{
title: 'Getting Started with Appwrite',
content: 'Learn the basics of Appwrite development...',
author: 'user-id',
publishedAt: new Date().toISOString()
}
);
// Get document
const post = await databases.getDocument(
'database-id',
'posts-collection-id',
'document-id'
);
// List documents
const posts = await databases.listDocuments(
'database-id',
'posts-collection-id',
[Query.equal('status', 'published')]
);
3. File Storage Operations
import { Client, Storage } from "appwrite";
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const storage = new Storage(client);
// File upload
const file = await storage.createFile(
'bucket-id',
'unique-file-id',
document.getElementById('file-input').files[0]
);
// List files
const files = await storage.listFiles('bucket-id');
// Get file download URL
const downloadUrl = storage.getFileDownload('bucket-id', 'file-id');
// Get file preview URL
const previewUrl = storage.getFilePreview(
'bucket-id',
'file-id',
400, // width
300, // height
'center', // gravity
80 // quality
);
4. Real-time Features
import { Client, Databases } from "appwrite";
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const databases = new Databases(client);
// Real-time subscription
const unsubscribe = client.subscribe(
['databases.database-id.collections.messages.documents'],
response => {
console.log('New message:', response);
// Update UI
updateMessageList(response.payload);
}
);
// Unsubscribe
// unsubscribe();
5. Cloud Function Execution
import { Client, Functions } from "appwrite";
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const functions = new Functions(client);
// Execute function
const execution = await functions.createExecution(
'function-id',
JSON.stringify({
operation: 'sendWelcomeEmail',
userId: 'user-id',
language: 'en'
})
);
console.log(execution.response);
6. React.js Application Integration Example
import React, { useState, useEffect } from 'react';
import { Client, Account, Databases } from 'appwrite';
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1')
.setProject('your-project-id');
const account = new Account(client);
const databases = new Databases(client);
function App() {
const [user, setUser] = useState(null);
const [posts, setPosts] = useState([]);
useEffect(() => {
// Get user info
account.get()
.then(setUser)
.catch(() => setUser(null));
// Get posts list
if (user) {
databases.listDocuments('database-id', 'posts-collection-id')
.then(response => setPosts(response.documents));
}
}, [user]);
const login = async (email, password) => {
try {
await account.createEmailPasswordSession(email, password);
const userData = await account.get();
setUser(userData);
} catch (error) {
console.error('Login error:', error);
}
};
const logout = async () => {
try {
await account.deleteSession('current');
setUser(null);
setPosts([]);
} catch (error) {
console.error('Logout error:', error);
}
};
return (
<div>
{user ? (
<div>
<h1>Hello, {user.name}</h1>
<button onClick={logout}>Logout</button>
<div>
{posts.map(post => (
<div key={post.$id}>
<h3>{post.title}</h3>
<p>{post.content}</p>
</div>
))}
</div>
</div>
) : (
<LoginForm onLogin={login} />
)}
</div>
);
}