Engine
Docs
Reading and writing to Schemas

Reading and writing to Schemas

Access to Schemas via code-generated libraries

Accessing Schemas via the libraries generated with the schemagen tool is the recommended way to use Schemas.

The schemagen tool of the Obelisk CLI can create libraries for each schema that wraps the low-level API of Schemas and types the results.

Assume the following configuration exists.

// definition of MySchema
import { ObeliskConfig } from "@0xobelisk/common";
 
export const obeliskConfig = {
  name: "examples",
  description: "examples",
  systems: [
    "example_system",
  ],
  schemas: {
    single_column: "u64",
    multi_column: {
      valueType: {
        state: "vector<u8>",
        last_set_time: "u64",
      }
    },
    ephemeral: {
      ephemeral: true,
      valueType: "u64",
    },
    single_value: {
      valueType: "u64",
      defaultValue: 1000
    },
    single_struct: {
      valueType: {
        admin: "address",
        fee: "u64",
      },
      defaultValue: {
        admin: "0x1",
        fee: 100,
      }
    },
    single_struct_temp: {
      valueType: {
        addr_list: "vector<address>",
        bool_list: "vector<bool>",
        name: "string",
        name_list: "vector<string>",
        map: "vector<vector<u8>>",
      },
      defaultValue: {
        addr_list: [
          "0x1804b821bba181110599b8757008eabe6f89f62774d7fafb5ee666ac742a41f8",
          "0x341ede3aa40e29d6716ce92b2fdb977be5ac1129aa43eb024a8b5a9478204fe2",
          "0x34ea3ab186a2b8f217a71301ae447e01e9aa3e22bd4bc827f45a3a2f5381d9c4",
          "0x4f42679d98cd10fbc3daf9996a40e1bb9213315680489b978fe1681b7f00091d",
        ],
        bool_list: [true, false, true],
        name: "tom",
        name_list: ["bob", "alice", "tom"],
        map: [
          [1, 2, 3, 4, 5],
          [1, 35, 5, 6, 7, 9],
        ],
      }
    } 
  }
} as ObeliskConfig;

It's a good idea to make a contains judgement before adding and removing records.

Existence of a record

// single_column
let is_exist = single_column_schema::contains(&world, entity_key);
 
// multi_column
let is_exist = multi_column_schema::contains(&world, entity_key);

Adding a record && Updating a record

// Adding a field
// args: world, entity_key, name, age, sex
// entity_key::from_u256() or entity_key::from_object() or entity_key::from_bytes() or customisation
 
// single_column
single_column_schema::set(&mut world, entity_key, "Alice plus");
 
// multi_column
multi_column_schema::set(&mut world, entity_key, "Bob plus", 10, false);
multi_column_schema::set_name(&mut world, entity_key, "Bob plus");
multi_column_schema::set_age(&mut world, entity_key, 11);
multi_column_schema::set_sex(&mut world, entity_key, true);
 
// single_value
single_value_schema::set(&mut world, 11);
 
// single_struct
single_struct_schema::set(&mut world, 0x2, 100);
single_struct_schema::set_admin(&mut world, 0x2);
single_struct_schema::set_fee(&mut world, 100);
 
// ephemeral schema
ephemeral::emit_ephemeral(10);

Deleting a record

// single_column
single_column_schema::remove(&mut world, entity_key);
 
// multi_column
multi_column_schema::remove(&mut world, entity_key);

Retrieving a record

Make sure the record exists before getting

// single_column
let name = single_column_schema::get(&world, entity_key);
 
// multi_column
let (name, age, sex) = multi_column_schema::get(&world, entity_key, "Bob plus", 10, false);
let name = multi_column_schema::get_name(&world, entity_key);
let age = multi_column_schema::get_age(&world, entity_key);
let sex = multi_column_schema::get_sex(&world, entity_key);
 
// single_value
let number = single_value_schema::get(&world);
 
// single_struct
let (admin, fee) = single_struct_schema::get(&world);
let admin = single_struct_schema::get_admin(&world);
let fee = single_struct_schema::get_fee(&world);