Head to your Appwrite Console and create a database and name it Oscar
. Optionally, add a custom database ID.
Create a table and name it My books
. Optionally, add a custom table ID.
Navigate to Columns and create columns by clicking Create column and select String. Columns define the structure of your table's rows. Enter Column key and Size. For example, title
and 100
.
Navigate to Settings > Permissions and add a new role Any. Check the CREATE and READ permissions, so anyone can create and read rows.
To create a row use the createRow
method.
In the Settings menu, find your project ID and replace <PROJECT_ID>
in the example.
Navigate to the Oscar
database, copy the database ID, and replace <DATABASE_ID>
. Then, in the My books
table, copy the table ID, and replace <TABLE_ID>
.
The response should look similar to this.
{
"title": "Hamlet",
"$id": "65013138dcd8618e80c4",
"$permissions": [],
"$createdAt": "2023-09-13T03:49:12.905+00:00",
"$updatedAt": "2023-09-13T03:49:12.905+00:00",
"$databaseId": "650125c64b3c25ce4bc4",
"$tableId": "650125cff227cf9f95ad"
}
To read and query data from your table, use the listRows
endpoint.
Like the previous step, replace <PROJECT_ID>
, <DATABASE_ID>
, and <TABLE_ID>
with their respective IDs.
For added type safety and better development experience, mobile and native SDKs support custom model types with the nestedType
parameter.
Define a data class or model that matches your table structure:
Automatic type generation
You can automatically generate type definitions for your tables using the Appwrite CLI type generation feature. Run appwrite types collection
to generate models for your collections.
Model methods
Models returned by native SDKs include helpful methods for data manipulation:
val book = books.rows.first()
// Convert to Map for debugging or manual manipulation
val bookMap = book.toMap()
Log.d("Appwrite", bookMap.toString())
// Create model from Map
val bookData = mapOf(
"title" to "The Great Gatsby",
"author" to "F. Scott Fitzgerald"
)
val newBook = Book.from(bookData, Book::class.java)
let book = books.rows.first!
// Convert to dictionary for debugging
let bookMap = book.toMap()
print(bookMap)
// Create model from dictionary
let bookData: [String: Any] = [
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald"
]
let newBook = Book.from(map: bookData)
// Encode to JSON
let jsonData = try JSONEncoder().encode(book)
let jsonString = String(data: jsonData, encoding: .utf8)