> For the complete documentation index, see [llms.txt](https://outerbase.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://outerbase.gitbook.io/untitled/introspection/generate-models.md).

# Generate models

Manually creating data classes to represent ideas in your application can be difficult to maintain accurately, particularly when you have an active code base and multiple application layers. Automatically generating models and keeping them consistently in sync helps everyone.

## Add script to package

To generate models in your project we can add a script to our `package.json` file that handles this automatically.

```json
"scripts" {
    "db:models": "sync-database-models PATH=./models API_KEY=your_api_key"
}
```

In the example above we name the command we'll call to generate these models `db:models` and we told the project what path, or the location in the project, on where to create and store the model files. Before we can do this though you must provide your API key from Outerbase as it's the remove server platform that does the database introspection with your connected database.

## Run the script

With our script in place all we need to do to generate a new Typescript file for each database table we have is by running

```
npm run db:models
```

## Sample output

After running the script above check the folder path you declared for your models to be saved to. Assuming everything ran successfully then you should see one file for each table in your database. If your database had multiple schemas (e.g. Postgres database) then your model folder will contain folders that map to each schema, with the table classes inside those.

When a table is in the `public` schema it will be created at the root level of the models folder.

```typescript
import { BaseTable, Column } from '@outerbase/sdk';

export class Users extends BaseTable {
    @Column({ name: "id", primary: true })
    id: string;

    @Column({ name: "email", nullable: false })
    email: string;

    @Column({ name: "password", nullable: true })
    password?: string;

    
    constructor(data: any) {
        super({
            _name: "users",
            _schema: "public"
        });

        this.id = data.id;
        this.email = data.email;
        this.password = data.password;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://outerbase.gitbook.io/untitled/introspection/generate-models.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
