CRUD Operations (Web)¶
Basic create, read, update, and delete operations in the web client.
Create Documents¶
const post = await db.table('posts').create({
title: 'My First Post',
content: 'Hello world!',
published: true
});
console.log(post.$id); // Document ID assigned by Appwrite
Read Documents¶
Get by ID¶
Get by ID (throw error if not found)¶
try {
const post = await db.table('posts').getOrFail('document-id');
} catch (error) {
console.error('Post not found');
}
Get All Documents¶
Get with Options¶
const posts = await db.table('posts').all({
limit: 10,
offset: 20,
orderBy: ['-createdAt', 'title'],
select: ['title', 'published']
});
Find First Match¶
Update Documents¶
const updated = await db.table('posts').update('document-id', {
title: 'Updated Title',
views: 42
});
Delete Documents¶
Count Documents¶
// Count all
const total = await db.table('posts').count();
// Count with filter
const published = await db.table('posts').count({ published: true });
Type Safety¶
Use TypeScript interfaces for type-safe operations:
interface Post {
$id: string;
title: string;
content: string;
published: boolean;
views: number;
}
const post = await db.table('posts').get('id') as Post;
const posts = await db.table('posts').all() as Post[];
Error Handling¶
try {
const post = await db.table('posts').create({
title: 'Test Post',
content: 'Content'
});
} catch (error) {
if (error.name === 'ORMValidationError') {
console.error('Validation failed:', error.errors);
} else {
console.error('Appwrite error:', error);
}
}
Next Steps¶
- Queries - Advanced queries and filtering
- Development Mode - Test without Appwrite