From afe6a3bf576aab77ec69d70ede2b9b85e14e7a3b Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sun, 22 Oct 2023 16:05:09 -0700 Subject: [PATCH] Environment data model backend --- .sqllsrc.json | 5 + .../20231022205109_environments.sql | 15 +++ src-tauri/sqlx-data.json | 118 ++++++++++++++++++ src-tauri/src/models.rs | 82 +++++++++++- src-web/components/WorkspaceHeader.tsx | 11 ++ 5 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 .sqllsrc.json create mode 100644 src-tauri/migrations/20231022205109_environments.sql diff --git a/.sqllsrc.json b/.sqllsrc.json new file mode 100644 index 00000000..dc809921 --- /dev/null +++ b/.sqllsrc.json @@ -0,0 +1,5 @@ +{ + "name": "yaak-dev", + "adapter": "sqlite3", + "filename": "src-tauri/db.sqlite" +} diff --git a/src-tauri/migrations/20231022205109_environments.sql b/src-tauri/migrations/20231022205109_environments.sql new file mode 100644 index 00000000..5bba9bdb --- /dev/null +++ b/src-tauri/migrations/20231022205109_environments.sql @@ -0,0 +1,15 @@ +CREATE TABLE environments +( + id TEXT NOT NULL + PRIMARY KEY, + model TEXT DEFAULT 'workspace' NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + deleted_at DATETIME, + workspace_id TEXT NOT NULL + REFERENCES workspaces + ON DELETE CASCADE, + name TEXT NOT NULL, + data TEXT NOT NULL + DEFAULT '{}' +); diff --git a/src-tauri/sqlx-data.json b/src-tauri/sqlx-data.json index d7e2858e..327f3c1a 100644 --- a/src-tauri/sqlx-data.json +++ b/src-tauri/sqlx-data.json @@ -398,6 +398,70 @@ }, "query": "\n INSERT INTO http_responses (\n id,\n request_id,\n workspace_id,\n elapsed,\n url,\n status,\n status_reason,\n content_length,\n body,\n body_path,\n headers\n )\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);\n " }, + "986763e31599881f287ef378002fc35d8e983af10a30a9aa4ade606dacf83260": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "workspace_id", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "model", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "created_at", + "ordinal": 3, + "type_info": "Datetime" + }, + { + "name": "updated_at", + "ordinal": 4, + "type_info": "Datetime" + }, + { + "name": "name", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "data!: Json>", + "ordinal": 6, + "type_info": "Text" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "\n SELECT id, workspace_id, model, created_at, updated_at, name,\n data AS \"data!: Json>\"\n FROM environments\n WHERE workspace_id = ?\n " + }, + "ab7294b681f1202ef06aaa26885147ead2db6ac740023793cda1e1c92665d996": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 4 + } + }, + "query": "\n INSERT INTO environments (\n id,\n workspace_id,\n name,\n data\n )\n VALUES (?, ?, ?, ?)\n " + }, "b19c275180909a39342b13c3cdcf993781636913ae590967f5508c46a56dc961": { "describe": { "columns": [], @@ -731,5 +795,59 @@ } }, "query": "\n INSERT INTO workspaces (id, name, description)\n VALUES (?, ?, ?)\n " + }, + "fb89f653780b3f3ab0dd0bb2af30c8d3945203819cb9df7bdd331df56a6ae690": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Text" + }, + { + "name": "model", + "ordinal": 1, + "type_info": "Text" + }, + { + "name": "workspace_id", + "ordinal": 2, + "type_info": "Text" + }, + { + "name": "created_at", + "ordinal": 3, + "type_info": "Datetime" + }, + { + "name": "updated_at", + "ordinal": 4, + "type_info": "Datetime" + }, + { + "name": "name", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "data!: Json>", + "ordinal": 6, + "type_info": "Text" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Right": 1 + } + }, + "query": "\n SELECT\n id,\n model,\n workspace_id,\n created_at,\n updated_at,\n name,\n data AS \"data!: Json>\"\n FROM environments\n WHERE id = ?\n " } } \ No newline at end of file diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index df7146c8..0a5b9c32 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -18,6 +18,18 @@ pub struct Workspace { pub description: String, } +#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Environment { + pub id: String, + pub workspace_id: String, + pub model: String, + pub created_at: NaiveDateTime, + pub updated_at: NaiveDateTime, + pub name: String, + pub data: Json>, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct HttpRequestHeader { @@ -31,11 +43,11 @@ pub struct HttpRequestHeader { #[serde(rename_all = "camelCase")] pub struct HttpRequest { pub id: String, + pub workspace_id: String, pub model: String, pub created_at: NaiveDateTime, pub updated_at: NaiveDateTime, pub sort_priority: f64, - pub workspace_id: String, pub name: String, pub url: String, pub method: String, @@ -193,6 +205,74 @@ pub async fn create_workspace( get_workspace(&id, pool).await } +pub async fn find_environments( + workspace_id: &str, + pool: &Pool, +) -> Result, sqlx::Error> { + sqlx::query_as!( + Environment, + r#" + SELECT id, workspace_id, model, created_at, updated_at, name, + data AS "data!: Json>" + FROM environments + WHERE workspace_id = ? + "#, + workspace_id, + ) + .fetch_all(pool) + .await +} + +pub async fn create_environment( + workspace_id: &str, + name: &str, + data: HashMap, + pool: &Pool, +) -> Result { + let id = generate_id(Some("en")); + let data_json = Json(data); + let trimmed_name = name.trim(); + sqlx::query!( + r#" + INSERT INTO environments ( + id, + workspace_id, + name, + data + ) + VALUES (?, ?, ?, ?) + "#, + id, + workspace_id, + trimmed_name, + data_json, + ) + .execute(pool) + .await?; + get_environment(&id, pool).await +} + +pub async fn get_environment(id: &str, pool: &Pool) -> Result { + sqlx::query_as!( + Environment, + r#" + SELECT + id, + model, + workspace_id, + created_at, + updated_at, + name, + data AS "data!: Json>" + FROM environments + WHERE id = ? + "#, + id, + ) + .fetch_one(pool) + .await +} + pub async fn duplicate_request(id: &str, pool: &Pool) -> Result { let existing = get_request(id, pool).await?; diff --git a/src-web/components/WorkspaceHeader.tsx b/src-web/components/WorkspaceHeader.tsx index 079e768e..89f3b586 100644 --- a/src-web/components/WorkspaceHeader.tsx +++ b/src-web/components/WorkspaceHeader.tsx @@ -7,6 +7,8 @@ import { RecentRequestsDropdown } from './RecentRequestsDropdown'; import { RequestActionsDropdown } from './RequestActionsDropdown'; import { SidebarActions } from './SidebarActions'; import { WorkspaceActionsDropdown } from './WorkspaceActionsDropdown'; +import { Button } from './core/Button'; +import { useDialog } from './DialogContext'; interface Props { className?: string; @@ -14,6 +16,7 @@ interface Props { export const WorkspaceHeader = memo(function WorkspaceHeader({ className }: Props) { const activeRequest = useActiveRequest(); + const dialog = useDialog(); return ( +