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 collections and attributes.

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

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 collections from your Appwrite project:

Bash
appwrite pull collections

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.
--help or -h
Displays help information for the command.

Example Usage

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

JSON
{
    "projectId": "682ca9a50004cf4b330f",
    "projectName": "Appwrite project",
    "databases": [
        {
            "$id": "684c678b00211ddac082",
            "name": "Library",
            "enabled": true
        }
    ],
    "collections": [
        {
            "$id": "684c6790002d457ee89d",
            "$permissions": [],
            "databaseId": "684c678b00211ddac082",
            "name": "Books",
            "enabled": true,
            "documentSecurity": false,
            "attributes": [
                {
                    "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 collection across all supported languages: