Use Type Safety with Database Models
mainThe SDK supports generic methods for database operations, allowing you to provide custom interfaces for full type safety in TypeScript or type hints in JavaScript.
TypeScript
Pass your interface as a generic parameter to methods like listDocuments or getDocument.
JavaScript
Use JSDoc @type annotations with Models.DocumentList<T> to enable IDE type hints.
Tip: You can use the appwrite types command to automatically generate TypeScript interfaces from your Appwrite database schema.
interface Book {
name: string;
author: string;
releaseYear?: string;
category?: string;
genre?: string[];
isCheckedOut: boolean;
}
const databases = new Databases(client);
try {
const documents = await databases.listDocuments<Book>(
'your-database-id',
'your-collection-id'
);
documents.documents.forEach(book => {
console.log(`Book: ${book.name} by ${book.author}`); // Now you have full type safety
});
} catch (error) {
console.error('Appwrite error:', error);
}