1
0
Fork 0
forked from 2sys/phonograph
phonograph/interim-models/src/workspace.rs

64 lines
1.5 KiB
Rust
Raw Normal View History

2025-08-04 13:59:42 -07:00
use derive_builder::Builder;
use sqlx::query_as;
use uuid::Uuid;
use crate::{client::AppDbClient, cluster::Cluster, macros::with_id_query};
2025-08-04 13:59:42 -07:00
/// A workspace is 1:1 with a Postgres "database".
2025-08-04 13:59:42 -07:00
#[derive(Clone, Debug)]
pub struct Workspace {
/// Primary key (defaults to UUIDv7).
2025-08-04 13:59:42 -07:00
pub id: Uuid,
/// Cluster housing the backing database for this workspace.
pub cluster_id: Uuid,
/// Postgres database name.
pub db_name: String,
/// Human friendly name for the workspace.
pub display_name: String,
/// ID of the user account that created this workspace.
2025-08-04 13:59:42 -07:00
pub owner_id: Uuid,
}
impl Workspace {
/// Build an insert statement to create a new workspace.
pub fn insert<'a>() -> InsertBuilder<'a> {
2025-10-22 00:43:53 -07:00
InsertBuilder::default()
2025-08-04 13:59:42 -07:00
}
pub async fn fetch_cluster(&self, app_db: &mut AppDbClient) -> sqlx::Result<Cluster> {
Cluster::with_id(self.cluster_id).fetch_one(app_db).await
2025-08-04 13:59:42 -07:00
}
}
with_id_query!(Workspace, sql = "select * from workspaces where id = $1");
2025-08-04 13:59:42 -07:00
#[derive(Builder, Clone, Copy, Debug)]
pub struct Insert<'a> {
cluster_id: Uuid,
db_name: &'a str,
2025-08-04 13:59:42 -07:00
owner_id: Uuid,
}
impl<'a> Insert<'a> {
pub async fn insert(self, app_db: &mut AppDbClient) -> sqlx::Result<Workspace> {
2025-08-04 13:59:42 -07:00
query_as!(
Workspace,
2025-08-04 13:59:42 -07:00
"
insert into workspaces
(cluster_id, db_name, owner_id)
values ($1, $2, $3)
2025-08-04 13:59:42 -07:00
returning *
",
self.cluster_id,
self.db_name,
2025-08-04 13:59:42 -07:00
self.owner_id
)
.fetch_one(&mut *app_db.conn)
.await
}
}