Skip to content

Commit

Permalink
add nested structs to model
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Sep 28, 2023
1 parent 746c526 commit 20f0eb9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 37 deletions.
4 changes: 2 additions & 2 deletions crates/torii/graphql/src/tests/types-test/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ initializer_class_hash = "0xbeef"
[tool.dojo.env]
rpc_url = "http://localhost:5050/"
# Default account for katana with seed = 0
account_address = "0x3ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0"
private_key = "0x300001800000000300000180000000000030000000000003006001800006600"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
# world_address = "0x789c94ef39aeebc7f8c4c4633030faefb8bee454e358ae53d06ced36136d7d6"
# keystore_password = "password"
# keystore_path = "../keystore.json"
2 changes: 1 addition & 1 deletion crates/torii/graphql/src/tests/types-test/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod components;
mod models;
mod systems;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use array::ArrayTrait;
use starknet::{ContractAddress, ClassHash};

#[derive(Component, Copy, Drop, Serde, SerdeLen)]
#[derive(Model, Copy, Drop, Serde)]
struct Record {
#[key]
record_id: u32,
Expand All @@ -10,9 +10,21 @@ struct Record {
type_u32: u32,
type_u64: u64,
type_u128: u128,
type_u256: u256,
//type_u256: u256,
type_bool: bool,
type_felt: felt252,
type_class_hash: ClassHash,
type_contract_address: ContractAddress,
}
type_nested: Nested,
}

#[derive(Copy, Drop, Serde, Introspect)]
struct Nested {
record_id: u32,
//type_more_nested: Option<NestedMore>,
}

#[derive(Copy, Drop, Serde, Introspect)]
struct NestedMore {
record_id: u32,
}
72 changes: 42 additions & 30 deletions crates/torii/graphql/src/tests/types-test/src/systems.cairo
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
#[system]
mod spawn {
use array::ArrayTrait;
use box::BoxTrait;
use option::OptionTrait;
use traits::{Into, TryInto};
use starknet::class_hash::{Felt252TryIntoClassHash};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starknet::{ContractAddress, ClassHash};
use types_test::models::{Record, Nested, NestedMore};

#[starknet::interface]
trait IRecords<TContractState> {
fn create(self: @TContractState, world: IWorldDispatcher, num_records: u8);
}

use dojo::world::Context;
use types_test::components::Record;
#[system]
mod records {
use types_test::models::{Record, Nested, NestedMore};
use super::IRecords;

fn execute(ctx: Context, num_records: u8) {
let mut curr_record = 0;
loop {
if curr_record == num_records {
break();
}
curr_record = curr_record + 1;
#[external(v0)]
impl RecordsImpl of IRecords<ContractState> {
fn create(self: @ContractState, world: IWorldDispatcher, num_records: u8) {
let mut curr_record = 0;
loop {
if curr_record == num_records {
break ();
}
curr_record = curr_record + 1;

let record_id = ctx.world.uuid();
let curr_felt: felt252 = curr_record.into();
set !(
ctx.world,
(
Record {
let record_id = world.uuid();
let curr_felt: felt252 = curr_record.into();
set!(
world,
(Record {
record_id,
type_u8: curr_record.into(),
type_u16: curr_record.into(),
type_u32: curr_record.into(),
type_u64: curr_record.into(),
type_u128: curr_record.into(),
type_u256: curr_record.into(),
type_bool: if curr_record % 2 == 0 { true } else { false },
//type_u256: curr_record.into(),
type_bool: if curr_record % 2 == 0 {
true
} else {
false
},
type_felt: curr_felt,
type_class_hash: curr_felt.try_into().unwrap(),
type_contract_address: curr_felt.try_into().unwrap(),
}
)
);
};
return ();
type_nested: Nested {
record_id
//type_more_nested: NestedMore { record_id }
}
})
);
};
return ();
}
}
}
}
2 changes: 1 addition & 1 deletion crates/torii/migrations/20230316154230_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CREATE TABLE model_members(
type TEXT NOT NULL,
key BOOLEAN NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id, model_idx) FOREIGN KEY (model_id) REFERENCES models(id) UNIQUE (id, member_idx)
PRIMARY KEY (id, member_idx) FOREIGN KEY (model_id) REFERENCES models(id)
);

CREATE INDEX idx_model_members_model_id ON model_members (model_id);
Expand Down

0 comments on commit 20f0eb9

Please sign in to comment.