-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor torii grpc * error freeeee! * Refactor torii grpc * rebase changes * clean up * Move model to individual query clause * Refactor subscription argument to keys * Add Torii grpc retrieve entities endpoint * Add cache to store schema ty and query * map rows to proto model * add grpc limit offset * refactor query logic * clippy & fmt * remove unused * fix tests * move limit offset to query clause --------- Co-authored-by: Tarrence van As <[email protected]>
- Loading branch information
Showing
16 changed files
with
627 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::collections::HashMap; | ||
|
||
use dojo_types::schema::Ty; | ||
use sqlx::SqlitePool; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::error::{Error, QueryError}; | ||
use crate::model::{parse_sql_model_members, SqlModelMember}; | ||
|
||
type ModelName = String; | ||
|
||
pub struct ModelCache { | ||
pool: SqlitePool, | ||
schemas: RwLock<HashMap<ModelName, Ty>>, | ||
} | ||
|
||
impl ModelCache { | ||
pub fn new(pool: SqlitePool) -> Self { | ||
Self { pool, schemas: RwLock::new(HashMap::new()) } | ||
} | ||
|
||
pub async fn schema(&self, model: &str) -> Result<Ty, Error> { | ||
{ | ||
let schemas = self.schemas.read().await; | ||
if let Some(schema) = schemas.get(model) { | ||
return Ok(schema.clone()); | ||
} | ||
} | ||
|
||
self.update_schema(model).await | ||
} | ||
|
||
async fn update_schema(&self, model: &str) -> Result<Ty, Error> { | ||
let model_members: Vec<SqlModelMember> = sqlx::query_as( | ||
"SELECT id, model_idx, member_idx, name, type, type_enum, enum_options, key FROM \ | ||
model_members WHERE model_id = ? ORDER BY model_idx ASC, member_idx ASC", | ||
) | ||
.bind(model) | ||
.fetch_all(&self.pool) | ||
.await?; | ||
|
||
if model_members.is_empty() { | ||
return Err(QueryError::ModelNotFound(model.into()).into()); | ||
} | ||
|
||
let ty = parse_sql_model_members(model, &model_members); | ||
let mut schemas = self.schemas.write().await; | ||
schemas.insert(model.into(), ty.clone()); | ||
|
||
Ok(ty) | ||
} | ||
|
||
pub async fn clear(&self) { | ||
self.schemas.write().await.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.