Renamed BonsaiDB document structs to singular form

This commit is contained in:
Philip (a-0) 2024-02-10 20:04:44 +01:00
parent ce7519225e
commit 4b00cef7e7
5 changed files with 38 additions and 38 deletions

View file

@ -12,7 +12,7 @@ use crate::{
#[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)]
#[collection(name = "apps", views = [])] #[collection(name = "apps", views = [])]
pub(super) struct DbApps { pub(super) struct DbApp {
#[natural_id] #[natural_id]
pub(super) id: AsKey<AppId>, pub(super) id: AsKey<AppId>,
pub(super) last_access: DateTime<Utc>, pub(super) last_access: DateTime<Utc>,
@ -22,8 +22,8 @@ pub(super) struct DbApps {
pub(super) default_pot: Option<PotId>, pub(super) default_pot: Option<PotId>,
} }
impl From<DbApps> for App { impl From<DbApp> for App {
fn from(value: DbApps) -> Self { fn from(value: DbApp) -> Self {
App { App {
id: (*value.id).clone(), id: (*value.id).clone(),
app_type: value.app_type, app_type: value.app_type,
@ -43,8 +43,8 @@ impl StateDB {
description: String, description: String,
app_type: String, app_type: String,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
DbApps::push( DbApp::push(
DbApps { DbApp {
id: AsKey::new(id), id: AsKey::new(id),
last_access: Utc::now(), last_access: Utc::now(),
app_type, app_type,
@ -59,7 +59,7 @@ impl StateDB {
} }
pub fn set_default_pot(&self, id: AppId, pot: PotId) -> anyhow::Result<()> { pub fn set_default_pot(&self, id: AppId, pot: PotId) -> anyhow::Result<()> {
DbApps::get(&AsKey::new(id), &self.db) DbApp::get(&AsKey::new(id), &self.db)
.unwrap() .unwrap()
.unwrap() .unwrap()
.modify(&self.db, |app| app.contents.default_pot = Some(pot.clone())) .modify(&self.db, |app| app.contents.default_pot = Some(pot.clone()))
@ -67,7 +67,7 @@ impl StateDB {
} }
pub fn get_default_pot(&self, id: AppId) -> anyhow::Result<Option<Pot>> { pub fn get_default_pot(&self, id: AppId) -> anyhow::Result<Option<Pot>> {
let pot_id = DbApps::get(&AsKey::new(id), &self.db)? let pot_id = DbApp::get(&AsKey::new(id), &self.db)?
.map(|app| app.contents.default_pot) .map(|app| app.contents.default_pot)
.ok_or(Error::msg("App not found"))? .ok_or(Error::msg("App not found"))?
.ok_or(Error::msg("Could not get default pot"))?; .ok_or(Error::msg("Could not get default pot"))?;
@ -75,7 +75,7 @@ impl StateDB {
} }
pub fn get_all_apps(&self) -> anyhow::Result<Vec<App>> { pub fn get_all_apps(&self) -> anyhow::Result<Vec<App>> {
Ok(DbApps::all(&self.db) Ok(DbApp::all(&self.db)
.query()? .query()?
.iter() .iter()
.map(|app| app.contents.clone().into()) .map(|app| app.contents.clone().into())
@ -83,7 +83,7 @@ impl StateDB {
} }
pub fn get_all_app_ids(&self) -> anyhow::Result<Vec<AppId>> { pub fn get_all_app_ids(&self) -> anyhow::Result<Vec<AppId>> {
Ok(DbApps::all(&self.db) Ok(DbApp::all(&self.db)
.query()? .query()?
.iter() .iter()
.map(|app| (*app.contents.id).clone()) .map(|app| (*app.contents.id).clone())
@ -105,7 +105,7 @@ impl StateDB {
} }
pub fn get_app(&self, id: AppId) -> anyhow::Result<Option<App>> { pub fn get_app(&self, id: AppId) -> anyhow::Result<Option<App>> {
DbApps::get(&AsKey::new(id), &self.db) DbApp::get(&AsKey::new(id), &self.db)
.map(|app_option| app_option.map(|app| app.contents.into())) .map(|app_option| app_option.map(|app| app.contents.into()))
.map_err(|e| anyhow!(e)) .map_err(|e| anyhow!(e))
} }

View file

@ -1,10 +1,10 @@
use bonsaidb::core::schema::Schema; use bonsaidb::core::schema::Schema;
use apps::DbApps; use apps::DbApp;
use elements::DbElement; use elements::DbElement;
use peers::DbPeers; use peers::DbPeer;
use pot_memberships::DbPotMemberships; use pot_memberships::DbPotMembership;
use pots::DbPots; use pots::DbPot;
mod apps; mod apps;
mod elements; mod elements;
@ -13,7 +13,7 @@ mod pot_memberships;
mod pots; mod pots;
#[derive(Schema, Debug)] #[derive(Schema, Debug)]
#[schema(name = "ubisync", collections = [DbElement, DbPotMemberships, DbApps, DbPots, DbPeers])] #[schema(name = "ubisync", collections = [DbElement, DbPotMembership, DbApp, DbPot, DbPeer])]
pub struct UbisyncSchema; pub struct UbisyncSchema;
#[cfg(test)] #[cfg(test)]

View file

@ -11,22 +11,22 @@ use crate::state::database::{as_key::AsKey, StateDB};
#[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)]
#[collection(name = "peers", views = [])] #[collection(name = "peers", views = [])]
pub(super) struct DbPeers { pub(super) struct DbPeer {
#[natural_id] #[natural_id]
pub(super) id: AsKey<types::PeerId>, pub(super) id: AsKey<types::PeerId>,
pub(super) name: Option<String>, pub(super) name: Option<String>,
} }
impl From<DbPeers> for Peer { impl From<DbPeer> for Peer {
fn from(value: DbPeers) -> Self { fn from(value: DbPeer) -> Self {
Peer::new((*value.id).clone(), value.name) Peer::new((*value.id).clone(), value.name)
} }
} }
impl StateDB { impl StateDB {
pub fn add_peer(&self, peer: Peer) -> anyhow::Result<()> { pub fn add_peer(&self, peer: Peer) -> anyhow::Result<()> {
DbPeers::push( DbPeer::push(
DbPeers { DbPeer {
id: AsKey::new(peer.id()), id: AsKey::new(peer.id()),
name: peer.name(), name: peer.name(),
}, },
@ -37,20 +37,20 @@ impl StateDB {
} }
pub fn get_peer(&self, id: PeerId) -> anyhow::Result<Option<Peer>> { pub fn get_peer(&self, id: PeerId) -> anyhow::Result<Option<Peer>> {
DbPeers::get(&AsKey::new(id), &self.db) DbPeer::get(&AsKey::new(id), &self.db)
.map(|doc| doc.map(|peer| Peer::new((*peer.contents.id).clone(), peer.contents.name))) .map(|doc| doc.map(|peer| Peer::new((*peer.contents.id).clone(), peer.contents.name)))
.map_err(|e| anyhow!(e)) .map_err(|e| anyhow!(e))
} }
pub fn get_all_peers(&self) -> anyhow::Result<Vec<Peer>> { pub fn get_all_peers(&self) -> anyhow::Result<Vec<Peer>> {
DbPeers::all(&self.db) DbPeer::all(&self.db)
.query() .query()
.map(|peers| peers.iter().map(|p| p.contents.clone().into()).collect_vec()) .map(|peers| peers.iter().map(|p| p.contents.clone().into()).collect_vec())
.map_err(|e| anyhow!(e)) .map_err(|e| anyhow!(e))
} }
pub fn set_peer_name(&self, id: PeerId, name: &Option<String>) -> anyhow::Result<()> { pub fn set_peer_name(&self, id: PeerId, name: &Option<String>) -> anyhow::Result<()> {
DbPeers::get(&AsKey::new(id), &self.db) DbPeer::get(&AsKey::new(id), &self.db)
.map_err(|e| anyhow!(e))? .map_err(|e| anyhow!(e))?
.ok_or(Error::msg("Peer not found"))? .ok_or(Error::msg("Peer not found"))?
.modify(&self.db, |doc| doc.contents.name = name.clone()) .modify(&self.db, |doc| doc.contents.name = name.clone())

View file

@ -12,13 +12,13 @@ use crate::state::database::{as_key::AsKey, StateDB};
#[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)]
#[collection(name = "pot_memberships", primary_key = u128, views = [DbPotMembershipsByPotId, DbPotMembershipsByAppId, DbPotMembershipsByBothIds])] #[collection(name = "pot_memberships", primary_key = u128, views = [DbPotMembershipsByPotId, DbPotMembershipsByAppId, DbPotMembershipsByBothIds])]
pub(super) struct DbPotMemberships { pub(super) struct DbPotMembership {
pub(super) pot_id: AsKey<PotId>, pub(super) pot_id: AsKey<PotId>,
pub(super) app_id: AsKey<AppId>, pub(super) app_id: AsKey<AppId>,
} }
#[derive(Debug, Clone, View, ViewSchema)] #[derive(Debug, Clone, View, ViewSchema)]
#[view(collection = DbPotMemberships, key = AsKey<PotId>, value = Vec<AppId>, name = "by-pot-id")] #[view(collection = DbPotMembership, key = AsKey<PotId>, value = Vec<AppId>, name = "by-pot-id")]
pub(super) struct DbPotMembershipsByPotId; pub(super) struct DbPotMembershipsByPotId;
impl MapReduce for DbPotMembershipsByPotId { impl MapReduce for DbPotMembershipsByPotId {
@ -26,7 +26,7 @@ impl MapReduce for DbPotMembershipsByPotId {
&self, &self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>, document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> { ) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let entry = DbPotMemberships::document_contents(document)?; let entry = DbPotMembership::document_contents(document)?;
document document
.header .header
.emit_key_and_value(entry.pot_id, vec![(*entry.app_id).clone()]) .emit_key_and_value(entry.pot_id, vec![(*entry.app_id).clone()])
@ -48,7 +48,7 @@ impl MapReduce for DbPotMembershipsByPotId {
} }
#[derive(Debug, Clone, View, ViewSchema)] #[derive(Debug, Clone, View, ViewSchema)]
#[view(collection = DbPotMemberships, key = AsKey<AppId>, value = Vec<PotId>, name = "by-app-id")] #[view(collection = DbPotMembership, key = AsKey<AppId>, value = Vec<PotId>, name = "by-app-id")]
pub(super) struct DbPotMembershipsByAppId; pub(super) struct DbPotMembershipsByAppId;
impl MapReduce for DbPotMembershipsByAppId { impl MapReduce for DbPotMembershipsByAppId {
@ -56,7 +56,7 @@ impl MapReduce for DbPotMembershipsByAppId {
&self, &self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>, document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> { ) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let entry = DbPotMemberships::document_contents(document)?; let entry = DbPotMembership::document_contents(document)?;
document document
.header .header
.emit_key_and_value(entry.app_id, vec![(*entry.pot_id).clone()]) .emit_key_and_value(entry.app_id, vec![(*entry.pot_id).clone()])
@ -78,7 +78,7 @@ impl MapReduce for DbPotMembershipsByAppId {
} }
#[derive(Debug, Clone, View, ViewSchema)] #[derive(Debug, Clone, View, ViewSchema)]
#[view(collection = DbPotMemberships, key = (AsKey<PotId>, AsKey<AppId>), value = DbPotMemberships, name = "by-both")] #[view(collection = DbPotMembership, key = (AsKey<PotId>, AsKey<AppId>), value = DbPotMembership, name = "by-both")]
pub(super) struct DbPotMembershipsByBothIds; pub(super) struct DbPotMembershipsByBothIds;
impl MapReduce for DbPotMembershipsByBothIds { impl MapReduce for DbPotMembershipsByBothIds {
@ -86,7 +86,7 @@ impl MapReduce for DbPotMembershipsByBothIds {
&self, &self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>, document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> { ) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let content = DbPotMemberships::document_contents(document)?; let content = DbPotMembership::document_contents(document)?;
document document
.header .header
.emit_key_and_value((content.pot_id.clone(), content.app_id.clone()), content) .emit_key_and_value((content.pot_id.clone(), content.app_id.clone()), content)
@ -104,8 +104,8 @@ impl StateDB {
"A member app which does not exist was meant to be added to a pot", "A member app which does not exist was meant to be added to a pot",
)) ))
} else { } else {
DbPotMemberships::push( DbPotMembership::push(
DbPotMemberships { DbPotMembership {
pot_id: AsKey::new(pot), pot_id: AsKey::new(pot),
app_id: AsKey::new(app), app_id: AsKey::new(app),
}, },

View file

@ -7,22 +7,22 @@ use crate::state::database::{as_key::AsKey, StateDB};
#[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)] #[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)]
#[collection(name = "pots", views = [])] #[collection(name = "pots", views = [])]
pub(super) struct DbPots { pub(super) struct DbPot {
#[natural_id] #[natural_id]
pub(super) id: AsKey<PotId>, pub(super) id: AsKey<PotId>,
pub(super) app_type: String, pub(super) app_type: String,
} }
impl From<DbPots> for Pot { impl From<DbPot> for Pot {
fn from(value: DbPots) -> Self { fn from(value: DbPot) -> Self {
Pot {id: (*value.id).clone(), app_type: value.app_type} Pot {id: (*value.id).clone(), app_type: value.app_type}
} }
} }
impl StateDB { impl StateDB {
pub fn add_pot(&self, id: PotId, app_type: String) -> anyhow::Result<()> { pub fn add_pot(&self, id: PotId, app_type: String) -> anyhow::Result<()> {
DbPots::push( DbPot::push(
DbPots { DbPot {
id: AsKey::new(id), id: AsKey::new(id),
app_type, app_type,
}, },
@ -33,7 +33,7 @@ impl StateDB {
} }
pub fn get_pot(&self, id: PotId) -> anyhow::Result<Option<Pot>> { pub fn get_pot(&self, id: PotId) -> anyhow::Result<Option<Pot>> {
DbPots::get(&AsKey::new(id), &self.db) DbPot::get(&AsKey::new(id), &self.db)
.map(|pot_opt| pot_opt.map(|pot| pot.contents.into())) .map(|pot_opt| pot_opt.map(|pot| pot.contents.into()))
.map_err(|e| anyhow!(e)) .map_err(|e| anyhow!(e))
} }