Skip to content

Type generation

The indobase CLI provides a simple way to generate types based on your indobase 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 indobase.json file and generates type definitions for each table.

Generating types

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

Bash
indobase pull tables

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

Bash
indobase 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 indobase 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 indobase.json file:

JSON
{
    "projectId": "682ca9a50004cf4b330f",
    "endpoint": "https://<REGION>.cloud.indobase.io/v1",
    "projectName": "indobase project",
    "databases": [
        {
            "$id": "684c678b00211ddac082",
            "name": "Library",
            "enabled": true
        }
    ],
    "tables": [
        {
            "$id": "684c6790002d457ee89d",
            "$permissions": [],
            "databaseId": "684c678b00211ddac082",
            "name": "Books",
            "enabled": true,
            "rowSecurity": false,
            "columns": [
                {
                    "key": "name",
                    "type": "varchar",
                    "required": true,
                    "array": false,
                    "size": 255,
                    "default": null
                },
                {
                    "key": "author",
                    "type": "varchar",
                    "required": true,
                    "array": false,
                    "size": 255,
                    "default": null
                },
                {
                    "key": "release_year",
                    "type": "datetime",
                    "required": false,
                    "array": false,
                    "format": "",
                    "default": null
                },
                {
                    "key": "category",
                    "type": "varchar",
                    "required": false,
                    "array": false,
                    "elements": [
                        "fiction",
                        "nonfiction"
                    ],
                    "format": "enum",
                    "default": null
                },
                {
                    "key": "genre",
                    "type": "varchar",
                    "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: