Skip to content

Commit

Permalink
keys nullable and slqite write
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Mar 10, 2025
1 parent 31bf2d0 commit b0e0664
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE entities_historical (
-- No primary key, since we are storing 1-M relationship
-- to retrieve all historical entities for a given entity_id.
id TEXT NOT NULL,
keys TEXT NOT NULL,
keys TEXT,
event_id TEXT NOT NULL,
-- The serialized data of the entity, which contains the Ty.
data TEXT NOT NULL,
Expand Down
40 changes: 27 additions & 13 deletions crates/torii/sqlite/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub struct EventMessageQuery {
pub struct EntityQuery {
pub entity_id: String,
pub model_id: String,
pub keys_str: String,
pub keys_str: Option<String>,
pub event_id: String,
pub block_timestamp: String,
pub is_historical: bool,
Expand Down Expand Up @@ -481,18 +481,32 @@ impl<'c, P: Provider + Sync + Send + 'static> Executor<'c, P> {
entity_counter += 1;

let data = serde_json::to_string(&entity.ty.to_json_value()?)?;
sqlx::query(
"INSERT INTO entities_historical (id, keys, event_id, data, model_id, \
executed_at) VALUES (?, ?, ?, ?, ?, ?) RETURNING *",
)
.bind(entity.entity_id.clone())
.bind(entity.keys_str.clone())
.bind(entity.event_id.clone())
.bind(data)
.bind(entity.model_id.clone())
.bind(entity.block_timestamp.clone())
.fetch_one(&mut **tx)
.await?;
if let Some(keys) = entity.keys_str {
sqlx::query(
"INSERT INTO entities_historical (id, keys, event_id, data, model_id, \
executed_at) VALUES (?, ?, ?, ?, ?, ?) RETURNING *",
)
.bind(entity.entity_id.clone())
.bind(keys)
.bind(entity.event_id.clone())
.bind(data)
.bind(entity.model_id.clone())
.bind(entity.block_timestamp.clone())
.fetch_one(&mut **tx)
.await?;
} else {
sqlx::query(
"INSERT INTO entities_historical (id, event_id, data, model_id, \
executed_at) VALUES (?, ?, ?, ?, ?) RETURNING *",
)
.bind(entity.entity_id.clone())
.bind(entity.event_id.clone())
.bind(data)
.bind(entity.model_id.clone())
.bind(entity.block_timestamp.clone())
.fetch_one(&mut **tx)
.await?;
}
}

sqlx::query(
Expand Down
13 changes: 10 additions & 3 deletions crates/torii/sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use dojo_types::schema::{Struct, Ty};
use dojo_world::config::WorldMetadata;
use dojo_world::contracts::abigen::model::Layout;
use dojo_world::contracts::naming::compute_selector_from_names;
use executor::EntityQuery;
use sqlx::{Pool, Sqlite};
use starknet::core::types::{Event, Felt, InvokeTransaction, Transaction};
use starknet_crypto::poseidon_hash_many;
Expand Down Expand Up @@ -337,8 +338,6 @@ impl Sql {
event_id=EXCLUDED.event_id RETURNING *"
};

if historical {}

let mut arguments = vec![
Argument::String(entity_id.clone()),
Argument::String(event_id.to_string()),
Expand All @@ -352,7 +351,15 @@ impl Sql {
self.executor.send(QueryMessage::new(
insert_entities.to_string(),
arguments,
QueryType::SetEntity(entity.clone()),
QueryType::SetEntity(EntityQuery {
event_id: event_id.to_string(),
block_timestamp: utc_dt_string_from_timestamp(block_timestamp),
entity_id: entity_id.clone(),
model_id: model_id.clone(),
keys_str: keys_str.map(|s| s.to_string()),
ty: entity.clone(),
is_historical,
}),
))?;

self.executor.send(QueryMessage::other(
Expand Down

0 comments on commit b0e0664

Please sign in to comment.