# Setting up Database Schema

The **`studio.schema.ts`** file is a TypeScript file that defines the databases and schema for the data stored within them. It outlines the entities and their relationships in the indexed blockchain data.&#x20;

Each project contains a single **`studio.schema.ts`** file where all schema definitions are centralised.

```typescript
const Transfer = {
  name: "Transfer",
  description: "This database stores ERC20 transfer events",
  db: "mongodb",
  type: "managed",
  reorg: true,
  properties: {
    from: "string?",
    to: "string?",
    amount: "string?",
  },
};

module.exports = { Transfer };
```

{% hint style="warning" %}
No comments are allowed in this file.
{% endhint %}

### Key Sections:

* <mark style="color:blue;">**name**</mark>: The name of the database being defined.
* <mark style="color:blue;">**description**</mark>: The description about database.
* <mark style="color:blue;">**db**</mark>: Specifies the database type, either `mongodb` or `postgres`.
* <mark style="color:blue;">**type**</mark>: Defines whether the database is `managed` by Blockflow or if you're using a `private` database.
* <mark style="color:blue;">**reorg**</mark>: Indicates whether the database should be reorganisation (reorg) aware.
* <mark style="color:blue;">**properties**</mark>: Contains the schema definition, including field names and their types, for the data stored in the database.

***

### Creating Indexed Fields

In addition to specifying the data types for your schema fields, you can also define whether a field should be indexed to improve query performance. Indexing is particularly useful for fields that are frequently used in filtering or sorting operations.&#x20;

To mark a field as indexed, simply add `@` at the end of the field’s type, as shown below:

```typescript
const Transfer = {
  name: "Transfer",
  description: "This database stores ERC20 transfer events",
  db: "mongodb",
  type: "managed",
  reorg: true,
  properties: {
    from: "string@", // This field is indexed
    to: "string?",
    amount: "string?",
  },
};

module.exports = { Transfer };
```

***

### Generated Types

Once you've finished defining all your databases, run the `blockflow typegen` command. This will generate two important files in the `src/types` folder: **`schema.ts`** and **`graphql.ts`**.

* **`schema.ts`**: Contains the schemas and TypeScript interfaces for the databases you defined in your project.

```typescript
export const Transfer = {
  "name": "XXXX-XXXX-XXXX-XXXX-Transfer",
  "db": "mongodb",
  "type": "managed",
  "properties": {
    "from": "string?",
    "to": "string?",
    "amount": "string?"
  },
  "reorg": true
};

export interface ITransfer {
  from: string;
  to: string;
  amount: string;
}
```

* **`graphql.ts`**: Contains auto-generated GraphQL entities that are used in custom GraphQL queries and mutations.

```typescript
import { Entity, PrimaryColumn, Column, BaseEntity } from "typeorm";
import { ObjectType, Field, ID } from "type-graphql";
import "reflect-metadata";

@Entity()
@ObjectType()
export class GTransfer extends BaseEntity {

  @Column()
  @Field({ nullable: true })
  from?: string;

  @Column()
  @Field({ nullable: true })
  to?: string;

  @Column()
  @Field({ nullable: true })
  amount?: string;

  @Column()
  @Field(() => Number)
  _blocknumber!: number;

  @Column()
  @Field()
  _blockhash!: string;

  @Column()
  @Field()
  _chainId!: string;

  @Column()
  @Field(() => Number)
  _version!: number;

  @Column()
  @Field()
  _refId!: string;
}
```

These files are essential for further development, providing type safety and facilitating interactions with your database and GraphQL API.
