Start with Databases_
Get started with Appwrite Databases. Follow a step-by-step guide to create your first database, define tables, and perform basic data operations.
2 min read
1. Create database
Head to your Appwrite Console and create a database and name it Oscar. Optionally, add a custom database ID.
2. Create table
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 Text. 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.
3. Create 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>.
import { Client, ID, TablesDB } from "appwrite";
const client = new Client() .setEndpoint('https://<REGION>.cloud.appwrite.io/v1') .setProject('<PROJECT_ID>');
const tablesDB = new TablesDB(client);
const promise = tablesDB.createRow({ databaseId: '<DATABASE_ID>', tableId: '<TABLE_ID>', rowId: ID.unique(), data: { title: "Hamlet" }});
promise.then(function (response) { console.log(response);}, function (error) { console.log(error);});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"}4. List rows
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.
import { Client, Query, TablesDB } from "appwrite";
const client = new Client() .setEndpoint("https://<REGION>.cloud.appwrite.io/v1") .setProject("<PROJECT_ID>")
const tablesDB = new TablesDB(client);
const promise = tablesDB.listRows({ databaseId: "<DATABASE_ID>", tableId: "<TABLE_ID>", queries: [ Query.equal('title', 'Hamlet') ]});
promise.then(function (response) { console.log(response);}, function (error) { console.log(error);});5. Type safety with models
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:
data class Book( val title: String, val author: String? = null, val pages: Int? = null, val isAvailable: Boolean = true)
val tablesDB = TablesDB(client)
try { // Use nestedType for type-safe responses val books = tablesDB.listRows( databaseId = "<DATABASE_ID>", tableId = "<TABLE_ID>", nestedType = Book::class.java )
for (book in books.rows) { Log.d("Appwrite", "Book: ${book.title} by ${book.author}") }} catch (e: AppwriteException) { Log.e("Appwrite", "Error: ${e.message}")}You can automatically generate type definitions for your tables using the Appwrite CLI type generation feature. Run appwrite types to generate models for your tables.
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 manipulationval bookMap = book.toMap()Log.d("Appwrite", bookMap.toString())
// Create model from Mapval 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 debugginglet bookMap = book.toMap()print(bookMap)
// Create model from dictionarylet bookData: [String: Any] = [ "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"]let newBook = Book.from(map: bookData)
// Encode to JSONlet jsonData = try JSONEncoder().encode(book)let jsonString = String(data: jsonData, encoding: .utf8)Was this page helpful?
Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.