use super::*;
#[derive(Clone)]
pub struct DatabaseClient {
client: ArcadeDbClient,
database: String,
}
impl std::ops::Deref for DatabaseClient {
type Target = ArcadeDbClient;
fn deref(&self) -> &Self::Target {
&self.client
}
}
impl std::ops::DerefMut for DatabaseClient {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.client
}
}
impl ArcadeDbClient {
pub fn to_database(&self, database: impl Into<String>) -> DatabaseClient {
DatabaseClient {
client: self.clone(),
database: database.into(),
}
}
}
impl DatabaseClient {
pub fn batch(&self) -> crate::batch::Batch {
self.client.batch()
}
pub async fn connect(
addr: &str,
username: &str,
password: &str,
database: &str,
) -> Result<Self> {
Ok(ArcadeDbClient::connect(addr, username, password, database)
.await?
.to_database(database))
}
pub fn database(&self) -> &str {
&self.database
}
pub async fn query(&self, query: &str) -> Result<QueryResult> {
self.client.query(&self.database, query).await
}
pub async fn query_with_values(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<QueryResult> {
self.client
.query_with_values(&self.database, query, parameters)
.await
}
pub async fn execute(&self, command: &str) -> Result<ExecuteCommandResponse> {
self.client.execute(&self.database, command).await
}
pub async fn execute_with_values(
&self,
command: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<ExecuteCommandResponse> {
self.client
.execute_with_values(&self.database, command, parameters)
.await
}
pub async fn stream_query<F>(&self, query: &str, on_batch: F) -> Result<StreamSummary>
where
F: FnMut(&QueryResultBatch) -> ControlFlow<()>,
{
self.client
.stream_query(&self.database, query, on_batch)
.await
}
pub async fn stream_query_with_values<F>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
on_batch: F,
) -> Result<StreamSummary>
where
F: FnMut(&QueryResultBatch) -> ControlFlow<()>,
{
self.client
.stream_query_with_values(&self.database, query, parameters, on_batch)
.await
}
pub async fn lookup_by_rid(&self, rid: &crate::Link) -> Result<Option<GrpcRecord>> {
self.client.lookup_by_rid(&self.database, rid).await
}
pub async fn lookup<T>(&self, rid: &crate::Link) -> Result<Option<T>>
where
T: for<'a> TryFrom<&'a GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client.lookup(&self.database, rid).await
}
pub async fn create_record(
&self,
class_name: &str,
props: Vec<(&str, crate::GrpcValue)>,
) -> Result<CreateRecordResponse> {
self.client
.create_record(&self.database, class_name, props)
.await
}
pub async fn update_record_partial(
&self,
rid: &crate::Link,
props: impl Into<crate::encode::Params>,
) -> Result<bool> {
self.client
.update_record_partial(&self.database, rid, props)
.await
}
pub async fn stream_query_with_options<F>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
options: crate::stream::StreamOptions,
on_batch: F,
) -> Result<StreamSummary>
where
F: FnMut(&QueryResultBatch) -> ControlFlow<()>,
{
self.client
.stream_query_with_options(&self.database, query, parameters, options, on_batch)
.await
}
pub async fn stream_query_rows<T, F>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
on_row: F,
) -> Result<StreamSummary>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
F: FnMut(T) -> ControlFlow<()>,
{
self.client
.stream_query_rows(&self.database, query, parameters, on_row)
.await
}
pub async fn delete_record(&self, rid: &crate::Link) -> Result<bool> {
self.client.delete_record(&self.database, rid).await
}
pub async fn fetch_optional<T>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<Option<T>>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client
.fetch_optional(&self.database, query, parameters)
.await
}
pub async fn fetch_one<T>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<T>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client
.fetch_one(&self.database, query, parameters)
.await
}
pub async fn query_scalar<T>(
&self,
query: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<Option<T>>
where
T: for<'de> crate::record::FromGrpcValue<'de>,
{
self.client
.query_scalar(&self.database, query, parameters)
.await
}
pub async fn write_returning_optional<T>(
&self,
command: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<Option<T>>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client
.write_returning_optional(&self.database, command, parameters)
.await
}
pub async fn upsert_returning<T>(
&self,
command: &str,
parameters: impl Into<crate::encode::Params>,
) -> Result<T>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client
.upsert_returning(&self.database, command, parameters)
.await
}
pub async fn lookup_by_key<T>(
&self,
type_name: &str,
key: &[(&str, crate::GrpcValue)],
) -> Result<Option<T>>
where
T: for<'r> TryFrom<&'r GrpcRecord, Error = crate::record::RecordDecodeError>,
{
self.client
.lookup_by_key(&self.database, type_name, key)
.await
}
pub async fn count_type(&self, type_name: &str) -> Result<i64> {
self.client.count_type(&self.database, type_name).await
}
pub async fn run_transaction<T, F, Fut>(
&self,
isolation: TransactionIsolation,
retries: u32,
body: F,
) -> Result<T>
where
F: Fn(TxCommands) -> Fut,
Fut: std::future::Future<Output = Result<T>>,
{
self.client
.run_transaction(&self.database, isolation, retries, body)
.await
}
pub async fn bulk_insert(
&self,
target_class: &str,
key_columns: &[&str],
rows: Vec<GrpcRecord>,
) -> Result<InsertSummary> {
self.client
.bulk_insert(&self.database, target_class, key_columns, rows)
.await
}
pub async fn bulk_upsert(
&self,
target_class: &str,
key_columns: &[&str],
update_columns_on_conflict: &[&str],
rows: Vec<GrpcRecord>,
) -> Result<InsertSummary> {
self.client
.bulk_upsert(
&self.database,
target_class,
key_columns,
update_columns_on_conflict,
rows,
)
.await
}
pub async fn bulk_upsert_dtos<T>(
&self,
target_class: &str,
rows: Vec<T>,
) -> Result<InsertSummary>
where
T: crate::record::ToGrpcRecord + Send,
{
self.client
.bulk_upsert_dtos(&self.database, target_class, rows)
.await
}
pub async fn transaction(&self, isolation: TransactionIsolation) -> Result<Transaction<'_>> {
self.client.transaction(&self.database, isolation).await
}
pub async fn execute_language(
&self,
language: &str,
command: &str,
) -> Result<ExecuteCommandResponse> {
self.client
.execute_language(&self.database, language, command)
.await
}
pub async fn cypher_query(&self, query: &str) -> Result<QueryResult> {
self.client.cypher_query(&self.database, query).await
}
pub async fn cypher_execute(&self, command: &str) -> Result<ExecuteCommandResponse> {
self.client.cypher_execute(&self.database, command).await
}
}