use crate::{
codec::CodecController,
database::{executor::Executor, schema_manager::Commands},
misc::Lease,
};
use alloc::string::String;
#[cfg(feature = "std")]
use std::{fs::read_to_string, path::Path};
impl<E> Commands<E>
where
E: Executor,
{
#[inline]
pub async fn seed<S>(
&mut self,
seeds: impl IntoIterator<Item = S>,
) -> Result<(), <E::Database as CodecController>::Error>
where
S: Lease<str>,
{
let mut buffer_cmd = String::new();
for elem in seeds {
buffer_cmd.push_str(elem.lease());
}
self
.executor
.transaction(|this| async {
this.execute_ignored(buffer_cmd.as_str()).await?;
Ok(((), this))
})
.await?;
Ok(())
}
#[cfg(feature = "std")]
#[inline]
pub async fn seed_from_dir(
&mut self,
dir: &Path,
) -> Result<(), <E::Database as CodecController>::Error> {
let iter = crate::database::schema_manager::misc::files(dir)?.filter_map(|el_rslt| {
let el = el_rslt.ok()?;
read_to_string(el.path()).ok()
});
self.seed(iter).await
}
}