mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-29 12:17:01 +02:00
Separate model for GQL introspection data (#222)
This commit is contained in:
55
src-tauri/yaak-models/src/queries/graphql_introspections.rs
Normal file
55
src-tauri/yaak-models/src/queries/graphql_introspections.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::models::{GraphQlIntrospection, GraphQlIntrospectionIden};
|
||||
use crate::util::UpdateSource;
|
||||
use chrono::{Duration, Utc};
|
||||
use sea_query::{Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_graphql_introspection(&self, request_id: &str) -> Option<GraphQlIntrospection> {
|
||||
self.find_optional(GraphQlIntrospectionIden::RequestId, request_id)
|
||||
}
|
||||
|
||||
pub fn upsert_graphql_introspection(
|
||||
&self,
|
||||
workspace_id: &str,
|
||||
request_id: &str,
|
||||
content: Option<String>,
|
||||
source: &UpdateSource,
|
||||
) -> Result<GraphQlIntrospection> {
|
||||
// Clean up old ones every time a new one is upserted
|
||||
self.delete_expired_graphql_introspections()?;
|
||||
|
||||
match self.get_graphql_introspection(request_id) {
|
||||
None => self.upsert(
|
||||
&GraphQlIntrospection {
|
||||
content,
|
||||
request_id: request_id.to_string(),
|
||||
workspace_id: workspace_id.to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
source,
|
||||
),
|
||||
Some(introspection) => self.upsert(
|
||||
&GraphQlIntrospection {
|
||||
content,
|
||||
..introspection
|
||||
},
|
||||
source,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_expired_graphql_introspections(&self) -> Result<()> {
|
||||
let cutoff = Utc::now().naive_utc() - Duration::days(7);
|
||||
let (sql, params) = Query::delete()
|
||||
.from_table(GraphQlIntrospectionIden::Table)
|
||||
.cond_where(Expr::col(GraphQlIntrospectionIden::UpdatedAt).lt(cutoff))
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
|
||||
let mut stmt = self.conn.resolve().prepare(sql.as_str())?;
|
||||
stmt.execute(&*params.as_params())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ mod batch;
|
||||
mod cookie_jars;
|
||||
mod environments;
|
||||
mod folders;
|
||||
mod graphql_introspections;
|
||||
mod grpc_connections;
|
||||
mod grpc_events;
|
||||
mod grpc_requests;
|
||||
|
||||
Reference in New Issue
Block a user