Skip to content

Commit 74e1138

Browse files
committed
chore: update readme
1 parent 5221112 commit 74e1138

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,77 @@ suspend fun main() {
115115
}
116116
```
117117

118+
### Type Safety with Models
119+
120+
The Appwrite Kotlin SDK provides type safety when working with database documents through generic methods. Methods like `listDocuments`, `getDocument`, and others accept a `nestedType` parameter that allows you to specify your custom model type for full type safety.
121+
122+
```kotlin
123+
data class Book(
124+
val name: String,
125+
val author: String,
126+
val releaseYear: String? = null,
127+
val category: String? = null,
128+
val genre: List<String>? = null,
129+
val isCheckedOut: Boolean
130+
)
131+
132+
val databases = Databases(client)
133+
134+
try {
135+
val documents = databases.listDocuments(
136+
databaseId = "your-database-id",
137+
collectionId = "your-collection-id",
138+
nestedType = Book::class.java // Pass in your custom model type
139+
)
140+
141+
for (book in documents.documents) {
142+
Log.d("Appwrite", "Book: ${book.name} by ${book.author}") // Now you have full type safety
143+
}
144+
} catch (e: AppwriteException) {
145+
Log.e("Appwrite", e.message ?: "Unknown error")
146+
}
147+
```
148+
149+
**Tip**: You can use the `appwrite types` command to automatically generate model definitions based on your Appwrite database schema. Learn more about [type generation](https://appwrite.io/docs/products/databases/type-generation).
150+
151+
### Working with Model Methods
152+
153+
All Appwrite models come with built-in methods for data conversion and manipulation:
154+
155+
**`toMap()`** - Converts a model instance to a Map format, useful for debugging or manual data manipulation:
156+
```kotlin
157+
val account = Account(client)
158+
val user = account.get()
159+
val userMap = user.toMap()
160+
Log.d("Appwrite", userMap.toString()) // Prints all user properties as a Map
161+
```
162+
163+
**`from(map:, nestedType:)`** - Creates a model instance from a Map, useful when working with raw data:
164+
```kotlin
165+
val userData: Map<String, Any> = mapOf(
166+
"\$id" to "123",
167+
"name" to "John",
168+
"email" to "[email protected]"
169+
)
170+
val user = User.from(userData, User::class.java)
171+
```
172+
173+
**JSON Serialization** - Models can be easily converted to/from JSON using Gson (which the SDK uses internally):
174+
```kotlin
175+
import com.google.gson.Gson
176+
177+
val account = Account(client)
178+
val user = account.get()
179+
180+
// Convert to JSON
181+
val gson = Gson()
182+
val jsonString = gson.toJson(user)
183+
Log.d("Appwrite", "User JSON: $jsonString")
184+
185+
// Convert from JSON
186+
val userFromJson = gson.fromJson(jsonString, User::class.java)
187+
```
188+
118189
### Error Handling
119190

120191
The Appwrite Kotlin SDK raises `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.

0 commit comments

Comments
 (0)