Use Type Safety with Database Models
mainThe SDK supports type-safe database operations. When calling methods like listDocuments or getDocument, pass your custom data class (Kotlin) or class (Java) to the nestedType parameter. This allows the SDK to automatically map database documents to your specific model.
data class Book(
val name: String,
val author: String,
val releaseYear: String? = null,
val category: String? = null,
val genre: List<String>? = null,
val isCheckedOut: Boolean
)
val databases = Databases(client)
try {
val documents = databases.listDocuments(
databaseId = "your-database-id",
collectionId = "your-collection-id",
nestedType = Book::class.java
)
for (book in documents.documents) {
Log.d("Appwrite", "Book: ${book.name} by ${book.author}")
}
} catch (e: AppwriteException) {
Log.e("Appwrite", e.message ?: "Unknown error")
}