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.

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

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 };

No comments are allowed in this file.

Key Sections:

  • name: The name of the database being defined.

  • description: The description about database.

  • db: Specifies the database type, either mongodb or postgres.

  • type: Defines whether the database is managed by Blockflow or if you're using a private database.

  • reorg: Indicates whether the database should be reorganisation (reorg) aware.

  • properties: 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.

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

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.

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.

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.

Last updated