You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71Lines changed: 71 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,6 +115,77 @@ suspend fun main() {
115
115
}
116
116
```
117
117
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 classBook(
124
+
valname:String,
125
+
valauthor:String,
126
+
valreleaseYear:String? = null,
127
+
valcategory:String? = null,
128
+
valgenre:List<String>? = null,
129
+
valisCheckedOut: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:
**JSON Serialization** - Models can be easily converted to/from JSON using Gson (which the SDK uses internally):
174
+
```kotlin
175
+
importcom.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
+
118
189
### Error Handling
119
190
120
191
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