CRUD Operations (Server)¶
Basic create, read, update, and delete operations on the server.
Create Documents¶
const user = await db.table('users').create({
name: 'John Doe',
email: 'john@example.com',
age: 30
});
console.log(user.$id); // Auto-generated ID
Read Documents¶
Get by ID¶
Get by ID (throw error if not found)¶
try {
const user = await db.table('users').getOrFail('document-id');
} catch (error) {
console.error('User not found');
}
Get All Documents¶
Get with Options¶
const users = await db.table('users').all({
limit: 10,
offset: 20,
orderBy: ['-createdAt', 'name'],
select: ['name', 'email']
});
Find First Match¶
Update Documents¶
Delete Documents¶
Count Documents¶
// Count all
const total = await db.table('users').count();
// Count with filter
const admins = await db.table('users').count({ role: 'admin' });
Type Safety¶
Use TypeScript interfaces for type-safe operations:
interface User {
$id: string;
name: string;
email: string;
age: number;
role: 'admin' | 'user';
}
const user = await db.table('users').get('id') as User;
const users = await db.table('users').all() as User[];
// Now you have full type safety
console.log(user.name.toUpperCase());
Error Handling¶
import { ORMValidationError } from 'appwrite-orm';
try {
const user = await db.table('users').create({
name: 'John',
email: 'invalid-email'
});
} catch (error) {
if (error instanceof ORMValidationError) {
console.error('Validation errors:', error.errors);
// error.errors is an array of { field, message, value }
} else {
console.error('Database error:', error);
}
}
Validation¶
The ORM validates data against your schema:
// Schema
{
name: { type: 'string', required: true },
email: { type: 'string', required: true },
age: { type: 'integer', min: 0, max: 120 }
}
// ✅ Valid
await db.table('users').create({
name: 'John',
email: 'john@example.com',
age: 30
});
// ❌ Validation error: missing required field
await db.table('users').create({
name: 'John'
// email is required
});
// ❌ Validation error: age out of range
await db.table('users').create({
name: 'John',
email: 'john@example.com',
age: 150 // max is 120
});
Next Steps¶
- Queries - Advanced queries
- Bulk Operations - Process multiple documents
- Joins - Query related data