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.
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
orpostgres
.type: Defines whether the database is
managed
by Blockflow or if you're using aprivate
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:
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.
graphql.ts
: Contains auto-generated GraphQL entities that are used in custom GraphQL queries and mutations.
These files are essential for further development, providing type safety and facilitating interactions with your database and GraphQL API.
Last updated