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)]
#[collection(name = "apps", views = [])]
pub(super) struct DbApps {
pub(super) struct DbApp {
#[natural_id]
pub(super) id: AsKey<AppId>,
pub(super) last_access: DateTime<Utc>,
@ -22,8 +22,8 @@ pub(super) struct DbApps {
pub(super) default_pot: Option<PotId>,
}
impl From<DbApps> for App {
fn from(value: DbApps) -> Self {
impl From<DbApp> for App {
fn from(value: DbApp) -> Self {
App {
id: (*value.id).clone(),
app_type: value.app_type,
@ -43,8 +43,8 @@ impl StateDB {
description: String,
app_type: String,
) -> anyhow::Result<()> {
DbApps::push(
DbApps {
DbApp::push(
DbApp {
id: AsKey::new(id),
last_access: Utc::now(),
app_type,
@ -59,7 +59,7 @@ impl StateDB {
}
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()
.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>> {
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)
.ok_or(Error::msg("App not found"))?
.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>> {
Ok(DbApps::all(&self.db)
Ok(DbApp::all(&self.db)
.query()?
.iter()
.map(|app| app.contents.clone().into())
@ -83,7 +83,7 @@ impl StateDB {
}
pub fn get_all_app_ids(&self) -> anyhow::Result<Vec<AppId>> {
Ok(DbApps::all(&self.db)
Ok(DbApp::all(&self.db)
.query()?
.iter()
.map(|app| (*app.contents.id).clone())
@ -105,7 +105,7 @@ impl StateDB {
}
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_err(|e| anyhow!(e))
}

View file

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

View file

@ -11,22 +11,22 @@ use crate::state::database::{as_key::AsKey, StateDB};
#[derive(Debug, Serialize, Deserialize, Collection, PartialEq, Clone)]
#[collection(name = "peers", views = [])]
pub(super) struct DbPeers {
pub(super) struct DbPeer {
#[natural_id]
pub(super) id: AsKey<types::PeerId>,
pub(super) name: Option<String>,
}
impl From<DbPeers> for Peer {
fn from(value: DbPeers) -> Self {
impl From<DbPeer> for Peer {
fn from(value: DbPeer) -> Self {
Peer::new((*value.id).clone(), value.name)
}
}
impl StateDB {
pub fn add_peer(&self, peer: Peer) -> anyhow::Result<()> {
DbPeers::push(
DbPeers {
DbPeer::push(
DbPeer {
id: AsKey::new(peer.id()),
name: peer.name(),
},
@ -37,20 +37,20 @@ impl StateDB {
}
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_err(|e| anyhow!(e))
}
pub fn get_all_peers(&self) -> anyhow::Result<Vec<Peer>> {
DbPeers::all(&self.db)
DbPeer::all(&self.db)
.query()
.map(|peers| peers.iter().map(|p| p.contents.clone().into()).collect_vec())
.map_err(|e| anyhow!(e))
}
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))?
.ok_or(Error::msg("Peer not found"))?
.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)]
#[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) app_id: AsKey<AppId>,
}
#[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;
impl MapReduce for DbPotMembershipsByPotId {
@ -26,7 +26,7 @@ impl MapReduce for DbPotMembershipsByPotId {
&self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let entry = DbPotMemberships::document_contents(document)?;
let entry = DbPotMembership::document_contents(document)?;
document
.header
.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)]
#[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;
impl MapReduce for DbPotMembershipsByAppId {
@ -56,7 +56,7 @@ impl MapReduce for DbPotMembershipsByAppId {
&self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let entry = DbPotMemberships::document_contents(document)?;
let entry = DbPotMembership::document_contents(document)?;
document
.header
.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)]
#[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;
impl MapReduce for DbPotMembershipsByBothIds {
@ -86,7 +86,7 @@ impl MapReduce for DbPotMembershipsByBothIds {
&self,
document: &'doc bonsaidb::core::document::BorrowedDocument<'_>,
) -> bonsaidb::core::schema::ViewMapResult<'doc, Self> {
let content = DbPotMemberships::document_contents(document)?;
let content = DbPotMembership::document_contents(document)?;
document
.header
.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",
))
} else {
DbPotMemberships::push(
DbPotMemberships {
DbPotMembership::push(
DbPotMembership {
pot_id: AsKey::new(pot),
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)]
#[collection(name = "pots", views = [])]
pub(super) struct DbPots {
pub(super) struct DbPot {
#[natural_id]
pub(super) id: AsKey<PotId>,
pub(super) app_type: String,
}
impl From<DbPots> for Pot {
fn from(value: DbPots) -> Self {
impl From<DbPot> for Pot {
fn from(value: DbPot) -> Self {
Pot {id: (*value.id).clone(), app_type: value.app_type}
}
}
impl StateDB {
pub fn add_pot(&self, id: PotId, app_type: String) -> anyhow::Result<()> {
DbPots::push(
DbPots {
DbPot::push(
DbPot {
id: AsKey::new(id),
app_type,
},
@ -33,7 +33,7 @@ impl StateDB {
}
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_err(|e| anyhow!(e))
}