Skip to content

Type generation

The Appwrite CLI provides a simple way to generate types based on your Appwrite database schema. This feature is particularly useful for developers who want to ensure type safety in their applications by generating type definitions that match their database tables and columns.

To generate types, the CLI reads the database schema from your project's appwrite.json file and generates type definitions for each table.

Generating types

First, ensure you have the Appwrite CLI installed and your project is initialised. Then, run the following command in your terminal to pull tables from your Appwrite project:

Bash
appwrite pull tables

To generate types, you can use the Appwrite CLI command:

Bash
appwrite types [options] <output-directory>

The following options are currently available:

OptionDescription
--language or -l
The programming language for which types can be generated. Choices include ts, js, php, kotlin, swift, java, dart, auto. The CLI will use auto as the default option if this option is skipped.
--strict or -s
Enables strict type generation. This option ensures that all the columns follow language conventions, even if that leads to mismatches with the schema defined in your Appwrite console.
--help or -h
Displays help information for the command.

Example usage

Suppose you want to generate types for a table with data on books with the following schema from your appwrite.json file:

JSON
{
    "projectId": "682ca9a50004cf4b330f",
    "endpoint": "https://<REGION>.cloud.appwrite.io/v1",
    "projectName": "Appwrite project",
    "databases": [
        {
            "$id": "684c678b00211ddac082",
            "name": "Library",
            "enabled": true
        }
    ],
    "tables": [
        {
            "$id": "684c6790002d457ee89d",
            "$permissions": [],
            "databaseId": "684c678b00211ddac082",
            "name": "Books",
            "enabled": true,
            "rowSecurity": false,
            "columns": [
                {
                    "key": "name",
                    "type": "string",
                    "required": true,
                    "array": false,
                    "size": 255,
                    "default": null
                },
                {
                    "key": "author",
                    "type": "string",
                    "required": true,
                    "array": false,
                    "size": 255,
                    "default": null
                },
                {
                    "key": "release_year",
                    "type": "datetime",
                    "required": false,
                    "array": false,
                    "format": "",
                    "default": null
                },
                {
                    "key": "category",
                    "type": "string",
                    "required": false,
                    "array": false,
                    "elements": [
                        "fiction",
                        "nonfiction"
                    ],
                    "format": "enum",
                    "default": null
                },
                {
                    "key": "genre",
                    "type": "string",
                    "required": false,
                    "array": true,
                    "size": 100,
                    "default": null
                },
                {
                    "key": "is_checked_out",
                    "type": "boolean",
                    "required": true,
                    "array": false,
                    "default": null
                }
            ],
            "indexes": []
        }
    ]
}

Here's how you can generate types for this table across all supported languages: