Models
Checkpoint introduces a robust ORM to streamline your interactions with the database.
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:
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:
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:
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
. Here's how you'd create a new Post
:
You then set the other properties:
Edit a model instance
Once the instance is defined, you can load an entity and edit it using loadEntity
like that :
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
:
Last updated