> For the complete documentation index, see [llms.txt](https://docs.checkpoint.snapshot.box/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.checkpoint.snapshot.box/core-concepts/models.md).

# Models

Checkpoint enables data manipulation in your blockchain database without having to construct SQL queries directly. It features a capability to generate model objects from a GraphQL schema, and this guide will lead you through the process of creating and saving a model object into your database.

### Generating Models

The first step is to generate the models from your GraphQL schema. This is done with the `checkpoint generate` command. Run it in your project environment like this:

```bash
yarn checkpoint generate
```

After running this command, your models will be created based on your GraphQL schema.

### Importing Models

To use the generated models in your scripts, you need to import them. This is done with an `import` statement at the top of your file. For instance, if you've generated a `Post` model, you can import it like this:

```javascript
import { Post } from '../.checkpoint/models';
```

This allows you to use the `Post` class to create new posts and save them to the database.

### Defining Your Model

Each model is defined in a GraphQL schema file, such as `schema.gql`. Every model object will have a unique `id` along with other properties. Here's an example of a `Post` model:

```graphql
scalar Text

type Post {
  id: String!
  author: String!
  content: Text!
  tag: String
  tx_hash: String!
  created_at: Int!
  created_at_block: Int!
}
```

Please note, this `Post` model is merely an example. Your actual model will have properties specific to your needs.

### Creating a model Instance

To create a new instance of your model, you instantiate it with the `id` and indexer name. Here's how you'd create a new `Post`:

```javascript
const post = new Post(`${author}/${tx.transaction_hash}`, "INDEXER_NAME");
```

{% hint style="info" %}
Indexer name allows you to assign this specific entity (Post) to specific indexer. Usually this represents certain network ("mainnet", "sepolia").
{% endhint %}

You then set the other properties:

```javascript
post.author = author;
post.content = content;
post.tag = tag;
post.tx_hash = tx.transaction_hash;
post.created_at = timestamp;
post.created_at_block = blockNumber;
```

### Edit a model instance

Once the instance is defined, you can load an entity and edit it using `loadEntity` like that :&#x20;

```typescript
const post = await Post.loadEntity(id);
post.author = '0x123...';
await post.save();
```

### Saving a model Instance

Once the instance is fully defined, you can save it into your database using the `save` method. This operation is asynchronous, so you should use `await`:

```javascript
await post.save();
```


---

# 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:

```
GET https://docs.checkpoint.snapshot.box/core-concepts/models.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
