Skip to content

Commit

Permalink
Refactor torii core add entity_model table (#1213)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Nov 28, 2023
1 parent 8c2f0d9 commit 09115ef
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 42 deletions.
24 changes: 7 additions & 17 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,19 @@ impl Sql {
};

let entity_id = format!("{:#x}", poseidon_hash_many(&keys));
let existing: Option<(String,)> =
sqlx::query_as("SELECT model_names FROM entities WHERE id = ?")
.bind(&entity_id)
.fetch_optional(&self.pool)
.await?;

let model_names = match existing {
Some((existing_names,)) if existing_names.contains(&entity.name()) => {
existing_names.to_string()
}
Some((existing_names,)) => format!("{},{}", existing_names, entity.name()),
None => entity.name().to_string(),
};
self.query_queue.enqueue(
"INSERT INTO entity_model (entity_id, model_id) VALUES (?, ?) ON CONFLICT(entity_id, \
model_id) DO NOTHING",
vec![Argument::String(entity_id.clone()), Argument::String(entity.name())],
);

let keys_str = felts_sql_string(&keys);
let insert_entities = "INSERT INTO entities (id, keys, model_names, event_id) VALUES (?, \
?, ?, ?) ON CONFLICT(id) DO UPDATE SET \
model_names=EXCLUDED.model_names, updated_at=CURRENT_TIMESTAMP, \
let insert_entities = "INSERT INTO entities (id, keys, event_id) VALUES (?, ?, ?) ON \
CONFLICT(id) DO UPDATE SET updated_at=CURRENT_TIMESTAMP, \
event_id=EXCLUDED.event_id RETURNING *";
let entity_updated: EntityUpdated = sqlx::query_as(insert_entities)
.bind(&entity_id)
.bind(&keys_str)
.bind(&model_names)
.bind(event_id)
.fetch_one(&self.pool)
.await?;
Expand Down
1 change: 0 additions & 1 deletion crates/torii/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub struct Entity {
pub id: String,
pub keys: String,
pub event_id: String,
pub model_names: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
Expand Down
1 change: 0 additions & 1 deletion crates/torii/graphql/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ lazy_static! {
pub static ref ENTITY_TYPE_MAPPING: TypeMapping = IndexMap::from([
(Name::new("id"), TypeData::Simple(TypeRef::named(TypeRef::ID))),
(Name::new("keys"), TypeData::Simple(TypeRef::named_list(TypeRef::STRING))),
(Name::new("model_names"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(Name::new("event_id"), TypeData::Simple(TypeRef::named(TypeRef::STRING))),
(
Name::new("created_at"),
Expand Down
14 changes: 7 additions & 7 deletions crates/torii/graphql/src/object/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ impl EntityObject {
IndexMap::from([
(Name::new("id"), Value::from(entity.id)),
(Name::new("keys"), Value::from(keys)),
(Name::new("model_names"), Value::from(entity.model_names)),
(Name::new("event_id"), Value::from(entity.event_id)),
(
Name::new("created_at"),
Expand All @@ -135,14 +134,15 @@ fn model_union_field() -> Field {
match ctx.parent_value.try_to_value()? {
Value::Object(indexmap) => {
let mut conn = ctx.data::<Pool<Sqlite>>()?.acquire().await?;
let model_names: Vec<String> = extract::<String>(indexmap, "model_names")?
.split(',')
.map(|s| s.to_string())
.collect();

let entity_id = extract::<String>(indexmap, "id")?;
let model_ids: Vec<(String,)> =
sqlx::query_as("SELECT model_id from entity_model WHERE entity_id = ?")
.bind(&entity_id)
.fetch_all(&mut conn)
.await?;

let mut results: Vec<FieldValue<'_>> = Vec::new();
for name in model_names {
for (name,) in model_ids {
let type_mapping = type_mapping_query(&mut conn, &name).await?;
let mut path_array = vec![name.clone()];

Expand Down
6 changes: 0 additions & 6 deletions crates/torii/graphql/src/tests/entities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod tests {
cursor
node {{
keys
model_names
}}
}}
page_info {{
Expand All @@ -46,7 +45,6 @@ mod tests {
{{
entity (id: "{:#x}") {{
keys
model_names
models {{
... on Record {{
__typename
Expand Down Expand Up @@ -93,12 +91,8 @@ mod tests {
// default without params
let entities = entities_query(&schema, "").await;
let connection: Connection<Entity> = serde_json::from_value(entities).unwrap();
let first_entity = connection.edges.first().unwrap();
let last_entity = connection.edges.last().unwrap();
assert_eq!(connection.edges.len(), 10);
assert_eq!(connection.total_count, 20);
assert_eq!(&first_entity.node.model_names, "Subrecord");
assert_eq!(&last_entity.node.model_names, "Record,RecordSibling");

// first key param - returns all entities with `0x0` as first key
let entities = entities_query(&schema, "(keys: [\"0x0\"])").await;
Expand Down
1 change: 0 additions & 1 deletion crates/torii/graphql/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub struct Edge<T> {

#[derive(Deserialize, Debug, PartialEq)]
pub struct Entity {
pub model_names: String,
pub keys: Option<Vec<String>>,
pub created_at: Option<String>,
}
Expand Down
2 changes: 0 additions & 2 deletions crates/torii/graphql/src/tests/models_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ mod tests {
}}
entity {{
keys
model_names
}}
}}
}}
Expand Down Expand Up @@ -92,7 +91,6 @@ mod tests {
assert_eq!(connection.total_count, 10);
assert_eq!(connection.edges.len(), 10);
assert_eq!(&record.node.__typename, "Record");
assert_eq!(&entity.model_names, "Record,RecordSibling");
assert_eq!(entity.keys.clone().unwrap(), vec!["0x0"]);
assert_eq!(record.node.depth, "Zero");
assert_eq!(nested.depth, "One");
Expand Down
4 changes: 0 additions & 4 deletions crates/torii/graphql/src/tests/subscription_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ mod tests {
"entityUpdated": {
"id": entity_id,
"keys":vec![keys_str],
"model_names": model_name,
"models" : [{
"__typename": model_name,
"depth": "Zero",
Expand Down Expand Up @@ -115,7 +114,6 @@ mod tests {
entityUpdated {
id
keys
model_names
models {
__typename
... on Record {
Expand Down Expand Up @@ -153,7 +151,6 @@ mod tests {
"entityUpdated": {
"id": entity_id,
"keys":vec![keys_str],
"model_names": model_name,
"models" : [{
"__typename": model_name,
"depth": "Zero",
Expand Down Expand Up @@ -220,7 +217,6 @@ mod tests {
entityUpdated(id: "0x579e8877c7755365d5ec1ec7d3a94a457eff5d1f40482bbe9729c064cdead2") {
id
keys
model_names
models {
__typename
... on Record {
Expand Down
6 changes: 3 additions & 3 deletions crates/torii/graphql/src/tests/types-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ version = 1

[[package]]
name = "dojo"
version = "0.3.12"
version = "0.3.13"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.12"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.12#12d58f29ec53454317f1f6d265007a053d279288"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"

[[package]]
name = "types_test"
Expand Down
40 changes: 40 additions & 0 deletions crates/torii/migrations/20231127235011_entity_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- NOTE: sqlite does not support deleteing columns. Workaround is to create new table, copy, and delete old.

-- Create new table without model_names column
CREATE TABLE entities_new (
id TEXT NOT NULL PRIMARY KEY,
keys TEXT,
event_id TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Copy from old entities
INSERT INTO entities_new (id, keys, event_id, created_at, updated_at)
SELECT id, keys, event_id, created_at, updated_at
FROM entities;

-- Disable foreign keys constraint so we can delete entities
PRAGMA foreign_keys = OFF;

-- Drop old entities
DROP TABLE entities;

-- Rename table and recreate indexes
ALTER TABLE entities_new RENAME TO entities;
CREATE INDEX idx_entities_keys ON entities (keys);
CREATE INDEX idx_entities_event_id ON entities (event_id);

-- Renable foreign keys
PRAGMA foreign_keys = ON;

-- New table to track entity to model relationships
CREATE TABLE entity_model (
entity_id TEXT NOT NULL,
model_id TEXT NOT NULL,
UNIQUE (entity_id, model_id),
FOREIGN KEY (entity_id) REFERENCES entities (id),
FOREIGN KEY (model_id) REFERENCES models (id)
);
CREATE INDEX idx_entity_model_entity_id ON entity_model (entity_id);
CREATE INDEX idx_entity_model_model_id ON entity_model (model_id);

0 comments on commit 09115ef

Please sign in to comment.